<template>
	<view class="fullHeight">
		<headers :showBack="true">
			<view class="headerName">
				{{pageTitle}}
			</view>
		</headers>
		<view class="formBox2" :class="isAdd?'':'editForm'">
			<form @submit="formSubmit">
				<view class="uni-form-item">
					<view class="uni-form-label">
						重复执行（不选默认只执行一次）
					</view>
					<view class="uni-form-input">
						<checkbox-group @change="checkboxChange" class="checkbox-group">
							<label class="uni-list-cell" v-for="(item,index) in colorList" :key="index">
								<view>
									<checkbox :value="item.value" :checked="item.checked" />
								</view>
								<view>{{item.name}}</view>
							</label>
						</checkbox-group>
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						<text class="star">*</text>执行时间
					</view>
					<view class="uni-form-input">
						<picker mode="time" :value="form.executeTime" @change="bindDateChange">
							<view class="uni-input uni-select cl" v-if="form.executeTime==''">
								请选择 <image class="icon-down" src="/static/icon-down-black.png" mode=""></image>
							</view>
							<view class="uni-input uni-select" v-else>{{form.executeTime}}
								<image class="icon-down" src="/static/icon-down-black.png" mode=""></image>
							</view>
						</picker>
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						<text class="star">*</text>开关
					</view>
					<view class="uni-form-input">
						 <radio-group @change="radioChange">
							<label class="uni-list-cell" v-for="(item, index) in switchArr" :key="item.value">
								<view>
									<radio :value="item.value" :checked="item.value == form.switchType" />
								</view>
								<view>{{item.name}}</view>
							</label>
						</radio-group>
					</view>
				</view>
				<view class="uni-form-item">
					<view class="uni-form-label">
						备注
					</view>
					<view class="uni-form-input">
						<input class="uni-input" placeholder-class="cl" name="remarks" :value="form.remarks" placeholder="请输入" />
					</view>
				</view>
				<view class="fixdBox">
					<button form-type="submit" type="primary" class="btn submitBtn big">保存</button>
				<button class="btn deleteBtn" v-show="!isAdd" @click="deleteFn">删除</button>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageTitle: '新增定时任务',
				selectIndex: 0,
				colorList: [{
						value: '1',
						name: '周一'
					},
					{
						value: '2',
						name: '周二'
					},
					{
						value: '3',
						name: '周三'
					},
					{
						value: '4',
						name: '周四'
					},
					{
						value: '5',
						name: '周五'
					},
					{
						value: '6',
						name: '周六'
					},{
						value: '7',
						name: '周日'
					}
				],
				switchArr:[
					{
						value: '2',
						name: '开'
					},{
						value: '1',
						name: '关'
					}
				],
				form: {
					remarks:'',
					executeTime:'',
					switchType:''
				},
				personList: [],
				projectDetail: {},
				isAdd: true,
				selectArr:[],
				sprayDevId:''
			}
		},
		onLoad(options) {			
			this.sprayDevId=options.sprayDevId
			if (options.id) {
				this.isAdd = false
				this.pageTitle='编辑定时任务'
				this.getDetails(options.id)
			}
			
		},
		
		methods: {
			radioChange: function(evt) {
				this.form.switchType=evt.detail.value
				// for (let i = 0; i < this.items.length; i++) {
				// 	if (this.items[i].value === evt.detail.value) {
				// 		this.current = i;
				// 		break;
				// 	}
				// }
			},
			checkboxChange: function (e) {
				// console.log(e)
				this.selectArr=e.detail.value
				// var items = this.colorList,
				// 	values = e.detail.value;
				// for (var i = 0, lenI = items.length; i < lenI; ++i) {
				// 	const item = items[i]
				// 	if(values.includes(item.value)){
				// 		this.$set(item,'checked',true)
				// 	}else{
				// 		this.$set(item,'checked',false)
				// 	}
				// }
			},
			deleteFn(){
				var that = this
				uni.showModal({
					title:'提示',
					content:'确定删除该定时任务吗？',
					success(res) {
						if(res.confirm){
							that.sendRequest({
								url: 'xmgl/sprayDevTimingTask/delete',
								data: {
									id: that.form.id
								},
								method: "POST",
								success(res) {
									uni.showToast({
										title:'删除成功！'
									})
									setTimeout(()=>{
										uni.navigateBack({})
									},1000)
									
								}
							})
						}
					}
				})
			},
			getDetails(id) {
				var that = this
				this.sendRequest({
					url: 'xmgl/sprayDevTimingTask/queryById',
					data: {
						id: id
					},
					method: "post",
					success(res) {
						that.form = res.result
						if(that.form.isRepeat=='1'){
							that.selectArr=[]
						}else{
							var a = that.form.repeatExecuteWeek;
							var arr=[]
							if(a.indexOf(',')!=-1){
								arr=a.split(',')
							}else{
								arr=[a]
							}
							// arr=arr.map(Number);
							that.selectArr=arr
							var items = that.colorList,
								values = arr;
							for (var i = 0, lenI = items.length; i < lenI; ++i) {
								const item = items[i]
								if(values.includes(item.value)){
									that.$set(item,'checked',true)
								}else{
									that.$set(item,'checked',false)
								}
							}
						}
					}
				})
			},
			bindDateChange: function(e) {
				this.form.executeTime = e.target.value
			},
			formSubmit(e) {
				var that = this
				console.log(e)
				var params = e.detail.value
				if (this.form.executeTime == '') {
					uni.showToast({
						title: '请选择执行时间',
						icon: 'none'
					})
					return false;
				}
				params.executeTime=this.form.executeTime
				if(this.selectArr.length>0){
					params.isRepeat=2
				}else{
					params.isRepeat=1
				}
				params.repeatExecuteWeek = this.selectArr.join(',');
				params.sprayDevId=this.sprayDevId
				params.switchType=this.form.switchType
				var url = 'xmgl/sprayDevTimingTask/edit'
				if (this.isAdd) {
					url = 'xmgl/sprayDevTimingTask/add'
				} else {
					params.id = this.form.id
				}
				this.sendRequest({
					url: url,
					data: params,
					method: "POST",
					success(res) {
						uni.showToast({
							title: '保存成功！'
						})
						uni.navigateBack({
							
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>

	.fixdBox{
		position: fixed;
		bottom: 30rpx;
		left: 30rpx;
		width: calc(100% - 60rpx);
		.submitBtn{
			margin-top: 0;
		}
	}
	.formBox2{
		padding-bottom: 80px;
		margin-top: 30rpx;
	}
	.editForm{
		padding-bottom: 130px;
	}
	// .checkbox-group{
	// 	margin-top: 20rpx;
	// }
	.uni-list-cell{
		display: inline-flex;
		align-items: center;
		margin-right: 40rpx;
		margin-top: 20rpx;
	}
</style>
