<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"></PermissionBtn>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction :actions="[
              {
                icon: 'clarity:info-standard-line',
                tooltip: '查看历史记录',
                onClick: handleHistory.bind(null, record),
              }
            ]" />
        </template>
      </template>

    </BasicTable>
    <SchemeDrawer @register="registerDrawer" @success="handleSuccess" />
    <HistoryDrawer @register="registerHistoryDrawer" @success="handleSuccess" />
  </PageWrapper>

</template>
<script lang="ts" setup>
  import { reactive, nextTick, ref } from 'vue';

  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getButtonList, getMenuDetail, deleteButton } from '@/api/demo/system';
  import { getLoadPage, del } from '@/api/sys/WFSchemeInfo';

  import { PageWrapper } from '@/components/Page';
  import SchemeTree from './SchemeTree.vue';

  import { useDrawer } from '@/components/Drawer';
  import SchemeDrawer from './SchemeDrawer.vue';
  import HistoryDrawer from './HistoryDrawer.vue'
  import { columns, searchFormSchema } from './scheme.data';
  import PermissionBtn from '@/components/PermissionBtn/index.vue'
  import { useMessage } from '@/hooks/web/useMessage';
  const { createConfirm, createMessage } = useMessage();

  import { useGo } from '@/hooks/web/usePage';
  const go = useGo();

  defineOptions({ name: 'SchemeManagement' });
  const searchInfo = reactive < Recordable > ({});

  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 handleView(record: Recordable) {
    go('/dashboard/scheme_preview/' + record.id);
  }
  // 查看历史记录
  function handleHistory(record) {
    openHistoryDrawer(true, {
      record
    })
  }
  function handleEdit() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请选择一个模板进行编辑');
    }
    const record = rows[0]
    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(record: Recordable) {
    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) {
          return createMessage.warn('删除成功');
        }else{
          return createMessage.warn('删除失败');
        }
      },
    });
  }

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

  function onFetchSuccess() {
    // 演示默认展开所有表项
    nextTick(expandAll);
  }
  function onBtnClicked(domId) {
    console.log(domId)
    switch (domId) {
      case 'btnAdd':
        go('/dashboard/scheme_preview/add');
        break;
      case 'btnEdit':
        handleEdit()
        break;
      case 'btnDelete':
        handleDelete()
        break;
      default:
        break;
    }
  }
</script>