<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 } from './BaosongTaskReview.api';
  import BaosongApproveModal from './components/BaosongApproveModal.vue';
  import { defHttp } from '/@/utils/http/axios';    

  const checkedKeys = ref<Array<string | number>>([]);

  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;

  let url = window.location.href;
  console.log("----------- ",url); // 输出当前路由的完整路径
  let intNumber = url.indexOf("Token=");
  intNumber=intNumber+6;
  let cctoken=url.substring(intNumber);
  console.log("----------- cctoken",cctoken); // 输出当前路由的完整路径
  const allReportDataUrl = "/api/jflow/getUserTokey"
  const allReportData = ref();
 
  const gettoken = async () => {
  allReportData.value  = await defHttp.get({url: allReportDataUrl, params:{Token:cctoken}});
  }

  onMounted(async () => {  
    await gettoken();  
    console.log("----------- gettoken ",allReportData.value); 
  }); 

  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }

  function handleEdit(record: Recordable) {
   record['token']=allReportData.value;
    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: handleApproval.bind(null, record),
      },

      {
        label: '下载',
        onClick: downLoadXml.bind(null, record),
      },
      {
        label: '归档',
        onClick: downLoadXml.bind(null, record),
      },
    ];
  }

  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },

      {
        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) {
    //{id: record.id}
    record['token']=allReportData.value;
    let retData = await downLoadTaskXml(record);
    
    goToFile(retData);
  }

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

  function goToFile(filepath) {
    if (!!filepath) {
      let url = getFileAccessHttpUrl(filepath);
      window.open(url);
    } else {
      message.warning('没有找到要下载的文件');
    }
  }
</script>

<style scoped></style>
