<template>
	<view class="jnpf-table">
		<view class="jnpf-table-item" v-for="(item,i) in tableFormData" :key="i">
			<view class="jnpf-table-item-title u-flex u-row-between u-p-r-20">
				<view class="jnpf-table-item-title-num u-line-1 u-p-r-20 u-p-l-20" @click="clickIcon(config)">
					{{config.__config__.label}}({{i+1}})
					<u-icon :name="config.__config__.tipLabel? 'question-circle-fill':''" class="u-m-l-10"
						color="#a0acb7"></u-icon>
				</view>
				<view class="jnpf-table-item-title-action" v-if="config.showDeleteBtn && !disabled" @click="delItem(i)">
					删除
				</view>
			</view>
			<view class="u-p-l-20 u-p-r-20 form-item-box" v-for="(childItem,cIndex) in item" :key="cIndex"
				v-if="!childItem.__config__.noShow && childItem.__config__.isVisibility&& childItem.__config__.jnpfKey!=='colorPicker'">
				<u-form-item :label="childItem.__config__.showLabel ? childItem.__config__.label : '' "
					:required="childItem.__config__.required"
					:left-icon='childItem.__config__.showLabel && childItem.__config__.tipLabel && childItem.__config__.label? "question-circle-fill":""'
					@clickIcon="clickIcon(childItem)" :left-icon-style="{'color':'#a8aaaf'}"
					:label-width="childItem.__config__.labelWidth ? childItem.__config__.labelWidth * 1.5 : 100*1.5">
					<block v-if="childItem.__config__.jnpfKey==='input'">
						<u-input input-align='right' v-model="tableFormData[i][cIndex].value"
							:placeholder="childItem.placeholder"
							:maxlength="childItem.maxlength?childItem.maxlength:140" :clearable='childItem.clearable'
							:disabled="disabled||childItem.disabled" @input="onChange($event,childItem,i)">
						</u-input>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='calculate'">
						<jnpf-calculation :expression='childItem.expression' :vModel='childItem.__vModel__'
							:config='childItem.__config__' :formData='formData' v-model="tableFormData[i][cIndex].value"
							:rowIndex="i" :precision="childItem.precision" :isAmountChinese="childItem.isAmountChinese"
							:thousands="childItem.thousands" />
					</block>
					<block v-if="childItem.__config__.jnpfKey==='textarea'">
						<u-input input-align='right' v-model="tableFormData[i][cIndex].value" type="textarea"
							:placeholder="childItem.placeholder"
							:maxlength="childItem.maxlength?childItem.maxlength:140"
							:disabled="disabled||childItem.disabled" @input="onChange($event,childItem,i)" />
					</block>
					<block v-if="childItem.__config__.jnpfKey==='inputNumber'">
						<jnpf-number-box v-model="tableFormData[i][cIndex].value" :step='childItem.step'
							:max='childItem.max' :min='childItem.min' :disabled="childItem.disabled"
							@change="change(arguments,childItem,i)" :isAmountChinese="childItem.isAmountChinese"
							:thousands="childItem.thousands" :addonAfter="childItem.addonAfter"
							:addonBefore="childItem.addonBefore" :controls="childItem.controls"
							:precision="childItem.precision">
						</jnpf-number-box>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='switch'">
						<jnpf-switch v-model="tableFormData[i][cIndex].value" :disabled="disabled||childItem.disabled"
							@change="onChange($event,childItem,i)">
						</jnpf-switch>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='radio'">
						<jnpf-radio v-model="tableFormData[i][cIndex].value" :options="childItem.options"
							:props="childItem.props" @change=" itemChange('radio')" :disabled="childItem.disabled"
							:direction='childItem.direction'>
						</jnpf-radio>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='checkbox'">
						<jnpf-checkbox v-model="tableFormData[i][cIndex].value" :options="childItem.options"
							:props="childItem.props" :disabled="disabled||childItem.disabled"
							@change="change(arguments,childItem,i)" :direction='childItem.direction'>
						</jnpf-checkbox>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='select'">
						<jnpf-select v-model="tableFormData[i][cIndex].value" :placeholder="childItem.placeholder"
							:options="childItem.options" :props="childItem.props" :multiple="childItem.multiple"
							:disabled="disabled||childItem.disabled" @change="change(arguments,childItem,i)">
						</jnpf-select>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='cascader'">
						<jnpf-cascader v-model="tableFormData[i][cIndex].value" :placeholder="childItem.placeholder"
							:options="childItem.options" :props="childItem.props" :multiple="childItem.multiple"
							:disabled="disabled||childItem.disabled" @change="change(arguments,childItem,i)">
						</jnpf-cascader>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='groupSelect'">
						<jnpf-group-select :vModel='childItem.__vModel__' v-model="tableFormData[i][cIndex].value"
							:multiple="childItem.multiple" :disabled="childItem.disabled"
							:selectType="childItem.selectType" :ableIds="childItem.ableIds"
							:placeholder="childItem.placeholder" @change="change(arguments,childItem,i)" />
					</block>
					<!-- 下拉补全 -->
					<block v-if="childItem.__config__.jnpfKey==='autoComplete'">
						<jnpf-auto-complete :disabled="childItem.disabled" :interfaceName="childItem.interfaceName"
							:placeholder="childItem.placeholder" :interfaceId="childItem.interfaceId"
							:total="childItem.total" v-model="tableFormData[i][cIndex].value"
							:templateJson="childItem.templateJson" :formData='formData'
							:relationField="childItem.relationField" :clearable='childItem.clearable'
							:propsValue="childItem.propsValue" :rowIndex="i"></jnpf-auto-complete>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='roleSelect'">
						<jnpf-role-select :vModel='childItem.__vModel__' v-model="tableFormData[i][cIndex].value"
							:multiple="childItem.multiple" :disabled="childItem.disabled"
							:selectType="childItem.selectType" :ableIds="childItem.ableIds"
							:placeholder="childItem.placeholder" @change="change(arguments,childItem,i)" />
					</block>
					<block v-if="childItem.__config__.jnpfKey==='timePicker'">
						<jnpf-date-time type="time" v-model="tableFormData[i][cIndex].value"
							:placeholder="childItem.placeholder" :disabled="disabled||childItem.disabled"
							@change="change(arguments,childItem,i)" :format="childItem.format"
							:startTime="childItem.startTime" :endTime='childItem.endTime'>
						</jnpf-date-time>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='datePicker'">
						<jnpf-date-time :type="childItem.type||'date'" v-model="tableFormData[i][cIndex].value"
							:placeholder="childItem.placeholder" :disabled="disabled||childItem.disabled"
							@change="change(arguments,childItem,i)" :format="childItem.format"
							:startTime="childItem.startTime" :endTime='childItem.endTime'>
						</jnpf-date-time>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='uploadImg'">
						<jnpf-upload v-model="tableFormData[i][cIndex].value" :disabled="disabled||childItem.disabled"
							:fileSize="childItem.fileSize" @change="change(arguments,childItem,i)"
							:tipText="childItem.tipText">
						</jnpf-upload>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='uploadFile'">
						<jnpf-file v-model="tableFormData[i][cIndex].value" :disabled="disabled||childItem.disabled"
							:limit="childItem.limit?childItem.limit:9" :sizeUnit="childItem.sizeUnit"
							:fileSize="childItem.fileSize" :accept="childItem.accept"
							@change="change(arguments,childItem,i)" :tipText="childItem.tipText" />
					</block>
					<block v-if="childItem.__config__.jnpfKey==='rate'">
						<jnpf-rate v-model="tableFormData[i][cIndex].value" :max="childItem.count"
							:disabled="disabled||childItem.disabled"
							@change="change(arguments,childItem,i)"></jnpf-rate>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='slider'">
						<jnpf-slider v-model="tableFormData[i][cIndex].value" :step="childItem.step"
							:min="childItem.min||0" :max="childItem.max||100" style="width: 100%;"
							:disabled="disabled||childItem.disabled" @change="change(arguments,childItem,i)" />
					</block>
					<block v-if="childItem.__config__.jnpfKey==='relationFormAttr'">
						<jnpf-relation-attr v-model="tableFormData[i][cIndex].value" :showField="childItem.showField"
							:relationField="childItem.relationField+'_jnpfRelation_'+i" type="relationFormAttr"
							:isStorage='childItem.isStorage' @change="change(arguments,childItem,i)">
						</jnpf-relation-attr>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='popupAttr'">
						<jnpf-relation-attr v-model="tableFormData[i][cIndex].value" :showField="childItem.showField"
							:relationField="childItem.relationField+'_jnpfRelation_'+i" type="popupAttr"
							:isStorage='childItem.isStorage' @change="change(arguments,childItem,i)">
						</jnpf-relation-attr>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='relationForm'">
						<jnpf-relation-select type="relation" v-model="tableFormData[i][cIndex].value"
							:placeholder="childItem.placeholder" :disabled="childItem.disabled"
							:modelId="childItem.modelId" :columnOptions="childItem.columnOptions"
							:relationField="childItem.relationField" :hasPage="childItem.hasPage"
							:pageSize="childItem.pageSize"
							:vModel="childItem.__config__.tableName ? childItem.__vModel__ + '_jnpfTable_' + childItem.__config__.tableName + (childItem
					                            .__config__.isSubTable ? '0' : '1')+'_jnpfRelation_'+i : childItem.__vModel__+'_jnpfRelation_'+i"
							@change="change(arguments,childItem,i)">
						</jnpf-relation-select>
					</block>
					<block v-if="childItem.__config__.jnpfKey === 'popupSelect'">
						<jnpf-popup-select type="popup" v-model="tableFormData[i][cIndex].value"
							:placeholder="childItem.placeholder" :disabled="childItem.disabled" :formData="formData"
							:templateJson="childItem.templateJson" :rowIndex="i" :interfaceId="childItem.interfaceId"
							:columnOptions="childItem.columnOptions" :relationField="childItem.relationField"
							:propsValue="childItem.propsValue" :hasPage="childItem.hasPage"
							:pageSize="childItem.pageSize"
							:vModel="childItem.__config__.tableName ? childItem.__vModel__ + '_jnpfTable_' + childItem.__config__.tableName + (childItem
					                            .__config__.isSubTable ? '0' : '1')+'_jnpfRelation_'+i : childItem.__vModel__+'_jnpfRelation_'+i"
							@change="change(arguments,childItem,i)">
						</jnpf-popup-select>
					</block>
					<block v-if="childItem.__config__.jnpfKey === 'popupTableSelect'">
						<jnpf-table-select v-model="tableFormData[i][cIndex].value" :placeholder="childItem.placeholder"
							:disabled="childItem.disabled" :interfaceId="childItem.interfaceId" :formData="formData"
							:templateJson="childItem.templateJson" :rowIndex="i"
							:columnOptions="childItem.columnOptions" :relationField="childItem.relationField"
							:propsValue="childItem.propsValue" :hasPage="childItem.hasPage"
							:pageSize="childItem.pageSize"
							:vModel="childItem.__config__.tableName ? childItem.__vModel__ + '_jnpfTable_' + childItem.__config__.tableName + (childItem.__config__.isSubTable ? '0' : '1')+'_jnpfRelation_'+i : childItem.__vModel__+'_jnpfRelation_'+i"
							:multiple="childItem.multiple" :filterable="childItem.filterable"
							@change="change(arguments,childItem,i)"></jnpf-table-select>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='organizeSelect'">
						<jnpf-com-select v-model="tableFormData[i][cIndex].value" :multiple="childItem.multiple"
							:placeholder="childItem.placeholder" :disabled="disabled||childItem.disabled"
							:selectType="childItem.selectType" :ableIds="childItem.ableIds"
							@change="change(arguments,childItem,i)">
						</jnpf-com-select>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='depSelect'">
						<jnpf-postordep-select type="department" v-model="tableFormData[i][cIndex].value"
							:multiple="childItem.multiple" :placeholder="childItem.placeholder"
							:disabled="childItem.disabled" :ableIds="childItem.ableIds"
							:selectType="childItem.selectType" @change="change(arguments,childItem,i)">
						</jnpf-postordep-select>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='posSelect'">
						<jnpf-postordep-select type="position" v-model="tableFormData[i][cIndex].value"
							:multiple="childItem.multiple" :placeholder="childItem.placeholder"
							:disabled="childItem.disabled" :ableIds="childItem.ableIds"
							:selectType="childItem.selectType" @change="change(arguments,childItem,i)">
						</jnpf-postordep-select>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='userSelect'">
						<jnpf-user-select v-model="tableFormData[i][cIndex].value" :multiple="childItem.multiple"
							:placeholder="childItem.placeholder" :disabled="childItem.disabled"
							:selectType="childItem.selectType" :ableIds="childItem.ableIds"
							:ableRelationIds="childItem.ableRelationIds" :clearable="childItem.clearable"
							@change="change(arguments,childItem,i)">
						</jnpf-user-select>
					</block>
					<!-- 用户组件 -->
					<block v-if="childItem.__config__.jnpfKey==='usersSelect'">
						<jnpf-user-choice v-model="tableFormData[i][cIndex].value" :multiple="childItem.multiple"
							:placeholder="childItem.placeholder" :disabled="childItem.disabled"
							:selectType="childItem.selectType" :ableIds="childItem.ableIds"
							:clearable="childItem.clearable" @change="change(arguments,childItem,i)">
						</jnpf-user-choice>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='treeSelect'">
						<jnpf-tree-select v-model="tableFormData[i][cIndex].value" :options="childItem.options"
							:props="childItem.props" :multiple="childItem.multiple" :placeholder="childItem.placeholder"
							:disabled="disabled||childItem.disabled" @change="change(arguments,childItem,i)"
							:filterable="childItem.filterable">
						</jnpf-tree-select>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='areaSelect'">
						<jnpf-city-select v-model="tableFormData[i][cIndex].value" :placeholder="childItem.placeholder"
							:level="childItem.level" :disabled="disabled||childItem.disabled"
							:multiple="childItem.multiple" @change="change(arguments,childItem,i)">
						</jnpf-city-select>
					</block>
					<block v-if="childItem.__config__.jnpfKey==='billRule'">
						<u-input input-align='right' v-model="tableFormData[i][cIndex].value" placeholder="系统自动生成"
							disabled @input="onChange($event,childItem,i)"></u-input>
					</block>
				</u-form-item>
			</view>

		</view>
		<view class="jnpf-table-addBtn" v-if="config.showAddBtn && !disabled && tableFormData.length>0"
			@click="addItem()">
			<u-icon name="plus" color="#2979ff"></u-icon>{{config.actionText}}
		</view>
		<view class="jnpf-table-addBtn" v-if="config.showAddBtn && tableFormData.length<=0 && !disabled"
			@click="addItem()">
			<u-icon name="plus" color="#2979ff"></u-icon>{{config.actionText}}
			{{config.__config__.label}}
		</view>
		<view class="jnpf-table-item" v-if="config.showSummary && summaryField.length">
			<view class="jnpf-table-item-title u-flex u-row-between">
				<text class="jnpf-table-item-title-num">{{config.__config__.label}}合计</text>
			</view>
			<view class=" u-p-l-20 u-p-r-20 form-item-box">
				<u-form-item v-for="(item,index) in summaryField" :label="item.__config__.label" :key="item.__vModel__">
					<u-input input-align='right' v-model="item.value" disabled placeholder=""></u-input>
				</u-form-item>
			</view>
		</view>
		<u-modal v-model="show" :content="content" width='70%' border-radius="16" :content-style="contentStyle"
			:titleStyle="titleStyle" :confirm-style="confirmStyle" :title="title" confirm-text="确定">
		</u-modal>
	</view>
