<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="80%" @visibleChange="handleBack">
    <template #footer></template>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from '../Gplxz.data';
  import { UploadOutlined } from '@ant-design/icons-vue';
  import { getToken } from '/@/utils/auth';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import GplxzModal from './modules/GplxzModal.vue';
  import DownHistoryModal from './modules/DownHistoryModal.vue';
  import { columnsHis, searchFormSchema } from '../Gplxz.data';
  import { list, getImportUrl, getExportUrl, saveOrUpdate } from '../Gplxz.api';
  import { message } from 'ant-design-vue';
  import { downloadFile } from '/@/api/common/api';
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '批量下载',
      api: list,
      columns:columnsHis,
      canResize: false,
      beforeFetch: async (params) => {
        params.delFlag = 1;
      },
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
      },
      actionColumn: {
        width: 100,
        fixed: 'right',
      },
      rowSelection: { type: 'radio' }, 
    },
    exportConfig: {
      name: '批量下载',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRows, selectedRowKeys }] = tableContext;
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 150,
    schemas: formSchema,
    showActionButtonGroup: false,
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    
  });
  //设置标题
  const title = computed(() => (!unref(isUpdate) ? '下载历史' : '下载历史'));
  /**
   * 下载附件
   */
  async function handleDetail(record) {
    let fileExtension = record.dkbmFile.split('.').pop();
    downloadFile('/sys/common/static' + record.dkbmFile, `${record.rwmc}.${fileExtension}`);
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '下载附件',
        onClick: handleDetail.bind(null, record),
        ifShow: () => {
          return record.dkbmFile;
        },
      },
    ];
  }
</script>

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