<template>
	<view class="pageWrapper">
		<CustomNavbar :title="navBarTitle" goBackUrl="/pages/home/home" @gobackDeal="gobackDeal">
		</CustomNavbar>

		<view class="body-wrapper">
			<view class="sumary-title">
				手动填写数据
			</view>
			<uni-row class="demo-uni-row mb-10">
				<uni-col :span="6" class="_title">
					<view>工程名称</view>
				</uni-col>
				<uni-col :span="16">
					<uni-data-select v-model="form.productName" :localdata="selectListLv1" :clear="false"
						@change="changeLv1"></uni-data-select>
				</uni-col>
			</uni-row>
			<uni-row class="demo-uni-row mb-10">
				<uni-col :span="6" class="_title">
					<view>爆区名称</view>
				</uni-col>
				<uni-col :span="16">
					<uni-data-select v-model="form.artilleryAreaName" :localdata="selectListLv2" :clear="false"
						@change="changeLv2"></uni-data-select>
				</uni-col>
			</uni-row>
			<uni-row class="demo-uni-row mb-10">
				<uni-col :span="6" class="_title">
					<view>炮孔排号</view>
				</uni-col>
				<uni-col :span="16">
					<uni-data-select v-model="form['cannonHoleArrayNum']" :localdata="selectListLv3" :clear="false"
						@change="changeLv3"></uni-data-select>
				</uni-col>
			</uni-row>
			<uni-row class="demo-uni-row mb-10">
				<uni-col :span="6" class="_title">
					<view>炮孔编号</view>
				</uni-col>
				<uni-col :span="16">
					<uni-data-select v-model="form['cannonHoleNum']" :localdata="selectListLv4" :clear="false"
						@change="changeLv4"></uni-data-select>
				</uni-col>
			</uni-row>

			<uni-row class="demo-uni-row mb-10">
				<uni-col :span="6" class="_title">
					<view>设计孔深</view>
				</uni-col>
				<uni-col :span="16">
					<view class="_content">
						{{form['expectedDepth'] || '-'}}
					</view>
				</uni-col>
			</uni-row>
			<uni-row class="demo-uni-row mb-10">
				<uni-col :span="6" class="_title">
					<view>装药选择</view>
				</uni-col>
				<uni-col :span="16">
					<uni-data-select v-model="form['status']" :localdata="zhuangyaoDict"
						:clear="false"></uni-data-select>
				</uni-col>
			</uni-row>
		</view>

		<view class="body-wrapper auto">
			<view class="sumary-title">
				自动更新数据
			</view>
			<uni-row class="demo-uni-row mb-10">
				<uni-col :span="6" class="_title">
					<view>测量孔深度</view>
				</uni-col>
				<uni-col :span="16">
					<view class="_content">
						{{form['actualDepth'] || '-'}}
					</view>
				</uni-col>
			</uni-row>
			<uni-row class="demo-uni-row mb-10 demo-uni-row-unborder">
				<uni-col :span="6" class="_title">
					<view>测量孔水位</view>
				</uni-col>
				<uni-col :span="16">
					<view class="_content angle">
						<view>{{form['actualWaterLevel'] || '-'}}</view>
					</view>
				</uni-col>
			</uni-row>
			<uni-row class="demo-uni-row mb-10 demo-uni-row-unborder">
				<uni-col :span="6" class="_title">
					<view>备注</view>
				</uni-col>
				<uni-col :span="16">
					<view class="_content unaffix">
						<view>{{form['remark'] || '-'}}</view>
					</view>
				</uni-col>
			</uni-row>
		</view>
		<view class="footer-wrapper" v-show="connected">
			<view class="funcBtn end" @tap="resetForm">
				取消
			</view>
			<view class="funcBtn end" @tap="saveData">
				保存
			</view>
		</view>

		<view class="" v-show="!connected" style="text-align: center; color: darkgray; margin-top:20rpx;">
			{{ pageLoadText }}
		</view>
		<!-- <button @click="showResultModal">点击模态框显示</button> -->
		<!-- <uni-popup ref="popup" background-color="#fff" @change="change">
			<view class="popup-content" >
				<view class="_header">
					孔实测结果
				</view>
				<view class="_body">
					<p>实测孔深： <span>{{form['actualDepth']|| '-'}}m</span></p>
					<p>实测倾角： <span>{{form['actualWaterLevel']|| '-'}}°</span></p>
				</view>
				<view class="funcBtn _btn" @click="saveTestResult">
					保存测量结果
				</view>
			</view>
		</uni-popup> -->
	</view>
