<template>
	<!-- 表单 -->
	<view style="margin: 0rpx 20rpx 0rpx 30rpx;">
		<u-form :model="model" :rules="rules" labelPosition="left" ref="uForm" labelWidth="70">
			<view v-for="(item,index) in list" :key="index">
				<u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
					v-if="item.type == 'input' && customerName(item)">
					<view class="card_form_item">
						<u-input v-model="model[`${item.field}`]" :disabled="item.disabled ? true : false"
							:placeholder="item.placeholder"></u-input>
					</view>
				</u-form-item>
				<u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
					v-else-if="item.type == 'select'">
					<view class="card_form_item">
						<u-input :placeholder="item.placeholder" suffixIcon="arrow-down"
							v-model="model[`${item.field}`]" @focus="change(item)"></u-input>
					</view>
				</u-form-item>
				<u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
					v-else-if="item.type == 'selectil'">
					<view class="card_form_item_selectil frameil" @click="change(item)">
						<view class="title_input_selectil" :class="model[`${item.field}`] ? '' : 'hsColor'">
							{{model[`${item.field}`] || item.placeholder}}
						</view>
						<u-icon name="arrow-down" size="18px"></u-icon>
					</view>
				</u-form-item>
				<!-- 保温箱选择 -->
				<u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
					v-else-if="item.type == 'selectkey' && getCooler()">
					<view class="card_form_item_selectil frameil" @click="incubatorChange(item)">
						<view class="title_input_selectil" :class="model[`${item.field}`] ? '' : 'hsColor'">
							{{model[`${item.field + 'name'}`] || item.placeholder}} {{model[`${item.field}`]}}
						</view>
						<u-icon name="arrow-down" size="18px"></u-icon>
					</view>
				</u-form-item>
				<u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
					v-else-if="item.type == 'textarea'">
					<view class="card_form_item">
						<u-textarea v-model="model[`${item.field}`]" autoHeight
							:placeholder="item.placeholder"></u-textarea>
					</view>
				</u-form-item>
			</view>
		</u-form>
		<u-popup :show="incubatorShow" closeable @close="close" @open="open">
			<view class="card_incubator">
				<view class="head_place">选择保温箱</view>
				<view class="card_search">
					<u-search placeholder="请输入保温箱名称" v-model="keyword" :showAction="false"
						@change="searchChange"></u-search>
				</view>
				<view style="display: flex;flex-direction: column;" v-if="incubatorData.length > 0">
					<view class="card_title_item" :class="item.name == model.coolerBoxIdname ? 'color_blue' : ''"
						v-for="(item,index) in incubatorData" :key="index" @click="incubatorClick(item)">
						{{item.name}}
					</view>
				</view>
				<view style="margin-bottom: 20px;" v-else>
					<u-empty mode="list" text="暂无保温箱" marginTop="50"></u-empty>
				</view>
			</view>
		</u-popup>
		<u-picker :defaultIndex="findIndex(model[fieldType], columns[0])" :show="show" :columns="columns"
			keyName="label" @confirm="confirm($event, fieldType)" @cancel="cancel"></u-picker>
	</view>
</template>

<script>
	export default {
		name: 'x-form',
		props: {
			list: {
				type: Array,
				default () {
					return []
				}
			},
			model: {
				type: Object,
				default () {
					return {}
				}
			},
			rules: {
				type: Object,
				default () {
					return {}
				}
			},
		},
		data() {
			return {
				show: false,
				columns: [],
				fieldType: '',
				incubatorShow: false,
				incubatorData: [],
				keyword: '',
				incuType: '',
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			customerName(value) {
				var userInfo = this.$cache.getCache('userInfo')
				if (userInfo.userType == 'customer') {
					if (value.field != 'customerName') {
						if (value.type == 'input') {
							return true
						}
					}
				} else {
					if (value.type == 'input') {
						return true
					}
				}
			},
			change(e) {
				this.columns = []
				this.fieldType = e.field
				this.show = true
				this.$nextTick(() => {
					this.columns.push(e.options)
				})
			},
			// 保温箱选择
			incubatorChange(event) {
				this.incuType = event.field
				// console.log(event, 3254)
				this.incubatorShow = true
				this.$api.get('/api/cooler-box', {
					page: 1,
					pageSize: 999,
					isBind: true,
					name: this.keyword,
				}).then(res => {
					if (res.code == 200) {
						this.incubatorData = res.data.list
					}
				})
			},
			// 选择保温箱
			incubatorClick(event) {
				this.$refs.uForm.validateField(this.incuType);
				this.model[this.incuType] = event.id
				this.$emit('incubatorClick', event)
				this.incubatorShow = false
			},
			// 搜索
			searchChange() {
				this.incubatorData = []
				this.incubatorChange()
			},
			// 选择确定
			confirm(value, type) {
				this.$refs.uForm.validateField(type);
				this.show = false
				this.model[type] = value.value[0].label
			},
			getCooler() {
				if (this.model.deliveryCondition == "保温箱") {
					return true
				}
			},
			// 点击确定
			cancel() {
				this.show = false
			},
			findIndex(code, list) {
				if (!code || !list) {
					return [0]
				}
				return [list.findIndex((item) => (item.label === code || item.label === code))]
			},
			close() {
				this.incubatorShow = false
			},
			open() {

			},
			//子组件校验，传递到父组件
			async validateForm() {
				let flag = null
				await this.$refs.uForm.validate(valid => {}).then(res => {
					flag = true
				}).catch(err => {
					flag = false
				})
				return flag
			}
		},
	}
</script>

<style lang="scss">
	.card_form_item {
		width: 100%;
	}

	.card_form_item_selectil {
		width: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		padding: 6px 9px;
		height: 52rpx;
	}

	.title_input_selectil {
		font-size: 30rpx;
		// width: 100%;
	}

	.hsColor {
		color: rgb(192, 196, 204);
	}

	// 伪元素1rpx边框
	.frameil {
		position: relative; //重要
	}

	.frameil::after {
		position: absolute;
		content: '';
		border: 2rpx solid #dadbde;
		width: 200%;
		height: 200%;
		top: 0;
		left: 0;
		transform: scale(0.5);
		transform-origin: 0 0;
		pointer-events: none;
		border-radius: 20rpx;
		/* 使伪元素不会阻止鼠标事件 */
	}

	.card_incubator {
		min-height: 600rpx;
		max-height: 900rpx;
	}

	.head_place {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 20rpx;
	}

	.card_search {
		margin: 20rpx;
	}

	.card_title_item {
		position: relative;
		padding: 30rpx;
		// border-bottom: 1rpx solid #dadbde;
	}

	.color_blue {
		color: #2979ff;
	}

	.card_title_item:before {
		content: " ";
		position: absolute;
		left: 0;
		bottom: 0;
		width: 100%;
		height: 1px;
		border-top: 1px solid #e7e6e4;
		-webkit-transform-origin: 0 0;
		transform-origin: 0 0;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
	}
</style>