<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,0)" 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" @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="checkTrue({index:index},item.HASHIDDEN,2)" 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 :loading="buttonloading" class="cu-btn bg-green" >提交</button>
		</view>
	</view>

</template>
<script>
  import { decryptAes } from "@/common/aesUtil";
	import {
		basePath,corpinfoId,loginUser,loginSession
	} from '@/common/tool.js';
	import gcoord from '@/common/gcoord.js'
	export default {
		data() {
			return {
				noClick:true,
				buttonloading: false,
				IDENTIFICATIONPARTS_ID:'',
				listId:'',
				listName:'',
				list: [],
				pd:[],
				// isChecked:'',
				CHECKRECORD_ID:'',
			}
		},
		onLoad(e){
			this.IDENTIFICATIONPARTS_ID=e.IDENTIFICATIONPARTS_ID;
			this.listId=e.listId;
			this.listName=e.listName;
			// 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_NAME : loginUser.NAME,
            CORPINFO_ID:loginUser.CORPINFO_ID,
            USER_ID:loginUser.USER_ID,
					},
					success: (res) => {
            res.data = decryptAes(res.data);
						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.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 请求提交保存
						this.buttonloading = true
						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,
								LATITUDE:latitude,
								LONGITUDE:longitude,
								ITEMS:JSON.stringify(items),
								IDS:ids.join(","),
								CHECKRECORD_ID:_this.CHECKRECORD_ID,
								tm:new Date().getTime(),
								CORPINFO_ID:loginUser.CORPINFO_ID,
								USER_ID:loginUser.USER_ID,
							},
							success: function(res){
                res.data = decryptAes(res.data);
								this.buttonloading = false
								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
									});
								}
							}
						})
					},
					fail: function(res){
						uni.hideLoading();
						uni.showToast({
							title: "获取位置失败",
							duration: 2000
						});
					}
				});

			},
			//点击合格
			checkTrue(e,HASHIDDEN,STATUS) {
				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,
              CORPINFO_ID:loginUser.CORPINFO_ID,
              USER_ID:loginUser.USER_ID,
						},
						success: function(res){
              res.data = decryptAes(res.data);
							if("success" == res.data.result){
								_this.list[e.index].ISNORMAL=STATUS;
								_this.list[e.index].HIDDEN_ID='';
								_this.$forceUpdate();//强制刷新
							}else {
								uni.showToast({
									title: "系统错误",
									duration: 2000
								});
							}
						}
					})
				}else{
					_this.list[e.index].ISNORMAL=STATUS;
					_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>