</template>

<script>
	import CustomNavbar from '@/pages/component/CustomNavbar.vue';
	import {
		blueToothMixin
	} from '../../common/mixin.js'
	import {
		getCurrentTime,
		showCustomToast,
		showCustomModal
	} from '../../common/util.js'
	import {
		getSelectList,
		getDetailInfo,
		saveData,
		getDict
	} from '../../api/blueToothMeasure.js'
	export default {
		mixins: [blueToothMixin],
		components: {
			CustomNavbar,
		},
		data() {
			return {
				navBarTitle: '炮孔测量',
				redirectUrl: '/pages/home/blueSearch?refer=blueMeasure',
				// 表单数据相关
				zhuangyaoDict: [],
				form: {
					'productName': '',
					'artilleryAreaName': '',
					'cannonHoleArrayNum': '',
					'cannonHoleNum': '',
					'status': '',
					'expectedDepth': '',
					'actualDepth': '',
					'actualWaterLevel': ''
				},
				dataList: [],
				selectListLv1: [],
				selectListLv2: [],
				selectListLv3: [],
				selectListLv4: [],
				selectListLv5: [],
			};
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			uni.hideLoading()
			// this.dataList = uni.getStorageSync('importDataList') || []
			// console.log('this.dataList',JSON.stringify(this.dataList));
			this.getDictInfo()
			this.getSelectListLv1()

			// this.initLink()
		},
		methods: {
			async getDictInfo() {
				let res = await getDict({
					type: 'zhuangyao_type'
				})
				if(res){
					this.zhuangyaoDict = res.map((item) => {
						return {
							text: item.dictLabel,
							value: item.dictValue
						}
					})
				}
			},
			changeLvCommonReset(lvNum) { // 下拉列表值改变，清空级联数据公共方法
				let newFildArr = []
				let newDataListSelectArr = []

				let formFieldArr = [
					'productName', 'artilleryAreaName', 'cannonHoleArrayNum', 'cannonHoleNum',
					'status', 'expectedDepth',
					'actualDepth', 'actualWaterLevel'
				]


				let dataListSelectArr = [
					'selectListLv1', 'selectListLv2', 'selectListLv3',
					'selectListLv4', 'selectListLv5'
				]

				// [1, 2, 3, 4, 5];  lvNum:2  => [3, 4, 5]
				newFildArr = formFieldArr.slice(lvNum);
				newFildArr.forEach((key) => {
					this['form'][key] = ''
				});
				newDataListSelectArr = dataListSelectArr.slice(lvNum);
				newDataListSelectArr.forEach((key) => {
					this[key] = []
				});

			},
			async getSelectListLv1() {
				// const dataArr = JSON.parse(JSON.stringify(this.dataList))
				// // 获取第一个数据列表
				// this.selectListLv1 = getCascadeList(dataArr, {
				// 	productName: null
				// }).map((item) => {
				// 	return {
				// 		text: item.productName,
				// 		value: item.productName
				// 	}
				// })
				const res = await getSelectList({
					productName: '',
					artilleryAreaName: '',
					cannonHoleArrayNum: '',
					cannonHoleNum: ''
				})
				console.log('响应res', res)
				this.selectListLv1 = res.map((item) => {
					return {
						text: item,
						value: item
					}
				})
			},
			changeLv1(e) {
				// console.log("e:", e);
				this.changeLvCommonReset(1)

				this.getSelectListLv2(e)

			},

			async getSelectListLv2(prevLvVal) {
				// const dataArr = JSON.parse(JSON.stringify(this.dataList))
				// this.selectListLv2 = getCascadeList(dataArr, {
				// 	productName: prevLvVal,
				// }).map((item) => {
				// 	return {
				// 		text: item.artilleryAreaName,
				// 		value: item.artilleryAreaName
				// 	}
				// })
				// //console.log('this.selectListLv2', this.selectListLv2)
				const res = await getSelectList({
					productName: prevLvVal,
					artilleryAreaName: '',
					cannonHoleArrayNum: '',
					cannonHoleNum: ''
				})
				console.log('响应res', res)
				this.selectListLv2 = res.map((item) => {
					return {
						text: item,
						value: item
					}
				})
			},
			changeLv2(e) {
				//console.log("e:", e);

				this.changeLvCommonReset(2)
				this.getSelectListLv3(e)
			},

			async getSelectListLv3(prevLvVal) {
				// const dataArr = JSON.parse(JSON.stringify(this.dataList))
				// this.selectListLv3 = getCascadeList(dataArr, {
				// 	productName: this.form.productName,
				// 	artilleryAreaName: prevLvVal
				// }).map((item) => {
				// 	return {
				// 		text: item.cannonHoleArrayNum,
				// 		value: item.cannonHoleArrayNum
				// 	}
				// })
				const res = await getSelectList({
					productName: this.form.productName,
					artilleryAreaName: prevLvVal,
					cannonHoleArrayNum: '',
					cannonHoleNum: ''
				})
				console.log('响应res', res)
				this.selectListLv3 = res.map((item) => {
					return {
						text: item,
						value: item
					}
				})
			},
			changeLv3(e) {
				//console.log("e:", e);
				this.changeLvCommonReset(3)
				this.getSelectListLv4(e)
			},

			async getSelectListLv4(prevLvVal) {
				// const dataArr = JSON.parse(JSON.stringify(this.dataList))
				// this.selectListLv4 = getCascadeList(dataArr, {
				// 	productName: this.form.productName,
				// 	artilleryAreaName: this.form.artilleryAreaName,
				// 	cannonHoleArrayNum: prevLvVal
				// }).map((item) => {
				// 	return {
				// 		text: item.cannonHoleNum,
				// 		value: item.cannonHoleNum
				// 	}
				// })
				const res = await getSelectList({
					productName: this.form.productName,
					artilleryAreaName: this.form.artilleryAreaName,
					cannonHoleArrayNum: prevLvVal,
					cannonHoleNum: ''
				})
				console.log('响应res', res)
				this.selectListLv4 = res.map((item) => {
					return {
						text: item,
						value: item
					}
				})
			},
			changeLv4(e) {
				// this.changeLvCommonReset(4)
				// //console.log("e:", e);
				// this.getSelectListLv5(e)
				this.getDetailInfo(e)
			},

			// getSelectListLv5(prevLvVal) {
			// 	const dataArr = JSON.parse(JSON.stringify(this.dataList))
			// 	this.selectListLv5 = getCascadeList(dataArr, {
			// 		productName: this.form.productName,
			// 		artilleryAreaName: this.form.artilleryAreaName,
			// 		cannonHoleArrayNum: this.form.cannonHoleArrayNum,
			// 		cannonHoleNum: prevLvVal,
			// 	}).map((item) => {
			// 		return {
			// 			text: item.boreholeNumber,
			// 			value: item.boreholeNumber
			// 		}
			// 	})
			// },
			// changeLv5(e) {
			// 	//console.log("e:", e);
			// 	this.getDetailInfo(e)
			// },
			async getDetailInfo() {
				let res = await getDetailInfo()
				if (res) {
					this.form['status'] = res['status']
					this.form['expectedDepth'] = res['expectedDepth']
					this.form['designInclination'] = res['designInclination']
					return
				}
				showCustomToast({
					title: '暂无详情信息'
				})
			},
			async saveData() { // 保存当前表单数据到本地
				let formFieldArr = [
					'productName', 'artilleryAreaName', 'cannonHoleArrayNum',
					'cannonHoleNum', 'boreholeNumber'
				]
				var validFlag = true
				for (var i = 0; i < formFieldArr.length; i++) {
					console.log('this.form[formFieldArr[i]]', i, this.form[formFieldArr[i]])
					if (!this.form[formFieldArr[i]]) {
						validFlag = false
						break;
					}
				}
				if (!validFlag) {
					showCustomModal({
						content: `手动填写数据不可为空`
					})
					return
				}
				// const index = this.dataList.findIndex((item) => {
				// 	return item.productName === this.form.productName &&
				// 		item.artilleryAreaName === this.form.artilleryAreaName &&
				// 		item.cannonHoleArrayNum === this.form.cannonHoleArrayNum &&
				// 		item.cannonHoleNum === this.form.cannonHoleNum &&
				// 		item.boreholeNumber === this.form.boreholeNumber
				// })
				if (this.form.measureDepth && this.form.shuiwei) {

					let res = await saveData({
						...this.form
					})
					// this.$set(this.dataList[index], 'actualWaterLevel', this.form.actualWaterLevel);
					// this.$set(this.dataList[index], 'actualDepth', this.form.actualDepth);
					// this.$set(this.dataList[index], 'remark', getCurrentTime());
					// uni.setStorageSync('importDataList', JSON.parse(JSON.stringify(this.dataList)))
					if (res) {
						uni.showToast({
							title: '保存成功',
							icon: false
						})
						this.resetForm()
					}
					return
				}
				showCustomModal({
					content: '孔深水位不能为空',
				})
			},
		}
	};

	/**
	 * 处理级联数据方法
	 */
	function getCascadeList(originArr = [], paramObj) {
		let sonArr = new Array()
		// let sonArr = new Set()


		if (paramObj.artilleryAreaName && paramObj.productName && paramObj.cannonHoleArrayNum && paramObj
			.cannonHoleNum) { // 返回第5级
			const tempArr = originArr.filter((item) => {
				return item.productName === paramObj.productName &&
					item.artilleryAreaName === paramObj.artilleryAreaName &&
					item.cannonHoleArrayNum === paramObj.cannonHoleArrayNum &&
					item.cannonHoleNum === paramObj.cannonHoleNum
			})
			const uniqueArray = Array.from(new Map(tempArr.map(item => [item.boreholeNumber, item])).values());
			return uniqueArray
		}

		if (paramObj.artilleryAreaName && paramObj.productName && paramObj.cannonHoleArrayNum) { // 返回第4级
			const tempArr = originArr.filter((item) => {
				return item.productName === paramObj.productName &&
					item.artilleryAreaName === paramObj.artilleryAreaName &&
					item.cannonHoleArrayNum === paramObj.cannonHoleArrayNum
			})
			const uniqueArray = Array.from(new Map(tempArr.map(item => [item.cannonHoleNum, item])).values());
			return uniqueArray
		}

		if (paramObj.artilleryAreaName && paramObj.productName) { // 返回第三级
			const tempArr = originArr.filter((item) => {
				return item.productName === paramObj.productName &&
					item.artilleryAreaName === paramObj.artilleryAreaName
			})
			const uniqueArray = Array.from(new Map(tempArr.map(item => [item.cannonHoleArrayNum, item])).values());
			return uniqueArray
		}


		if (paramObj.productName) { // 返回2级列表
			const tempArr = originArr.filter((item) => { // 二级列表基于1级选择后的值进行过滤
				return item.productName === paramObj.productName
			})
			const uniqueArray = Array.from(new Map(tempArr.map(item => [item.artilleryAreaName, item])).values());
			return uniqueArray
		}


		if (!paramObj.productName) { // 返回1级列表
			const uniqueArray = Array.from(new Map(originArr.map(item => [item.productName, item])).values());
			return uniqueArray
		}
	}
