<template>
  <div>
    <BasicTable
      @register="registerTable"
      @selection-change="handleSelectionChange"
    >
      <template #toolbar>
        <a-button
          v-auth="['client_add']"
          type="primary"
          @click="handleAdd()"
        >新增客户端</a-button>
        <a-button
          v-auth="['client_edit']"
          type="primary"
          :disabled="state.single"
          @click="handleEdit()"
        >修改客户端</a-button>
        <a-button
          v-auth="['client_del']"
          type="primary"
          :disabled="state.multiple"
          @click="handleDel()"
        >删除客户端</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                icon: 'fa6-regular:pen-to-square',
                auth: ['client_edit'],
                onClick: handleEdit.bind(null, record)
              },
              {
                label: '删除',
                icon: 'ant-design:delete-outlined',
                color: 'error',
                auth: ['client_del'],
                onClick: handleDel.bind(null, record)
              }]"
          />
        </template>
      </template>
    </BasicTable>
  </div>
</template>

<script lang="ts" setup>
  /**
   * 提供模板规范代码参考,请尽量保证编写代码风格跟模板规范代码一致
   * 采用vben-动态表格表单封装组件编写,采用 setup 写法
   * Copyright © 2020-2022 <a href="http://www.entfrm.com/">entfrm</a> All rights reserved.
   * author entfrm开发团队-王翔
   */
  import { reactive, toRaw } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { listClient, delClient } from '/@/api/platform/system/controller/client';
  import { useModal } from '/@/components/Modal';
  import { columns, searchFormSchema } from './formJson.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: listClient,
    rowKey: 'clientId',
    columns,
    formConfig: {
      compact: true,
      labelWidth: 100,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      autoAdvancedLine: 3,
      fieldMapToTime: [['dateRange', ['beginTime', 'endTime'], 'YYYY-MM-DD']]
    },
    rowSelection: { type: 'checkbox' },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    clickToRowSelect: false,
    showIndexColumn: false,
    actionColumn: {
      width: 220,
      title: '操作',
      dataIndex: 'action',
      fixed: false
    },
    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 || { clientId: getSelectRowKeys() };
    openModal(true, { _tag: 'edit', record });
  }

  /** 删除按钮操作,行内删除 */
  async function handleDel(record?: Recordable) {
    const clientIds = record?.clientId || getSelectRowKeys();
    createConfirm({
      iconType: 'warning',
      title: '警告',
      content: `是否确认删除客户端编号为${clientIds}客户端吗?`,
      onOk: async () => {
        await delClient(clientIds);
        createMessage.success('删除成功!');
        handleRefreshTable();
      }
    });
  }

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