<template>
  <BasicModal
    v-bind="$attrs"
    @register="historyModal"
    title="历史记录"
    width="1000px"
    @fullscreen="onFullscreen"
    :showCancelBtn="false"
    :showOkBtn="false"
  >
    <BasicTable @register="historyTable" :searchInfo="searchInfo">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'currentVersion'">
          <Switch
            :checked="record.id === isUseSchemeId"
            :disabled="record.id === isUseSchemeId"
            @change="changehistoryModal(record)"
          />
        </template>
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '预览',
                onClick: () => {
                  fromTablePreviewModal(record, eFormPreview);
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <!-- 预览弹窗 -->
    <VFormPreview ref="eFormPreview" :formConfig="formConfig" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import { cloneDeep } from 'lodash-es';
  import { Switch } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { BasicModal, useModalInner } from '@/components/Modal';

  import { IFormConfig } from '@/views/demo/form-design/typings/v-form-component';
  import { IToolbarMethods } from '@/views/demo/form-design/typings/form-type';
  import VFormPreview from '@/views/demo/form-design/components/VFormPreview/index.vue';

  import { getHistoryColumns } from './modelData';
  import { functionGetSchemePageList, functionUpdateScheme } from '@/api/demo/formScheme';
  import { cardNestStructure } from '@/views/demo/onlineform/util.ts';

  const searchInfo = reactive<Recordable>({});
  // 在使用的schemeId
  const isUseSchemeId = ref('');
  // 异步传参
  const [historyModal, { setModalProps }] = useModalInner(async (data) => {
    isUseSchemeId.value = data.schemeId;
    setModalProps({ confirmLoading: false });
    searchInfo.schemeInfoId = data.id;
    reload();
  });
  // 异步传参后查询历史记录表格
  const [historyTable, { reload }] = useTable({
    canResize: true,
    resizeHeightOffset: 150,
    api: functionGetSchemePageList,
    columns: getHistoryColumns(),
    rowKey: 'id',
    showTableSetting: false,
    handleSearchInfoFn(info) {
      return info;
    },
  });

  // 子组件实例
  const eFormPreview = ref<null | IToolbarMethods>(null);
  // endregion
  const formConfig = ref<IFormConfig>({
    // 表单配置
    schemas: [],
    layout: 'horizontal',
    labelLayout: 'flex',
    labelWidth: 100,
    labelCol: {},
    wrapperCol: {},
    currentItem: {
      component: '',
      componentProps: {},
    },
    activeKey: 1,
  });

  // 历史记录表格-修改当前版本
  async function changehistoryModal(record) {
    var query = {
      // info的id
      id: record.schemeInfoId,
      // scheme的id
      schemeId: record.id,
    };
    await functionUpdateScheme(query);
    isUseSchemeId.value = record.id;
    reload();
  }

  // 表单-预览  @\views\demo\onlineform\formdesign\index.vue:211
  function fromTablePreviewModal(record, Modal: IToolbarMethods) {
    let scheme = JSON.parse(record.scheme);
    scheme.formInfo.tabList = cardNestStructure(scheme.formInfo.tabList)
    if(scheme.formInfo.tabList.length > 1){
      let data = [{
        component: 'Tabs',
        componentProps: {
          options: []
        }
      }]
      data[0].componentProps.options = scheme.formInfo.tabList.map((item, index) => {
        return {
          label: item.text,
          value: index,
          children: item.schemas
        }
      })
      formConfig.value.schemas = data
    }else{
      formConfig.value.schemas = scheme.formInfo.tabList[0].schemas;
    }
    Modal?.showModal(cloneDeep(formConfig.value));
  }
</script>
