<template>
  <div>
    <BasicTable @register="registerTable"
                @fetch-success="handleSelectionChange"
                @selection-change="handleSelectionChange"
    >
      <template #toolbar>
        <a-button v-auth="['role_add']"
                  type="primary"
                  @click="handleAdd()"
        >新增角色</a-button>
        <a-button v-auth="['role_edit']"
                  type="primary"
                  :disabled="state.single"
                  @click="handleEdit()"
        >修改角色</a-button>
        <a-button v-auth="['role_del']"
                  type="primary"
                  :disabled="state.multiple"
                  @click="handleDel()"
        >删除角色</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="[
          {
            label: '编辑',
            icon: 'fa6-regular:pen-to-square',
            auth: ['role_edit'],
            onClick: handleEdit.bind(null, record)
          },
          {
            label: '删除',
            icon: 'ant-design:delete-outlined',
            auth: ['role_del'],
            color: 'error',
            onClick: handleDel.bind(null, record)
          }]"
        />
      </template>
    </BasicTable>
    <RoleDrawer @register="registerDrawer" @success="handleSuccess"/>
  </div>
</template>
<script lang="ts" setup>
  /**
   * Copyright © 2020-2022 <a href="http://www.entfrm.com/">entfrm</a> All rights reserved.
   * author entfrm开发团队-王翔
   */
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { listRole, delRole } from '/@/api/system/role';
  import { useDrawer } from '/@/components/Drawer';
  import RoleDrawer from './RoleDrawer.vue';
  import { columns, searchFormSchema } from './role.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { reactive, toRaw } from 'vue';

  const { createConfirm } = useMessage();
  const { createMessage } = useMessage();

  const state = reactive({
    // 选中数组
    ids: [],
    // 非单个禁用
    single: true,
    // 非多个禁用
    multiple: true
  });

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    title: '角色列表',
    api: listRole,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    rowSelection: { type: 'checkbox' },
    showIndexColumn: false,
    clickToRowSelect: false,
    actionColumn: {
      width: 220,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: false
    }
  });

  /** 处理多选框选中数据 */
  function handleSelectionChange(selection?: Recordable) {
    const rawRows = toRaw(selection?.rows) || [];
    state.ids = rawRows.map(item => item.id);
    state.single = rawRows.length != 1;
    state.multiple = !rawRows.length;
  }

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

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

  /** 删除按钮操作,行内删除 */
  async function handleDel(record?: Recordable) {
    const ids = record?.id || toRaw(state.ids);
    createConfirm({
      iconType: 'warning',
      title: '警告',
      content: `是否确认删除角色编号为${ids}角色吗?`,
      onOk: async () => {
        await delRole(ids);
        createMessage.success('删除成功!');
        handleSuccess();
      }
    });
  }

  /** 处理表单提交成功 */
  function handleSuccess() {
    reload();
  }

</script>
