<script setup lang="ts">
import { usePageSearch, type PageHooks, type PageEvents } from "/@/hooks/page";
import { ElButton, ElMessage, ElMessageBox, ElTooltip } from "element-plus";
import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
import contentConfig from "./config/content.config";
import searchConfig from "./config/search.config";
import { useRouter } from "vue-router";
import { httpFZStatus } from "/@/api/invoiceInOut/summary";
import { ref } from "vue";
import { useUserInfo } from "/@/hooks/core/useUser";

const PageName = "productManager";
const router = useRouter();

const contentRef = ref(null)

const { isSuperUser } = useUserInfo()

// 使用页面搜索钩子
const hooks: PageHooks = {
  pageSearchHook: () => usePageSearch(
    undefined, 
    undefined, 
    searchConfig
  )
};

// 页面事件处理
const events: PageEvents = {
  content: {
    preview: data => router.push(`/invoiceInOut/summaryDetail?data=${JSON.stringify(data)}`)
  }
};

// 状态变更处理
async function handleStatusChange(id, status) {
  try {
    await ElMessageBox.alert("是否确认审核", "提示", {
      type: "warning",
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      showCancelButton: true
    });


    const result = await httpFZStatus({id, status})

    if(result.code == 0){
      contentRef.value?.onSearch()
    }else {
      ElMessage.warning(result.message)
    }


  } catch (err) {
    console.error("状态变更失败:", err);
  }
}
</script>

<template>
  <PageAuth :pageName="PageName">
    <PageContainer
      :hooks="hooks"
      :events="events"
      :searchConfig="searchConfig"
      :contentConfig="contentConfig"
      :get-content-ref="ref => contentRef = ref"
    >
      <template #content_action="row">
        <ElTooltip content="审批通过" placement="top"  v-if="row.status == '1' && !isSuperUser">
          <ElButton
            link
            size="small"
            type="primary"
            @click="handleStatusChange(row.id, '2')"
            :icon="useRenderIcon('checkbox-circle-line')"
          />
        </ElTooltip>
        
        <ElTooltip content="改为未封账" placement="top" v-if="row.status == '1' && !isSuperUser">
          <ElButton
            link
            size="small"
            type="primary"
            @click="handleStatusChange(row.id, '0')"
            :icon="useRenderIcon('close-circle-line')"
          />
        </ElTooltip>

        <ElTooltip content="解除封账" placement="top"  v-if="row.status == '2' && !isSuperUser">
          <ElButton
            link
            size="small"
            type="primary"
            @click="handleStatusChange(row.id, '0')"
            :icon="useRenderIcon('link')"
          />
        </ElTooltip>
        <ElTooltip content="改为待审批" placement="top" v-if="row.status == '0' && !isSuperUser">
          <ElButton
            link
            size="small"
            type="primary"
            @click="handleStatusChange(row.id, '1')"
            :icon="useRenderIcon('compass')"
          />
        </ElTooltip>
      </template>
    </PageContainer>
  </PageAuth>
</template>
