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

		<form action="">
			<view class="form-item ">
				<view class="title">
					<text class="necessary">*</text>
					设备编号:
				</view>
				<input name="input" v-model="formMess.device_code"></input>
			</view>
			<view class="form-item">
				<view class="title"><text class="necessary">*</text>设备名称:</view>
				<input name="input" v-model="formMess.device_name"></input>
			</view>
			<view class="form-item">
				<view class="title"><text class="necessary">*</text>单元地址:</view>
				<input name="input" v-model="formMess.unitinfo"></input>
			</view>
			<view class="form-item">
				<view class="title"><text class="necessary">*</text>所属楼层:</view>
				<select name="" id="" v-model="formMess.louyu" filterable clearable>
					<option value="">请选择</option>
					<option value="-10">楼层B10</option>
					<option value="-9">楼层B9</option>
					<option value="-8">楼层B8</option>
					<option value="-7">楼层B7</option>
					<option value="-6">楼层B6</option>
					<option value="-5">楼层B5</option>
					<option value="-4">楼层B4</option>
					<option value="-3">楼层B3</option>
					<option value="-2">楼层B2</option>
					<option value="-1">楼层B1</option>
					<option :value=item v-for="item in 60">楼层{{item}}</option>
				</select>
			</view>
			<view class="form-item">
				<view class="title"><text class="necessary">*</text>物联网卡号:</view>
				<input name="input" v-model="formMess.sim"></input>
			</view>
			<view class="form-item">
				<view class="title"><text class="necessary">*</text>所属单位:</view>
				<select name="" id="" v-model="formMess.company_code">
					<option value="0">请选择</option>
					<option :value="item.owner_code" v-for="(item,index) in companyListData" :key="index"
						style="width:80%">{{item.owner_name}}</option>
				</select>
			</view>
			<view class="form-item">
				<view class="title"><text class="necessary">*</text>设备类型:</view>
				<select name="" id="" placeholder="" v-model="formMess.type">
					<option value="0">请选择设备类型</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>
				</select>
			</view>
			<view class="form-item">
				<view class="title"><text class="necessary">*</text>传输方式:</view>

				<select name="" id="" placeholder="" v-model="formMess.transfer_type">
					<option value="0">请选择传输方式</option>
					<option  v-if="this.formMess.type!=1&&this.formMess.type!=7" value="NB">NB</option>
					<option value="4G">4G</option>
					<option v-if="this.formMess.type!=1&&this.formMess.type!=7" value="Lora">Lora</option>
				</select>
			</view>
			<view class="form-item margin-top">
				<view class="title">
					<text class="necessary">*</text>
					是否顶楼
				</view>
				<radio-group name="gender" @change="radioChange">
					<label class="margin-right">
						<radio value="1" /><text>是</text>
					</label>
					<label>
						<radio value="0" checked /><text>否</text>
					</label>
				</radio-group>
			</view>


			<!-- 水表NB必填参数 start -->
			<block v-if="this.formMess.transfer_type=='NB'&&this.formMess.type==2">
				<view class="form-item" >
					<view class="title"><text class="necessary">*</text>NB类型:</view>
					<select name="" id=""  v-model="formMess.deviceType">
