<template>
  <TabFormEditModal
    v-if="openTabEditModal" v-model:open="openTabEditModal"
    :uuid="uuid" :is-restore-data="tabEditViewIsRestoreData"
    :window-no="mCurdWindowNo" :tab-index="mTabIndex" :type="tabEditViewType" :model-data-id="tabEditViewRecordId"
    width="90%" :current-page="tabEditViewCurrentPage" :current-index="tabEditViewCurrentIndex"
    :total-records="tabEditViewTotalRecords" :task-info-id="tabEditViewTaskInfoId"
    :simple-filter-params="tabEditViewSimpleFilterParams" :complex-filter-params="tabEditViewComplexFilterParams"
    :workflow-type="tabEditViewWorkflowType" :mask-closable="false" :model-data-create="modelDataCreate"
    :sort-str="tabEditViewSortStr" @open-tab-form-view="openTabFormView"
  />
  <TabFormViewModal
    v-if="openTabViewModal" v-model:open="openTabViewModal"
    :uuid="uuid" :window-no="mCurdWindowNo" :tab-index="mTabIndex"
    :type="tabFormViewType" :model-data-id="tabFormViewRecordId" width="90%" :current-page="tabFormViewCurrentPage"
    :current-index="tabFormViewCurrentIndex" :total-records="tabFormViewTotalRecords" :simple-filter-params="tabFormViewSimpleFilterParams"
    :task-info-id="tabFormViewTaskInfoId" :workflow-type="tabFormViewWorkflowType" :mask-closable="false" :complex-filter-params="tabFormViewComplexFilterParams"
    :sort-str="tabFormViewSortStr" @open-tab-edit-view="openTabEditView"
  />
</template>

<script setup>
import { ref, onMounted, defineEmits, watch, defineProps } from 'vue';

import TabFormEditModal from './tabFormEdit/TabFormEditModal.vue';
import TabFormViewModal from './tabFormView/TabFormViewModal.vue';


const props = defineProps({
  // 是否打开
  open: {
    type: Boolean,
    default: false,
  },
  // 窗口类型
  // Edit:编辑界面,可选项如下(create:创建数据,edit:编辑数据,editRestore:恢复数据)
  // View:表单界面,可选项如下(view:视图数据,viewRestore:恢复数据)
  viewType: {
    type: String,
    default: '',
  },
  // CURD窗口编号
  curdWindowNo: {
    type: String,
    default: '',
  },
  // 页签编号
  tabIndex: {
    type: Number,
    default: 0,
  },
  // 数据记录Id
  modelDataId: {
    type: Number,
    default: null,
  },
  // 当前记录所在的页码
  currentPage: {
    type: Number,
    default: 0,
  },
  // 当前记录的排序
  currentIndex: {
    type: Number,
    default: 0,
  },
  // 记录的总数量
  totalRecords: {
    type: Number,
    default: 0,
  },
  // 窗口唯一Id(必须是唯一的)
  uuid: {
    type: String,
    default: '',
  },
  // 是否恢复草稿数据(false:不恢复，true:恢复)
  isRestoreData: {
    type: Boolean,
    default: false,
  },
  // 工作流任务Id(taskId)
  taskInfoId: {
    type: String,
    default: null,
  },
  // 工作流任务类型(approve:审批任务,copyTask:抄送任务)
  workflowType: {
    type: String,
    default: null,
  },
  sortStr: {
    type: String,
    default: null,
  },
  simpleFilterParams: {
    type: String,
    default: null,
  },
  complexFilterParams: {
    type: Object,
    default: null,
  },
  // 创建新数据时，填充已知的值FieldValue
  modelDataCreate: {
    type: Object,
    default: function(){
      return {};
    },
  },
});


const mCurdWindowNo = ref(null);
const mTabIndex = ref(0);

const openTabEditModal = ref(false);
const tabEditViewType = ref('create');
const tabEditViewRecordId = ref(null);
const tabEditViewCurrentPage = ref(null);
const tabEditViewCurrentIndex = ref(null);
const tabEditViewTotalRecords = ref(null);
const tabEditViewIsRestoreData = ref(false);
const tabEditViewTaskInfoId = ref(null);
const tabEditViewWorkflowType = ref(null);
const tabEditViewSortStr = ref(null);
const tabEditViewSimpleFilterParams = ref(null);
const tabEditViewComplexFilterParams = ref(null);

const openTabViewModal = ref(false);
const tabFormViewType = ref('view');
const tabFormViewRecordId = ref(null);
const tabFormViewCurrentPage = ref(null);
const tabFormViewCurrentIndex = ref(null);
const tabFormViewTotalRecords = ref(null);
const tabFormViewTaskInfoId = ref(null);
const tabFormViewWorkflowType = ref(null);
const tabFormViewSortStr = ref(null);
const tabFormViewSimpleFilterParams = ref(null);
const tabFormViewComplexFilterParams = ref(null);

