<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <SchemeTree
      ref="childRef"
      class="w-1/4 xl:w-1/5"
      @select="handleSelect"
      @edit="classifyEdit"
      @add="handleCreate"
      @success="handleSuccess"
    />
    <BasicTable
      class="w-3/4 xl:w-4/5"
      @register="registerTable"
      @fetch-success="onFetchSuccess"
      :searchInfo="searchInfo"
    >
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleCreate"> 新增 </a-button> -->
        <PermissionBtn @btnEvent="onBtnClicked" />
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '历史记录',
                onClick: handleHistory.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <SchemeDrawer @register="registerDrawer" @success="handleSuccess" />
    <HistoryDrawer @register="registerHistoryDrawer" @success="handleSuccess" />
    <a-modal
      width="100%"
      wrap-class-name="full-modal"
      v-model:open="auditOpen"
      title="流程设计"
      :destroyOnClose="true"
    >
      <template #footer> </template>
      <Preview ref="posRef" :code="code" @closeModel="closeMolder" />
    </a-modal>
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { reactive, nextTick, ref } from 'vue';

  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getLoadPage, del } from '@/api/sys/WFSchemeInfo';

  import { PageWrapper } from '@/components/Page';
  import { useDrawer } from '@/components/Drawer';
  import { PermissionBtn } from '@/components/PermissionBtn/index';
  import { Preview, SchemeTree, SchemeDrawer, HistoryDrawer } from './page';
  import { columns, searchFormSchema } from './scheme.data';
  import { useMessage } from '@/hooks/web/useMessage';
  import { flowStore } from '@/store/modules/flow';

  const { createConfirm, createMessage } = useMessage();
  const flowWfDataStore = flowStore();

  defineOptions({ name: 'SchemeManagement' });
  const searchInfo = reactive<Recordable>({});
  const auditOpen = ref(false);
  const code = ref('');
  const [registerHistoryDrawer, { openDrawer: openHistoryDrawer }] = useDrawer();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, expandAll, getSelectRows, clearSelectedRowKeys }] = useTable({
    title: '模板列表',
    api: getLoadPage,
    columns,
    rowKey: 'id',
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    isTreeTable: true,
    striped: false,
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    rowSelection: {
      //多选框
      // type: 'checkbox',
      type: 'radio',
    },
    handleSearchInfoFn(info) {
      return info;
    },
    actionColumn: {
      width: 100,
      title: '操作',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
    },
  });
  const childRef = ref<any>();
  function handleCreate() {
    openDrawer(true, {
      isUpdate: false,
    });
  }
  // 查看历史记录
  function handleHistory(record) {
    openHistoryDrawer(true, {
      record,
    });
  }
  function handleEdit() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请选择一个模板进行编辑');
    }
    const record = rows[0];
    auditOpen.value = true;
    code.value = record.code;
    // go('/dashboard/scheme_preview/add?code=' + record.code);
  }
  async function classifyEdit(record) {
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }
  function handleSelect(category = '') {
    searchInfo.category = category;
    clearSelectedRowKeys();
    reload();
  }
  async function handleDelete() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请选择一个模板进行删除');
    }
    const query = {
      id: rows[0].id,
    };
    createConfirm({
      iconType: 'info',
      title: '删除',
      content: '确定要删除当前模板吗',
      onOk: async () => {
        const data = await del(query);
        reload();
        if (data) {
          createMessage.success('删除成功');
        } else {
          createMessage.error('删除失败');
        }
      },
    });
  }

  function handleSuccess() {
    childRef.value.fetch();
    clearSelectedRowKeys();
    reload();
  }

  function onFetchSuccess() {
    // 演示默认展开所有表项
    nextTick(expandAll);
  }
  function onBtnClicked(domId) {
    switch (domId) {
      case 'btnAdd':
        flowWfDataStore.setWfDataAll([]);
        auditOpen.value = true;
        code.value = '';
        // go('/dashboard/scheme_preview/add');
        break;
      case 'btnEdit':
        handleEdit();
        break;
      case 'btnDelete':
        handleDelete();
        break;
      default:
        break;
    }
  }
  function closeMolder() {
    auditOpen.value = false;
    clearSelectedRowKeys();
    reload();
  }
</script>
<style lang="less">
  .full-modal {
    .ant-modal {
      max-width: 100%;
      top: 0;
    }

    .ant-modal-content {
      height: calc(100vh);
    }

    .ant-modal-body {
      height: 85%;
    }
  }
</style>
