<!-- 表单预览 - 菜单方式 -->
<template>
	<div>
		<v-form-render ref="vFormRef" :form-json="dataForm.formJson" :form-data="dataForm.formData" :option-data="dataForm.optionData"></v-form-render>
		<div>
			<el-button v-for="(item, index) in dataForm.btnList" v-prevent-reclick type="primary" @click="btnForm(item)">{{ item.btnLabel }} </el-button>
		</div>
	</div>
</template>
<script setup lang="ts">
import { getFormConfig, getInfo, saveForm, updateForm } from '../api/formDevReview'
import { useRouter } from 'vue-router'
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()
const formCode = router.currentRoute.value.params.formCode + ''
let id = router.currentRoute.value.params.id
if (id == '0') {
	id = ''
}
const vFormRef = ref()
const dataForm = reactive({
	formCode: '',
	formJson: {},
	formData: {},
	optionData: {},
	btnList: []
})
dataForm.formCode = formCode

const showForm = () => {
	var data = { formCode: formCode }

	getFormConfig(data).then(response => {
		dataForm.formJson = JSON.parse(response.data.formJson)
		dataForm.btnList = response.data.fastFormBtnList
		vFormRef.value.setFormJson(dataForm.formJson)
		if (id != '') {
			var obj = { devFormCode: dataForm.formCode, btnCode: 'add', btnFormData: { id: id } }
			getInfo(obj).then(res => {
				vFormRef.value.setFormData(res.data)
			})
		}
	})
}
const btnForm = (btnData: any) => {
	if (btnData.serviceType == 10 || btnData.serviceType == 20) {
		submitForm()
	}
}
const submitForm = () => {
	vFormRef.value
		.getFormData()
		.then(formData => {
			ElMessageBox.confirm('请确定要执行此操作吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				var obj = { devFormCode: formCode, btnCode: 'add', btnFormData: formData }
				if (id != '') {
					obj.btnFormData.id = id
					updateForm(obj).then(() => {
						ElMessage.success({
							message: '修改成功',
							duration: 500
						})
					})
				} else {
					saveForm(obj).then(() => {
						ElMessage.success({
							message: '保存成功',
							duration: 500
						})
					})
				}
			})
		})
		.catch(error => {
			ElMessage.error({
				message: error,
				duration: 500
			})
		})
}
showForm()
</script>
