<template>
	<view class="page">
		<u-navbar :title="title" :border-bottom="false" :background="bgc" title-color='#000' title-size='36'
			:title-bold='true' height='45' id="navbar">
		</u-navbar>
		<view class="tit">
			<image src="https://api.ccttiot.com/smartmeter/img/static/ufvwKG3vcJYtbkf0Oeij" mode=""></image>
			除螨设置
		</view>
		<view class="tit1">
			<image style="width: 42rpx;height: 42rpx;" src="https://api.ccttiot.com/smartmeter/img/static/utZh1saVjZgOBz3orDkL" mode=""></image>
			间隔时长
		</view>
		<view class="set_li">
			每隔<view class="ipt_box"><input type="text" class="ips" v-model="miteInterval" placeholder=" "
					placeholder-class="my-placeholder" /> </view>天/次 &nbsp;&nbsp; 除螨工作
		</view>
		<view class="tit" style="margin-top: 58rpx;">
			<image style="width: 52rpx;height: 52rpx;"
				src="https://api.ccttiot.com/smartmeter/img/static/ulPpf3J16ARy14XH4R7r" mode=""></image>
			保护设置
		</view>
		<view class="tit1">
			<image style="width: 40rpx;height: 41rpx;"
				src="https://api.ccttiot.com/smartmeter/img/static/ul8NIQoFMG44Nj0W1geF" mode=""></image>
			电量保护(%)
		</view>
		<view class="set_li">
			电量少于<view class="ipt_box"><input type="text" class="ips" v-model="minPower" placeholder=" "
					placeholder-class="my-placeholder" /> </view>加热板/风扇停止工作
		</view>
		<view class="tit1" style="display: flex;align-items: center;justify-content: space-between;">
			<view class="">
				<image src="https://api.ccttiot.com/smartmeter/img/static/unI3QR7NPZsPSn5ryMMH" mode=""></image>
				加热(℃)
			</view>
			<!-- <view class="" style="display: flex;align-items: center;justify-content: space-between;font-size: 32rpx;color: #3D3D3D;">
				<view class="bot">{{retxt}}</view>
				<view class="cont" @click="btnrekg">
					<view class="top" style="padding-top: 10rpx;">
						<view class="rt">
							<image v-if="reimgflag" style="width: 160rpx;height: 80rpx;"
								src="https://api.ccttiot.com/smartmeter/img/static/uccaWar4FDp37op7haWM " mode="aspectFit">
							</image>
							<image style="width: 160rpx;height: 80rpx;" v-else
								src="https://api.ccttiot.com/smartmeter/img/static/ur9HRtZKY7YcxGewJARz" mode="aspectFit">
							</image>
						</view>
					</view>
				</view>
			</view> -->
		</view>
		<!-- <view style="color: red;font-size: 26rpx;margin-left: 65rpx;">
			切记!开关关闭的话加热是不会工作的!!!
		</view> -->
		<view class="set_li">
			箱外温度低于<view class="ipt_box"><input type="text" class="ips" v-model="minHeat" placeholder=" "
					placeholder-class="my-placeholder" /> </view>加热板工作
		</view>
		<view class="set_li">
			箱内高于箱外<view class="ipt_box"><input type="text" class="ips" v-model="maxHeat" placeholder=" "
					placeholder-class="my-placeholder" /> </view>加热板停止工作
		</view>
		<view class="tit1" style="display: flex;align-items: center;justify-content: space-between;">
			<view class="">
				<image src="https://api.ccttiot.com/smartmeter/img/static/urouHoLnzjyyRn0MmNoL" mode=""></image>
				风扇(℃ 、ppm)
			</view>
			<!-- <view class="" style="display: flex;align-items: center;justify-content: space-between;font-size: 32rpx;color: #3D3D3D;">
				<view class="bot">{{fstxt}}</view>
				<view class="cont" @click="btnfskg">
					<view class="top" style="padding-top: 10rpx;">
						<view class="rt">
							<image v-if="fsimgflag" style="width: 160rpx;height: 80rpx;"
								src="https://api.ccttiot.com/smartmeter/img/static/uccaWar4FDp37op7haWM " mode="aspectFit">
							</image>
							<image style="width: 160rpx;height: 80rpx;" v-else
								src="https://api.ccttiot.com/smartmeter/img/static/ur9HRtZKY7YcxGewJARz" mode="aspectFit">
							</image>
						</view>
					</view>
				</view>
			</view> -->
		</view>
		<!-- <view style="color: red;font-size: 26rpx;margin-left: 65rpx;">
			切记!开关关闭的话风扇是不会工作的!!!
		</view> -->
		<view class="set_li">
			箱内温度高于<view class="ipt_box"><input type="text" class="ips" v-model="maxFan" placeholder=" "
					placeholder-class="my-placeholder" /> </view>风扇工作
		</view>
		<view class="set_li">
			箱内温度低于<view class="ipt_box"><input type="text" class="ips" v-model="minFan" placeholder=" "
					placeholder-class="my-placeholder" /> </view>风扇停止工作
		</view>
		<view class="set_li">
			CO₂浓度高于<view class="ipt_box"><input type="text" class="ips" v-model="maxCo2" placeholder=" "
					placeholder-class="my-placeholder" /> </view>风扇工作
		</view>
		<view class="set_li">
			CO₂浓度低于<view class="ipt_box"><input type="text" class="ips" v-model="minCo2" placeholder=" "
					placeholder-class="my-placeholder" /> </view>风扇停止工作
		</view>
		<view class="tit" style="margin-top: 58rpx;">
			<image src="https://api.ccttiot.com/smartmeter/img/static/udbzkOF4S1rNec8U44KT" mode=""></image>
			震动通知 
		</view>
		<view class="tit1">
			<image src="https://api.ccttiot.com/smartmeter/img/static/u3I0roygrn4BHJaWAT24" mode=""></image>
			单次震动
		</view>
		<view class="" style="padding-left: 50rpx;padding-top: 40rpx;box-sizing: border-box;">
			<u-checkbox-group>
				<u-checkbox active-color="#FFCC25" @change="checkboxChange(item.type)" v-model="item.checked"
					v-for="(item, index) in list" :key="index" :name="item.name">{{item.name}}</u-checkbox>
			</u-checkbox-group>
		</view>
		<view class="tit1">
			<image src="https://api.ccttiot.com/smartmeter/img/static/uG0iEjqzc5if82ocNeWo" mode=""></image>
			持续震动
		</view>
		<view class="" style="padding-left: 50rpx;padding-top: 40rpx;box-sizing: border-box;">
			<u-checkbox-group>
				<u-checkbox active-color="#FFCC25" @change="checkboxChanges(item.type)" v-model="item.checked"
					v-for="(item, index) in lists" :key="index" :name="item.name">{{item.name}}</u-checkbox>
			</u-checkbox-group>
		</view>
		<view class="btn_box">
			<view class="btns" @click="btncz">
				重置
			</view>
			<view class="btn1" @click="btnbc">
				保存
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: " #F4FAF8",
				},
				title: "设置",
				code: '',
				retxt:'关',
				reimgflag:false,
				fstxt:'关',
				fsimgflag:false,
				list: [{
						name: '短信',
						disabled: false,
						checked: false,
						type: 1
					},
					{
						name: '电话',
						disabled: false,
						checked: false,
						type: 2
					},
					{
						name: '微信',
						disabled: false,
						checked: false,
						type: 3
					}
				],
				lists: [{
						name: '短信',
						disabled: false,
						checked: false,
						type: 1
					},
					{
						name: '电话',
						disabled: false,
						checked: false,
						type: 2
					},
					{
						name: '微信',
						disabled: false,
						checked: false,
						type: 3
					}
				],
				value: [],
				values: [],
				miteInterval: '', //除螨间隔时间(天)
				minPower: '', //电量保护最低电量%
				minHeat: '', //加热板开始工作的最低箱外温度℃
				maxHeat: '', //加热板停止工作的箱内外温差℃
				maxFan: '', //风扇开始工作的最高箱内温度℃
				minFan: '', //风扇停止工作的最低箱内温度℃
				minCo2: '', //风扇停止工作的最低二氧化碳浓度℃
				maxCo2: '', //风扇开始工作的最高二氧化碳浓度℃
			}
		},
		onLoad() {
			this.getinfo()
		},
		onShow() {

		},
		methods: {
			// 是否打开加热开关
			btnrekg(){
				if(this.reimgflag == true){
					this.reimgflag = false
					this.retxt = '关'
				}else{
					this.reimgflag = true
					this.retxt = '开'
				}
			},
			// 是否打开风扇开关
			btnfskg(){
				if(this.fsimgflag == true){
					this.fsimgflag = false
					this.fstxt = '关'
				}else{
					this.fsimgflag = true
					this.fstxt = '开'
				}
			},
		    // 点击进行重置 数据恢复成初始化
			btncz() {
				this.$u.get(`/farm/userSetting/default`).then(res => {
					if (res.code == 200) {
						this.miteInterval = res.data.miteInterval
							this.minPower = res.data.minPower
							this.minHeat = res.data.minHeat
							this.maxHeat = res.data.maxHeat
							this.maxFan = res.data.maxFan
							this.minFan = res.data.minFan
							this.minCo2 = res.data.minCo2
							this.maxCo2 = res.data.maxCo2
							this.value = []
							this.values = []
						this.list.forEach(item => {
							if (item.checked) {
								item.checked = false
							}
						})
						this.lists.forEach(item => {
							if (item.checked) {
								item.checked = false
							}
						})
					}
				})
			},
			// 选择单次震动
			checkboxChange(e) {
				let index = this.value.indexOf(e)
				if (index > -1) {
					this.value.splice(index, 1)
				} else {
					this.value.push(e)
				}
				console.log(this.value)
			},
			// 选择持续震动
			checkboxChanges(e) {
				let index = this.values.indexOf(e)
				if (index > -1) {
					this.values.splice(index, 1)
				} else {
					this.values.push(e)
				}
			},
			// 获取设置数据值
			getinfo() {
				this.$u.get(`farm/userSetting/my`).then(res => {
					if (res.code == 200) {
						this.miteInterval = res.data.miteInterval,
							this.minPower = res.data.minPower,
							this.minHeat = res.data.minHeat,
							this.maxHeat = res.data.maxHeat,
							this.maxFan = res.data.maxFan,
							this.minFan = res.data.minFan,
							this.minCo2 = res.data.minCo2,
							this.maxCo2 = res.data.maxCo2
						this.list.forEach(item => {
							if (res.data.singleNotice.includes(item.type.toString())) {
								item.checked = true
							}
						})
						this.value = res.data.singleNotice
						this.lists.forEach(item => {
							if (res.data.multipleNotice.includes(item.type.toString())) {
								item.checked = true
							}
						})
						this.values = res.data.multipleNotice
					}
				})
			},
			// 保存设置数据值
			btnbc() {
				let data = {
					miteInterval: this.miteInterval,
					minPower: this.minPower,
					minHeat: this.minHeat,
					maxHeat: this.maxHeat,
					maxFan: this.maxFan,
					minFan: this.minFan,
					minCo2: this.minCo2,
					maxCo2: this.maxCo2,
					singleNotice: this.value,
					multipleNotice: this.values
				}
				this.$u.put(`farm/userSetting/user`, data).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '保存成功',
							icon: 'success',
							duration: 1000
						})
					} else if (res.code == 401) {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
			}

		}
	}
