<template>
  <div>
    <BasicTable
      :rowSelection="{ type: 'checkbox' }"
      @register="registerTable"
      :clickToRowSelect="false"
    >
      <template #toolbar>
        <Authority value="api:yt:role:saveOrUpdateRoleInfoWithMenu:post">
          <a-button type="primary" @click="handleCreate">新增角色</a-button>
        </Authority>
        <Authority value="api:yt:role:delete">
          <Popconfirm
            title="您确定要批量删除数据"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleDeleteOrBatchDelete(null)"
          >
            <a-button type="primary" color="error" :disabled="hasBatchDelete"> 批量删除 </a-button>
          </Popconfirm>
        </Authority>
      </template>
      <template #status="{ record }">
        <Switch
          :checked="record.status === 1"
          :loading="record.pendingStatus"
          checkedChildren="启用"
          unCheckedChildren="禁用"
          @change="(checked:boolean)=>statusChange(checked,record)"
        />
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              auth: 'api:yt:role:saveOrUpdateRoleInfoWithMenu:update',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              auth: 'api:yt:role:delete',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              ifShow: record.roleType != RoleEnum.SYS_ADMIN,
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDeleteOrBatchDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <RoleDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, nextTick } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { delRole, getRoleListByPage, setRoleStatus } from '/@/api/system/system';
  import { useDrawer } from '/@/components/Drawer';
  import RoleDrawer from './RoleDrawer.vue';
  import { columns, searchFormSchema } from './role.data';
  import { RoleEnum } from '/@/enums/roleEnum';
  import { Authority } from '/@/components/Authority';
  import { useBatchDelete } from '/@/hooks/web/useBatchDelete';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Switch, Popconfirm } from 'ant-design-vue';

  export default defineComponent({
    name: 'RoleManagement',
    components: { BasicTable, RoleDrawer, TableAction, Authority, Switch, Popconfirm },
    setup() {
      const [registerDrawer, { openDrawer }] = useDrawer();
      function handleSuccess() {
        reload();
      }
      const [registerTable, { setProps, reload, setSelectedRowKeys }] = useTable({
        title: '角色列表',
        api: getRoleListByPage,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        pagination: {
          hideOnSinglePage: false,
        },
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });
      const { hasBatchDelete, handleDeleteOrBatchDelete, selectionOptions, resetSelectedRowKeys } =
        useBatchDelete(delRole, handleSuccess, setProps);
      selectionOptions.rowSelection.getCheckboxProps = (record: Recordable) => {
        // Demo:status为1的选择框禁用
        if (record.status === 1) {
          return { disabled: true };
        } else {
          return { disabled: false };
        }
      };
      nextTick(() => {
        setProps(selectionOptions);
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      const statusChange = async (checked, record) => {
        setProps({
          loading: true,
        });
        setSelectedRowKeys([]);
        resetSelectedRowKeys();
        const newStatus = checked ? 1 : 0;
        const { createMessage } = useMessage();
        try {
          await setRoleStatus(record.id, newStatus);
          if (newStatus) {
            createMessage.success(`启用成功`);
          } else {
            createMessage.success('禁用成功');
          }
        } finally {
          setProps({
            loading: false,
          });
          reload();
        }
      };

      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleSuccess,
        RoleEnum,
        hasBatchDelete,
        handleDeleteOrBatchDelete,
        statusChange,
      };
    },
  });
</script>
