<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <BaosongApproveModal @register="registerAppModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="baosong-baosongTaskReview" setup>
  import { ref,nextTick } from 'vue'; 
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';

  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns, searchFormSchema } from './BaosongTaskReview.data';
  import { list, deleteOne, batchDelete, downLoadTaskXml, saveOrUpdate } from './BaosongTaskReview.api';
  import BaosongApproveModal from './components/BaosongApproveModal.vue';
  import { useRoute,useRouter } from 'vue-router';
 
  const route = useRoute();
  const router = useRouter();

  const [registerModal, { openModal }] = useModal();
  const [registerAppModal, { openModal: openAppModal }] = useModal();
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '',
      api: list,
      columns,
      autoCreateKey: true,
      showIndexColumn: true,
      canResize: false,
      pagination: true,
      rowKey: setRowkey,
      formConfig: {
        labelWidth: 160,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 240,
        fixed: 'right',
      },
      rowSelection: {
        type: 'radio',
        onChange: (selectedRowKeys, selectedRows) => {
          console.log('Selected Row Keys: ', selectedRowKeys);
          console.log('Selected Rows: ', selectedRows);
        },
      },

      beforeFetch(params) {
        params = Object.assign(params,{ token: allReportData.value})
      },
    },
  });

  
  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;
  
  const allReportData = ref();
 
  function handleEdit(record: Recordable) { 
   record.appSta=9;
   saveOrUpdate(record,true).then((res) => {
      handleSuccess();
   })
  }

  function handleDetail(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }

  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }


  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  async function handlePreview(record) {
    alert(JSON.stringify(record));
    await gotoPage(record);
  }

  function getTableAction(record) {
    return [
      {
        label: '审核',
        onClick: handleApproval.bind(null, record),
      },
      {
        label: '预览',
        onClick: handlePreview.bind(null, record),
      },
      {
        label: '归档',
        disabled:!(record.appSta==1),
        onClick: handleEdit.bind(null, record),
      },
    ];
  }

  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }

  function handleApproval(record: Recordable) {
    openAppModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }

  async function downLoadXml(record) {
    let retData = await downLoadTaskXml(record);
    handleDownloadFile(retData);
  }

  function setRowkey(row) {
    return row.taskid + '_' + row.tplid;
  }

const handleDownloadFile = (filePath) => {
  const globOnlineViewUrl: string = import.meta.env.VITE_GLOB_ONLINE_VIEW_URL;
  const url = globOnlineViewUrl + filePath;
  if (url) {
    window.open(url);
  }
};

const gotoPage = async (item) => {
 
  let targetPath = ''
  const tplid = item.tplid
  const taskid = item.taskid
  if(tplid === 7) {
    targetPath = '/baosong/report/components/Tb1'
  } else if(tplid === 8) {
    targetPath = '/baosong/report/components/Tb2'
  } else if(tplid === 9) {
    targetPath = '/baosong/report/components/Tb3'
  } else if(tplid === 10) {
    targetPath = '/baosong/report/components/Tb4'
  } else if(tplid === 11) {
    targetPath = '/baosong/report/components/Tb5'
  } else if(tplid === 12) {
    targetPath = '/baosong/report/components/Tb6'
  } else if(tplid === 13) {
    targetPath = '/baosong/report/components/Tb7'
  } else if(tplid === 14) {
    targetPath = '/baosong/report/components/Tb8'
  } else if(tplid === 15) {
    targetPath = '/baosong/report/components/Tb9'
  } else if(tplid === 16) {
    targetPath = '/baosong/report/components/Tb10'
  } else if(tplid === 23) {
    targetPath = '/baosong/report/components/Tb11'
  } else if(tplid === 25) {
    targetPath = '/baosong/report/components/Rr1'
  } else if(tplid === 1) {
    targetPath = '/baosong/report/components/Tm1'
  } else if(tplid === 20) {
    targetPath = '/baosong/report/components/Tm2'
  } else if(tplid === 2) { 
    targetPath = '/baosong/report/components/Tq1'
  } else if(tplid === 3) {
    targetPath = '/baosong/report/components/Tq2'
  } else if(tplid === 4) {
    targetPath = '/baosong/report/components/Tq3'
  } else if(tplid === 5) { 
    targetPath = '/baosong/report/components/Tq4'
  }
  
  if (targetPath) {
    await nextTick();
    router.push({
      path: targetPath,
      query: {
        taskId: taskid,
        tplId: tplid,
        comfrom: 'archive'
      }
    });
  }
}
</script>

<style scoped></style>
