<template>
  <BasicModal
    v-bind="$attrs"
    @register="historyModal"
    title="历史记录"
    width="1000px"
    @fullscreen="onFullscreen"
  >
    <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.scheme);
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <!-- 预览弹窗 -->
    <PreviewModal @register="previewModal" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { ref, reactive } from 'vue';
  import { Switch } from 'ant-design-vue';
  import { BasicModal, useModalInner, useModal } from '@/components/Modal';
  import { getHistoryColumns } from './modelData';
  import { functionGetSchemePageList, functionUpdateScheme } from '@/api/demo/formScheme';
  import PreviewModal from './PreviewModal.vue';

  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,
    api: functionGetSchemePageList,
    columns: getHistoryColumns(),
    rowKey: 'id',
    showTableSetting: true,
    handleSearchInfoFn(info) {
      return info;
    },
  });
  // 预览弹窗
  const [previewModal, { openModal: handlePreviewModal }] = useModal();

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

  // 表单-预览
  async function fromTablePreviewModal(scheme: string) {
    handlePreviewModal(true, {
      scheme,
    });
  }
</script>
