<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="djlr">
						<u-input v-model="formData.djlr" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="其他特殊困难群众 (人)" prop="qttsknqz">
						<u-input v-model="formData.qttsknqz" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="上门帮扶 (次)" prop="smbf">
						<u-input v-model="formData.smbf" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<view class="big-title">
						帮改隐患 (处) :
					</view>
					<u-form-item label="电源" prop="bgyhdy" class="children-text">
						<u-input v-model="formData.bgyhdy" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="火源" prop="bgyhhy" class="children-text">
						<u-input v-model="formData.bgyhhy" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="气源" prop="bgyhqy" class="children-text">
						<u-input v-model="formData.bgyhqy" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="清理可燃物及其他隐患" prop="bgyhqlkrwjqtyh" class="children-text">
						<u-input v-model="formData.bgyhqlkrwjqtyh" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="安装独立式火灾报警探测器-非联网型 (个)" prop="hzbjtcqzsflwx">
						<u-input v-model="formData.hzbjtcqzsflwx" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="安装独立式火灾报警探测器-联网型 (个)" prop="hzbjtcqzslwx">
						<u-input v-model="formData.hzbjtcqzslwx" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="安装智能救助设施-智能求助设施 (套)" prop="azznjzssznqzss">
						<u-input v-model="formData.azznjzssznqzss" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="安装智能救助设施-人脸识别警示装置 (套)" prop="azznjzssrlsbjszz">
						<u-input v-model="formData.azznjzssrlsbjszz" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="火灾报警探测器总数-非联网型 (个)" prop="azdlshzbjtcqflwx">
						<u-input v-model="formData.azdlshzbjtcqflwx" placeholder="请输入" :disabled="isview" />
					</u-form-item>
					<u-form-item label="火灾报警探测器总数-联网型 (个)" prop="azdlshzbjtcqlwx">
						<u-input v-model="formData.azdlshzbjtcqlwx" 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>
								拍照上传
							</view>
							<uni-load-more v-show="loading" status="loading" :content-text="contentText" />
						</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-street">
										<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 {
		createTsrqbf,
		getIdTsrqbf,
		updateTsrqbf
	}
	from '@/api/fire_inspetItem/index';
	import {
		takePhotos,
		NumericRule
	} from '@/utils/common.js';
	import config from '@/config'
	const baseUrlImg = config.baseUrlImg
	export default {
		data() {
			return {
				contentText: {
					contentrefresh: '上传照片中',
				},
				screenHeight: this.$screenHeight,
				wheight: '',
				photograph: `${baseUrlImg}/checkActive/photograph.png`,
				dateTime: '',
				formData: {
					djlr: '',
					qttsknqz: '',
					smbf: '',
					bgyhdy: '',
					bgyhhy: '',
					bgyhqy: '',
					bgyhqlkrwjqtyh: '',
					hzbjtcqzsflwx: '',
					hzbjtcqzslwx: '',
					azznjzssznqzss: '',
					azznjzssrlsbjszz: '',
					azdlshzbjtcqflwx: '',
					azdlshzbjtcqlwx: '',
					dxshgcmyjznccsypzyhsyq: '',
					reporttime: '',
					streetRepPicsDOList: []
				},
				rules: {
					djlr: NumericRule(),
					qttsknqz: NumericRule(),
					smbf: NumericRule(),
					bgyhdy: NumericRule(),
					bgyhhy: NumericRule(),
					bgyhqy: NumericRule(),
					bgyhqlkrwjqtyh: NumericRule(),
					hzbjtcqzsflwx: NumericRule(),
					hzbjtcqzslwx: NumericRule(),
					azznjzssznqzss: NumericRule(),
					azznjzssrlsbjszz: NumericRule(),
					azdlshzbjtcqflwx: NumericRule(),
					azdlshzbjtcqlwx: NumericRule(),
					dxshgcmyjznccsypzyhsyq: NumericRule(),
					reporttime: [{
						required: true,
						message: '请输入日期',
						trigger: ['change', 'blur'],
					}],
				},
				isedit: false, //编辑
				isview: false, //查看
				loading: false
			}
		},
		onReady() {
			this.$refs.customForm.setRules(this.rules);
		},
		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)
			}

		},
		mounted() {
			uni.createSelectorQuery().in(this).select('.heigthButton').boundingClientRect(data => {
				this.wheight = data.height
			}).exec()
		},
		methods: {
			getIdData() {
				getIdTsrqbf({
					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 = '默认名称';
						}
						if (this.isedit && !item.repid) {
							item.repid = this.parentId
						}
					});
					console.log(formList, 'this.formData');
					if (this.isedit) {
						updateTsrqbf(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 {
						createTsrqbf(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>