<template>
  <PageWrapper
    contentClass="flex"
    contentFullHeight
    fixedHeight
    dense
  >
    <div class="m-4 mr-0 overflow-hidden bg-white w-1/4 xl:w-1/5">
      <BasicTree title="流程分类"
                 toolbar
                 search
                 defaultExpandAll
                 :clickRowToExpand="false"
                 :treeData="state.treeData"
                 :fieldNames="{ key: 'id', title: 'name' }"
                 @select="handleSelect"
      />
    </div>
    <BasicTable
      class="w-3/4 xl:w-4/5"
      @register="registerTable"
      @selection-change="handleSelectionChange"
    >
      <template #tableTitle>
        <a-button
          type="primary"
          @click="handleAdd()"
        >新增</a-button>
        <a-button
          type="primary"
          @click="handleAdd()"
        >数据库导入表单</a-button>
        <a-button
          type="primary"
          :disabled="state.single"
          @click="handleEdit()"
        >修改</a-button>
        <a-button
          type="primary"
          :disabled="state.multiple"
          @click="handleDel()"
        >移除</a-button>
        <a-button
          type="primary"
          :disabled="state.single"
          @click="handleEdit()"
        >生成代码</a-button>
        <a-button
          type="primary"
          :disabled="state.single"
          @click="handleEdit()"
        >创建菜单</a-button>
        <a-button
          type="primary"
          :disabled="state.single"
          @click="handleEdit()"
        >添加到java类型</a-button>
        <a-button
          type="primary"
          :disabled="state.single"
          @click="handleEdit()"
        >获取流程表单</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                icon: 'fa6-regular:pen-to-square',
                onClick: handleEdit.bind(null, record)
              },
              {
                label: '移除',
                icon: 'ant-design:delete-outlined',
                color: 'error',
                onClick: handleDel.bind(null, record)
              }]"
            :dropDownActions="[
              {
                label: '删除',
                icon: 'ant-design:delete-outlined',
                color: 'error',
                onClick: handleDel.bind(null, record)
              },
              {
                label: '同步数据库',
                icon: 'fa-regular:copy',
                onClick: handleCopy.bind(null, record)
              }]"
          />
        </template>
      </template>
    </BasicTable>
    <GenTableModal @register="registerModal" @success="handleRefreshTable"/>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { reactive, toRaw } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { listGenCodeTemplateGroup, delGenCodeTemplateGroup } from '/@/api/platform/system/controller/gencodeTemplateGroup';
  import { useModal } from '/@/components/Modal';
  import GenTableModal from './GenTableModal.vue';
  import { columns, searchFormSchema } from './genTable.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import {PageWrapper} from '/@/components/Page';
  import {BasicTree, TreeItem} from '/@/components/Tree';

  interface TableState {
    single: boolean;
    multiple: boolean;
    treeData: TreeItem[];
  }

  /** 通用变量统一声明区域 */
  const state = reactive<TableState>({
    // 非单个禁用
    single: true,
    // 非多个禁用
    multiple: true,
    treeData: []
  });
  const { createConfirm, createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload, clearSelectedRowKeys, getSelectRowKeys }] = useTable({
    api: listGenCodeTemplateGroup,
    rowKey: 'id',
    columns,
    formConfig: {
      compact: true,
      labelWidth: 100,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      autoAdvancedLine: 3
    },
    rowSelection: { type: 'checkbox' },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    clickToRowSelect: false,
    showIndexColumn: false,
    actionColumn: {
      width: 220,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right'
    },
    handleSearchInfoFn: () => clearSelectedRowKeys()
  });

  /** 处理多选框选中数据 */
  function handleSelectionChange(selection?: Recordable) {
    const rowSelection = toRaw(selection?.keys) || [];
    state.single = rowSelection.length != 1;
    state.multiple = !rowSelection.length;
  }

  function handleAdd() {
    openModal(true,{ _tag: 'add' });
  }

  function handleCopy(record?: Recordable) {
    record = record || { id: getSelectRowKeys() };
  }

  function handleEdit(record?: Recordable) {
    record = record || { id: getSelectRowKeys() };
    openModal(true, { _tag: 'edit', record });
  }

  async function handleDel(record?: Recordable) {
    const ids = record?.id || getSelectRowKeys();
    createConfirm({
      iconType: 'warning',
      title: '警告',
      content: `是否确认删除id编号为${ids}的数据吗?`,
      onOk: async () => {
        await delGenCodeTemplateGroup(ids);
        createMessage.success('删除成功!');
        handleRefreshTable();
      }
    });
  }

  function handleRefreshTable() {
    clearSelectedRowKeys();
    reload();
  }

</script>
