<script setup lang="ts">
import { ref, shallowRef ,onMounted, nextTick} from "vue";
import { ElMessage } from "element-plus";
import contentConfig from "./config/content.config";
import searchConfig from "./config/search.config";
import PurchaseModal from "./components/purchase-modal.vue";
import { usePermission } from "/@/hooks/core/usePermission";
import { template } from "./config/xls-template";
import { usePageSearch, type PageHooks, type PageEvents } from "/@/hooks/page";
import { utils, writeFile } from "xlsx";
import {
  cgdStatusOptions,
  cg_order_source_options,
  cg_order_type_options,
  hasAccountOptions,
  sendStatusOptions,
  send_status_list
} from "/@/utils/status";
const PageName = "porder";
const loading = shallowRef(false);
const selectlist = ref<any[]>([]);
const purchaseModalRef = ref<InstanceType<typeof PurchaseModal> | null>(null);
const contentRef = ref<any>(null);
const {  hasPermissionWithCode } = usePermission("porder");

const hooks: PageHooks = {
  pageSearchHook: () => usePageSearch(undefined, undefined, searchConfig)
};

const events: PageEvents = {
  content: {
    preview: row => purchaseModalRef.value.onDisplay(row)
  },
  search:{
    reset: () => contentRef.value.onSearchPro({ size:15, page:1, total: "2" })
  }
};


async function handleExport() {
  if (selectlist.value.length === 0) {
    ElMessage.warning("请选择一条采购单");
    return;
  }
  const judieTemplate = template;
  const judieFilename = "采购单导出";

  const data: any[] = [];
  selectlist.value.forEach(item => {
    const _template = { ...judieTemplate };
    _template["采购单编码"] = item.sequenceNo;
    _template["采购主单编码"] = item.cxCode;
    _template["销售订单编码"] = item.qrdCode;
    _template["供应商端"] = item.has_account
      ? (hasAccountOptions.find(s => s.value == item.has_account + "") || {})
          .label || ""
      : "";
    _template["状态"] = item.status
      ? (cgdStatusOptions.find(s => s.value == item.status + "") || {}).label ||
        ""
      : "";
    _template["销售单发货状态"] = item.qrdSend
      ? (send_status_list.find(s => s.value == item.qrdSend + "") || {})
          .label || ""
      : "";
    _template["采购单状态"] = item.cgd_status
      ? (sendStatusOptions.find(s => s.value == item.cgd_status + "") || {})
          .label || ""
      : "";
    _template["订单来源"] = item.cgdSource
      ? (
          cg_order_source_options.find(s => s.value == item.cgdSource + "") ||
          {}
        ).label || ""
      : "";
    _template["商品来源"] = item.cgdType
      ? (cg_order_type_options.find(s => s.value == item.cgdType + "") || {})
          .label || ""
      : "";


    _template["商品编码"] = item.goodNo;
    _template["商品名称"] = item.goodName;
    _template["商品名称"] = item.goodName;
    _template["税率"] = item.tax;
    _template["类目编码"] = item.orderTax ? item.orderTax.cat_code : '';
    _template["货物和劳务名称"] = item.orderTax ? item.orderTax.cat_name : '';
    _template["商品和服务分类简称"] = item.orderTax ? item.orderTax.short_name : '';
    _template["开票税率"] = item.orderTax ? item.orderTax.tax ? item.orderTax.tax + '%' : '' : '';
    _template["开票类型"] = item.orderTax ? item.open_type == '1' ? '原商品开票' : item.open_type === '2' ? '子商品开票' : '' : '';
    _template["商品数量"] = item.goodNum;
    _template["商品单价"] = item.goodPrice;
    _template["商品总价"] = item.totalPrice;
    _template["卖出方公司编码"] = item.supplierNo;
    _template["卖出方公司名称"] = item.supplierName;
    _template["备库单编码"] = item.bkCode;
    _template["采购员"] = item.ownerName;
    _template["买入方公司编号"] = item.companyNo;
    _template["买入方公司名称"] = item.companyName;
    _template["创建时间"] = item.addtime;
    data.push(_template);
  });
  //创建数据表
  const workBook = utils.book_new();
  const workSheet = utils.json_to_sheet(data);
  utils.book_append_sheet(workBook, workSheet, "sheet");

  //导出模板
  writeFile(workBook, `${judieFilename}.xlsx`, { bookType: "xlsx" });
}

onMounted(() => {nextTick(()  =>  contentRef.value.onSearchPro({ size:15, page:1, total: "2" }))})
</script>

<template>
  <PageAuth :pageName="PageName">
    <PageContainer
      :hooks="hooks"
      :events="events"
      :contentConfig="contentConfig"
      :search-config="searchConfig"
      :get-content-ref="ref => contentRef = ref"
      @content-select-change="items => (selectlist = items)"
    >
      <template #content_header>
        <ElButton 
          @click="handleExport"
          :loading="loading"
          size="small"
          >采购单导出</ElButton
        >
      </template>
    </PageContainer>
    <PurchaseModal ref="purchaseModalRef" />
  </PageAuth>
</template>