</script>

<style lang="scss">
	/deep/ .u-checkbox {
		margin-right: 80rpx;
	}

	page {
		background-color: #F4FAF8;
	}

	.page {
		// position: relative;
		width: 750rpx;
		padding: 40rpx;

		.btn_box {
			margin-top: 136rpx;
			display: flex;
			flex-wrap: nowrap;
			align-items: center;

			.btns {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 322rpx;
				height: 88rpx;
				background: #FFF5D6;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				border: 2rpx solid #FFC107;

				font-weight: 500;
				font-size: 32rpx;
				color: #FFC107;
			}

			.btn1 {
				display: flex;
				align-items: center;
				justify-content: center;
				margin-left: 30rpx;
				width: 322rpx;
				height: 88rpx;
				background: #FFCC25;
				border-radius: 20rpx 20rpx 20rpx 20rpx;

				font-weight: 500;
				font-size: 32rpx;
				color: #3D3D3D;
			}
		}

		.tit {
			display: flex;
			flex-wrap: nowrap;
			align-items: center;

			image {
				margin-right: 22rpx;
				width: 52rpx;
				height: 52rpx;

			}

			font-weight: 600;
			font-size: 36rpx;
			color: #3D3D3D;
		}

		.tit1 {
			margin-top: 30rpx;
			margin-left: 6rpx;
			display: flex;
			flex-wrap: nowrap;
			align-items: center;

			image {
				margin-right: 18rpx;
				width: 46rpx;
				height: 46rpx;

			}

			font-weight: 400;
			font-size: 32rpx;
			color: #3D3D3D;
		}

		.set_li {
			margin-top: 34rpx;
			margin-left: 65rpx;
			display: flex;
			flex-wrap: nowrap;
			align-items: center;
			font-weight: 400;
			font-size: 32rpx;
			color: #808080;

			.ipt_box {
				display: flex;
				align-items: center;
				justify-content: center;
				margin: 0 16rpx;
				// width: 98rpx;
				height: 88rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(0, 0, 0, 0.15);
				border-radius: 20rpx 20rpx 20rpx 20rpx;

				.ips {
					// margin-left: 15rpx;
					text-align: center;
					width: 100rpx;

				}



				.my-placeholder {
					font-weight: 400;
					font-size: 32rpx;
					color: #808080;
				}

			}


		}

	}
</style>