<template>
	<view class="jnpf-wrap jnpf-wrap-form">
		<JnpfParser :formConf="formConf" ref="dynamicForm" v-if="!loading" @submit="sumbitForm" :key="key" />
		<view class="buttom-actions">
			<u-button class="buttom-btn" @click.stop="cancel">取消</u-button>
			<u-button class="buttom-btn" type="primary" @click.stop="submit" :loading="btnLoading">
				{{config.confirmButtonText||'确定'}}
			</u-button>
		</view>
	</view>
</template>

<script>
	import {
		getConfigDataByMenuId,
		getModelInfo,
		createModel
	} from '@/api/apply/visualDev'
	import {
		getDataInterfaceRes
	} from '@/api/common'
	export default {
		data() {
			return {
				config: {},
				id: "",
				modelId: "",
				formConf: {},
				dataForm: {},
				key: +new Date(),
				loading: false,
				btnLoading: false,
				isPreview: true,
				formData: {},
				isAdd: false,
				userInfo: {}
			}
		},
		onLoad(e) {
			this.userInfo = uni.getStorageSync('userInfo') || {}
			this.loading = true
			let data = e.data ? JSON.parse(decodeURIComponent(e.data)) : {}
			this.config = data.config
			this.id = data.id
			this.modelId = data.modelId
			this.isPreview = data.isPreview
			if (this.id != null && this.id != undefined && this.id != '') {
				this.isAdd = false
			} else {
				this.isAdd = true
			}
			uni.setNavigationBarTitle({
				title: this.config.popupTitle
			})
			if (this.config.modelId) this.getConfigData(data.row)
		},
		methods: {
			getConfigData(row) {
				getConfigDataByMenuId({
					menuId: this.config.modelId
				}).then(res => {
					this.config.modelId = res.data.id
					if (res.code !== 200 || !res.data) {
						uni.showToast({
							title: res.msg || '请求出错，请重试',
							icon: 'none'
						})
						return
					}
					this.formConf = JSON.parse(res.data.formData)
					const setDataFun = (formData) => {
						if (this.config.formOptions.length) {
							for (let k in formData) {
								for (let i = 0; i < this.config.formOptions.length; i++) {
									const e = this.config.formOptions[i]
									if (e.currentField == '@formId') this.formData[e.field] = formData.id;
									if (e.currentField == k) this.formData[e.field] = formData[k]
								}
							}
						}
						this.fillFormData(this.formConf, this.formData)
						this.key = +new Date()
						this.loading = false
					}
					if (this.id) {
						getModelInfo(this.modelId, this.id).then(res => {
							let dataForm = res.data
							if (!dataForm.data) return
							const formData = JSON.parse(dataForm.data)
							this.formData = {}
							setDataFun({
								...formData,
								id: this.id
							})
						})
					} else {
						const formData = row
						setDataFun(formData)
					}
				}).catch(() => {})
			},
			fillFormData(form, data) {
				const loop = list => {
					for (let i = 0; i < list.length; i++) {
						let item = list[i]
						let vModel = item.__vModel__
						let config = item.__config__
						if (vModel) {
							let val = data.hasOwnProperty(vModel) ? data[vModel] : config.defaultValue
							if (!config.isSubTable) config.defaultValue = val
							if (config.defaultCurrent) {
								if (config.jnpfKey === 'datePicker') {
									if (!data.hasOwnProperty(vModel)) {
										let format = this.jnpf.handelFormat(item.format)
										let dateStr = this.jnpf.toDate(new Date().getTime(), format)
										let time = format === 'yyyy' ? '-01-01 00:00:00' : format === 'yyyy-MM' ?
											'-01 00:00:00' : format === 'yyyy-MM-dd' ?
											' 00:00:00' : ''
										val = new Date(dateStr + time).getTime()
										config.defaultValue = val
									}
								}
								if (config.jnpfKey === 'timePicker') {
									if (!data.hasOwnProperty(vModel)) {
										config.defaultValue = this.jnpf.toDate(new Date(), item.format)
									}
								}
								if (config.jnpfKey === 'organizeSelect' && this.userInfo.organizeIds?.length) {
									config.defaultValue = item.multiple ? this.userInfo.organizeIds :
										this.userInfo.organizeId
								}
								if (config.jnpfKey === 'posSelect' && this.userInfo.positionIds?.length) {
									config.defaultValue = item.multiple ? this.userInfo.positionIds :
										this.userInfo.positionId
								}
								const userId = this.userInfo.userId
								if (config.jnpfKey === 'userSelect' && userId) {
									config.defaultValue = item.multiple ? [userId] : userId;
								}
								if (config.jnpfKey === 'usersSelect' && userId) {
									config.defaultValue = [userId + '--user'];
								}
								if (config.jnpfKey === 'sign' && this.userInfo.signImg) {
									config.defaultValue = this.userInfo.signImg
								}
							}
							let noShow = !item.__config__.noShow ? false : item.__config__.noShow
							let isVisibility = false
							if (!item.__config__.visibility || (Array.isArray(item.__config__.visibility) && item
									.__config__.visibility.includes('app'))) isVisibility = true
							this.$set(item.__config__, 'isVisibility', isVisibility)
							this.$set(item.__config__, 'noShow', noShow)
						} else {
							let noShow = false,
								isVisibility = false
							if (!item.__config__.visibility || (Array.isArray(item.__config__.visibility) && item
									.__config__.visibility.includes('app'))) isVisibility = true
							this.$set(item.__config__, 'isVisibility', isVisibility)
							this.$set(item.__config__, 'noShow', noShow)
						}
						if (item.__config__ && item.__config__.children && Array
							.isArray(item.__config__.children)) {
							loop(item.__config__.children)
						}

					}
				}
				loop(form.fields)
			},
			cancel() {
				uni.navigateBack();
			},
			sumbitForm(data, callback) {
				if (!data) return
				this.btnLoading = true
				const successFun = (res, callback) => {
					if (callback && typeof callback === "function") callback()
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								this.btnLoading = false
								if (this.config.isRefresh) uni.$emit('refresh')
								uni.navigateBack()
							}, 1500)
						}
					})
				}
				if (this.config.customBtn) {
					const query = {
						paramList: this.jnpf.getParamList(this.config.templateJson, {
							...data,
							id: this.id
						}) || []
					};
					getDataInterfaceRes(this.config.interfaceId, query).then(res => {
						successFun(res, callback)
					}).catch(() => {
						this.btnLoading = false
					})
				} else {
					this.dataForm.data = JSON.stringify(data)
					createModel(this.config.modelId, this.dataForm).then(res => {
						successFun(res, callback)
					}).catch(() => {
						this.btnLoading = false
					})
				}
			},
			submit() {
				if (this.isPreview) {
					uni.showToast({
						title: '功能预览不支持数据保存',
						icon: 'none'
					})
					return
				}
				this.$refs.dynamicForm && this.$refs.dynamicForm.submitForm()
			},
		}
	}
</script>

<style scoped lang="scss">
	page {
		background-color: #f0f2f6;
	}
</style>