<script setup lang="ts">
  import { PageWrapper } from '/@/components/Page';
  import { useTable, TableAction, BasicTable } from '/@/components/Table';
  import { TableColumns } from './components/data.config';
  import formEdit from './components/formEdit.vue';
  import FormDetail from './components/detail.vue';
  import { useDrawer } from '/@/components/Drawer';
  import { ref } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { searchFormSchema } from './components/data.config';
  import {
    getTextbookWarehouseRecordExportQuery,
    getTextbookWarehouseRecordPage,
  } from '/@/services/apis/TextbookWarehouseRecordController';
  import { downloadByData } from '/@/utils/file/download';
  import { formatToDate } from '/@/utils/dateUtil';
  import { useModal } from '/@/components/Modal';
  import FormImport from '/@/views/sys/import/index.vue';
  import FormStore from './components/store.vue';
  import ListStore from './components/storeList.vue';
  import FormDiscount from './components/editDiscount.vue';
  import DiscountList from './components/discountList.vue';
  import {
    postTextbookSubscriptionInstockroomExcelImport,
    postTextbookSubscriptionSubscriptionInstockroomExportQuery,
  } from '/@/services/apis/TextbookSubscriptionController';

  const [formReg, {}] = useDrawer();
  const [formDetailReg, { openModal }] = useModal();
  const [formStoreRegister, { openDrawer }] = useDrawer();
  const [registerImportModal, { openModal: openImportModal }] = useModal();
  const [formStoreListRegister, { openModal: openStoreListModal }] = useModal();
  const [formDiscountRegister, { openModal: openDiscountModal }] = useModal();
  const [formDiscountListRegister, { openModal: openDiscountListModal }] = useModal();
  const selectedRowKeysList = ref<string[] | number[]>([]);
  const [registerTable, { reload, getForm }] = useTable({
    api: getTextbookWarehouseRecordPage,
    rowKey: 'id',
    columns: TableColumns,
    title: '教材入库记录',
    bordered: true,
    showTableSetting: true,
    beforeFetch: (params) => {
      return params;
    },
    rowSelection: {
      type: 'checkbox',
      fixed: true,
      onChange: (selectedRowKeys) => {
        selectedRowKeysList.value = selectedRowKeys;
      },
    },
    useSearchForm: true,
    formConfig: {
      labelWidth: 100,
      schemas: searchFormSchema,
    },

    actionColumn: {
      width: 100,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  });
  const { createMessage } = useMessage();

  const handleExport = async () => {
    const fromData = getForm().getFieldsValue();
    const data = await getTextbookWarehouseRecordExportQuery(fromData);
    downloadByData(data.data, `教材入库记录${formatToDate(new Date())}.xlsx`);
    createMessage.success('导出成功');
  };

  const handleView = (record) => {
    openModal(true, {
      baseData: { id: record.id },
    });
  };

  const handelImport = () => {
    openImportModal(true, {
      baseData: {
        title: '教材导入',
        upload: postTextbookSubscriptionInstockroomExcelImport,
        params: {},
        errorName: '教材导入错误记录',
        download: postTextbookSubscriptionSubscriptionInstockroomExportQuery,
        // templatePath:
        //   'https://zhxy.cqtlzjzx.com/minio/static/resources/%E7%99%BD%E5%90%8D%E5%8D%95%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xlsx',
        templateName: '教材导入模板',
      },
    });
  };

  const handelStore = () => {
    openDrawer(true, {});
  };

  const handleStoreList = (record, type) => {
    openStoreListModal(true, {
      baseData: { id: record.id },
      type: type,
    });
  };
  const handleEditDiscount = (record) => {
    openDiscountModal(true, {
      baseData: { ...record },
    });
  };
  const handleDiscountList = (record) => {
    openDiscountListModal(true, {
      baseData: { ...record },
    });
  };
</script>

<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handelStore"> 教材入库 </a-button>
        <a-button type="primary" @click="handelImport"> 教材导入 </a-button>
        <!-- <a-button type="primary" @click="handleExport()"> 导出指定记录 </a-button> -->
        <a-button type="primary" @click="handleExport">导出记录</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            // {
            //   label: '查看',
            //   onClick: handleView.bind(null, record),
            // },
            {
              label: '修改折扣',
              onClick: handleEditDiscount.bind(null, record),
            },
          ]"
        />
      </template>
      <template #warehouseNumber="{ record }">
        <a @click="handleStoreList(record, 1)">
          {{ record.warehouseNumber }}
        </a>
      </template>
      <template #recedeNumber="{ record }">
        <a @click="handleStoreList(record, 2)">
          {{ record.recedeNumber }}
        </a>
      </template>
      <template #discount="{ record }">
        <a @click="handleDiscountList(record)">
          {{ record.discount }}
        </a>
      </template>
    </BasicTable>
    <formEdit @register="formReg" @success="reload" />
    <FormDetail @register="formDetailReg" />
    <FormImport @register="registerImportModal" @success="reload" />
    <FormStore @register="formStoreRegister" @success="reload" />
    <ListStore @register="formStoreListRegister" />
    <FormDiscount @register="formDiscountRegister" @success="reload" />
    <DiscountList @register="formDiscountListRegister" />
  </PageWrapper>
</template>

<style scoped lang="less"></style>
