<template>
	<view class="appWrapper padding-top" style="height:calc(100vh - 250rpx);overflow:scroll">

		<form action="" class="funcAdd">
			<view class="form-item selectBox">
				<view class="title"><text class="necessary">*</text>报备类型:</view>
				<select name="" id="" placeholder=""clearable  v-model="repType">
					<option value="">请选择</option>
					<option value="1">单位</option>
					<option value="2">类型</option>
					<option value="3">设备</option>
				</select>
				<text class="icon iconfont margin-right-sm margin-left">&#xe63d;</text>
			</view>
			<view class="form-item selectBox" v-if="repType==2||repType==3">
				<view class="title"><text class="necessary">*</text>设备类型:</view>
				<select name="" id="" placeholder=""clearable  v-model="deviceType">
					<option value="">请选择</option>
					<option value="1">火系统</option>
					<option value="2">水系统</option>
					<option value="3">烟感系统</option>
					<option value="4">消防栓</option>
					<option value="5">液位</option>
					<option value="6">RTU</option>
					<option value="7">电气火灾</option>
					<option value="16">视频监控</option>
					<option value="128">井盖</option>
					<option value="131">可燃气体</option>
					<option value="130">门禁监测</option>
					<option value="129">地磁监测</option>
					<option value="17">电梯监测</option>
				</select>
				<text class="icon iconfont margin-right-sm margin-left">&#xe63d;</text>
			</view>
			
			<view class="form-item" v-if="repType">
				<view class="title">
					<text class="necessary">*</text>
					开始时间:
				</view>
				<view class="example-body">
					<uni-datetime-picker v-model="startTime" :start="now"/>
				</view>
			</view>
			<view class="form-item" v-if="repType">
				<view class="title">
					<text class="necessary">*</text>
					结束时间:
				</view>
				<view class="example-body">
					<uni-datetime-picker v-model="endTime" :start="startTime" />
				</view>
			</view>
			
			

			
			<view class="form-item " v-if="repType==3&&deviceType==6">
				<view class="title"><text class="necessary">*</text>端口号:</view>
				<input name="input" v-model="portId"></input>
			</view>
			
			<view class="form-item selectBox" v-if="repType==3&&deviceType==6">
				<view class="title"><text class="necessary">*</text>端口类型:</view>
				<select name="" id="" placeholder=""clearable  v-model="portType">
					<option value="">请选择</option>
					<option value="1">数字量</option>
					<option value="2">模拟量</option>
				</select>
				<text class="icon iconfont margin-right-sm margin-left">&#xe63d;</text>
			</view>
			
			<view class="form-item selectBox" v-if="repType==3&&deviceType">
				<view class="title"><text class="necessary">*</text>报备设备:</view>
				<select name="" id="" placeholder=""clearable  v-model="deviceNo">
					<option value="">请选择</option>
					<option value="1">编号1</option>
					<option value="2">编号2</option>
				</select>
				<text class="icon iconfont margin-right-sm margin-left">&#xe63d;</text>
			</view>
			
			<view class="form-item " v-if="repType">
				<view class="title"><text class="necessary"></text>备注:</view>
				<input name="input" v-model="remark"></input>
			</view>
			
			<view class="btn-area submitBottomBtn padding-lr-sm">
				<button class="bg-blue round  margin-top" @tap="submit()">提 交 </button>
			</view>
		</form>


	</view>
</template>
<style scoped>
	/deep/ .funcAdd .uni-date-x--border, .uni-date-x{
		border-radius:0!important
	}
	.funcAdd .example-body{
		margin:0;
		width:calc(100% - 200rpx)
	}
</style>

