<script setup lang="ts">
import { ref } from "vue";
import { utils, writeFile } from 'xlsx';
import { usePageSearch, type PageHooks } from "/@/hooks/page";
import { usePermission } from "/@/hooks/core/usePermission";
import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
import { useUserInfo } from "/@/hooks/core/useUser";
import contentConfig from "./config/content.config";
import searchConfig from "./config/search.config";
import { template } from "./config/template";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router"
import { httpDelete } from "/@/api/postSupplierOrder/sendOrder";
import { useAsync } from "/@/hooks";
import { orderOptions } from "./config/options"

import { hasAccountOptions, xs_order_source_options, xs_order_type_options } from "/@/utils/status";

const PageName = "supSendOrder";

const { push } = useRouter();
const pageContentRef = ref<any>(null);
const selectlist = ref<any[]>([]);
const hooks: PageHooks = {
  pageSearchHook: () => usePageSearch(undefined, undefined, searchConfig)
};

const { run, loading } = useAsync({
  success:() => pageContentRef.value.onSearch()
})

const { isSuperUser } = useUserInfo();

const { hasPermissionWithCode } = usePermission(PageName);

function handleExport(){
  const workBook = utils.book_new();
  const workSheet = utils.json_to_sheet([template]);
  utils.book_append_sheet(workBook, workSheet, "sheet");
  //导出模板
  writeFile(workBook, `发货运单模板.xlsx`, { bookType: "xlsx" });
}

function handleExportPage() { 
  if (selectlist.value.length === 0) { 
    ElMessage.warning('请选择至少一条发货运单');
    return;
  }

  const template: any[] = [];
  
  for (const item of selectlist.value) { 
    template.push({
      '发货运单编号': item.TrackCode,
      '发货工单编号': item.outChildCode,
      '发货单编号': item.fhdChild ? item.fhdChild.outCode : '--',
      '销售订单编号': item.orderCode,
      '采购单编号': item.cgdNo,
      '状态': orderOptions.find(({ value }) => Number(value) === Number(item.status))?.label || '--',
      '供应商端': hasAccountOptions.find(({ value }) => Number(value) === Number(item.has_account))?.label || '--',
      '订单来源':  xs_order_source_options.find(({ value }) => String(value) === String(item.fhdChild ? item.fhdChild.order_source : ''))?.label || '--',
      '商品类型':  xs_order_type_options.find(({ value }) => String(value) === String(item.fhdChild ? item.fhdChild.order_type : ''))?.label || '--',
      '商品编号':  item.fhdChild ? item.fhdChild.spuCode : '--',
      '商品名称':  item.fhdChild ? item.fhdChild.good_name : '--',
      '物流供应商编号':  item.fhdChild ? item.fhdChild.supplierNo : '--',
      '物流供应商名称': item.fhdChild ? item.fhdChild.supplierName : '--',
      '业务公司编号': item.fhdChild ? item.fhdChild.companyNo : '--',
      '业务公司名称':  item.fhdChild ? item.fhdChild.companyName : '--',
      '创建时间':  item.create_time,
      '物流费用':  item.post_fee,
      '创建人':  item.apply_name,
      '备注':item.remark
    })
  }

  
  const workBook = utils.book_new();
  const workSheet = utils.json_to_sheet(template);
  utils.book_append_sheet(workBook, workSheet, "sheet");
  //导出模板
  writeFile(workBook, `发货运单.xlsx`, { bookType: "xlsx" });
}

const onDelete = async ({ id }: any) => run(httpDelete({ id }));
</script>

<template v-loading="loading">
  <PageAuth :pageName="PageName">
    <PageContainer 
      :hooks="hooks"
      :contentConfig="contentConfig" 
      :searchConfig="searchConfig"
      :get-content-ref="ref => pageContentRef = ref"
       @content-select-change="items => (selectlist = items)"
    >
      <template #content_header>
        <ElButton type="primary" @click="handleExportPage" size="small" v-if="hasPermissionWithCode('048')">导出</ElButton>
        <el-button type="primary" size="small" @click="handleExport" v-if="hasPermissionWithCode('051')">下载模板</el-button>
        <el-button size="small" @click="() => push('/postSupplierOrder/batchImportSendOrder')" v-if="hasPermissionWithCode('050') && !isSuperUser">批量导入</el-button>
      </template>


      <template #content_action="row">
        <ElPopconfirm
              title="是否确认删除?"
              @confirm="onDelete(row)"
              v-if="hasPermissionWithCode('006') && Number(row.status) === 0 && !isSuperUser"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="primary"
                  :size="'mini'"
                  :icon="useRenderIcon('delete')"
                />
              </template>
        </ElPopconfirm>
      </template>
    </PageContainer>
  </PageAuth>
</template>
./content.config