</template>

<script>
	import {
		getDataInterfaceRes
	} from '@/api/common'
	const dyOptionsList = ['radio', 'checkbox', 'select', 'cascader', 'treeSelect']
	export default {
		name: 'jnpf-table',
		inject: ["parameter", "relations"],
		model: {
			prop: 'value',
			event: 'input'
		},
		props: {
			config: {
				type: Object,
				default: () => {}
			},
			formData: {
				type: Object,
				required: true
			},
			value: {
				type: [Array, String],
				default: () => ([])
			}
		},
		data() {
			return {
				dataInterfaceInfo: [],
				activeRowIndex: 0,
				tableData: [],
				tableFormData: [],
				summaryField: [],
				isIgnore: false,
				show: false,
				addType: 0,
				addTableConf: {},
				tableVmodel: '',
				childRelations: {},
				userInfo: {},
				content: '',
				contentStyle: {
					fontSize: '28rpx',
					padding: '20rpx',
					lineHeight: '44rpx',
					textAlign: 'left'
				},
				titleStyle: {
					padding: '20rpx'
				},
				confirmStyle: {
					height: '80rpx',
					lineHeight: '80rpx',
				},
				title: '提示',
			}
		},
		watch: {
			tableFormData: {
				handler(val, oldVal) {
					const data = this.submit(true)
					this.$emit('input', data)
					this.getTableSummaries()
				},
				deep: true
			},

		},
		computed: {
			disabled() {
				return this.config.disabled
			},
		},
		created() {
			this.userInfo = uni.getStorageSync('userInfo') || {}
			for (let key in this.relations) {
				if (key.includes('-')) {
					let tableVModel = key.split('-')[0]
					if (tableVModel === this.config.__vModel__) {
						let newKey = key.split('-')[1]
						this.childRelations[newKey] = this.relations[key]
					}
				}
			}
			uni.$on('linkPageConfirm', (subVal, Vmodel) => {
				if (this.config.__vModel__ === Vmodel) return subVal.forEach(t => this.tableFormData.unshift(this
					.getEmptyItem(t)))
				if (this.tableFormData.length) this.formatData()
				this.$nextTick(() => {
					uni.$emit('initCollapse')
				})
			})
			this.addType = this.config.addType || 0
			this.addTableConf = this.config.addTableConf
			let summaryField = this.config.summaryField || []
			this.summaryField = []
			this.tableData = this.config.__config__.children || []
			for (let i = 0; i < summaryField.length; i++) {
				for (let o = 0; o < this.tableData.length; o++) {
					if (this.tableData[o].__vModel__ === summaryField[i]) {
						this.summaryField.push(this.tableData[o])
						this.summaryField = this.summaryField.map(o => ({
							value: '0.00',
							...o
						}))
					}
				}
			}
			this.buildOptions()
			uni.$on('handleRelation', this.handleRelationForParent)
		},
		methods: {
			clickIcon(e) {
				if (!e.__config__.tipLabel) return
				this.content = e.__config__.tipLabel
				this.title = e.__config__.label
				this.show = true
			},
			getTableSummaries() {
				const newVal = this.tableFormData.map((row, i) => row.reduce((p, c) => {
					p[c.__vModel__] = c.value
					return p
				}, {}))
				for (let i = 0; i < this.summaryField.length; i++) {
					let val = 0
					for (let j = 0; j < newVal.length; j++) {
						if (newVal[j][this.summaryField[i].__vModel__]) {
							let data = isNaN(newVal[j][this.summaryField[i].__vModel__]) ? 0 :
								Number(newVal[j][this.summaryField[i].__vModel__])
							val += data
							this.summaryField[i].value = this.summaryField[i].thousands ? Number(val).toLocaleString(
								'zh', {
									maximumFractionDigits: '2',
									minimumFractionDigits: '2'
								}) : val.toFixed(2)
						}
					}
				}
			},
			handleRelationForParent(e, defaultValue, st) {
				if (!this.tableFormData.length) return
				for (let i = 0; i < this.tableFormData.length; i++) {
					let row = this.tableFormData[i];
					for (let j = 0; j < row.length; j++) {
						let item = row[j];
						const vModel = item.jnpfKey === 'popupSelect' ? item.__vModel__.substring(0, item.__vModel__
							.indexOf('_jnpfRelation_')) : item.__vModel__
						if (e.__vModel__ === vModel) {
							if (e.opType === 'setOptions') {
								item.options = []
								let query = {
									paramList: this.getParamList(e.__config__.templateJson, this.formData, i)
								}
								getDataInterfaceRes(e.__config__.propsUrl, query).then(res => {
									item.options = Array.isArray(res.data) ? res.data : []
								})
							}
							if (e.opType === 'setUserOptions') {
								if (e.relationField.includes('-')) {
									const [attr1, attr2] = e.relationField.split('-')
									this.$nextTick(() => {
										let value = this.formData[attr1][i][attr2] || []
										this.$set(this.tableFormData[i][j], 'ableRelationIds', Array.isArray(
											value) ? value : [
											value
										])
									})
								} else {
									let value = this.formData[e.relationField] || []
									this.$set(this.tableFormData[i][j], 'ableRelationIds', Array.isArray(value) ? value : [
										value
									])
								}
							}
							this.$nextTick(() => {
								if (e.opType === 'setDate') {
									let startTime = 0
									let endTime = 0
									if (e.__config__.startRelationField && e.__config__.startTimeType == 2) {
										if (e.__config__.startRelationField.includes('-')) {
											const [attr0, attr5] = e.__config__.startRelationField.split('-')
											startTime = this.formData[attr0][i][attr5] || 0
										} else {
											startTime = this.formData[e.__config__.startRelationField] || 0
										}
									} else {
										startTime = e.startTime
									}
									if (e.__config__.endRelationField && e.__config__.endTimeType == 2) {
										if (e.__config__.endRelationField.includes('-')) {
											const [attr3, attr4] = e.__config__.endRelationField.split('-')
											endTime = this.formData[attr3][i][attr4] || 0
										} else {
											endTime = this.formData[e.__config__.endRelationField] || 0
										}
									} else {
										endTime = e.endTime
									}
									item.startTime = startTime
									item.endTime = endTime
								}
								if (e.opType === 'setTime') {
									let format = e.format
									let startTime = ''
									let endTime = ''
									if (e.__config__.startRelationField && e.__config__.startTimeType == 2) {
										if (e.__config__.startRelationField.includes('-')) {
											const [attr0, attr5] = e.__config__.startRelationField.split('-')
											startTime = this.formData[attr0][i][attr5] || '00:00:00'
										} else {
											startTime = this.formData[e.__config__.startRelationField] ||
												'00:00:00'
										}
										startTime = startTime + ':00'
										if (startTime.split(':').length == 3) {
											startTime = startTime
										} else {
											startTime = startTime + ':00'
										}
									} else {
										startTime = e.startTime
									}
									if (e.__config__.endRelationField && e.__config__.endTimeType == 2) {
										if (e.__config__.endRelationField.includes('-')) {
											const [attr3, attr4] = e.__config__.endRelationField.split('-')
											endTime = this.formData[attr3][i][attr4] || '23:59:59'
										} else {
											endTime = this.formData[e.__config__.endRelationField] || '23:59:59'
										}
										if (endTime.split(':').length == 3) {
											endTime = endTime
										} else {
											endTime = endTime + ':00'
										}
									} else {
										endTime = e.endTime
									}
									item.startTime = startTime
									item.endTime = endTime
								}
							})
							if (item.value != defaultValue) {
								if (st || !item.value) item.value = defaultValue
							}
						}
					}
				}
			},
			handleRelation(data, rowIndex) {
				const currRelations = this.childRelations
				for (let key in currRelations) {
					if (key === data.__vModel__) {
						for (let i = 0; i < currRelations[key].length; i++) {
							const e = currRelations[key][i];
							const config = e.__config__
							const jnpfKey = config.jnpfKey
							let defaultValue = ''
							if (['checkbox', 'cascader'].includes(jnpfKey) || (['select', 'treeSelect',
									'popupSelect',
									'popupTableSelect', 'userSelect'
								].includes(jnpfKey) && e.multiple)) {
								defaultValue = []
							}
							let row = this.tableFormData[rowIndex];
							for (let j = 0; j < row.length; j++) {
								let item = row[j];
								const vModel = item.jnpfKey === 'popupSelect' ? item.__vModel__.substring(0, item
									.__vModel__.indexOf('_jnpfRelation_')) : item.__vModel__
								if (e.__vModel__ === vModel) {
									if (e.opType === 'setOptions') {
										item.options = []
										let query = {
											paramList: this.getParamList(config.templateJson, this
												.formData,
												rowIndex)
										}
										getDataInterfaceRes(config.propsUrl, query).then(res => {
											item.options = Array.isArray(res.data) ? res.data : []
										})
									}
									if (e.opType === 'setUserOptions') {
										let value = this.getFieldVal(e.relationField, rowIndex) || []
										item.ableRelationIds = Array.isArray(value) ? value : [value]
									}
									if (e.opType === 'setDate') {
										let startTime = 0
										let endTime = 0
										if (config.startRelationField && config.startTimeType == 2) {
											startTime = this.getFieldVal(config.startRelationField, rowIndex) || 0
										} else {
											startTime = e.startTime
										}
										if (config.endRelationField && config.endTimeType == 2) {
											endTime = this.getFieldVal(config.endRelationField, rowIndex) || 0
										} else {
											endTime = e.endTime
										}
										item.startTime = startTime
										item.endTime = endTime
									}
									if (e.opType === 'setTime') {
										let startTime = 0
										let endTime = 0
										if (config.startRelationField && config.startTimeType == 2) {
											startTime = this.getFieldVal(config.startRelationField, rowIndex) ||
												'00:00:00'
											if (startTime.split(':').length == 3) {
												startTime = startTime
											} else {
												startTime = startTime + ':00'
											}
										} else {
											startTime = e.startTime
										}
										if (config.endRelationField && config.endTimeType == 2) {
											endTime = this.getFieldVal(config.endRelationField, rowIndex) ||
												'23:59:59'
											if (endTime.split(':').length == 3) {
												endTime = endTime
											} else {
												endTime = endTime + ':00'
											}
										} else {
											endTime = e.endTime
										}
										item.startTime = startTime
										item.endTime = endTime
									}
									if (item.value != defaultValue) {
										item.value = defaultValue
										this.$nextTick(() => this.handleRelation(item, rowIndex));
									}
								}
							}
						}
					}
				}
			},
			handleDefaultRelation(data, rowIndex = 0) {
				const currRelations = this.childRelations
				for (let key in currRelations) {
					if (key === data) {
						for (let i = 0; i < currRelations[key].length; i++) {
							const e = currRelations[key][i];
							const config = e.__config__
							let defaultValue = ''
							let row = this.tableFormData[rowIndex];
							for (let j = 0; j < row.length; j++) {
								let item = row[j];
								const vModel = item.jnpfKey === 'popupSelect' ? item.__vModel__.substring(0, item
									.__vModel__.indexOf('_jnpfRelation_')) : item.__vModel__
								if (e.__vModel__ === vModel) {
									if (e.opType === 'setUserOptions') {
										let value = this.getFieldVal(e.relationField, rowIndex) || []
										item.ableRelationIds = Array.isArray(value) ? value : [value]
									}
									if (e.opType === 'setDate') {
										let startTime = 0
										let endTime = 0
										if (config.startRelationField && config.startTimeType == 2) {
											startTime = this.getFieldVal(config.startRelationField, rowIndex) || 0
										} else {
											startTime = e.startTime
										}
										if (config.endRelationField && config.endTimeType == 2) {
											endTime = this.getFieldVal(config.endRelationField, rowIndex) || 0
										} else {
											endTime = e.endTime
										}
										item.startTime = startTime
										item.endTime = endTime
									}
									if (e.opType === 'setTime') {
										let startTime = 0
										let endTime = 0
										if (config.startRelationField && config.startTimeType == 2) {
											startTime = this.getFieldVal(config.startRelationField, rowIndex) ||
												'00:00:00'
											if (startTime.split(':').length == 3) {
												startTime = startTime
											} else {
												startTime = startTime + ':00'
											}
										} else {
											startTime = e.startTime
										}
										if (config.endRelationField && config.endTimeType == 2) {
											endTime = this.getFieldVal(config.endRelationField, rowIndex) ||
												'23:59:59'
											if (endTime.split(':').length == 3) {
												endTime = endTime
											} else {
												endTime = endTime + ':00'
											}
										} else {
											endTime = e.endTime
										}
										item.startTime = startTime
										item.endTime = endTime
									}
								}
							}
						}
					}
				}
			},
			getFieldVal(field, rowIndex) {
				let val = ''
				if (field.includes('-')) {
					let childVModel = field.split('-')[1]
					let list = this.tableFormData[rowIndex].filter(o => o.__vModel__ === childVModel)
					if (!list.length) {
						val = ''
					} else {
						let item = list[0]
						val = item.value
					}
				} else {
					val = this.formData[field] || ''
				}
				return val
			},
			buildRowAttr(rowIndex, val) {
				let row = this.tableFormData[rowIndex];
				for (let i = 0; i < row.length; i++) {
					let item = row[i];
					const config = item.__config__
					for (let key in this.value[rowIndex]) {
						if (key === item.__vModel__) {
							item.value = this.value[rowIndex][key]
						}
					}
					if (val && dyOptionsList.indexOf(config.jnpfKey) > -1) {
						if (config.dataType === 'dynamic' && config.propsUrl) {
							this.handleRelation(item, rowIndex)
							if (cur.options && cur.options.length && (!config.templateJson || !config.templateJson
									.length || !this.hasTemplateJsonRelation(config.templateJson))) continue
							let query = {
								paramList: config.templateJson ? this.getParamList(config.templateJson, this
									.formData,
									rowIndex) : [],
							}
							const matchInfo = JSON.stringify({
								id: config.propsUrl,
								query
							});
							const item = {
								matchInfo,
								rowIndex,
								colIndex: i
							};
							const infoIndex = this.dataInterfaceInfo.findIndex(item => item.matchInfo === matchInfo);
							let useCacheOptions = false;
							if (infoIndex === -1) {
								this.dataInterfaceInfo.push(item);
							} else {
								const cacheOptions = this.getCacheOptions(infoIndex);
								if (cacheOptions.length) {
									item.options = cacheOptions;
									useCacheOptions = true;
								}
							}
							if (!useCacheOptions) {
								getDataInterfaceRes(config.propsUrl, query).then(res => {
									item.options = Array.isArray(res.data) ? res.data : []
								})
							}
						}
					}
					if (config.jnpfKey === 'userSelect' && item.relationField && item.selectType !== 'all' && item
						.selectType !== 'custom') {
						let value = this.getFieldVal(item.relationField, rowIndex) || []
						item.ableRelationIds = Array.isArray(value) ? value : [value]
					}
					if (config.jnpfKey === 'datePicker') {
						let startTime = 0
						let endTime = 0
						if (config.startRelationField && config.startTimeType == 2) {
							startTime = this.getFieldVal(config.startRelationField, rowIndex) || 0
						} else {
							startTime = item.startTime
						}
						if (config.endRelationField && config.endTimeType == 2) {
							endTime = this.getFieldVal(config.endRelationField, rowIndex) || 0
						} else {
							endTime = item.endTime
						}
						item.startTime = startTime
						item.endTime = endTime
					}
					if (config.jnpfKey === 'timePicker') {
						let startTime = 0
						let endTime = 0
						if (config.startRelationField && config.startTimeType == 2) {
							startTime = this.getFieldVal(config.startRelationField, rowIndex) || '00:00:00'
							if (startTime && (startTime.split(':').length == 3)) {
								startTime = startTime
							} else {
								startTime = startTime + ':00'
							}
						} else {
							startTime = item.startTime
						}
						if (config.endRelationField && config.endTimeType == 2) {
							endTime = this.getFieldVal(config.endRelationField, rowIndex) || '23:59:59'
							if (startTime.split(':').length == 3) {
								endTime = endTime
							} else {
								endTime = endTime + ':00'
							}
						} else {
							endTime = item.endTime
						}
						item.startTime = startTime
						item.endTime = endTime
					}
				}
			},
			// 获取缓存options数据
			getCacheOptions(index) {
				const item = this.dataInterfaceInfo[index];
				if (item.rowIndex === -1) {
					return this.tableData[item.colIndex].options || [];
				} else {
					return this.tableFormData[item.rowIndex][item.colIndex].options || [];
				}
			},
			// 判断templateJson里是否有关联字段
			hasTemplateJsonRelation(templateJson) {
				return templateJson.some(o => o.relationField);
			},
			getParamList(templateJson, formData, index) {
				for (let i = 0; i < templateJson.length; i++) {
					if (templateJson[i].relationField) {
						if (templateJson[i].relationField.includes('-')) {
							let childVModel = templateJson[i].relationField.split('-')[1]
							let list = this.tableFormData[index].filter(o => o.__vModel__ === childVModel)
							if (!list.length) {
								templateJson[i].defaultValue = ''
							} else {
								let item = list[0]
								templateJson[i].defaultValue = item.value
							}
						} else {
							templateJson[i].defaultValue = formData[templateJson[i].relationField] || ''
						}
					}
				}
				return templateJson
			},
			getDefaultParamList(templateJson, formData) {
				for (let i = 0; i < templateJson.length; i++) {
					if (templateJson[i].relationField) {
						if (templateJson[i].relationField.includes('-')) {
							let childVModel = templateJson[i].relationField.split('-')[1]
							let list = this.tableData.filter(o => o.__vModel__ === childVModel)
							templateJson[i].defaultValue = ''
							if (list.length) templateJson[i].defaultValue = list[0].__config__.defaultValue || ''
						} else {
							templateJson[i].defaultValue = formData[templateJson[i].relationField] || ''
						}
					}
				}
				return templateJson
			},
			buildOptions() {
				for (let i = 0; i < this.tableData.length; i++) {
					const config = this.tableData[i].__config__
					if (dyOptionsList.indexOf(config.jnpfKey) > -1) {
						if (config.dataType === 'dictionary' && config.dictionaryType) {
							this.$store.dispatch('base/getDicDataSelector', config.dictionaryType).then(res => {
								this.tableData[i].options = res
							})
						}
						if (config.dataType === 'dynamic' && config.propsUrl) {
							let query = {
								paramList: config.templateJson ? this.getDefaultParamList(config.templateJson, this
									.formData) : [],
							}
							const matchInfo = JSON.stringify({
								id: config.propsUrl,
								query
							});
							const item = {
								matchInfo,
								rowIndex: -1,
								colIndex: i
							};
							this.dataInterfaceInfo.push(item);
							getDataInterfaceRes(config.propsUrl, query).then(res => {
								this.tableData[i].options = Array.isArray(res.data) ? res.data : []
							})
						}
					}
				}
				this.init()
			},
			init() {
				if (Array.isArray(this.value) && this.value.length) {
					this.value.forEach(t => this.tableFormData.push(this.getEmptyItem(t)))
					this.initRelationData()
				}
				this.$nextTick(() => {
					uni.$emit('initCollapse')
				})
			},
			initRelationData() {
				const handleRelationFun = (list) => {
					list.forEach(cur => {
						this.handleDefaultRelation(cur.__vModel__)
						if (cur.__config__.children) handleRelationFun(cur.__config__.children)
					})
				}
				handleRelationFun(this.config.__config__.children)
			},
			addItem(val) {
				if (this.addType == 0) {
					this.tableFormData.push(this.getEmptyItem(val))
					if (this.tableFormData.length) this.formatData()
					this.$nextTick(() => {
						uni.$emit('initCollapse')
					})
					const rowIndex = this.tableFormData.length - 1
					this.buildRowAttr(rowIndex, val)
				} else {
					this.tableVmodel = this.config.__vModel__
					let data = {
						addTableConf: this.addTableConf,
						formData: this.formData,
						tableVmodel: this.tableVmodel
					}
					uni.navigateTo({
						url: '/pages/apply/tableLinkage/index?data=' + encodeURIComponent(JSON
							.stringify(
								data))
					})
				}
			},
			delItem(i, title) {
				var that = this
				uni.showModal({
					title: '提示',
					content: '确认删除该条数据吗？',
					success: function(res) {
						if (res.confirm) {
							that.tableFormData.splice(i, 1)
							that.$nextTick(() => {
								uni.$emit('initCollapse')
							})
						}
					}
				})
			},
			formatData() {
				this.tableFormData.forEach((item, index) => {
					item.forEach((it) => {
						if (it.__config__.jnpfKey == 'inputNumber') {
							this.$set(it, 'value', Number(it.value))
						}
						if (this.tableFormData.length) {
							if (it.__config__.jnpfKey === 'datePicker' && it.__config__.defaultCurrent ==
								true && index === this.tableFormData.length - 1) {
								this.$set(it, 'value', new Date().getTime())
							} else if (it.__config__.jnpfKey === 'organizeSelect' && it.__config__
								.defaultCurrent == true && index === this.tableFormData.length - 1) {
								if (this.userInfo.organizeIdList instanceof Array && this.userInfo
									.organizeIdList.length > 0) {
									if (it.multiple == true) {
										this.$set(it, 'value', [this.userInfo.organizeIdList])
									} else {
										this.$set(it, 'value', this.userInfo.organizeIdList)
									}
								} else {
									this.$set(it, 'value', [])
								}
							} else {

							}
						}
					})
				})
			},
			getEmptyItem(val) {
				return this.tableData.map(t => {
					if (t.__config__.jnpfKey === 'datePicker' && t.__config__.defaultCurrent) {
						let format = this.jnpf.handelFormat(t.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' : ''
						t.__config__.defaultValue = new Date(dateStr + time).getTime()
					}
					if (t.__config__.jnpfKey === 'timePicker' && t.__config__.defaultCurrent) {
						t.__config__.defaultValue = this.jnpf.toDate(new Date(), t.format)
					}
					if (t.__config__.jnpfKey === 'inputNumber') {
						let defVal;
						if (t.__config__.defaultValue === undefined) {
							t.__config__.defaultValue = defVal
						}
					}
					let res = {
						...t,
						value: val ? val[t.__vModel__] : t.__config__.defaultValue
					}
					return res
				})
			},
			checkData(item) {
				if ([null, undefined, ''].includes(item.value)) return false
				if (Array.isArray(item.value)) return item.value.length > 0
				return true
			},
			submit(noShowToast) {
				let res = true
				outer: for (let i = 0; i < this.tableFormData.length; i++) {
					const row = this.tableFormData[i]
					for (let j = 0; j < row.length; j++) {
						if (row[j].__config__.required && !this.checkData(row[j]) && row[j].__config__
							.isVisibility && !row[j].__config__.noShow) {
							res = false
							if (!noShowToast) this.$u.toast(
								`${this.config.__config__.label}(${i+1})${row[j].__config__.label}不能为空`
							)
							break outer
						}
						if (row[j].__config__.regList && row[j].__config__.regList.length && row[j]
							.__config__
							.isVisibility) {
							let regList = row[j].__config__.regList
							for (let ii = 0; ii < regList.length; ii++) {
								const item = regList[ii];
								let pattern = eval(item.pattern)
								if (row[j].value && !pattern.test(row[j].value)) {
									if (!noShowToast) this.$u.toast(
										`${this.config.__config__.label}(${i+1})${row[j].__config__.label}${item.message}`
									)
									res = false
									break outer
								}
							}
						}
					}
				}
				const data = this.tableFormData.map(row => row.reduce((p, c) => (p[c.__vModel__] = c.value, p), {})) || []
				return noShowToast ? data : res ? data : false
			},
			setTableFormData(prop, value) {
				let activeRow = this.tableFormData[this.activeRowIndex]
				for (let i = 0; i < activeRow.length; i++) {
					if (activeRow[i].__vModel__ === prop) {
						activeRow[i].value = value
						break
					}
				}
			},
			getTableFieldOptions(prop) {
				let res = []
				for (let i = 0; i < this.tableData.length; i++) {
					if (this.tableData[i].__vModel__ === prop) {
						let item = this.tableData[i]
						let isTreeSelect = item.__config__.jnpfKey === 'treeSelect' || item.__config__.jnpfKey ===
							'cascader'
						res = item.options || []
						break
					}
				}
				return res
			},
			onChange(val, data, rowIndex) {
				let res = val.value ? val.value : val
				this.activeRowIndex = rowIndex
				var jnpfKey = data.__config__.jnpfKey
				if (jnpfKey == "inputNumber" || jnpfKey == "switch" || jnpfKey == "input" || jnpfKey == "textarea") {
					this.setScriptFunc(res, data)
				}
				this.handleRelation(data, rowIndex)
			},
			change(val, data, rowIndex) {
				this.activeRowIndex = rowIndex
				this.handleRelation(data, rowIndex)
				let res = ''
				if (val.length > 1) {
					res = val[1]
				} else {
					if (data.__config__.jnpfKey == "uploadImg" || data.__config__.jnpfKey == "uploadFile") {
						res = val[0]
					} else {
						res = this.getDefalutData(data)
					}
				}
				this.setScriptFunc(res, data)
			},
			setScriptFunc(res, data) {
				if (data && data.on && data.on.change) {
					const func = this.jnpf.getScriptFunc(data.on.change);
					if (!func) return
					func.call(this, {
						data: res,
						...this.parameter
					})
				}
			},
			getDefalutData(item) {
				let config = item.__config__
				let data = ''
				if (['select', 'radio', 'checkbox'].includes(config.jnpfKey)) {
					let options = item.options
					let props = item.props
					if (config.jnpfKey === 'checkbox' || (config.jnpfKey === 'select' && item.multiple)) {
						let _data = []
						outer: for (let i = 0; i < item.value.length; i++) {
							inner: for (let j = 0; j < options.length; j++) {
								if (item.value[i] === options[j][props.value]) {
									_data.push(options[j])
									break inner
								}
							}
						}
						data = _data
					} else {
						let _data = {}
						for (let i = 0; i < options.length; i++) {
							if (item.value == options[i][props.value]) {
								_data = options[i]
								break
							}
						}
						data = _data
					}
				}
				return data ? data : item.value ? item.value : config.defaultValue
			},
		}
	}
</script>