<script lang="ts" setup>
import DefaultToolButton from "@/components/base/default-tool-button/DefaultToolButton.vue";
import DefaultOperateButtonColumn from "@/components/base/default-column/DefaultOperateButtonColumn.vue";
import { $t } from "@/common/languages";
import { formatDate } from "@/common/utils/format-utils";
import ExpandablePageableTable from "@/components/base/expandable-pageable-table/ExpandablePageableTable.vue";
import DefaultStatusSwitchColumn from "@/components/base/default-column/DefaultStatusSwitchColumn.vue";
import { useStatus } from "@/common/languages/mapping/base-info-mapping";
import { usePage } from "@/composables/use-page";
import { ref } from "vue";
import BaseItemDialog from "@/components/base/base-item-dialog/BaseItemDialog.vue";

/**
 * 必须要的变量
 */

const getPageUrl = "/production/productionreturn/getProductionReturnPage";
const getItemUrl = "/production/productionreturn/getProductionReturnItemList";
const approveUrl = "/production/productionreturn/approvingProductionReturn";
const rejectUrl = "/production/productionreturn/rejectProductionReturn";
const searchers = [{ name: "searchCode", type: "text" as const, placeholder: $t("_prop.common.searchCode") }];
/**
 * 基本不变通用变量
 */
const tableRef = ref<InstanceType<typeof ExpandablePageableTable> | null>(null);
const { getFormStatusLabel } = useStatus();
const { useApprove, useReject, useGeneralPageRef } = usePage(tableRef);
const { itemVisible, itemParentId } = useGeneralPageRef();
/**
 * 可以自定义的变量
 */

const approve = (row: any) => {
  useApprove(approveUrl, row.id);
};
const reject = (row: any) => {
  useReject(rejectUrl, row.id);
};
const showItem = (row: any) => {
  itemParentId.value = row.id;
  itemVisible.value = true;
};

const operateButtonClick = (eventName: string, row: any) => {
  switch (eventName) {
    case "approve":
      approve(row);
      break;
    case "reject":
      reject(row);
      break;
    case "showItem":
      showItem(row);
      break;
  }
};
const authShowFunc = (row: any, button: globalThis.ButtonProp) => {
  if (row.formStatus === 0 && button.eventName === "reject") return false;
  if (row.formStatus === 1 && button.eventName === "approve") return false;
  return true;
};

// 表单状态标签类型映射
const getFormStatusTagType = (code: number | null): string => {
  const tagTypeMap: Record<number, string> = {
    0: "info", // 未审核
    1: "success", // 已审核
    2: "danger", // 已驳回
    3: "warning", // 未完成
    4: "success", // 已完成
    5: "primary", // 入库中
    6: "info", // 已导入
    7: "warning", // 审核中
    8: "danger", // 已退回
  };
  return tagTypeMap[code ?? -1] || "info";
};
</script>
<template>
  <ExpandablePageableTable
    :url="getPageUrl"
    ref="tableRef"
    :searchers="searchers"
    :item-url="getItemUrl"
    item-id-key="id"
    item-id-name="returnId"
    item-field-name="returnItems"
  >
    <template #tool-button>
      <DefaultToolButton />
    </template>
    <template #columns>
      <el-table-column :label="$t('_prop.production.production_return.formCode')" prop="formCode" />
      <el-table-column :label="$t('_prop.production.production_return.storeName')" prop="storeName" />
      <el-table-column :label="$t('_prop.production.production_return.formMark')" prop="formMark" />
      <el-table-column :label="$t('_prop.common.createDate')" prop="createDate" :formatter="formatDate" />
      <DefaultStatusSwitchColumn
        status-param-name="formStatus"
        :status-label-mapping="getFormStatusLabel"
        :tag-type-mapping="getFormStatusTagType"
      />
      <DefaultOperateButtonColumn @operate-button-click="operateButtonClick" :auth-show-func="authShowFunc" />
    </template>
    <template #item-content="{ itemData }">
      <el-table :data="itemData" size="small" border stripe>
        <el-table-column :label="$t('_prop.warehouse.warehouse_item.partNumber')" prop="partNumber" />
        <el-table-column :label="$t('_prop.warehouse.warehouse_item.productSpecs')" prop="productSpecs" />
        <el-table-column :label="$t('_prop.production.production_return.returnQty')" prop="returnQty" />
      </el-table>
    </template>
  </ExpandablePageableTable>
  <BaseItemDialog
    :title="$t('_title.production.production_return.showItem')"
    v-model:visible="itemVisible"
    :url="getItemUrl"
    parent-param-name="returnId"
    v-model:parent-param-value="itemParentId"
  >
    <template #columns>
      <el-table-column :label="$t('_prop.warehouse.warehouse_item.partNumber')" prop="partNumber" />
      <el-table-column :label="$t('_prop.warehouse.warehouse_item.productSpecs')" prop="productSpecs" />
      <el-table-column :label="$t('_prop.production.production_return.returnQty')" prop="returnQty" />
    </template>
  </BaseItemDialog>
</template>
