<script setup lang="ts">
import { usePageSearch, type PageHooks, type PageEvents } from "/@/hooks/page";
import { ElButton, ElDialog, ElInput, 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 remark = ref('')
const visible = ref(false)
const _id = ref('')

// 使用页面搜索钩子
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
    });

    if(status == '3'){
      visible.value = true
      _id.value = id
      return
    }

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

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


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

async function handleStatusChangeNo() {
  try {

    if(remark.value.trim().length === 0){
      ElMessage.warning('请输入驳回备注')
      return
    }

    const result = await httpFZStatus({ id: _id.value, status:'3', remark: remark.value })
    if (result.code == 0) {
      contentRef.value?.onSearch()
      visible.value = false
    } else {
      ElMessage.warning(result.message)
    }


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


  <!-- { value: '0', label: '未封帐', type: 'info' },
  { value: '1', label: '待审批', type: 'warning' },
  { value: '2', label: '封账完成', type: 'success' },
  { value: '3', label: '封账驳回', type: 'warning' },
  { value: '4', label: '封账失败' , type: 'danger'}, -->
<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 == '3' && !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>

        <ElTooltip content="审核驳回" placement="top" v-if="row.status == '1' && !isSuperUser">
          <ElButton link size="small" type="primary" @click="handleStatusChange(row.id, '3')"
            :icon="useRenderIcon('close')" />
        </ElTooltip>
      </template>
    </PageContainer>

    <ElDialog title="驳回备注" center  v-model="visible" @close="remark = '';_id = ''">
      <ElInput type="textarea" v-model="remark" placeholder="驳回备注" />

      <div class="w-full mt-[10px] flex justify-end">
        <ElButton type="primary" @click="handleStatusChangeNo">保存</ElButton>
      </div>
    </ElDialog>
  </PageAuth>
</template>
