<template>
	<view>
		<cu-custom bgColor="bg-gradual-blueness" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">隐患排查项</block>
		</cu-custom>
		<scroll-view scroll-y>
<!--			<view class="check-items-title">-->
<!--				<view class="check-items-tag">-->
<!--					排查项目-->
<!--				</view>-->
<!--				<view class="check-items-text">-->
<!--					{{pd.PARTSNAME}}-->
<!--				</view>-->
<!--			</view>-->
			<view class="check-items" v-for="(item,index) in list" :key="item.LISTCHECKITEM_ID">
				<view class="title">存在风险:{{item.RISK_DESCR}}</view>
				<view class="title">检查内容:{{item.CHECK_CONTENT}}</view>
				<view class="check-items-select">
					<radio-group class="block">
						<view class="flex justify-between padding-sm">
							<view class="select">
								<view @click="checkTrue({index:index},item.HASHIDDEN)" class="flex align-center">
									<radio class='mr10' :class="item.ISNORMAL==0?'checked':''" :checked="item.ISNORMAL==0" disabled></radio>
									<text>合格</text>
								</view>
								<view v-if="item.ISNORMAL==0 && isTongyong" @click="$noMultipleClicks(gotoAddImg,item.RECORDITEM_ID)" class="cuIcon-infofill text-blue unqualified"></view>
							</view>
							<view class="select">
								<view class="flex align-center" @click="$noMultipleClicks(goToAdd,{checkContent:item.CHECK_CONTENT,riskId:item.LISTCHECKITEM_ID,itemId:item.RECORDITEM_ID,index:index,LISTMANAGER_ID:item.LISTMANAGER_ID,HASHIDDEN:item.HASHIDDEN})">
									<radio class='red mr10' :class="item.ISNORMAL==1?'checked':''" :checked="item.ISNORMAL==1" disabled ></radio>
									<text>不合格</text>
								</view>
								<view v-if="item.ISNORMAL==1" @click="$noMultipleClicks(goToEdit,item.HIDDEN_ID)" class="cuIcon-infofill text-blue unqualified"></view>
							</view>
							<view class="select">
								<view @click="checkNotInvolved({index:index},item.HASHIDDEN)" class="flex align-center">
									<radio class='mr10' :class="item.ISNORMAL==2?'checked':''" :checked="item.ISNORMAL==2" disabled></radio>
									<text>不涉及</text>
								</view>
							</view>
						</view>
					</radio-group>
				</view>
			</view>
			<view class="cu-tabbar-height"></view>
		</scroll-view>
		<view class="bottom-fixed" @click="$noMultipleClicks(save)">
			<button class="cu-btn bg-green" >提交</button>
		</view>
	</view>

