<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="wui-form-list">
				<view class="cu-form-group" >
					<view class="title">申请单位：</view>
					{{pd.APPLY_DEPARTMENT_NAME}}
				</view>

				<view class="cu-form-group" >
					<view class="title">申请人：</view>
					{{pd.APPLY_USER_NAME}}
				</view>

				<view class="cu-form-group">
					<view class="title">编号：</view>
					<input name="input" ref="CHECK_NO" :disabled="forbidEdit" v-model="pd.CHECK_NO" placeholder="请输入编号"></input>
				</view>

				<view class="cu-form-group ">
					<view class="title">作业开始时间</view>
					<ruiDatePicker v-if="!forbidEdit"
								   :start="todayDate"
								   fields="minute"
								   :value="pd.WORK_START_DATE?pd.WORK_START_DATE:''"
								   @change="changeStartDate"
					></ruiDatePicker>
					<view v-else>{{pd.WORK_START_DATE}}</view>
				</view>
				<view class="cu-form-group">
					<view class="title">作业结束时间</view>
					<ruiDatePicker v-if="!forbidEdit"
								   :start="todayDate"
								   fields="minute"
								   :value="pd.WORK_END_DATE?pd.WORK_END_DATE:''"
								   @change="changeEndDate"
					></ruiDatePicker>
					<view v-else>{{pd.WORK_END_DATE}}</view>
				</view>

				<view class="cu-form-group bb-default">
					<view class="title">作业地点：</view>
					<input name="input" ref="WORK_PLACE" :disabled="forbidEdit" v-model="pd.WORK_PLACE" placeholder="请输入施工地点"></input>
				</view>

				<view class="cu-form-textarea">
					<view class="cu-form-title">作业内容：</view>
					<textarea maxlength="255" :disabled="forbidEdit" v-model="pd.WORK_CONTENT" placeholder="请输入作业内容"></textarea>
				</view>

				<view class="cu-form-group" >
					<view class="title">电源接入点及许可用电功率：</view>
					<input name="input" ref="ALLOW_POWER" :disabled="forbidEdit" v-model="pd.ALLOW_POWER" placeholder="请输入电源接入点及许可用电功率"></input>
				</view>

				<view class="cu-form-group">
					<view class="title">用电设备名称及额定功率：</view>
					<input name="input" ref="RATED_POWER" :disabled="forbidEdit" v-model="pd.RATED_POWER" placeholder="请输入用电设备名称及额定功率"></input>
				</view>

				<view class="cu-form-group">
					<view class="title">工作电压：</view>
					<input name="input" ref="WORK_VOLTAGE" :disabled="forbidEdit" v-model="pd.WORK_VOLTAGE" placeholder="请输入工作电压"></input>
				</view>

				<template v-if="pd.APPLY_STATUS && pd.APPLY_STATUS!=0 && pd.APPLY_STATUS!=1">
					<view class="cu-form-group" >
						<view class="title">作业人</view>
						<input name="input" ref="WORK_USER" :disabled="forbidEdit" v-model="pd.WORK_USER" placeholder="请输入作业人"></input>
					</view>

					<view class="cu-form-group" >
						<view class="title">电工证号</view>
						<input name="input" ref="CARD_NO" :disabled="forbidEdit" v-model="pd.CARD_NO" placeholder="请输入电工证号"></input>
					</view>

					<view class="cu-form-group bb-default" >
						<view class="title">监护人</view>
						<input name="input" ref="GUARDIAN" :disabled="forbidEdit" v-model="pd.GUARDIAN" placeholder="请输入监护人"></input>
					</view>

					<view class="cu-form-textarea" >
						<view class="cu-form-title">涉及的其他特殊作业</view>
						<textarea maxlength="255" :disabled="forbidEdit" v-model="pd.SPECIAL_WORK" placeholder="请输入涉及的其他特殊作业"></textarea>
					</view>

					<view class="cu-form-textarea" >
						<view class="cu-form-title">风险辨识结果</view>
						<textarea maxlength="255" :disabled="forbidEdit" v-model="pd.RISK_IDENTIFICATION" placeholder="请输入风险辨识结果"></textarea>
					</view>
				</template>
			</view>

			<view class="wui-form-list" v-if="pd.APPLY_STATUS && pd.APPLY_STATUS!=0 && pd.APPLY_STATUS!=1" style="padding-top: 20upx;">
				<view class="wui-title" style="margin-left: 20upx;">
					<text class="text-semi">安全防护措施</text>
				</view>
				<view class="wui-table" style="padding: 0 20upx;">
					<uni-table name='measuresList' border stripe emptyText="暂无更多数据" >
						<!-- 表头行 -->
						<uni-tr>
							<uni-th align="center" style="font-weight: bold;">主要安全措施</uni-th>
							<uni-th align="center" style="font-weight: bold;width: 100px">操作</uni-th>
						</uni-tr>
						<uni-tr v-for="(item,index) in measuresList" :key="item.BUS_ELECTRICITY_MEASURES_ID">
							<uni-td>{{item.PROTECTIVE_MEASURES}}</uni-td>
							<uni-td align="center">
								<radio-group class="wui-radio-group">
									<view class="group" v-if="item.STATUS=='-1'">
										<radio class='radio'  value="-1"  checked>
											<text class="wui-pl10">不合格</text></radio>
									</view>
									<view class="group" v-if="item.STATUS=='1'">
										<radio class='radio' value="1" checked>
											<text class="wui-pl10">合格</text></radio>
									</view>
									<view class="group" v-if="item.STATUS=='2'">
										<radio class='radio' value="2" checked>
											<text class="wui-pl10">不涉及</text></radio>
									</view>
								</radio-group>
							</uni-td>
						</uni-tr>
					</uni-table>
				</view>
				<view v-if="pd.APPLY_STATUS && pd.APPLY_STATUS!=0 && pd.APPLY_STATUS!=1">
					<view class="cu-form-group">
						<view class="title">补充措施：</view>
					</view>
					<view v-if="pd.CONFIRM_USER_MEASURES" class="ty-group">
						<text class="title">
							作业单位：</text>
						<text>{{ pd.CONFIRM_USER_MEASURES}}</text>
					</view>
					<view v-if="pd.AUDIT_USER_MEASURES" class="ty-group">
						<text class="title">
							配送电单位：</text>
						<text>{{ pd.AUDIT_USER_MEASURES}}</text>
					</view>
					<view v-if="pd.APPROVE_USER_MEASURES" class="ty-group">
						<text class="title">
							审批人：</text>
						<text>{{ pd.APPROVE_USER_MEASURES}}</text>
					</view>
				</view>

				<view class="cu-form-group" v-if="pd.APPLY_STATUS && pd.APPLY_STATUS!=0 && pd.APPLY_STATUS!=1">
					<view class="title">安全交底人：</view>
					<input name="input" ref="CONFESS_USER" disabled="disabled" v-model="pd.CONFESS_USER" placeholder="请输入安全交底人"></input>
				</view>

				<view class="cu-form-group" v-if="pd.APPLY_STATUS && pd.APPLY_STATUS!=0 && pd.APPLY_STATUS!=1">
					<view class="title">接受交底人：</view>
					<input name="input" ref="ACCEPT_CONFESS_USER" disabled="disabled" v-model="pd.ACCEPT_CONFESS_USER" placeholder="请输入接受交底人"></input>
				</view>

				<view class="cu-form-group" v-if="pd.APPLY_STATUS && pd.APPLY_STATUS!=0 && pd.APPLY_STATUS!=1">
					<view class="title">教育人：</view>
					<input name="input" ref="EDUCATE_USER" disabled="disabled" v-model="pd.EDUCATE_USER" placeholder="请输入教育人"></input>
				</view>

				<view class="cu-form-group" v-if="pd.APPLY_STATUS && pd.APPLY_STATUS!=0 && pd.APPLY_STATUS!=1">
					<view class="title">接受教育人：</view>
					<input name="input" ref="ACCEPT_EDUCATE_USER" disabled="disabled" v-model="pd.ACCEPT_EDUCATE_USER" placeholder="请输入接受教育人"></input>
				</view>

				<view class="cu-form-group" v-if="pd.APPLY_STATUS && pd.APPLY_STATUS!=0 && pd.APPLY_STATUS!=1">
					<view>
						<text class="title">分析人：</text>
						<text>{{pd.ANALYZE_USER_NAME}}</text>
					</view>
					<button class="cu-btn bg-green sm" @click="$noMultipleClicks(goToDetail,pd.ELECTRICITY_ID)">分析详情</button>
				</view>
			</view>

			<view class="wui-form-list" v-if="!forbidEdit">
				<view class="cu-form-group" v-if="!forbidEdit">
					<view class="title">作业(地点)单位</view>
					<view class="picker-tree-box">
						<view class="picker-tree" @tap="showConfirmTree">{{pd.CONFIRM_DEPARTMENT_NAME?pd.CONFIRM_DEPARTMENT_NAME:'请选择'}}</view>
					</view>
					<tki-tree ref="tkiTree_confirm"
							  :selectParent=true
							  :range="treeNode"
							  rangeKey="name"
							  @confirm="confirmtreeConfirm"
							  @cancel="confirmtreeCancel"></tki-tree>
				</view>
				<view class="cu-form-group" v-if="!forbidEdit">
					<view class="title">作业(地点)单位负责人</view>
					<picker @change="PickerConfirm" :value="confirmindex" :range="confirmUserList" range-key="NAME" :disabled="confirmUserList.length == 0" @click="isBlankList('confirm')">
						<view class="picker">
							{{pd.CONFIRM_USER_NAME?pd.CONFIRM_USER_NAME:'请选择'}}
						</view>
					</picker>
				</view>
			</view>

			<view class="wui-form-list" v-if="forbidEdit">
				<view class="cu-form-textarea" style="border: none;">
					<view class="cu-form-title">作业(地点)单位负责人意见</view>
					<textarea maxlength="255" disabled="disabled" v-model="pd.CONFIRM_CONTENT"></textarea>
				</view>
				<view class="cu-form-group" style="border-bottom: 1px solid #eee;border-top: 1px dashed #eee;">
					<view class="title">作业(地点)单位负责人</view>
					{{pd.CONFIRM_USER_NAME}} {{pd.CONFIRM_USER_SIGNER_TIME}}
				</view>

				<view class="cu-form-textarea" style="border: none;">
					<view class="cu-form-title">配送电单位负责人意见</view>
					<textarea maxlength="255" disabled="disabled" v-model="pd.AUDIT_CONTENT"></textarea>
				</view>
				<view class="cu-form-group" style="border-bottom: 1px solid #eee;border-top: 1px dashed #eee;">
					<view class="title">配送电单位负责人</view>
					{{pd.AUDIT_USER_NAME}} {{pd.AUDIT_USER_SIGNER_TIME}}
				</view>

				<view class="cu-form-textarea" style="border: none;">
					<view class="cu-form-title">审批人意见</view>
					<textarea maxlength="255" disabled="disabled" v-model="pd.APPROVE_CONTENT"></textarea>
				</view>
				<view class="cu-form-group" v-if="forbidEdit" style="border-bottom: 1px solid #eee;border-top: 1px dashed #eee;">
					<view class="title">审批人</view>
					{{pd.APPROVE_USER_NAME}} {{pd.APPROVE_USER_SIGNER_TIME}}
				</view>

				<view class="cu-form-textarea" style="border: none;">
					<view class="cu-form-title">验收人意见</view>
					<textarea maxlength="255" disabled="disabled" v-model="pd.ACCEPT_CONTENT"></textarea>
				</view>
				<view class="cu-form-group" v-if="forbidEdit" style="border-bottom: 1px solid #eee;border-top: 1px dashed #eee;">
					<view class="title">验收人</view>
					{{pd.ACCEPT_USER_NAME}} {{pd.ACCEPT_USER_SIGNER_TIME}}
				</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,'1')">提交</button>
			<button v-if="!forbidEdit" class="cu-btn bg-green margin-tb-sm lg" @click="$noMultipleClicks(goSubmit,'0')">暂存</button>
			<button v-if="forbidEdit" 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 writingBoard from "@/components/writing-board/writing-board.vue"
	import gcoord from '@/common/gcoord.js'
	import ruiDatePicker from '@/components/rattenking-dtpicker/rattenking-dtpicker.vue';
	export default {
		components: {
			tkiTree,ruiDatePicker,writingBoard
		},
		data() {
			return {
				baseImgPath:baseImgPath,
				isUps:false,
				forbidEdit:true,// 禁止修改
				msg:'add',
				noClick:true,
				treeNode:[],//部门下拉数据
				confirmUserList:[],
				confirmindex:-1,
				pd:{
					OTHER_PROTECTIVE_MEASURES:';_;;_;;_;',
					},// 数据
				measuresList:[],
				rules:[
					{name:'CHECK_NO',message:'请输入编号'},
					{name:'WORK_PLACE',message:'请输入施工地点'},
					{name:'WORK_CONTENT',message:'请输入作业内容'},
					{name:'WORK_START_DATE',message:'请选择工作开始时间'},
					{name:'WORK_START_DATE',message:'请选择工作结束时间'},
					{name:'ALLOW_POWER',message:'请输入电源接入点及许可用电功率'},
					{name:'RATED_POWER',message:'请输入用电设备名称及额定功率'},
					{name:'WORK_VOLTAGE',message:'请输入工作电压'},
					{name:'CONFIRM_USER_ID',message:'请选择作业(地点)单位负责人'},
				],
				todayDate:'',

			}
		},
		onLoad(event){
			this.todayDate =  formatDate(new Date(), 'yyyy-MM-dd hh:mm');
			this.pd.ELECTRICITY_ID = event.ELECTRICITY_ID;
			if(this.pd.ELECTRICITY_ID){
				this.msg="edit";
				this.getData();
			}else {
				// 初始化作业负责人
				this.pd.APPLY_DEPARTMENT_ID = loginUser.DEPARTMENT_ID;
				this.pd.APPLY_DEPARTMENT_NAME = loginUser.DEPARTMENT_NAME;
				this.pd.APPLY_USER_ID = loginUser.USER_ID;
				this.pd.APPLY_USER_NAME = loginUser.NAME;
				this.forbidEdit = false;
				this.pd.OTHER_PROTECTIVE_MEASURES = ''
			}
			// 初始化现场作业负责人
			this.getDept();
			loginSession();

		},
		methods: {
			//跳转事件
			goToDetail(e) {
				uni.navigateTo({
					url: '/pages/application/electricity/electricity-gas/gas-list?ELECTRICITY_ID='+e
				});
			},
			getData() {
				var _this = this;
				uni.showLoading({
					title: '请稍候'
				})
				uni.request({
					url: basePath + '/app/electricity/goEdit',
					method: 'POST',
 					header: {
						'Content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						ELECTRICITY_ID: _this.pd.ELECTRICITY_ID,
					},
					success: (res) => {
						if ("success" == res.data.result) {
							uni.hideLoading();
							_this.pd = res.data.pd; //参数map
							_this.pd = Object.assign(_this.pd,
							{
								"CONFIRM_USER_MEASURES": _this.pd.OTHER_PROTECTIVE_MEASURES.split(";_;")[0],
								"AUDIT_USER_MEASURES": _this.pd.OTHER_PROTECTIVE_MEASURES.split(";_;")[1],
								"APPROVE_USER_MEASURES": _this.pd.OTHER_PROTECTIVE_MEASURES.split(";_;")[2]
							})
							_this.pd.OTHER_PROTECTIVE_MEASURES = ''
							_this.measuresList = res.data.measuresList
							if(_this.pd.APPLY_STATUS < 1){
								_this.forbidEdit = false;
							}
						} else if ("exception" == data.result) {
							uni.showToast({
								title: '错误',
								duration: 2000
							});
						}
					}
				});
			},
			goSubmit(STATUS){
				var _this = this;
				let required = true
				uni.showLoading({
					title: '请稍候'
				})
				if(STATUS==1) {
					this.rules.map(({name,message})  => {
						if (!this.pd[name]) {
							uni.showToast({
								icon: 'none',
								title: message,
								duration: 1500
							});
							required = false
						}
					})
					if (!required) {
						return
					}
				}
				const formData={}
				Object.keys(this.pd).map(key => {
					formData[key]=this.pd[key]
				})
				formData.CORPINFO_ID=corpinfoId
				formData.CREATOR=loginUser.USER_ID
				formData.OPERATOR=loginUser.USER_ID
				formData.ACTION_USER=loginUser.NAME
				formData.APPLY_STATUS=STATUS
				uni.request({
					url: basePath + "app/electricity/"+_this.msg,
					method: 'POST',
					dataType: 'json',
					header:{
						'Content-type':'application/x-www-form-urlencoded'
					},
					data: formData,
					success: (res) => {
						uni.showToast({
							icon:'none',
							title: '保存成功',
							duration: 2000
						});
						_this.goback()
					},
					fail: (err) => {
						uni.hideLoading();
						uni.showModal({
							content: err.errMsg,
							showCancel: false
						});
					}
				})
			},
			getDept() {
				var _this = this;
				uni.request({
					url: basePath + '/app/sys/listTree',//部门下拉接口
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type':'application/x-www-form-urlencoded'
					},
					data: {
						CORPINFO_ID : corpinfoId,
					},
					success: (res) => {
						if("success" == res.data.result){
							_this.treeNode=eval(res.data.zTreeNodes);
						} else {
							uni.showToast({
								title: res.data.message,
								duration: 2000
							});
						}
					}
				});
			},
			//获取人员列表
			getUserList(dept,list,i){
				//发送 post 请求
				var _this=this
				uni.request({
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type':'application/x-www-form-urlencoded'
					},
					url: basePath+'/app/sys/listUser',
					data: {DEPARTMENT_ID:dept,tm:new Date().getTime()},
					success: function(res){
						if("success" == res.data.result){
							if(i>-1) {
								_this[list][i] = [];
								_this[list][i] = res.data.userList;
								_this.$forceUpdate();//强制刷新
							}else{
								_this[list]=res.data.userList;
							}
						}else{
							uni.showToast({
								title: res.data.message,
								duration: 2000
							});
						}
					}
				})
			},

			/*
			*确认人
		 	*/
			// 确定回调事件
			confirmtreeConfirm(e) {
				this.isUps=false;
				this.pd.CONFIRM_DEPARTMENT_ID=e[0].id;
				this.pd.CONFIRM_DEPARTMENT_NAME=e[0].name;
				this.$forceUpdate();//强制刷新
				this.getUserList(e[0].id,'confirmUserList');
			},
			// 取消回调事件
			confirmtreeCancel(e) {
				this.isUps=false;
			},
			// 显示树形选择器
			showConfirmTree() {
				this.isUps=true
				this.$refs.tkiTree_confirm._show();
			},
			PickerConfirm(e) {
				this.confirmindex = e.detail.value;
				this.pd.CONFIRM_USER_ID=this.confirmUserList[this.confirmindex].USER_ID;
				this.pd.CONFIRM_USER_NAME=this.confirmUserList[this.confirmindex].NAME;
				this.$forceUpdate();//强制刷新
			},
			isBlankList(userType) {
				switch(userType) {
					case 'confirm':
						if(this.confirmUserList.length == 0) {
							uni.showToast({
								icon: 'none',
								title: '请先选择作业(地点)单位',
								duration: 1500
							})
						}
						break
				}
			},

			changeStartDate(e) {
				this.pd.WORK_START_DATE = e
				this.$forceUpdate();//强制刷新
			},
			changeEndDate(e) {
				this.pd.WORK_END_DATE = e
				this.$forceUpdate();//强制刷新
			},

			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>

</style>