<!-- 						<option value="">烟感</option>
 -->						<option value="MultiSensor">水表</option>
						<option value="WaterMeter">宇洁戎情水表</option>
						<option value="topsailSensor">拓扑索尔</option>
					</select>
				</view>
				<view class="form-item" >
					<view class="title"><text class="necessary">*</text>NB厂商名称:</view>
					<select name="" id="" v-model="formMess.manufacturerName">
						<option value="ST">ST</option>
						<option value="ZHYQ">ZHYQ</option>
						<option value="shanghaijunyanTest">shanghaijunyanTest</option>
						<option value="Topsail">Topsail</option>
					</select>
				</view>
				<view class="form-item" >
					<view class="title"><text class="necessary">*</text>NB设备型号:</view>
					<select name="" id="" v-model="formMess.model">
						<!-- <option value="">烟感型号</option> -->
						<option value="PT124B-2892-010">水表型号</option>
						<option value="WPTA">宇洁戎情水表型号</option>
						<option value="TH12">拓扑索尔水表液位</option>
					</select>
				</view>
				<view class="form-item" >
					<view class="title"><text class="necessary">*</text>NB厂商id:</view>
					<select name="" id="" v-model="formMess.manufacturerId">
						<option value="ST">ST</option>
						<option value="ZHYQ">ZHYQ</option>
						<option value="JunyanTest">JunyanTest</option>
						<option value="Topsail">Topsail</option>
					</select>
				</view>
				<view class="form-item" >
					<view class="title"><text class="necessary">*</text>NB地址:</view>
					<input name="input" v-model="formMess.location"></input>
				</view>
				<view class="form-item" >
					<view class="title"><text class="necessary">*</text>NB协议:</view>
					<input name="input" v-model="formMess.protocolType"></input>
				</view>
				
			</block>
			<!-- 水表NB必填参数 end -->
			
			<!-- 用传4G必填参数 -->
			<block v-if="this.formMess.type==1&&this.formMess.transfer_type=='4G'">
				<view class="form-item"  >
					<view class="title"><text class="necessary">*</text>解析方法:</view>
					<select name="" id="" v-model="formMess.owner_xh_a">
						<option value="z">请选择</option>
						<option :value=item v-for="item in 7">方法{{item}}</option>
					</select>
				</view>
				<view class="form-item">
					<view class="title"></view>
					<select name="" id="" v-model="formMess.owner_xh_b">
						<option value="z">请选择</option>
						<option :value=item v-for="item in 7">方法{{item}}</option>
					</select>
				</view>
				<view class="form-item">
					<view class="title"></view>
					<select name="" id="" v-model="formMess.owner_xh_c">
						<option value="z">请选择</option>
						<option :value=item v-for="item in 7">方法{{item}}</option>
					</select>
				</view>
				
			</block>
			
			
			<!-- 电气火灾参数 start -->
			<block v-if="this.formMess.type==7&&this.formMess.transfer_type=='4G'">
				<view class="form-item">
					<view class="title"><text class="necessary">*</text>最高层:</view>
					<select name="" id="" v-model="formMess.max_level" filterable clearable>
						<option value="">请选择</option>
						<option value="-10">楼层B10</option>
						<option value="-9">楼层B9</option>
						<option value="-8">楼层B8</option>
						<option value="-7">楼层B7</option>
						<option value="-6">楼层B6</option>
						<option value="-5">楼层B5</option>
						<option value="-4">楼层B4</option>
						<option value="-3">楼层B3</option>
						<option value="-2">楼层B2</option>
						<option value="-1">楼层B1</option>
						<option :value=item v-for="item in 60">楼层{{item}}</option>
					</select>
				</view>
				<view class="form-item">
					<view class="title"><text class="necessary">*</text>最底层:</view>
					<select name="" id="" v-model="formMess.min_level" filterable clearable>
						<option value="">请选择</option>
						<option value="-10">楼层B10</option>
						<option value="-9">楼层B9</option>
						<option value="-8">楼层B8</option>
						<option value="-7">楼层B7</option>
						<option value="-6">楼层B6</option>
						<option value="-5">楼层B5</option>
						<option value="-4">楼层B4</option>
						<option value="-3">楼层B3</option>
						<option value="-2">楼层B2</option>
						<option value="-1">楼层B1</option>
						<option :value=item v-for="item in 60">楼层{{item}}</option>
					</select>
				</view>
				<view class="form-item ">
					<view class="title">
						<text class="necessary">*</text>
						倍率:
					</view>
					<input name="input" v-model="formMess.owner_bl"></input>
				</view>
			</block>
			<!-- 电气火灾参数 end -->
			

			<view class="btn-area submitBottomBtn padding-lr-sm">
				<button class="bg-blue round  margin-top" @tap="submit()">提 交 </button>
			</view>
		</form>


	</view>
</template>

