<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined" v-auth="'gplxz-add'">添加下载任务</a-button>
        <a-button type="primary" @click="handleCreate" v-auth="'gplxz-create'">生成下载地址</a-button>
        <a-button  type="primary" preIcon="ant-design:export-outlined" v-auth="'gplxz-moban'" @click="onExportXls">模版下载</a-button>
        <a-button  type="primary"  v-auth="'gplxz-moban'" @click="handleDpwnHistory">历史下载</a-button>
        <!-- <j-upload-button  type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button> -->
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>

    <!-- 表单区域 -->
    <GplxzModal @register="registerModal" @success="handleSuccess"></GplxzModal>

    <!-- 历史下载区域 -->
    <DownHistoryModal @register="registerModalHistory" @success="handleSuccess"></DownHistoryModal>

    <!-- 查看下载地址 -->
    <a-modal v-model:visible="reviewOpen" title="查看下载地址" ok-text="确认" :footer="null" style="height:200px;">
      <p style="height:30px;"></p>
      <p style="padding-left:20px;">下载地址： {{reviewData.url}} <a-button v-if="reviewData.url" type="primary" @click="copyClick(reviewData.url)" >复制地址</a-button></p>
      <p style="padding-left:20px;">说明：</p>
      <p style="padding-left:40px;">1.请尽快保留地址，下载地址有效期为3天，超过有效期地址将不可用。</p>
      <p style="padding-left:40px;">2.下载文件过大时，会对服务器产生影响，因此建议在服务器闲时进行下载。</p>
      <p style="height:30px;"></p>
    </a-modal>
  </div>
</template>

<script lang="ts" name="zcgl-gplxz" setup>
  import { ref, computed, unref } from 'vue';
  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 { columns, searchFormSchema } from './Gplxz.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, saveOrUpdate  } from './Gplxz.api';
  import { message } from 'ant-design-vue';

  const reviewOpen = ref(false)
  const reviewData = ref({})
  //注册model
  const [registerModal, { openModal }] = useModal();
  const [registerModalHistory, { openModal:openModalHistory }] = useModal();
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '批量下载',
      api: list,
      columns,
      canResize: false,
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
      },
      actionColumn: {
        width: 180,
        fixed: 'right',
      },
      rowSelection: { type: 'radio' }, 
    },
    exportConfig: {
      name: '批量下载',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  });

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

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  /**
   * 下载历史事件
   */
  function handleDpwnHistory() {
    openModalHistory(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  
  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }
  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    reviewOpen.value = true;
    reviewData.value = record

    console.log(reviewOpen.value, reviewData.value)
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, reload);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, reload);
  }
  /**
   * 成功回调
   */
  function handleSuccess({ isUpdate, values }) {
    reload();
  }
  /**
   * 生成下载地址
   */
  function handleCreate() {
    if (selectedRows.value.length <1) {
      message.error(`请选择要生成下载地址的任务`);
      return
    }
    if (selectedRows.value[0].status==1 || selectedRows.value[0].status==2 ) {
      message.error(`该任务已在生成中或者已完成，不需要重复生成`);
      return
    }
    selectedRows.value[0].status = 1
    saveOrUpdate(selectedRows.value[0], true).then(res =>{
    }).catch(()=>{
      selectedRows.value = [];
      reload()
    })
  }
  /**
   * 复制门户
   * @param value
   */
  function copyClick(value) {
    // 创建input元素
    const el = document.createElement('input');
    // 给input元素赋值需要复制的文本
    el.setAttribute('value', value);
    // 将input元素插入页面
    document.body.appendChild(el);
    // 选中input元素的文本
    el.select();
    // 复制内容到剪贴板
    document.execCommand('copy');
    // 删除input元素
    document.body.removeChild(el);
    message.success('复制成功');
  };

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '删除',
        auth: 'gplxz-delete',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
        ifShow: () => {
          return !record.status;
        },
        auth: 'gplxz-edit',
      },
      {
        label: '查看地址',
        onClick: handleDetail.bind(null, record),
        ifShow: () => {
          return record.status==2;
        },
        auth: 'gplxz-review',
      },
      {
        label: '错误原因',
        popConfirm: {
          title: record.rest||'生成失败，请重新生成',
          showCancel: false
        },
        ifShow: () => {
          return record.status==3;
        },
        auth: 'gplxz-reason',
      },
    ];
  }
</script>
<style scoped></style>
