<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button primary color="error" @click="handleCreate"> 新增商品分类</a-button>
        <a-button ghost color="warning" @click="expandAll">展开全部</a-button>
        <a-button type="primary" @click="collapseAll">折叠全部</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <AddCategoryModal @register="registeraddCategoryModal" @update="reload" />
    <EditCategoryModal @register="registerEditCategoryModal" @update="reload" />
  </div>
</template>

<script lang="ts">
  import { defineComponent, h, nextTick } from 'vue';
  import { BasicTable, useTable, BasicColumn, TableAction } from '/@/components/Table';
  import { categoryApi, deleteCategoryApi } from '/@/api/product/category';
  import { Tag } from 'ant-design-vue';

  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { makeTree } from '/@/utils/treeUtils';

  import AddCategoryModal from './addCategoryModal.vue';
  import EditCategoryModal from './editCategoryModal.vue';
  import { useModal } from '/@/components/Modal';

  export default defineComponent({
    components: { BasicTable, TableAction, AddCategoryModal, EditCategoryModal },
    setup() {
      const { createMessage } = useMessage();

      const { t } = useI18n();

      const columns: BasicColumn[] = [
        {
          title: 'ID',
          dataIndex: 'id',
          fixed: 'left',
          width: 100,
        },
        {
          title: '分类名称',
          dataIndex: 'name',
          width: 200,
          align: 'left',
        },
        {
          title: '排序',
          dataIndex: 'orderNo',
          width: 50,
        },
        {
          title: '级数',
          dataIndex: 'level',
          width: 50,
        },
        {
          title: '状态',
          dataIndex: 'isShow',
          width: 80,
          customRender: ({ record }) => {
            const status = record.isShow;
            const enable = status === 1;
            const color = enable ? 'green' : 'red';
            const text = enable ? '启用' : '停用';
            return h(Tag, { color: color }, () => text);
          },
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          width: 180,
        },
      ];
      const [registeraddCategoryModal, { openModal: openAddCategoryModal }] = useModal();
      const [registerEditCategoryModal, { openModal: openEditategoryModal }] = useModal();
      const [registerTable, { reload, expandAll, collapseAll }] = useTable({
        title: '商品分类',
        api: categoryApi,
        columns: columns,
        isTreeTable: true,
        useSearchForm: true,
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        showIndexColumn: true,
        pagination: false,
        rowKey: 'id',
        bordered: true,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
        afterFetch(data) {
          const treeData = makeTree(data);
          return treeData;
        },
      });

      function handleCreate(record: Recordable) {
        openAddCategoryModal(true, record);
      }
      function handleEdit(record: Recordable) {
        openEditategoryModal(true, record);
      }
      async function handleDelete(record: Recordable) {
        await deleteCategoryApi([record.id]);
        createMessage.success(t('common.optSuccess'));
        nextTick(reload);
      }
      return {
        registerTable,
        createMessage,
        t,
        handleCreate,
        handleEdit,
        handleDelete,
        expandAll,
        collapseAll,
        registeraddCategoryModal,
        registerEditCategoryModal,
        reload,
      };
    },
  });
</script>
