<!--
@ 作者: 秦东
@ 时间: 2024-05-31 09:22:06
@ 备注: 执行表格
-->
<script lang='ts' setup>
import { json2string,objToStringify,string2json,stringToObj } from '@/utils/DesignForm/form'
import { judgeSubmitCancel,gainAppPageInfo } from '@/api/DesignForm/requestapi'
import {  Edit,Picture as IconPicture  } from '@element-plus/icons-vue'
import { appPageDataInit,appWorkFlow } from "@/api/date/type"
import { notAsA_BasisForJudgment,asAnApprovalActionControl,fixedValueControl,timeControl,timeEquation,submitButton, afreshSubmitButton } from '@/utils/workflow/const'

const props = defineProps({
	pickAppMenu:{
		type:Object,
		default(){
			return {}
		}
	}
});
const activePage = ref(0)	//默认选择的页面
const appInitData = ref<appPageDataInit>("")
//app表单
const stateForm = reactive<any>({
	type: 1, // 1新增；2修改；3查看（表单模式） ；4查看； 5设计
    formData: {
      list: [],
      form: {},
      config: {},
	  powerstr:{}
    },
    dict: {},
    formId: "1",
    id: 0,
	formId: 0,
	versionId: 0,
    loading: true
})
//表单列表
const stateList = reactive({
    tableData: {
      // tableProps: {}, //表格所有参数
      columns: [],
      config: {},
	  controlBtn:[],
	  operateBtn:[]
    },
    searchData: [],
    loading: false,
    attrObj: {},
    config: {
		pageSize:10,
		searchIsShow:true,
		searchFormIsShow:true,
		openPageMode:"drawer"
	},
    tagList: {},
    formId: props.formKey || '',
    formList: [], // 所有可选表单数据源
    name: '',
    treeData: {}, // 左侧树相关
    previewVisible: false,
    tabsName: 'second',
    formFieldList: [], // 表单数据源所有可选字段
	formApi:{
		type:"1",
		addApiUrl:"",
		editApiUrl:"",
		delApiUrl:"",
		lookApiUrl:""
	},
    dict: {},
    refreshTable: true
})
const appFormTitle = ref("")
const versionTitle = ref<string>("")	//表单名称
const versionId = ref<string>("")	//表单版本号
const appFlowInfo = ref<appWorkFlow>("")	//流程
const isFlow = ref(2)
const mastesformjson = ref("")	//
/**
@ 作者: 秦东
@ 时间: 2024-06-03 09:54:30
@ 功能: 加载页面
*/
onMounted(() => {
	initLoadData()

});
/**
@ 作者: 秦东
@ 时间: 2024-06-03 09:00:10
@ 功能: 初始化数据
*/
const initLoadData = () => {
	console.log("initLoadData",props.pickAppMenu.type,props.pickAppMenu.type != 1)
	if(props.pickAppMenu.type != 1){
		appFormTitle.value = props.pickAppMenu.label
		gainAppPageInfo({id:props.pickAppMenu.id})
		.then((data)=>{
			console.log("获取初始化表单数据",data)
			appInitData.value = data.data
			console.log("获取初始化表单数据---!",appInitData.value)
			mastesformjson.value = data.data.appForm.mastesformjson
			isFlow.value = data.data.appForm.flowIsOpen
			if (data.data.page && data.data.pageList  && data.data.appFlow){
				activePage.value = 1
			}else if (data.data.page && !data.data.pageList  && data.data.appFlow){
				activePage.value = 2
			}else if (!data.data.page && !data.data.pageList  && data.data.appFlow){
				activePage.value = 3
			}else if (!data.data.page && data.data.pageList  && data.data.appFlow){
				activePage.value = 1
			}else if (data.data.page && !data.data.pageList  && !data.data.appFlow){
				activePage.value = 2
			}else{
				activePage.value = 1
			}
			versionId.value = data.data.appForm.id.toString()
			versionTitle.value = data.data.appForm.name
			if(data.data.pageList){
				let stateData = string2json(data.data.appForm.listjson)
				stateList.tableData = stateData.tableData
				stateList.searchData = stateData.searchData
				stateList.loading = stateData.loading
				stateList.attrObj = stateData.attrObj
				stateList.config = stateData.config
				stateList.tagList = stateData.tagList
				stateList.formList = stateData.formList
				stateList.name = stateData.name
				stateList.treeData = stateData.treeData
				stateList.previewVisible = stateData.previewVisible
				stateList.formFieldList = stateData.formFieldList
				stateList.formApi = stateData.formApi
				stateList.dict = stateData.dict
				stateList.refreshTable = stateData.refreshTable
			}
			if(data.data.page){
				console.log("data.data.mastesform", data.data)
				stateForm.id=data.data.appForm.version.toString()
				stateForm.formId =data.data.appForm.cfid.toString()
				stateForm.versionId =data.data.appForm.id.toString()
				stateForm.formData = stringToObj(data.data.appForm.mastesform)
				stateForm.dict = string2json(data.data.appForm.dict)
				stateForm.formData.powerstr = string2json(data.data.appForm.powerstr)
				console.log("data.data.mastesform", stateForm.formData)
				
				judgeSubmitCancel({"name":data.data.appForm.mastesformjson})
					.then((datajud:any) =>{
						if(datajud.code == 0){
							if (datajud.data == 3 || datajud.data == 4){
								stateForm.formData.list.push(submitButton)
							}
						}
					})
			}
			if(data.data.appFlow){
				appFlowInfo.value = data.data.workFlow
			}
		})
	}
	
}
/**
@ 作者: 秦东
@ 时间: 2024-06-03 09:26:17
@ 功能: 监听菜单数据变化
*/
watch(() => props.pickAppMenu,(val:any) => {
	initLoadData()
},{
    deep: true
})

