<template>
	<view >
		<cu-custom bgColor="bg-gradual-blueness" :isBack="true" >
			<block slot="backText">返回</block>
			<block slot="content">有限空间作业申请</block>
		</cu-custom>
		<scroll-view scroll-y="false" >
		<view class="form">
<!-- 			<view class="cu-form-textarea">
				<view class="cu-form-title text-hui">检查内容</view>
				<view class="f24 text-black">
				{{pd.CHECK_CONTENT}}
				</view>
			</view> -->
			<view class="de-card-list de-list">
				<!-- <view class="de-title">
					<text class="text-semi">隐患验收</text>
				</view> -->
				<view class="cu-form-group">
					<view class="title">申请审批编号</view>
					<input name="input" :disabled="forbidEdit" v-model="pd.APPROVENUMBER" placeholder="请输入申请审批编号"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">有限空间名称</view>
					<picker @change="limitspaceChange" :disabled="forbidEdit" :value="lindex" :range="limitSpaceList" range-key="NAME">
						<view class="picker">
							{{pd.LIMITSPACENAME?pd.LIMITSPACENAME:'请选择'}}
						</view>
					</picker>
				</view>
				<view class="cu-form-group">
					<view class="title">作业单位</view>
					<input name="input" :disabled="forbidEdit" v-model="pd.UNIT" placeholder="请输入作业单位"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">作业内容</view>
					<input name="input" :disabled="forbidEdit" v-model="pd.CONTONT" placeholder="请输入作业内容"></input>
				</view>
				<view class="cu-form-group ">
					<view class="title">作业开始时间</view>
					<ruiDatePicker v-if="!forbidEdit"
					:start="todayDate"
					fields="minute"
					:value="pd.STARTTIME?pd.STARTTIME:'请选择'"
					@change="changeStartDate"
					></ruiDatePicker>
					<view v-else>{{pd.STARTTIME}}</view>
					<!-- <picker mode="date" :start="todayDate" :disabled="forbidEdit" :value="pd.STARTTIME" @change="changeStartDate">
						<view class="picker">
							{{pd.STARTTIME?pd.STARTTIME:'请选择'}}
						</view>
					</picker> -->
				</view>
				<view class="cu-form-group bb">
					<view class="title">作业结束时间</view>
					<ruiDatePicker v-if="!forbidEdit"
					:start="todayDate"
					fields="minute"
					:value="pd.ENDTIME?pd.ENDTIME:'请选择'"
					@change="changeEndDate"
					></ruiDatePicker>
					<view v-else>{{pd.ENDTIME}}</view>
				<!-- 	<picker mode="date" :start="todayDate" :disabled="forbidEdit" :value="pd.ENDTIME" @change="changeEndDate">
						<view class="picker">
							{{pd.ENDTIME?pd.ENDTIME:'请选择'}}
						</view>
					</picker> -->
				</view>
				<view class="cu-form-textarea bb-default" style="border-top: 1upx solid #eee;" >
					<view class="cu-form-title" >可能存在风险及有害因素</view>
					<textarea maxlength="-1" :disabled="forbidEdit" v-model="pd.HARMFULFACTOR" placeholder="请输入可能存在风险及有害因素"></textarea>
				</view>
				<view class="cu-form-group ">
					<view class="title">作业负责人</view>
					<input name="input" disabled="true" v-model="pd.PRINCIPALNAME" placeholder="请输入作业负责人"></input>
				</view>
				<view class="cu-form-group ">
					<view class="title">监护人员</view>
					<picker @change="guardianChange" :disabled="forbidEdit" :value="gindex" :range="userList" range-key="USERNAME">
						<view class="picker">
							{{pd.GUARDIANNAME?pd.GUARDIANNAME:'请选择'}}
						</view>
					</picker>
				</view>
				<view class="cu-form-group ">
					<view class="title">作业人员</view>
					<input name="input" :disabled="forbidEdit" v-model="pd.WORKER" placeholder="请输入作业人员"></input>
				</view>
				<view class="cu-form-group ">
					<view class="title">其他人员</view>
					<input name="input" :disabled="forbidEdit" v-model="pd.OTHERPERSON" placeholder="请输入其他人员"></input>
				</view>
			</view>
			<view class="de-card-list de-list">
				<view class="de-title">
					<text class="text-semi">主要安全防护措施</text>
				</view>
				<view class="cu-form-group ">
					<view class="title text-hui">制定有限空间作业方案并经审核、批准</view>
					<view class="select">
						<view  class="flex align-center">
							<radio class='mr10' :disabled="forbidEdit" :checked="pd.ISFLOW==1" @click="isflowClick(pd.ISFLOW)"></radio>
						</view>
					</view>
				</view>
				<view class="cu-form-group " style="height: 150upx;">
					<view class="title text-hui" style="height: auto;line-height: 24px;">参加作业人员经有限空间作业安全相关培训合格</view>
					<view class="select">
						<view  class="flex align-center">
							<radio class='mr10' :disabled="forbidEdit" :checked="pd.ISTRAIN==1" @click="istrainClick(pd.ISTRAIN)"></radio>
						</view>
					</view>
				</view>
				<view class="cu-form-group " style="height: 150upx;">
					<view class="title text-hui" style="height: auto;line-height: 24px;">安全防护设备、个体防护用品、作业设备和工具齐全有效、满足要求</view>
					<view class="select">
						<view  class="flex align-center">
							<radio class='mr10' :disabled="forbidEdit" :checked="pd.ISEQUIPMENT==1" @click="isequipmentClick(pd.ISEQUIPMENT)"></radio>
						</view>
					</view>
				</view>
				<view class="cu-form-group ">
					<view class="title text-hui">应急救援装备满足要求</view>
					<view class="select">
						<view  class="flex align-center">
							<radio-group disabled="true">
								<radio class='mr10' :disabled="forbidEdit" :checked="pd.ISRESCUE==1" @click="isrescueClick(pd.ISRESCUE)"></radio>
							</radio-group>

						</view>
					</view>
				</view>
				<view class="cu-form-group ">
					<view class="title">现场作业负责人</view>
					<picker @change="sitesupervisorChange" :disabled="forbidEdit" :value="sindex" :range="userList" range-key="USERNAME">
						<view class="picker">
							{{pd.SITESUPERVISORNAME?pd.SITESUPERVISORNAME:'请选择'}}
						</view>
					</picker>
				</view>
				<view class="cu-form-group ">
					<view class="title">审批负责人</view>
					<picker @change="approverChange" :disabled="forbidEdit" :value="aindex" :range="userList" range-key="USERNAME">
						<view class="picker">
							{{pd.APPROVERNAME?pd.APPROVERNAME:'请选择'}}
						</view>
					</picker>
				</view>
				<view class="cu-form-group ">
					<view class="title">检测人员</view>
					<picker @change="inspectorChange" :disabled="forbidEdit" :value="iindex" :range="jcuserList" range-key="USERNAME">
						<view class="picker">
							{{pd.INSPECTORNAME?pd.INSPECTORNAME:'请选择'}}
						</view>
					</picker>
				</view>
			</view>
		</view>
		<view class="cu-bar btn-group" style="margin-top: 30upx;">
			<button v-if="!forbidEdit" class="cu-btn bg-blue margin-tb-sm lg" @click="$noMultipleClicks(goSubmit,'2')">提交</button>
			<button v-if="!forbidEdit && msg == 'add'" class="cu-btn bg-green margin-tb-sm lg" @click="$noMultipleClicks(goSubmit,'1')">取消</button>
			<button v-if="!forbidEdit && msg == 'edit'" class="cu-btn bg-green margin-tb-sm lg" @click="$noMultipleClicks(goback)">取消</button>
		</view>
		<view class="padding flex flex-direction">

		</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		basePath,corpinfoId,deptId,loginUser,formatDate,loginSession,baseImgPath
	} from '@/common/tool.js';
	import tkiTree from "@/components/select-tree/select-tree.vue"
	import gcoord from '@/common/gcoord.js'
	import ruiDatePicker from '@/components/rattenking-dtpicker/rattenking-dtpicker.vue';
	export default {
		components: {
			tkiTree,ruiDatePicker
		},
		data() {
			return {
				isUps:false,
				msg:'add',
				riskId:'',
				itemId:'',
				index:'',
				noClick:true,
				limitSpaceList:[],
				pd:{
					ISFLOW:0,
					ISTRAIN:0,
					ISEQUIPMENT:0,
					ISRESCUE:0,
				},// 数据
				aindex:-1,
				userList:[],//用户列表
				sindex:-1,
				lindex:-1,
				gindex:-1,
				todayDate:'',
				forbidEdit:true,// 禁止修改
				jcuserList:[],// 检测人员可选
				iindex:-1,

			}
		},
		onLoad(event){
			var _this = this;
			this.todayDate =  formatDate(new Date(), 'yyyy-MM-dd hh:mm');
			this.pd.LIMITSPACEAPPROVE_ID = event.LIMITSPACEAPPROVE_ID;
			if(this.pd.LIMITSPACEAPPROVE_ID){
				this.msg="edit";
				this.getData();
			}else {
				// 初始化作业负责人
				this.pd.PRINCIPAL = loginUser.USER_ID;
				this.pd.PRINCIPALNAME = loginUser.NAME;
				this.forbidEdit = false;

				this.jcuserList.push({"USER_ID":loginUser.USER_ID,"USERNAME":loginUser.NAME});
			}
			// 初始化现场作业负责人

			this.pd.SITESUPERVISOR = this.pd.PRINCIPAL;
			this.pd.SITESUPERVISORNAME = this.pd.PRINCIPALNAME;
			this.getDict();
			this.getUserList();
			this.getLimitSpace();
			loginSession();
		},
		methods: {
			getData() {
				var _this = this;
				uni.showLoading({
					title: '请稍候'
				})
				uni.request({
					url: basePath + '/app/limitspaceapprove/goEdit',
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						LIMITSPACEAPPROVE_ID: _this.pd.LIMITSPACEAPPROVE_ID,
					},
					success: (res) => {
						if ("success" == res.data.result) {
							uni.hideLoading();
							_this.pd = res.data.pd; //参数map
							if(_this.pd.STATUS < '2' || _this.pd.STATUS == '6'){
								_this.forbidEdit = false;
							}
							this.jcuserList.push({"USER_ID":res.data.pd.SITESUPERVISOR,"USERNAME":res.data.pd.SITESUPERVISORNAME});
							this.jcuserList.push({"USER_ID":res.data.pd.GUARDIAN,"USERNAME":res.data.pd.GUARDIANNAME});
						} else if ("exception" == data.result) {
							uni.showToast({
								title: '错误',
								duration: 2000
							});
						}
					}
				});
			},
			//获取数据字典数据
			getDict: function () {
				var _this = this;
				uni.request({
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type': 'application/x-www-form-urlencoded'
					},
					url: basePath + '/dictionaries/getLevels?tm=' + new Date().getTime(),
					data: {DICTIONARIES_ID: 'dad23a464729485ba364994942db83cc'},
					dataType: 'json',
					success: function (res) {
						_this.finiteSpaceList = res.data.list;
					}
				});
				uni.request({
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type': 'application/x-www-form-urlencoded'
					},
					url: basePath + '/dictionaries/getLevels?tm=' + new Date().getTime(),
					data: {DICTIONARIES_ID: 'a178357ce6ce4d6a9ac95def6aca3a14'},
					dataType: 'json',
					success: function (res) {
						_this.riskGradeList = res.data.list;
					}
				});
			},
			// 获取用户列表
			getUserList(){
				var _this = this;
				uni.request({
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type': 'application/x-www-form-urlencoded'
					},
					url: basePath + '/app/user/listUserByCorp?tm=' + new Date().getTime(),
					data: {CORPINFO_ID: corpinfoId},
					dataType: 'json',
					success: function (res) {
						_this.userList = res.data.userList;
					}
				});
			},
			// 获取有限空间列表
			getLimitSpace(){
				var _this = this;
				uni.request({
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type': 'application/x-www-form-urlencoded'
					},
					url: basePath + '/app/limitspace/listAllSpace?tm=' + new Date().getTime(),
					data: {CORPINFO_ID: corpinfoId},
					dataType: 'json',
					success: function (res) {
						_this.limitSpaceList = res.data.varList;
					}
				});
			},
			guardianChange(e){
				this.gindex = e.detail.value;
				this.pd.GUARDIAN=this.userList[this.gindex].USER_ID;
				this.pd.GUARDIANNAME=this.userList[this.gindex].USERNAME;	// 操作并重置监测人员
				this.jcuserList.splice(0,this.jcuserList.length);
				if(this.pd.SITESUPERVISOR != this.pd.GUARDIAN){
					this.jcuserList.push({"USER_ID":this.pd.SITESUPERVISOR,"USERNAME":this.pd.SITESUPERVISORNAME});
					this.jcuserList.push({"USER_ID":this.pd.GUARDIAN,"USERNAME":this.pd.GUARDIANNAME});
				} else {
					this.jcuserList.push({"USER_ID":this.pd.GUARDIAN,"USERNAME":this.pd.GUARDIANNAME});
				}
				this.pd.INSPECTOR = '';
				this.pd.INSPECTORNAME = '';
				this.iindex = -1;
				this.$forceUpdate();//强制刷新
			},
			sitesupervisorChange(e){
				this.sindex = e.detail.value;
				this.pd.SITESUPERVISOR=this.userList[this.sindex].USER_ID;
				this.pd.SITESUPERVISORNAME=this.userList[this.sindex].USERNAME;
				this.jcuserList.splice(0,this.jcuserList.length);
				if(this.pd.SITESUPERVISOR != this.pd.GUARDIAN){
					this.jcuserList.push({"USER_ID":this.pd.SITESUPERVISOR,"USERNAME":this.pd.SITESUPERVISORNAME});
					this.jcuserList.push({"USER_ID":this.pd.GUARDIAN,"USERNAME":this.pd.GUARDIANNAME});
				} else {
					this.jcuserList.push({"USER_ID":this.pd.GUARDIAN,"USERNAME":this.pd.GUARDIANNAME});
				}
				// 操作并重置监测人员
				this.pd.INSPECTOR = '';
				this.pd.INSPECTORNAME = '';
				this.iindex = -1;
				this.$forceUpdate();//强制刷新
			},
			approverChange(e){
				this.aindex = e.detail.value;
				this.pd.APPROVER=this.userList[this.aindex].USER_ID;
				this.pd.APPROVERNAME=this.userList[this.aindex].USERNAME;
				this.$forceUpdate();//强制刷新
			},
			limitspaceChange(e){
				this.lindex = e.detail.value;
				this.pd.LIMITSPACE_ID=this.limitSpaceList[this.lindex].LIMITSPACE_ID;
				this.pd.LIMITSPACENAME=this.limitSpaceList[this.lindex].NAME;
				this.$forceUpdate();//强制刷新
			},
			inspectorChange(e){
				this.iindex = e.detail.value;
				this.pd.INSPECTOR=this.jcuserList[this.iindex].USER_ID;
				this.pd.INSPECTORNAME=this.jcuserList[this.iindex].USERNAME;
				this.$forceUpdate();//强制刷新
			},
			isflowClick(e){
				if(!this.forbidEdit){
					if(e==1){
						this.pd.ISFLOW = 0;
					} else if(e==0){
						this.pd.ISFLOW = 1;
					}
				}
			},
			istrainClick(e){
				if(!this.forbidEdit){
					if(e==1){
						this.pd.ISTRAIN = 0;
					} else if(e==0){
						this.pd.ISTRAIN = 1;
					}
				}

			},
			isequipmentClick(e){
				if(!this.forbidEdit){
					if(e==1){
						this.pd.ISEQUIPMENT = 0;
					} else if(e==0){
						this.pd.ISEQUIPMENT = 1;
					}
				}
			},
			isrescueClick(e){
				if(!this.forbidEdit){
					if(e==1){
						this.pd.ISRESCUE = 0;
					} else if(e==0){
						this.pd.ISRESCUE = 1;
					}
				}
			},
			isPosInt(){
				if(!/^[1-9]\d*$/.test(this.pd.MAXPERSON)){
					uni.showToast({
						icon: 'none',
						title: '请输入正整数',
						duration: 1500
					});
				}
			},
			changeStartDate(e) {
				this.pd.STARTTIME = e
				this.$forceUpdate();//强制刷新
			},
			changeEndDate(e) {
				this.pd.ENDTIME = e
				this.$forceUpdate();//强制刷新
			},
			goSubmit(STATUS) {
				var _this = this;
				uni.showLoading({
					title: '请稍候'
				})
				if (!_this.pd.APPROVENUMBER) {
					uni.showToast({
						icon: 'none',
						title: '请填申请审批编号',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.LIMITSPACE_ID) {
					uni.showToast({
						icon: 'none',
						title: '请选择有限空间',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.UNIT) {
					uni.showToast({
						icon: 'none',
						title: '请填作业单位',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.CONTONT) {
					uni.showToast({
						icon: 'none',
						title: '请填作业内容',
						duration: 1500
					});
					return;
				}

				if (!_this.pd.STARTTIME || _this.pd.STARTTIME == '请选择') {
					uni.showToast({
						icon: 'none',
						title: '请选择作业起始时间',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.ENDTIME || _this.pd.ENDTIME == '请选择') {
					uni.showToast({
						icon: 'none',
						title: '请选择作业结束时间',
						duration: 1500
					});
					return;
				}
				if(new Date(_this.pd.STARTTIME.replace(/-/g,'/')) > new Date(_this.pd.ENDTIME.replace(/-/g,'/'))){
					uni.showToast({
						icon: 'none',
						title: '作业结束时间不能早于开始时间',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.HARMFULFACTOR) {
					uni.showToast({
						icon: 'none',
						title: '请输入可能存在风险及有害因素',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.PRINCIPAL) {
					uni.showToast({
						icon: 'none',
						title: '请选择作业负责人',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.GUARDIAN) {
					uni.showToast({
						icon: 'none',
						title: '请选择监护人员',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.WORKER) {
					uni.showToast({
						icon: 'none',
						title: '请填作业人员',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.OTHERPERSON) {
					uni.showToast({
						icon: 'none',
						title: '请填写其他人员',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.ISFLOW) {
					uni.showToast({
						icon: 'none',
						title: '请选择制定有限空间作业方案并经审核、批准',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.ISTRAIN) {
					uni.showToast({
						icon: 'none',
						title: '请选择参加作业人员经有限空间作业安全相关培训合格',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.ISEQUIPMENT) {
					uni.showToast({
						icon: 'none',
						title: '请选择安全防护设备、个体防护用品、作业设备和工具齐全有效、满足要求',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.ISRESCUE) {
					uni.showToast({
						icon: 'none',
						title: '请选择应急救援装备满足要求',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.SITESUPERVISOR) {
					uni.showToast({
						icon: 'none',
						title: '请选择现场作业负责人',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.APPROVER) {
					uni.showToast({
						icon: 'none',
						title: '请选择审批负责人',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.INSPECTOR) {
					uni.showToast({
						icon: 'none',
						title: '请选择检测人员',
						duration: 1500
					});
					return;
				}
				uni.request({
					url: basePath+'/app/limitspaceapprove/'+_this.msg,
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type':'application/x-www-form-urlencoded'
					},
					data: {
						LIMITSPACEAPPROVE_ID:this.pd.LIMITSPACEAPPROVE_ID,
						APPROVENUMBER:this.pd.APPROVENUMBER,
						LIMITSPACE_ID:this.pd.LIMITSPACE_ID,
						LIMITSPACENAME:this.pd.LIMITSPACENAME,
						UNIT:this.pd.UNIT,
						CONTONT:this.pd.CONTONT,
						STARTTIME:this.pd.STARTTIME,
						ENDTIME:this.pd.ENDTIME,
						PRINCIPAL:this.pd.PRINCIPAL,
						STATUS:STATUS,
						GUARDIAN:this.pd.GUARDIAN,
						WORKER:this.pd.WORKER,
						OTHERPERSON:this.pd.OTHERPERSON,
						HARMFULFACTOR:this.pd.HARMFULFACTOR,
						ISFLOW:this.pd.ISFLOW,
						ISTRAIN:this.pd.ISTRAIN,
						ISEQUIPMENT:this.pd.ISEQUIPMENT,
						ISRESCUE:this.pd.ISRESCUE,
						SITESUPERVISOR:this.pd.SITESUPERVISOR,
						APPROVER:this.pd.APPROVER,
						CORPINFO_ID:corpinfoId,
						USER:loginUser.USER_ID,
						INSPECTOR:this.pd.INSPECTOR
					},
					success: (res) => {
						uni.hideLoading();
						if ("success" == res.data.result) {
							if(STATUS=="1"){
								uni.showToast({
									icon: 'none',
									title: '已经暂存为待提交数据',
									duration: 1500
								});
							} else if (STATUS =="2"){
								uni.showToast({
									icon: 'none',
									title: '提交成功',
									duration: 1500
								});
							}

							setTimeout(function(){
								var pages = getCurrentPages();	// 获取当前页面栈
								var prePage = pages[pages.length - 2];	// 上二级页面
								prePage.$vm.initflag = true;    // A 页面 init方法 为true
								uni.navigateBack({delta: 1});
								uni.hideLoading();
							},1500);
						}else{
							uni.showToast({
								icon:'none',
								title: '系统错误',
								duration: 2000
							});
						}
					}
				});
			},
			goback(){
				var pages = getCurrentPages();	// 获取当前页面栈
				var prePage = pages[pages.length - 2];	// 上二级页面
				prePage.$vm.initflag = true;    // A 页面 init方法 为true
				uni.navigateBack({delta: 1});
				uni.hideLoading();
			},
			radioType(e){
				this.HASINSTRUCTOR = e.detail.value
			},
			goToEdit(){
				this.forbidEdit = false;
			}
		}
	}
</script>

<style>
	.prevent {
			width: 100%;
			height: 100%;
			position: fixed;
			top: 0;
			left: 0;
			overflow: hidden;
		}
	.cu-form-title{
		padding: 20upx 0;
	}
	.cu-form-textarea{
		background-color: #ffffff;
		padding: 1upx 30upx 20upx;
		min-height: 100upx;
	}
	.cu-form-textarea textarea {
		height: 4.6em;
		width: 100%;
		line-height: 1.2em;
		flex: 1;
		font-size: 28upx;
		padding: 0;
	}
	.selected{
		display: flex;
		align-items: center;
		height: 100upx;
	}
	.selected .radio{
		 transform:scale(0.8);
		 margin-right: 10upx;
	}
	.group{
		display: flex;
		align-items: center;
	}
	.cu-form-group .title{
		font-size: 28upx;
		color: #666;
	}
	.cu-bar .action:first-child {
	    font-size: 28upx;
	}
</style>