const emits = defineEmits(['update:open']);


watch([openTabEditModal, openTabViewModal], (newValues, oldValues) => {
  console.log('First name or last name changed.');
  console.log('New values:', newValues);
  console.log('Old values:', oldValues);
  if(newValues[0] == false && newValues[1] == false){
    // emits('update:open', false);
    triggerRefresh();
  }
});

const triggerRefresh = () => {
  const event = new CustomEvent('refreshCurdData');
  window.dispatchEvent(event);
};


onMounted(() => { 
  if(props.open == false){
    openTabEditModal.value = false;
    openTabViewModal.value = false;
    return;
  }

  mCurdWindowNo.value = props.curdWindowNo;
  mTabIndex.value = props.tabIndex;


  // 窗口类型
  // Edit:编辑界面,可选项如下(create:创建数据,edit:编辑数据,editRestore:恢复数据)
  // View:表单界面,可选项如下(view:视图数据,viewRestore:恢复数据)
  if(props.viewType == 'create' || props.viewType == 'edit' || props.viewType == 'editRestore'){
    
    openTabEditView({
      type: props.viewType,
      windowNo: props.curdWindowNo,
      tabIndex: props.tabIndex,
      modelDataId: props.modelDataId,
      currentPage: props.currentPage,
      currentIndex: props.currentIndex,
      totalRecords: props.totalRecords,
      uuid: props.uuid,
      isRestoreData: props.isRestoreData,
      taskInfoId: props.taskInfoId,
      workflowType: props.workflowType,
      sortStr: props.sortStr,
      simpleFilterParams: props.simpleFilterParams,
      complexFilterParams: props.complexFilterParams,
    });
  }else if(props.viewType == 'view' || props.viewType == 'viewRestore'){
    openTabFormView({
      type: props.viewType,
      windowNo: props.curdWindowNo,
      tabIndex: props.tabIndex,
      modelDataId: props.modelDataId,
      currentPage: props.currentPage,
      currentIndex: props.currentIndex,
      totalRecords: props.totalRecords,
      uuid: props.uuid,
      isRestoreData: props.isRestoreData,
      taskInfoId: props.taskInfoId,
      workflowType: props.workflowType,
      sortStr: props.sortStr,
      simpleFilterParams: props.simpleFilterParams,
      complexFilterParams: props.complexFilterParams,
    });
  }
});

/**
 * 打开页签视图模式
 * @param param 信息
 *          属性1: type: 类型,
 *          属性1: windowNo: CURD窗口编号,
 *          属性2: tabIndex: CURD页签编号,
 *          属性3: modelDataId: 数据记录id,
 *          属性4: currentPage: 当前记录所在的页码,
 *          属性5: currentIndex: 当前记录的排序,
 *          属性6: totalRecords: 记录的总数量,
 *          属性7: uuid: 窗口唯一Id,
 */
const openTabFormView = function (param) {

  console.log('openTabFormView', param);

  tabFormViewType.value = param.type;
  mCurdWindowNo.value = param.windowNo;
  mTabIndex.value = param.tabIndex;
  tabFormViewRecordId.value = param.modelDataId;
  tabFormViewCurrentPage.value = param.currentPage;
  tabFormViewCurrentIndex.value = param.currentIndex;
  tabFormViewTotalRecords.value = param.totalRecords;
  tabFormViewSortStr.value = param.sortStr;
  tabFormViewSimpleFilterParams.value = param.simpleFilterParams;
  tabFormViewComplexFilterParams.value = param.complexFilterParams;

  openTabEditModal.value = false;
  openTabViewModal.value = true;
};

/**
 * 打开页签编辑模式
 * @param param 信息
 *          属性1: type: 类型,
 *          属性1: windowNo: CURD窗口编号,
 *          属性2: tabIndex: CURD页签编号,
 *          属性3: modelDataId: 数据记录id,
 *          属性4: currentPage: 当前记录所在的页码,
 *          属性5: currentIndex: 当前记录的排序,
 *          属性6: totalRecords: 记录的总数量,
 *          属性7: uuid: 窗口唯一Id,
 */
const openTabEditView = function (param) {
  tabEditViewType.value = param.type;
  mCurdWindowNo.value = param.windowNo;
  mTabIndex.value = param.tabIndex;
  tabEditViewRecordId.value = param.modelDataId;
  tabEditViewCurrentPage.value = param.currentPage;
  tabEditViewCurrentIndex.value = param.currentIndex;
  tabEditViewTotalRecords.value = param.totalRecords;
  tabEditViewIsRestoreData.value = param.isRestoreData;
  tabEditViewSortStr.value = param.sortStr;
  tabEditViewSimpleFilterParams.value = param.simpleFilterParams;
  tabEditViewComplexFilterParams.value = param.complexFilterParams;

  openTabViewModal.value = false;
  openTabEditModal.value = true;
};

</script>