<template>
  <div>
    <BasicDrawer v-bind="$attrs" @register="register" width="60%" title="字典值配置">
      <div style="background-color: #f0f2f5">
        <BasicTable @register="registerTable">
          <template #toolbar>
            <Authority value="api:yt:dictItem:post">
              <a-button type="primary" @click="handleCreate"> 新增字典值 </a-button>
            </Authority>
          </template>
          <template #action="{ record }">
            <TableAction
              :actions="[
                {
                  label: '编辑',
                  icon: 'clarity:note-edit-line',
                  auth: 'api:yt:dictItem:update',
                  onClick: handleEdit.bind(null, record),
                },
                {
                  label: '删除',
                  icon: 'ant-design:delete-outlined',
                  color: 'error',
                  auth: 'api:yt:dictItem:delete',
                  popConfirm: {
                    title: '是否确认删除',
                    confirm: handleDelete.bind(null, record),
                  },
                  ifShow: record.status == 0,
                },
              ]"
            />
          </template>
        </BasicTable>
        <ItemDrawer @register="registerDrawer" @success="handleSuccess" />
      </div>
    </BasicDrawer>
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { sysDictItemPage, deleteDictItem } from '/@/api/system/dict';
  import { BasicDrawer, useDrawer, useDrawerInner } from '/@/components/Drawer';
  import ItemDrawer from '/@/views/system/dict/item/ItemDrawer.vue';
  import { columns, searchFormSchema } from './dict.item.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import Authority from '/@/components/Authority/src/Authority.vue';

  export default defineComponent({
    name: 'ItemIndex',
    components: { BasicDrawer, BasicTable, ItemDrawer, TableAction, Authority },
    setup() {
      let dictId;
      const disabled = ref(false);
      const { createMessage } = useMessage();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [register] = useDrawerInner((data) => {
        dictId = data.data.id;
        if (
          data.data.dictCode === 'enabled_platform_admin_auth' ||
          data.data.dictCode === 'disabled_tenant_auth' ||
          data.data.dictCode === 'enabled_sysadmin_auth'
        ) {
          disabled.value = true;
        } else {
          disabled.value = false;
        }

        reload();
      });
      const [registerTable, { reload }] = useTable({
        title: '字典值列表',
        api: sysDictItemPage,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 140,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
        beforeFetch: getDictId,
        immediate: true,
      });

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

      /**
       * 获取父页面传递的dictId值
       * @param data 请求之前的参数
       */
      function getDictId(data) {
        Reflect.set(data, 'dictId', dictId);
      }

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

      function handleDelete(record: Recordable) {
        let ids = [record.id];
        deleteDictItem(ids).then((result) => {
          createMessage.success(result.message);
          handleSuccess();
        });
      }

      function handleSuccess() {
        reload();
      }
      return {
        register,
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        disabled,
      };
    },
  });
</script>
