<template>
  <div>
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleCreate"> 新增角色 </a-button> -->
        <!-- <a-button type="primary" @click="handleItem"> 获取选中行 </a-button> -->
        <PermissionBtn @btnEvent="onBtnClicked"></PermissionBtn>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: viewAccount.bind(null, record),
              },
            ]" />
        </template>
      </template>
    </BasicTable>
    <RoleModal @register="registerModal" @success="handleSuccess" />
    <ModulesModal @register="registerModulesModal" @success="handleSuccess" />
    <AccountModal @register="registerAccountModal" @success="handleSuccess" />
    
  </div>
</template>
<script lang="ts" setup>
  import { reactive } from 'vue';

  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getRoleListByPage, deleteRole } from '@/api/demo/system';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '@/components/Modal';
  import RoleModal from './RoleModal.vue';
  import ModulesModal from './ModulesModal.vue';
  import AccountModal from './AccountModal.vue';
  import PermissionBtn from '@/components/PermissionBtn/index.vue'

  import { columns, searchFormSchema } from './role.data';

  defineOptions({ name: 'RoleManagement' });

  const { createConfirm, createMessage } = useMessage();
  const [registerModal, { openModal: openRoleModal }] = useModal();
  const [registerModulesModal, { openModal: openModulesModal }] = useModal();
  const [registerAccountModal, { openModal: openAccountModal }] = useModal();
  const searchInfo = reactive < Recordable > ({});

  const [registerTable, { reload, getSelectRows, clearSelectedRowKeys }] = useTable({
    // 表格名称
    title: '角色列表',
    // 获取数据的接口
    api: getRoleListByPage,
    // 表单列信息 BasicColumn[]
    columns,
    rowKey: 'id',
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    // 使用搜索表单
    useSearchForm: true,
    // 显示表格设置工具
    showTableSetting: true,
    // 是否显示表格边框
    bordered: true,
    // 序号列
    showIndexColumn: false,
    // 勾选列
    rowSelection: {//多选框
      // type: 'checkbox',
      type: 'radio',
    },
    // 搜索
    handleSearchInfoFn(info) {
      return info;
    },
    actionColumn: {
      width: 80,
      title: '查看账号',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
      fixed: undefined,
    },
  });

  function handleCreate() {
    openRoleModal(true, {
      isUpdate: false,
    });
  }
  function viewAccount(record: Recordable) {
    openAccountModal(true, {
      record,
    });
  }

  async function handleItem() {
    let rows = getSelectRows();
    if (!rows.length) {
      return createMessage.warn('请勾选对应项');
    }
    let idList = [] as any;
    rows.forEach((item) => {
      idList.push(item.id);
    });
  }

  function handleEdit() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请勾选一个角色进行编辑');
    }
    const record = rows[0]
    openRoleModal(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请勾选一个角色进行删除');
    }
    const query = [rows[0].id]
    createConfirm({
      iconType: 'info',
      title: '删除',
      content: '确定要删除当前角色吗',
      onOk: async () => {
        const data = await deleteRole(query);
        handleSuccess()
      },
    });
  }

  function handleSuccess() {
    clearSelectedRowKeys()
    reload();
  }
  function onBtnClicked(domId) {
    switch (domId) {
      case 'btnAdd':
        handleCreate()
        break;
      case 'btnEdit':
        handleEdit()
        break;
      case 'btnDelete':
        handleDelete()
        break;
      case 'btnModules':
        let rows = getSelectRows();
        if (rows.length == 0) {
          return createMessage.warn('请勾选一个角色进行编辑');
        }
        const record = rows[0]
        openModulesModal(true, {
          record,
        });
        break;
      default:
        break;
    }
  }
</script>