<template>
  <!--定义表格-->
  <BasicTable @register="registerTable" :expandFixed="true">
    <template #tableTitle>
      <a-button type="primary" v-auth="'zcfg-add'" @click="add">新增</a-button>
      <!-- <a-button type="primary" v-auth="'zcfg-edit'" @click="handleEdit">编辑</a-button>
      <a-button type="primary" v-auth="'zcfg-del'" @click="handleDelete">删除</a-button>
      <a-button type="primary" v-auth="'zcfg-below'" @click="handleAddSub">添加下级</a-button> -->
    </template>

    <!--操作栏-->
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <Classify @register="registerModal" @success="handleSuccess" />
</template>

<script lang="ts" name="basic-table-demo" setup>
  import { ActionItem, BasicTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useModal } from '/@/components/Modal';
  import { typeList, typeDel } from './api';
  import { columns, searchFormSchema } from './data';
  import Classify from './Model/classify.vue';

  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    designScope: 'basic-table-demo-ajax',
    tableProps: {
      title: '政策法规',
      api: typeList,
      columns: columns,
      actionColumn: {
        width: 120,
      },

      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
    },
  });
  //BasicTable绑定注册
  const [registerTable, { reload }, {}] = tableContext;
  const [registerModal, { openModal }] = useModal();

  //   点击新增
  function add() {
    openModal(true, {
      record: '',
      showFooter: true,
      title: '新增文档分类',
      isEdit: false,
    });
  }
  //   点击编辑
  function handleEdit(record) {
    openModal(true, {
      record: record,
      showFooter: true,
      title: '编辑文档分类',
      isEdit: true,
    });
  }
  //   刷新列表
  function handleSuccess() {
    reload();
  }

  /**
   * 操作栏
   */
  function getTableAction(record: any): ActionItem[] {
    return [
      {
        label: '编辑',
        auth: 'zcfg-edit',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '添加下级',
        auth: 'zcfg-below',
        onClick: handleAddSub.bind(null, record),
      },

      {
        label: '删除',
        color: 'error',
        auth: 'zcfg-del',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }

  /**
   * 添加下级
   */
  function handleAddSub(record: any) {
    openModal(true, {
      record: { pid: record.id },
      showFooter: true,
      title: '添加下级',
      isEdit: true,
    });
  }
  /**
   * 删除
   */
  async function handleDelete(record) {
    await typeDel({ id: record.id });
    reload();
  }
</script>
<style lang="less" scoped>
  // :deep(.ant-table-row-expand-icon) {
  //   float: none !important;
  //   margin-top: 3px;
  // }
</style>