<script>

	export default {

		data() {
			return {
				id:'',

				repType:'',
				deviceType:'',
				startTime:'',
				endTime:'',
				remark:'',
				portId:'',
				portType:'',
				deviceNo:'',
				now:''
			
			}
		},
		onLoad: function(option) {
			
			this.id=option.id;
			this.getSiteList();
			
			uni.setNavigationBarTitle({
				title: option.id?"编辑":"新增",
			});
			
			var nowTemp = new Date();
			this.now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);



		},
		watch: {
			startTime:function(){
				alert(this.startTime)
			}
			
		},
		methods: {

			async submit() {
				
				//提交验证
				
				if (!this.repType) {
					uni.showToast({
						title: "请输入报备类型",
						icon: "none"
					});
					return
				}
				if(this.repType==1){
					if (!this.startTime.replace(/^\s*/g,'')) {
						uni.showToast({
							title: "请选择开始时间",
							icon: "none"
						});
						return
					}
					if (!this.endTime.replace(/^\s*/g,'')) {
						uni.showToast({
							title: "请选择结束时间",
							icon: "none"
						});
						return
					}
				}
				if(this.repType==2||this.repType==3){
					if (!this.deviceType) {
						uni.showToast({
							title: "请选择设备类型",
							icon: "none"
						});
						return
					}
					if (!this.startTime.replace(/^\s*/g,'')) {
						uni.showToast({
							title: "请输入开始时间",
							icon: "none"
						});
						return
					}
					if (!this.endTime.replace(/^\s*/g,'')) {
						uni.showToast({
							title: "请输入结束时间",
							icon: "none"
						});
						return
					}
				}
				
				if(this.repType==3&&this.deviceType){
					
					if (this.deviceType==6) {
						if (!this.portId.replace(/^\s*/g,'')) {
							uni.showToast({
								title: "请输入端口号",
								icon: "none"
							});
							return
						}
						if (!this.portType) {
							uni.showToast({
								title: "请选择端口类型",
								icon: "none"
							});
							return
						}
						if (!this.deviceNo) {
							uni.showToast({
								title: "请选择报备设备",
								icon: "none"
							});
							return
						}
					}
					if (!this.deviceNo) {
						uni.showToast({
							title: "请选择报备设备",
							icon: "none"
						});
						return
					}
				}
				

				let queryParam = {};
				if(this.id){
					queryParam.id=this.id
				}
				queryParam.repType = this.repType;
				queryParam.companyCode='10012';
				queryParam.remark = this.remark;
				queryParam.startTime = this.startTime;
				queryParam.endTime = this.endTime;
				
				if (this.repType==1) {
					this.addDevice(queryParam)
					return;
				}
				if (this.repType==2) {
					queryParam.deviceType = this.deviceType;
					this.addDevice(queryParam)
					return;
				}
				if (this.repType==3) {
					queryParam.deviceType = this.deviceType;
					if(this.deviceType==6){
						queryParam.portType = this.portType;
						queryParam.portId = this.portId;
					}
					queryParam.deviceNo = this.deviceNo;
					this.addDevice(queryParam)
					return;
				}
				
				this.addDevice(queryParam)
			
			},
			
			async addDevice(params = {}) {
				const res = await this.$myRequest({
					url: 'DeviceConfig/setDevice',
					data: params
				})
				if (res.data.flag) {
					// uni.showToast({
					// 	title: "提交成功",
					// });
					// setTimeout(() => {
					// 	uni.navigateTo({
					// 		url: '/pages/deviceManage/deviceManage',
					// 	});
					// }, 1000);
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/accountManage/success/success',
						});
					}, 1000);
				}
			},
			
			

			radioChange(e) {
				console.log('type:' + e.detail.value);
				this.radioOne = e.detail.value;
			},

			//单位下拉请求数据
			async getSiteList() {
				const res = await this.$myRequest({
					url: 'Index/getCompanyList',
				})
				this.companyListData = res.data.data;
			},






		}
	}
</script>




<style lang="scss">
	input,
	select,
	option {
		line-height: 70rpx;
		padding: 0 20rpx;
		height: 70rpx;
		border: 1px solid #EDEDED;
		// width:500rpx!important;
		background-color: #fff;
		box-sizing: border-box;
		appearance: none;
		-moz-appearance: none;
		-webkit-appearance: none;
		color: #999;
	}




	/* 站点多选下拉样式end */
</style>
