<template>
  <div>
    <div class="header">
      <SvgIcon size="19" name="list" />
      <div class="title">用户列表</div>
    </div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" :icon="h(PlusOutlined)">新 增</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
                divider: true,
                disabled: record.username === 'admin'
              },
              {
                label: '删除',
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  confirm: handleDelete.bind(null, record),
                  placement: 'topRight',
                },
                disabled: record.username === 'admin'
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <RoleDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import {defineComponent, h, onMounted} from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import * as UserApi from '/@/api/system/userApi';
  import { useDrawer } from '/@/components/Drawer';
  import RoleDrawer from './drawer.vue';
  import { columns, searchFormSchema } from './user.data';
  import {useMessage} from "@/hooks/web/useMessage";
  import {SvgIcon} from "@/components/Icon";
  import {PlusOutlined} from "@ant-design/icons-vue";
  export default defineComponent({
    name: 'UserPage',
    methods: {PlusOutlined, h},
    components: {SvgIcon, BasicTable, RoleDrawer, TableAction },
    setup() {
      const { createMessage } = useMessage();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload }] = useTable({
        api: UserApi.search,
        columns,
        formConfig: {
          labelWidth: 0,
          schemas: searchFormSchema,
          showAdvancedButton: false,
        },
        useSearchForm: true,
        showTableSetting: false,
        bordered: true,
        showIndexColumn: false,
        canResize: false,
        pagination: false,
        actionColumn: {
          width: 180,
          title: '操作',
          dataIndex: 'action',
          fixed: undefined,
        },
      });

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

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

      function handleDelete(record: Recordable) {
        UserApi.remove(record.id).then(_ => {
          createMessage.success('删除用户成功！');
          reload();
        }, (err) =>{
          createMessage.error(`删除用户失败：${err}`);
        })
      }

      function handleSuccess() {
        reload();
      }

      onMounted(() => {
        WebViewService.setIsLoading(false).then(() => {});
      });

      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
      };
    },
  });
</script>
<style lang="scss" scoped>
  @use '@/assets/custom.scss';
</style>