</script>

<style lang="scss">
	::v-deep .uni-row::after {
		clear: both;
		width: 608rpx;
		border-bottom: 1rpx solid #E3E6EC;
		transform: translateX(28rpx);
	}


	.pageWrapper {
		width: 100vw;
		height: calc(100vh-44rpx);
		padding-top: 44rpx;
		background: linear-gradient(180deg, #007AFF 0%, #419AFF 16%, #EFF1F4 43%);
	}

	.body-wrapper {
		margin: 0 auto;
		width: 690rpx;
		padding-top: 20rpx;
		padding-left: 15rpx;
		padding-right: 15rpx;
		background: #FFFFFF;
		border-radius: 10rpx;


		&.auto {
			margin-top: 30rpx;
			background-color: #D1E5FB;

			.demo-uni-row {
				padding-top: 10rpx;
				padding-bottom: 10rpx;
				// border-bottom: 1px solid #F1F8FF;
			}

			::v-deep .uni-row::after {
				border-bottom-color: #FFFFFF;
			}

			.demo-uni-row-unborder::after {
				// 特殊没有底部边线 处理
				border-bottom-color: transparent;
			}
		}

		.sumary-title {
			position: relative;
			padding-left: 50rpx;
			font-size: 32rpx;
			color: #007AFF;
			margin-top: 20rpx;
			margin-bottom: 20rpx;

			&:before {
				content: '';
				width: 16rpx;
				height: 16rpx;
				background: #007AFF;
				border-radius: 50%;
				position: absolute;
				left: 20rpx;
				top: 50%;
				transform: translateY(-50%);
			}
		}
	}

	.logo-wrapper {
		display: flex;
		justify-content: center;

		image {
			width: 366rpx;
			height: 432rpx;
			margin: 0 auto;
		}
	}

	.mb-10 {
		margin-bottom: 10rpx;
	}

	._title {
		// border: 1px solid lightgray;
		height: 70rpx;
		text-align: center;
		line-height: 70rpx;
		margin-right: 10rpx;
		font-size: 29rpx;
		font-weight: bold;
		color: #000000;
	}

	._content {
		// border: 1px solid lightgray;
		height: 70rpx;
		line-height: 70rpx;
		padding-left: 20rpx;
		position: relative;

		&:after {
			content: 'm';
			width: 70rpx;
			height: 70rpx;
			// border-left: 1px solid lightgray;
			position: absolute;
			right: -12rpx;
			top: 0;
			text-align: center;
			line-height: 70rpx;
			font-size: 29rpx;
			color: #000000;
		}
	}

	.funcBtn {
		width: 280rpx;
		height: 70rpx;
		line-height: 70rpx;
		background: #007AFF;
		border-radius: 35rpx;
		text-align: center;
		font-size: 29rpx;
		color: #FFFFFF;
		border: 2px solid #007AFF;

		&.end {
			background: #FFFFFF;
			;
			color: #007AFF;
		}

		&.loading {
			opacity: 0.5;
		}
	}

	.footer-wrapper {
		display: flex;
		justify-content: space-around;
		margin-top: 50rpx;
	}


	.angle {
		&:after {
			content: '°';
		}
	}

	.unaffix {
		&:after {
			display: none;
		}
	}

	/**
 * 蓝牙连接互动相关
 */
	page {
		color: #333;
	}

	/*划线  */
	.line {
		background-color: blue;
		height: 5px;
		width: 100%;
	}

	.div {
		background-color: white;
		height: 10px;
		width: 10px;
	}

	.blank {
		height: 150rpx;
	}
</style>