<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :ellipsis="false">
      <!--插槽:table标题-->
      <template #tableTitle>
        <!-- <a-button type="primary" @click="handleAdd" v-auth="'monitor-add'" preIcon="ant-design:plus-outlined"> 新增</a-button> -->
        <a-button type="primary" preIcon="ant-design:export-outlined" v-auth="'monitor-export'" @click="onExportXlsx"> 导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" v-auth="'monitor-import'" @click="onImportXls">导入</j-upload-button>
        <a-button type="primary" v-auth="'monitor-import-his'" @click="onImportXlsHis"> 导入历史</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
        <!-- :dropDownActions="getDropDownAction(record)" -->
      </template>
    </BasicTable>

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

   <!-- 历史 -->
   <RateHis @register="registerHisModal"></RateHis>
  </div>
</template>

<script setup lang="ts">
  import { ActionItem, BasicTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import SaveModal from './modules/SaveModal.vue';
  import RateHis from './modules/RateHis.vue';
  import { useModal } from '/@/components/Modal';
  import { list, getExportUrl, getImportUrl, del } from './api';
  import { columns, searchFormSchema } from './data';
  import { useRouter } from 'vue-router';

  //注册model
  const [registerModal, { openModal }] = useModal();
  const [registerHisModal, { openModal: openHisModal }] = useModal();
  // 列表页面公共参数、方法
  const { tableContext, onExportXlsx, onImportXls } = useListPage({
    tableProps: {
      title: '重点建设用地安全利用率',
      api: list,
      columns,
      canResize: false,
      actionColumn: {
        width: 160,
        fixed: 'right',
      },
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
    },
    exportConfig: {
      name: '重点建设用地安全利用',
      url: getExportUrl,
    },
    importConfig: {
      name: '重点建设用地安全利用',
      url: getImportUrl,
    },
  });
  //BasicTable绑定注册
  const [registerTable, { reload }, {}] = tableContext;

  /**
   * 成功回调
   */
  function handleSuccess() {
    reload();
  }
  // 新增
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  // 查看
  function handleView(record) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }
  /**
   * 编辑事件
   */
  function handleEdit(record) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }
  //   删除
  async function handleDelete(record) {
    await del({ id: record.id });
    handleSuccess();
  }
  function onImportXlsHis() {
    openHisModal(true, {
      isUpdate: true,
      showFooter: true,
    });
  }
  
  /**
   * 操作栏
   */
  function getTableAction(record: any): ActionItem[] {
    return [
      {
        label: '详情',
        auth: 'monitor-view',
        onClick: handleView.bind(null, record),
      },
      // {
      //   label: '删除',
      //   auth: 'monitor-del',
      //   popConfirm: {
      //     title: '是否确认删除',
      //     confirm: handleDelete.bind(null, record),
      //   },
      // },
      {
        label: '编辑',
        auth: 'monitor-edit',
        onClick: handleEdit.bind(null, record),
      },
    ];
  }
  // 下来
  function getDropDownAction(record): ActionItem[] {
    return [
      {
        label: '编辑',
        auth: 'monitor-edit',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '删除',
        auth: 'monitor-del',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }
</script>
