<template>
  <div>
    <BasicTable
      @register="registerTable"
      @selection-change="handleSelectionChange"
    >
      <template #toolbar>
        <a-button
          type="primary"
          :disabled="state.multiple"
          @click="handleAudit()"
        >驳回申请</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction :actions="[
            {
              label: '审核',
              icon: 'fa6-regular:pen-to-square',
              onClick: handleEdit.bind(null, record)
            },
            {
              label: '驳回',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              onClick: handleAudit.bind(null, record)
            }]"
          />
        </template>
      </template>
    </BasicTable>
    <!--弹出窗体区域-->
    <EnterpriseAuditModal @register="registerModal" @success="handleRefreshTable"/>
  </div>
</template>

<script lang="ts" setup>
  import { reactive, toRaw } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { listPushEnterprise, rejectAuthPushEnterprise } from '/@/api/platform/common/controller/pushEnterprise';
  import { useModal } from '/@/components/Modal';
  import EnterpriseAuditModal from './EnterpriseAuditModal.vue';
  import { columns, searchFormSchema } from '../pushEnterprise/enterprise.data';
  import { useMessage } from '/@/hooks/web/useMessage';

  /** 类型规范统一声明定义区域 */
  interface TableState {
    single: boolean;
    multiple: boolean;
  }

  /** 通用变量统一声明区域 */
  const state = reactive<TableState>({
    // 非单个禁用
    single: true,
    // 非多个禁用
    multiple: true
  });
  const { createConfirm, createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload, clearSelectedRowKeys, getSelectRowKeys }] = useTable({
    title: '企业审核列表',
    api: listPushEnterprise,
    rowKey: 'id',
    columns,
    formConfig: {
      compact: true,
      labelWidth: 80,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      autoAdvancedLine: 3,
      rowProps: { gutter: 4 },
      actionColOptions: {
        style: { textAlign: 'left' },
      }
    },
    rowSelection: { type: 'checkbox' },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    clickToRowSelect: false,
    showIndexColumn: false,
    actionColumn: {
      width: 220,
      title: '操作',
      dataIndex: 'action',
      fixed: false
    },
    searchInfo: {
      status: '0'
    },
    handleSearchInfoFn: () => clearSelectedRowKeys()
  });

  /** 处理多选框选中数据 */
  function handleSelectionChange(selection?: Recordable) {
    const rowSelection = toRaw(selection?.keys) || [];
    state.single = rowSelection.length != 1;
    state.multiple = !rowSelection.length;
  }

  /** 新增按钮操作,行内新增与工具栏局域新增通用 */
  function handleAdd() {
    openModal(true,{ _tag: 'add' });
  }

  /** 编辑按钮操作,行内编辑 */
  function handleEdit(record?: Recordable) {
    record = record || { id: getSelectRowKeys() };
    openModal(true, { _tag: 'edit', record });
  }

  async function handleAudit(record?: Recordable) {
    const ids = record?.id || getSelectRowKeys();
    createConfirm({
      iconType: 'warning',
      title: '警告',
      content: `是否确认驳回编号为${ids}的数据?`,
      onOk: async () => {
        await rejectAuthPushEnterprise(ids);
        createMessage.success('驳回成功!');
        handleRefreshTable();
      }
    });
  }

  /** 处理表格刷新 */
  function handleRefreshTable() {
    clearSelectedRowKeys();
    reload();
  }

</script>