<script>
	// var graceChecker = require("../../common/graceChecker.js");
	// import add from '../../common/select.js';
	export default {

		data() {
			return {

				companyListData: [],
				index: -1,
	

				
				
				
				formMess: {
					"device_code":"",
					"device_name":"",
					"unitinfo":"",
					"louyu": "",
					"sim":"",
					company_code: 0,
					
					"type":0,
					transfer_type: 0,
					
					"owner_bl":"",//倍率
					"is_top":"0", //是否顶楼
					
					"deviceType":"",
					"manufacturerName":"",
					"model":"",
					"location":"",
					"protocolType":"",
					"manufacturerId":"",
					
					"min_level":"",
					"max_level":"",
					
					"radioOne":0,
					
					"owner_xh_a":'z',
					"owner_xh_b":'z',
					"owner_xh_c":'z'
					
	
				},

				


			}
		},
		onLoad: function(option) {
			this.getSiteList();



		},
		watch: {
			
		},
		methods: {

			async submit() {
				//提交验证
				
				// if (!this.formMess.device_code.replace(/^\s*/g,'')) {
				// 	uni.showToast({
				// 		title: "请输入设备编号",
				// 		icon: "none"
				// 	});
				// 	return
				// }
				// if (!this.formMess.device_name.replace(/^\s*/g,'')) {
				// 	uni.showToast({
				// 		title: "请输入设备名称",
				// 		icon: "none"
				// 	});
				// 	return
				// }
				// if (!this.formMess.unitinfo.replace(/^\s*/g,'')) {
				// 	uni.showToast({
				// 		title: "请输入单元地址",
				// 		icon: "none"
				// 	});
				// 	return
				// }
				// if (!this.formMess.louyu) {
				// 	uni.showToast({
				// 		title: "请选择所属楼层",
				// 		icon: "none"
				// 	});
				// 	return
				// }
				// if (!this.formMess.sim.replace(/^\s*/g,'')) {
				// 	uni.showToast({
				// 		title: "请输入物联网卡号",
				// 		icon: "none"
				// 	});
				// 	return
				// }
				// if (!this.formMess.company_code) {
				// 	uni.showToast({
				// 		title: "请选择所在单位",
				// 		icon: "none"
				// 	});
				// 	return
				// }
				// if (!this.formMess.type) {
				// 	uni.showToast({
				// 		title: "请选择设备类型",
				// 		icon: "none"
				// 	});
				// 	return
				// }
				// if (!this.formMess.transfer_type) {
				// 	uni.showToast({
				// 		title: "请选择传输方式",
				// 		icon: "none"
				// 	});
				// 	return
				// }
				
				if(this.formMess.type==1&&this.formMess.transfer_type=="4G"){
					if (this.formMess.owner_xh_a=='z') {
						uni.showToast({
							title: "请选择第一个解析方法",
							icon: "none"
						});
						return
					}
					if (this.formMess.owner_xh_b=='z') {
						uni.showToast({
							title: "请选择第二个解析方法",
							icon: "none"
						});
						return
					}
					if (this.formMess.owner_xh_c=='z') {
						uni.showToast({
							title: "请选择第三个解析方法",
							icon: "none"
						});
						return
					}
				}
				
				if(this.formMess.transfer_type=='NB'&&this.formMess.type==2){
					if (!this.formMess.deviceType) {
						uni.showToast({
							title: "请选择水表设备类型",
							icon: "none"
						});
						return
					}
					if (!this.formMess.manufacturerName) {
						uni.showToast({
							title: "请选择水表NB厂商名称",
							icon: "none"
						});
						return
					}
					if (!this.formMess.model) {
						uni.showToast({
							title: "请选择水表NB设备型号",
							icon: "none"
						});
						return
					}
					if (!this.formMess.manufacturerId) {
						uni.showToast({
							title: "请选择水表NB厂商ID",
							icon: "none"
						});
						return
					}
					if (!this.formMess.location.replace(/^\s*/g,'')) {
						uni.showToast({
							title: "请输入水表NB地址",
							icon: "none"
						});
						return
					}
					if (!this.formMess.protocolType.replace(/^\s*/g,'')) {
						uni.showToast({
							title: "请输入水表NB协议",
							icon: "none"
						});
						return
					}
				}
				
				
				if(this.formMess.type==7&&this.formMess.transfer_type=='4G'){
					if (!this.formMess.max_level) {
						uni.showToast({
							title: "请选择最高层",
							icon: "none"
						});
						return
					}
					if (!this.formMess.min_level) {
						uni.showToast({
							title: "请选择最低层",
							icon: "none"
						});
						return
					}
					
				
					if (!this.formMess.owner_bl.replace(/^\s*/g,'')) {
						uni.showToast({
							title: "请输入倍率",
							icon: "none"
						});
						return
					}
				}
	
				let queryParam = {};
				queryParam.device_code = this.formMess.device_code;
				queryParam.device_name = this.formMess.device_name;
				queryParam.unitinfo = this.formMess.unitinfo;
				queryParam.louyu = this.formMess.louyu;
				queryParam.sim = this.formMess.sim;
				
				queryParam.company_code = this.formMess.company_code;
				queryParam.type = this.formMess.type;
				queryParam.transfer_type = this.formMess.transfer_type;
				queryParam.is_top = this.formMess.radioOne;
				
				if (this.formMess.type==1&&this.formMess.transfer_type=="4G") {
					alert(1)
					queryParam.owner_xh_a = this.formMess.owner_xh_a;
					queryParam.owner_xh_b = this.formMess.owner_xh_b;
					queryParam.owner_xh_c = this.formMess.owner_xh_c;
					this.addDevice(queryParam)
					return;
				}
				if (this.formMess.transfer_type=='NB'&&this.formMess.type==2) {
					alert(2)
					queryParam.deviceType = this.formMess.deviceType;
					queryParam.manufacturerName = this.formMess.manufacturerName;
					queryParam.model = this.formMess.model;
					queryParam.location = this.formMess.location;
					queryParam.protocolType = this.formMess.protocolType;
					queryParam.manufacturerId = this.formMess.manufacturerId;
					this.addDevice(queryParam)
					return;
				}
				if (this.formMess.type==7&&this.formMess.transfer_type=='4G') {
					alert(3)
					queryParam.max_level = this.formMess.max_level;
					queryParam.min_level = this.formMess.min_level;
					queryParam.owner_bl = this.formMess.owner_bl;
					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);
				}
			},
			
			

			radioChange(e) {
				console.log('type:' + e.detail.value);
				this.formMess.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>