function optionsValue3Get3(data: any,fieldName: string){
	
	for(let i = 0; i < stateForm.formData.list.length; i++){
		if(stateForm.formData.list[i].name==fieldName){
			stateForm.formData.list[i].options = []
			for(let j = 0;j<data.length;j++){
				stateForm.formData.list[i].options.push(data[j])
			}
		}
	}
}
</script>
<template>
	<el-row>
		<el-col :span="24" class="pageBox pageHeader">
			<el-text class="wordFont">{{ appFormTitle}}</el-text>
		</el-col>
		<el-col :span="24">
			<el-scrollbar class="scroBox">
				<el-card class="tispMsg"  shadow="always">
		
					<AppPageList v-if="appInitData.pageList"
						:data="stateList.tableData"
						:search-data="stateList.searchData"
						:config="stateList.config"
						:form-id="appInitData.appForm.cfid.toString()"
						:versionid="appInitData.versionId" 
						:versiontitle="versionTitle"
						:sign-code="appInitData.signCode"
						:pickAppMenu="props.pickAppMenu"
					/>
					<AppFormPage
						v-else
						ref="formEl"
						:number="props.pickAppMenu.id"
						:isWorkFlow="isFlow"
						:form-data="stateForm.formData"
						:type="1"
						:mastesformjson="mastesformjson"
						:flowkey="appInitData.flowkey"
						:groupid="appInitData.groupid"
						:sign-code="appInitData.signCode"
						:version-id="appInitData.versionId"
						add-url="addData"
						@optionsValue3Get3="optionsValue3Get3"
					/>

				</el-card>
			</el-scrollbar>
		</el-col>
	</el-row>
	
</template>
<style  lang='scss' scoped>
.pageBox{
	padding: 0 15px;
	.wordFont{
		font-size: 20px;
	}
}
.pageHeader{
	display: flex;
	align-items: center;
	justify-content: space-between;
	background-color: #FFFFFF;
	padding:15px 15px 5px 15px;
	margin-bottom: 15px;
}
.scroBox{
	padding:0 15px;
	height: calc(100vh - 110px);
	.tispMsg{
		margin:0 auto 15px auto;
	}
}
</style>