</template>
<script>
	import {
		basePath,corpinfoId,loginUser,loginSession
	} from '@/common/tool.js';
	import gcoord from '@/common/gcoord.js'
	export default {
		data() {
			return {
				noClick:true,
				IDENTIFICATIONPARTS_ID:'',
				listId:'',
				listName:'',
				list: [],
				pd:[],
				// isChecked:'',
				CHECKRECORD_ID:'',
				isTongyong:true,
			}
		},
		onLoad(e){
			this.IDENTIFICATIONPARTS_ID=e.IDENTIFICATIONPARTS_ID;
			this.listId=e.listId;
			this.listName=e.listName;
			if(corpinfoId =='ffbf90f7064340ae9d56c16d4f560423'){
				this.isTongyong=false;
			}
			// this.isChecked=e.isChecked;
			this.CHECKRECORD_ID=e.CHECKRECORD_ID;
			this.getData();
			loginSession();
		},
		onShow() {
			var _this = this;
			if(_this.initflag){
				_this.initflag = false;
				_this.$forceUpdate();//强制刷新
			}
		},
		methods: {
			getData() {
				var _this = this;
				uni.showLoading({
					title: '请稍候'
				})
				uni.request({
					url: basePath + '/app/listmanager/goCheck',
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type':'application/x-www-form-urlencoded'
					},
					data: {
						LISTMANAGER_ID: _this.listId,
						IDENTIFICATIONPARTS_ID: _this.IDENTIFICATIONPARTS_ID,
						USER_ID : loginUser.NAME,
					},
					success: (res) => {
						if("success" == res.data.result){
							uni.hideLoading();
							_this.pd=res.data.pd;
							_this.list = res.data.varList;
							if(res.data.records){
								for (var i = 0; i <_this.list.length ; i++) {
									for (var j = 0; j <res.data.records.length ; j++) {
										if(_this.list[i].LISTCHECKITEM_ID==res.data.records[j].LISTCHECKITEM_ID){
											_this.list[i].ISNORMAL = res.data.records[j].ISNORMAL;
											_this.list[i].HIDDEN_ID = res.data.records[j].HIDDEN_ID;
											_this.list[i].RECORDITEM_ID = res.data.records[j].RECORDITEM_ID;
											// _this.list[i].is_checked = 1;
										}
									}
								}
							}
						} else {
							uni.showToast({
								title: res.data.message,
								duration: 2000
							});
						}
					}
				});
			},
			//去保存
			save: function (){
				var _this = this;
				uni.showLoading({
					title: '请稍候'
				})
				var items=[];
				var ids=[];
				for(var i=0;i<_this.list.length;i++){
					var item = {};
					if(_this.list[i].ISNORMAL>-1 || _this.list[i].HASHIDDEN > 0) {
						item.LISTCHECKITEM_ID = _this.list[i].LISTCHECKITEM_ID;

						item.ISNORMAL = _this.list[i].ISNORMAL;
						item.RECORDITEM_ID = _this.list[i].RECORDITEM_ID;
						items.push(item);
						if (_this.list[i].HIDDEN_ID) {
							ids.push(_this.list[i].HIDDEN_ID)
						}
					}
				}
				let longitude;
				let latitude;
				uni.request({
					url: basePath+'/app/checkrecord/add',
					xhrFields: {
						withCredentials: true
					},
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type':'application/x-www-form-urlencoded'
					},
					data: {
						LISTMANAGER_ID:_this.listId,
						LIST_NAME:_this.listName,
						CREATOR:loginUser.NAME,
						CORPINFO_ID:corpinfoId,
						LATITUDE:latitude,
						LONGITUDE:longitude,
						ITEMS:JSON.stringify(items),
						IDS:ids.join(","),
						CHECKRECORD_ID:_this.CHECKRECORD_ID,
						tm:new Date().getTime()
					},
					dataType:"json",
					success: function(res){
						if("success" == res.data.result){
							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({});
								uni.hideLoading();
							},1500);
						}else {
							uni.showToast({
								title: "系统错误",
								duration: 2000
							});
						}
					}
				})
				// uni.getLocation({
				// 	type: 'GCJ02',
				// 	success: function (res1) {
				// 		longitude = res1.longitude;
				// 		latitude = res1.latitude;
				// 		var result = gcoord.transform(
				// 				[longitude, latitude],    // 经纬度坐标
				// 				gcoord.GCJ02,               // 当前坐标系
				// 				gcoord.BD09                // 目标坐标系
				// 		);
				// 		longitude = result[0];
				// 		latitude =  result[1];
				// 		//发送 post 请求提交保存
				//
				// 	},
				// 	fail: function(res){
				// 		uni.hideLoading();
				// 		uni.showToast({
				// 			title: "获取位置失败",
				// 			duration: 2000
				// 		});
				// 	}
				// });

			},
			// 不涉及
			checkNotInvolved(e , HASHIDDEN){
				if(HASHIDDEN > 0){
					uni.showToast({
						icon:'none',
						title: "此检查内容存在未处理隐患,无法检查",
						duration: 1000
					});
					return;
				}
				let _this=this;
				if(_this.list[e.index].HIDDEN_ID){
					//不合格改为合格时 删除旧隐患
					uni.request({
						url: basePath+'/app/hidden/delete',
						xhrFields: {
							withCredentials: true
						},
						method: 'POST',
						dataType: 'json',
						header: {
							'Content-type':'application/x-www-form-urlencoded'
						},
						data: {
							HIDDEN_ID:_this.list[e.index].HIDDEN_ID
						},
						dataType:"json",
						success: function(res){
							if("success" == res.data.result){
								_this.list[e.index].ISNORMAL=0;
								_this.list[e.index].HIDDEN_ID='';
								_this.$forceUpdate();//强制刷新
							}else {
								uni.showToast({
									title: "系统错误",
									duration: 2000
								});
							}
						}
					})
				}else{
					_this.list[e.index].ISNORMAL=2;
					_this.list[e.index].HIDDEN_ID='';
					_this.$forceUpdate();//强制刷新
				}
			},
			//点击合格
			checkTrue(e,HASHIDDEN) {
				if(HASHIDDEN > 0){
					uni.showToast({
						icon:'none',
						title: "此检查内容存在未处理隐患,无法检查",
						duration: 1000
					});
					return;
				}
				// if(this.list[e.index].ISNORMAL==1){
				// 	return;
				// }
				let _this=this;
				if(_this.list[e.index].HIDDEN_ID){
					//不合格改为合格时 删除旧隐患
					uni.request({
						url: basePath+'/app/hidden/delete',
						xhrFields: {
							withCredentials: true
						},
						method: 'POST',
						dataType: 'json',
						header: {
							'Content-type':'application/x-www-form-urlencoded'
						},
						data: {
							HIDDEN_ID:_this.list[e.index].HIDDEN_ID
						},
						dataType:"json",
						success: function(res){
							if("success" == res.data.result){
								_this.list[e.index].ISNORMAL=0;
								_this.list[e.index].HIDDEN_ID='';
								_this.$forceUpdate();//强制刷新
							}else {
								uni.showToast({
									title: "系统错误",
									duration: 2000
								});
							}
						}
					})
				}else{
					_this.list[e.index].ISNORMAL=0;
					_this.list[e.index].HIDDEN_ID='';
					_this.$forceUpdate();//强制刷新
				}
			},
			//跳转事件
			goToAdd(e) {
				if(e.HASHIDDEN > 0){
					uni.showToast({
						icon:'none',
						title: "此检查内容存在未处理隐患,无法检查",
						duration: 1000
					});
					return;
				}
				if(this.list[e.index].ISNORMAL==1){
					return;
				}

				uni.navigateTo({
					url: '/pages/application/hidden-trouble-investigation/hidden-trouble-app/exclude-form?checkContent='+encodeURIComponent(JSON.stringify(e.checkContent.replace('%','%25')))
							+'&riskId='+ encodeURIComponent(JSON.stringify(e.riskId))
							+'&itemId='+ encodeURIComponent(JSON.stringify(e.itemId))
							+'&LISTMANAGER_ID='+ encodeURIComponent(JSON.stringify(e.LISTMANAGER_ID))
							+'&index='+e.index
				});
			},
			//跳转事件
			goToEdit(e) {
				uni.navigateTo({
					url: '/pages/application/hidden-trouble-investigation/hidden-trouble-app/exclude-form?HIDDEN_ID='+e
				});
			},
			//添加合格照片
			gotoAddImg(e) {
				uni.navigateTo({
					url: '/pages/application/hidden-trouble-investigation/hidden-trouble-app/check-items-imgs?RECORDITEM_ID='+e
				});
			},
		}
	}
</script>

<style>
.select radio {
		transform: scale(0.9);
	}

</style>