<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <MenuTree ref="childRef" class="w-1/4 xl:w-1/5" @select="handleSelect" />
    <BasicTable
      class="w-3/4 xl:w-4/5"
      @register="registerTable"
      @fetch-success="onFetchSuccess"
      :searchInfo="searchInfo"
    >
      <template #toolbar>
        <PermissionBtn @btn-event="onBtnClicked" />
      </template>
    </BasicTable>
    <FormModal @submitsuccess="submitsuccess" @register="registerModal" v-if="formModalVisible" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { reactive, nextTick, ref } from 'vue';

  import { BasicTable, useTable } from '@/components/Table';

  import { PageWrapper } from '@/components/Page';
  import PermissionBtn from '@/components/PermissionBtn/index.vue';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useModal } from '@/components/Modal';

  import MenuTree from './FormTree.vue';
  import FormModal from './FormModal.vue';

  import { fun_GetPageList, fun_DeleteForm } from '@/api/demo/formModule';
  import { formModuleColumns, searchFormSchema } from './formModule.data';

  const { createConfirm, createMessage } = useMessage();
  defineOptions({ name: 'FormModule' });
  const searchInfo = reactive<Recordable>({});
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload, expandAll, getSelectRows, clearSelectedRowKeys }] = useTable({
    title: '发布列表',
    api: fun_GetPageList,
    columns: formModuleColumns,
    rowKey: 'id',
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    isTreeTable: true,
    striped: false,
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    // 序号列
    showIndexColumn: true,
    rowSelection: {
      type: 'radio',
    },
    handleSearchInfoFn(info) {
      return info;
    },
  });

  const childRef = ref<any>();
  // 是否打开弹窗
  const formModalVisible = ref(true);

  // searchInfo的树分支条件查询
  function handleSelect(ModuleId = '') {
    searchInfo.pmoduleid = ModuleId;
    clearSelectedRowKeys();
    reload();
  }

  // 表单新增
  function handleAddForm() {
    formModalVisible.value = true;
    openModal(true, {});
  }
  // 表单编辑
  function handleEdit() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请选择一个按钮进行编辑');
    }
    const record = rows[0];
    record.type = '2';
    formModalVisible.value = true;
    openModal(true, record);
  }
  // 表单删除
  async function handleDelete() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请选择一个表单进行删除');
    }
    console.log(rows);

    const query = { id: rows[0].id };
    createConfirm({
      iconType: 'info',
      title: '删除',
      content: '确定要表单当前按钮吗',
      onOk: async () => {
        const data = await fun_DeleteForm(query);
        if (data.result) {
          createMessage.success('删除成功');
        } else {
          createMessage.warn('删除失败');
        }
        reload();
      },
    });
  }

  // 表单弹窗保存传回数据
  function submitsuccess() {
    reload();
    formModalVisible.value = false;
  }

  function onFetchSuccess() {
    // 演示默认展开所有表项
    nextTick(expandAll);
  }

  // 按钮
  function onBtnClicked(domId) {
    switch (domId) {
      case 'btnAdd':
        handleAddForm();
        break;
      case 'btnEdit':
        handleEdit();
        break;
      case 'btnDelete':
        handleDelete();
        break;
      default:
        break;
    }
  }
</script>
