<template>
  <div>
    <!--引用表格-->
   <BasicTable @register="registerTable" :rowSelection="rowSelection" :loading="loading">
     <!--插槽:table标题-->
      <template #tableTitle>
          <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
          <a-button  type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
          <j-upload-button  type="primary" preIcon="ant-design:import-outlined" @click="onImportXml">导入模板内容[XML]</j-upload-button>

          <a-button type="primary" @click="handleImportDes" preIcon="ant-design:plus-outlined"> 导入填报说明</a-button>

          <a-dropdown v-if="selectedRowKeys.length > 0">
              <template #overlay>
                <a-menu>
                  <a-menu-item key="1" @click="batchHandleDelete">
                    <Icon icon="ant-design:delete-outlined"></Icon>
                    删除
                  </a-menu-item>
                </a-menu>
              </template>
              <a-button>批量操作
                <Icon icon="mdi:chevron-down"></Icon>
              </a-button>
        </a-dropdown>
      </template>
       <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{text}">
         <div v-html="text"></div>
      </template>
      <template #fileSlot="{text}">
         <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
         <!-- <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">下载</a-button> -->
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <BaosongTplModal @register="registerModal" @success="handleSuccess"></BaosongTplModal>
    <BaosongTplDrawer ref="refBaosongTplDrawer"></BaosongTplDrawer>
  </div>
</template>

<script lang="ts" name="baosong-baosongTpl" setup>
  import {ref, computed, unref} from 'vue';
  import {BasicTable, useTable, TableAction} from '/@/components/Table';
  import {useModal} from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useListPage } from '/@/hooks/system/useListPage'
  import BaosongTplModal from './components/BaosongTplModal.vue'
  import {columns, searchFormSchema} from './BaosongTpl.data';
  import {list, deleteOne, batchDelete, getImportUrl,getExportUrl,importTplByXml,importTplItemDes} from './BaosongTpl.api';

  import BaosongTplDrawer from './components/BaosongTplDrawer.vue'
  

  const {createMessage,createConfirm } = useMessage();
  const loading = ref(false)
  const refBaosongTplDrawer = ref()
  const refBaosongTplCfgDrawer = ref()

  const [registerModal, {openModal}] = useModal();

  const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({
      tableProps:{
           title: '报送模板',
           api: list,
           columns,
           canResize:false,
           formConfig: {
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
              ],
            },
            defSort :{
             column: 'tp',
             order: 'asc',
            },
           actionColumn: {
               width: 120,
               fixed:'right'
            },
            // rowSelection:{
            //   type:"radio"
            // }
      },
      exportConfig: {
        name:"报送模板",
        url: getExportUrl,
      },
      importConfig: {
        url: getImportUrl,
        success: handleSuccess
      },
  })

  const [registerTable, {reload},{ rowSelection, selectedRowKeys,selectedRows }] = tableContext

   /**
    * 新增事件
    */
  function handleAdd() {
     openModal(true, {
       isUpdate: false,
       showFooter: true,
     });
  }
   /**
    * 编辑事件
    */
  function handleEdit(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: true,
     });
   }
   /**
    * 详情
   */
  function handleDetail(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: false,
     });
   }
   /**
    * 删除事件
    */
  async function handleDelete(record) {
     await deleteOne({id: record.id}, handleSuccess);
   }
   /**
    * 批量删除事件
    */
  async function batchHandleDelete() {
     await batchDelete({ids: selectedRowKeys.value}, handleSuccess);
   }
   /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }
   /**
      * 操作栏
      */
  function getTableAction(record){
       return [
         {
           label: '编辑',
           onClick: handleEdit.bind(null, record),
         },
         {
           label: '模板项管理',
           onClick: showTplItems.bind(null, record),
         }
       ]
   }
     /**
        * 下拉操作栏
        */
  function getDropDownAction(record){
       return [
         {
           label: '详情',
           onClick: handleDetail.bind(null, record),
         }, {
           label: '删除',
           popConfirm: {
             title: '是否确认删除',
             confirm: handleDelete.bind(null, record),
           }
         }
       ]
   }

  function onImportXml(file) {

    if(!selectedRows.value[0] || selectedRows.value.length>1) {
      createMessage.info("只能选择一条记录！");
      return
    }
    let row = selectedRows.value[0]

    
    let msg = ""
    if(file.file.name.indexOf(row.code)==-1){
      msg = "您选择的模板与您上传的xml不对应，是否继续";
    } else {
      if(row.childNum>0) {
        msg = "继续执行此操作，将要会覆盖原来的数据，是否继续？"
      } else {
        msg = "您是否确认从选择的文件中导入数据？"
      }
    }

    createConfirm({
      iconType: 'warning',
      title: "导入确认",
      content: msg,
      okText: '确认',
      cancelText: '取消',
      onOk: () => {
       
        loading.value = true
        importTplByXml(file,row,function(){
          loading.value = false
          handleSuccess()
        });
      }
    });
  }

  function showTplItems(Tpl) {
    refBaosongTplDrawer.value.setIniData(Tpl)
  }


  async function handleImportDes() {
    loading.value = true
    await importTplItemDes({});
    loading.value = false
  }

</script>

<style scoped>

</style>