<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, computed,onMounted, unref } from 'vue'; 
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { message } from 'ant-design-vue';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { getFileAccessHttpUrl } from '/@/utils/common/compUtils';
  import { columns, searchFormSchema } from './BaosongTaskReview.data';
  import { list, deleteOne, batchDelete, downLoadTaskXml, saveOrUpdate } from './BaosongTaskReview.api';
  import BaosongApproveModal from './components/BaosongApproveModal.vue';

  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();
  }

  function getTableAction(record) {
    return [
      {
        label: '审核',
        onClick: handleApproval.bind(null, record),
      },
      // {
      //   label: '下载',
      //   onClick: downLoadXml.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);
    }
  };
</script>

<style scoped></style>
