<template>
  <div>
    <BasicTable :clickToRowSelect="false" @register="registerTable">
      <template #toolbar>
        <Authority value="api:yt:dict:post">
          <a-button type="primary" @click="handleCreate"> 新增字典 </a-button>
        </Authority>
        <Authority value="api:yt:dict:delete">
          <Popconfirm
            title="您确定要批量删除数据"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleDeleteOrBatchDelete(null)"
          >
            <a-button color="error" :disabled="hasBatchDelete"> 批量删除 </a-button>
          </Popconfirm>
        </Authority>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '字典值配置',
              icon: 'ant-design:appstore-add-outlined',
              onClick: handleItem.bind(null, record),
            },
            {
              label: '编辑',
              auth: 'api:yt:dict:update',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              auth: 'api:yt:dict:delete',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDeleteOrBatchDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
      <template #dictCode="{ record }">
        <Tag color="green">
          {{ record.dictCode }}
        </Tag>
      </template>
    </BasicTable>
    <DictDrawer @register="registerDrawer" @success="handleSuccess" />
    <ItemIndex @register="registerItemDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, nextTick } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { sysDictPage, deleteDict } from '/@/api/system/dict';
  import { useDrawer } from '/@/components/Drawer';
  import DictDrawer from './DictDrawer.vue';
  import ItemIndex from './item/ItemIndex.vue';
  import { columns, searchFormSchema } from './dict.data';
  import { Tag, Popconfirm } from 'ant-design-vue';
  import { useBatchDelete } from '/@/hooks/web/useBatchDelete';
  import { Authority } from '/@/components/Authority';

  export default defineComponent({
    name: 'DictManagement',
    components: { BasicTable, DictDrawer, ItemIndex, TableAction, Tag, Authority, Popconfirm },
    setup() {
      const [registerDrawer, { openDrawer: openDrawer }] = useDrawer();
      const [registerItemDrawer, { openDrawer: openItemDrawer }] = useDrawer();

      const [registerTable, { reload, setProps }] = useTable({
        title: '字典配置列表',
        api: sysDictPage,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 300,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });
      const { hasBatchDelete, handleDeleteOrBatchDelete, selectionOptions } = useBatchDelete(
        deleteDict,
        handleSuccess,
        setProps
      );
      nextTick(() => {
        setProps(selectionOptions);
      });

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

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

      function handleItem(record: Recordable) {
        openItemDrawer(true, {
          data: record,
        });
      }

      function handleSuccess() {
        reload();
      }

      return {
        registerTable,
        registerDrawer,
        registerItemDrawer,
        handleCreate,
        handleEdit,
        handleItem,

        handleSuccess,
        hasBatchDelete,
        handleDeleteOrBatchDelete,
      };
    },
  });
</script>
