<template>
	<view class="uni-container">
		<view class="example container" :style="'height: ' + (screenHeight - wheight - 75) + 'px'">
			<u--form ref="customForm" :model="formData" labelPosition="left" labelWidth="100%" labelAlign="left">
				<view class="juli-bottom">
					<u-form-item label="检查单位或小区 (个)" prop="jcdwhxq">
						<u-input v-model="formData.jcdwhxq" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="检查通道 (处)" prop="jctd">
						<u-input v-model="formData.jctd" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="发现隐患(处)" prop="fxyh">
						<u-input v-model="formData.fxyh" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="整改隐患 (处)" prop="zgyh">
						<u-input v-model="formData.zgyh" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="安装消防通道智能监控设备 (套)" prop="azxftdznjksb">
						<u-input v-model="formData.azxftdznjksb" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="施划消防车道标线的单位或小区 (个)" prop="shxfcdbxhdwxq">
						<u-input v-model="formData.shxfcdbxhdwxq" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<picker mode="date" :value="dateTime" @change="bindDateChange" :disabled="isview">
						<view class="datatime-street">
							<u-form-item label="填报日期" prop="reporttime" :required="true">
								<u-input v-model="formData.reporttime" placeholder="选择日期" class="add-date"
									:disabled="isview" />
							</u-form-item>
						</view>
					</picker>
					<view>
						<view class="paizhao">现场照片</view>
						<view class="paizhao-posit" v-if="!isview">
							<image :src="photograph" @click="photoUpload" class="tiantupian-photo"></image>
							<view class="">
								拍照上传
							</view>
						</view>
					</view>
					<view v-if="formData.streetRepPicsDOList" class="show-photo">
						<view v-for="(items,indexs) in formData.streetRepPicsDOList" :key="indexs">
							<view class="show-photo-main">
								<view class="show-photo-main-img">
									<image :src="items.url" class="show-photo-main-img-item">
										<u-icon name="close" color="#4CB2B6" size="19" @click="closeImage(items,indexs)"
											v-if="!isview">
										</u-icon>
								</view>
								<view class="">
									<view>
										图片名称
									</view>
									<view class="img-name">
										<u-input v-model="items.feildname" placeholder="请输入图片名称" :disabled="isview" />
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</u--form>
		</view>
		<view class="sub-botn heigthButton">
			<u-button v-if="isedit" @click="submit('customForm')" type="primary" shape="circle" size="large"
				color="#4CB2B6" text="确认修改" style="width:200px">
			</u-button>
			<u-button v-else-if="!formData.id" @click="submit('customForm')" type="primary" shape="circle" size="large"
				color="#4CB2B6" text="确认新增" style="width:200px">
			</u-button>
			<u-button v-else-if="isview" @click="submit('customForm')" type="primary" shape="circle" size="large"
				color="#4CB2B6" text="返回" style="width:200px">
			</u-button>
		</view>
	</view>
</template>

<script>
	import {
		createXftdpczl,
		getIdXftdpczl,
		updateXftdpczl
	}
	from '@/api/fire_inspetItem/index';
	import {
		takePhotos,
		NumericRule
	} from '@/utils/common.js';
	import config from '@/config'
	const baseUrlImg = config.baseUrlImg
	export default {
		data() {
			return {
				screenHeight: this.$screenHeight,
				wheight: '',
				photograph: `${baseUrlImg}/checkActive/photograph.png`,
				dateTime: '',
				formData: {
					jcdwhxq: '',
					jctd: '',
					fxyh: '',
					zgyh: '',
					azxftdznjksb: '',
					shxfcdbxhdwxq: '',
					reporttime: '',
					streetRepPicsDOList: []
				},
				rules: {
					jcdwhxq: NumericRule(),
					jctd: NumericRule(),
					fxyh: NumericRule(),
					zgyh: NumericRule(),
					azxftdznjksb: NumericRule(),
					shxfcdbxhdwxq: NumericRule(),
					reporttime: [{
						required: true,
						message: '请输入日期',
						trigger: ['change', 'blur'],
					}],
				},
				isedit: false, //编辑
				isview: false, //查看
			}
		},
		onReady() {
			this.$refs.customForm.setRules(this.rules);
		},
		mounted() {
			uni.createSelectorQuery().in(this).select('.heigthButton').boundingClientRect(data => {
				this.wheight = data.height
			}).exec()
		},
		onLoad(option) {
			if (option.editData) {
				this.isedit = true
				this.parentId = option.editData
				this.getIdData()
				// this.formData = JSON.parse(option.editData)
			} else if (option.viewData) {
				this.isview = true
				this.parentId = option.viewData
				this.getIdData()
				// this.formData = JSON.parse(option.viewData)
			}

		},
		methods: {
			getIdData() {
				getIdXftdpczl({
					id: this.parentId
				}).then(response => {
					this.formData = response.data
					console.log(response, 'response')
				})
			},
			//拍照
			closeImage(val, index) {
				this.formData.streetRepPicsDOList.splice(index, 1)
			},
			photoUpload() {
				takePhotos(this)
			},
			bindDateChange(e) {
				this.formData.reporttime = e.detail.value
			},
			submit(ref) {
				this.$refs[ref].validate().then(res => {
					const formList = this.formData
					Object.keys(formList).forEach(key => {
						formList[key] = formList[key] === '' ? 0 : formList[key];
					});
					formList.streetRepPicsDOList.forEach((item) => {
						// 删除除了feildname和url以外的多余字段
						Object.keys(item).forEach((key) => {
							if (key !== 'feildname' && key !== 'url' && key !== 'repid') {
								delete item[key];
							}
						});
						// 如果feildname为空，赋值为默认名称
						if (!item.feildname) {
							item.feildname = '默认名称';
						}
					});
					console.log(formList, 'this.formData');
					if (this.isedit) {
						updateXftdpczl(JSON.stringify(this.formData)).then(res => {
							this.$modal.msg('修改成功')
							this.isedit = false
							setTimeout(() => {
								uni.navigateBack()
							}, 400)
						})
					} else if (this.isview) {
						this.isview = false
						uni.navigateBack()
					} else {
						createXftdpczl(JSON.stringify(this.formData)).then(res => {
							this.$modal.msg('新增成功')
							setTimeout(() => {
								uni.navigateBack()
							}, 400)
						})
					}

				}).catch(err => {
					console.log(err);
				})
			}
		}
	}
</script>


<style lang="scss" scoped>
	body {
		background-color: #f5f7f9;
	}

	page {
		background-color: #f5f7f9 !important;
	}

	.uni-container {
		height: 100vh;
		background-color: #f5f7f9 !important;
	}

	/deep/.uni-card {
		position: relative;
		box-shadow: 0px 0px 3px 0px rgba(86, 165, 168, 0.15);
		border-radius: 30rpx;
	}

	.example {
		overflow-y: auto;
	}
</style>