<template><!-- 源自pages/application/hidden-trouble-investigation/hidden-trouble-app/check-items-custom-list.vue -->
	<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" v-for="(item,index) in list" :key="item.CUSTOM_ITEM_ID">
				<view class="title">检查内容:{{item.CHECK_CONTENT}}</view>
        <view class="title">检查标准:{{item.CHECK_STANDARD}}</view>
        <view v-if="item.REFERENCE_BASIS" class="title">参考依据:{{item.REFERENCE_BASIS}}</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,
								{
                  CHECK_CONTENT:item.CHECK_CONTENT,
                  CUSTOM_ITEM_ID:item.CUSTOM_ITEM_ID,
                  RECORDITEM_ID:item.RECORDITEM_ID,
                  index:index,
                  CUSTOM_ID:item.CUSTOM_ID,
                  HASHIDDEN:item.HASHIDDEN,
                  CHECK_CATEGORY:item.CHECK_CATEGORY,
                  CHECK_CATEGORY_NAME:item.CHECK_CATEGORY_NAME,
                  CHECK_ITEM:item.CHECK_ITEM,
                  CHECK_ITEM_NAME:item.CHECK_ITEM_NAME,
                  CHECK_STANDARD: item.CHECK_STANDARD,
                  REFERENCE_BASIS: item.REFERENCE_BASIS,
                  CHECK_RESULT: item.CHECK_UNQUALIFIED
                })">
									<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>
            <view v-if="item.ISNORMAL==0 || item.ISNORMAL==1" class="input" >
              <textarea :disabled="item.OPERATION_TYPE === 1 ? true : false" maxlength="-1" v-model="item.CHECK_RESULT" placeholder="检查详细描述(必填项)"  :class="item.OPERATION_TYPE === 1 ? 'bgh' : ''"   style="border: 1px solid #eeeeee;  z-index: 999; height: 50px; padding: 10px;font-size: 12px; width: 100%"></textarea>
            </view>
					</radio-group>
				</view>
			</view>
			<view class="cu-tabbar-height"></view>
		</scroll-view>
		<view class="bottom-fixed" @tap.stop="openAuth('ACCESS_FINE_LOCATION')">
			<button :loading="buttonloading" class="cu-btn bg-green" >提交</button>
		</view>

    <yk-authpup ref="authpup" type="top" @changeAuth="$noMultipleClicks(save)" :permissionID="permissionID"></yk-authpup>
	</view>

</template>
<script>
	import {
		basePath,corpinfoId,loginUser,loginSession
	} from '@/common/tool.js';
	import gcoord from '@/common/gcoord.js'
  import ykAuthpup from "@/components/yk-authpup/yk-authpup"
	export default {
		data() {
			return {
        permissionID:'',
				noClick:true,
				buttonloading: false,
				listId: '',
        CHECK_ITEM: '',
				listName:'',
        baoBaoType:'',
				list: [],
				pd:[],
				CHECKRECORD_ID:'',
			}
		},
		onLoad(e){
      this.CHECK_ITEM = e.CHECK_ITEM
			this.listId=e.listId;
			this.listName=e.listName;
			this.baoBaoType=e.baoBaoType
			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/hiddenDangerCheckStandardCustom/goCheck', // /app/listmanager/goCheck
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type':'application/x-www-form-urlencoded'
					},
					data: {
						CUSTOM_ID: _this.listId,
            CHECK_ITEM: _this.CHECK_ITEM,
						USER_NAME : loginUser.NAME,
            CORPINFO_ID:loginUser.CORPINFO_ID,
            USER_ID:loginUser.USER_ID,
					},
					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].CUSTOM_ITEM_ID==res.data.records[j].CUSTOM_ITEM_ID){
											_this.list[i].ISNORMAL = res.data.records[j].ISNORMAL;
											_this.list[i].HIDDEN_ID = res.data.records[j].HIDDEN_ID;
                      _this.list[i].CHECKRECORD_ID = res.data.records[j].CHECKRECORD_ID;
											_this.list[i].RECORDITEM_ID = res.data.records[j].RECORDITEM_ID;
                      if (res.data.records[j].ISNORMAL === 0) {
                        _this.list[i].CHECK_QUALIFIED = res.data.records[j].CHECK_RESULT;
                      } else if (res.data.records[j].ISNORMAL === 1) {
                        _this.list[i].CHECK_UNQUALIFIED = res.data.records[j].CHECK_RESULT;
                      }
											// _this.list[i].is_checked = 1;
										}
									}
								}
							}
						} else {
							uni.showToast({
								title: res.data.message,
								duration: 2000
							});
						}
					}
				});
			},

      openAuth(permissionID){
        this.permissionID = permissionID;
        setTimeout(()=>{
          this.$refs['authpup'].open();
        },200)
      },

			//去保存
			save: function (){
        var _this = this;
        for(var i=0;i<_this.list.length;i++){
          if(this.list[i].ISNORMAL && this.list[i].OPERATION_TYPE === 2) {
            var CHECK_RESULT = ''
            if (this.list[i].ISNORMAL === 0 || this.list[i].ISNORMAL === 1) {
              CHECK_RESULT = _this.list[i].CHECK_RESULT
            } else if (this.list[i].ISNORMAL === 2) {
              CHECK_RESULT = "不涉及"
            }
            if (!CHECK_RESULT) {
              uni.showToast({
                icon: 'none',
                title: '第'+(i+1)+'项检查为填写检查，检查结果不能为空',
                duration: 1500
              });
              return;
            }
          }
        }
				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.CUSTOM_ITEM_ID = _this.list[i].CUSTOM_ITEM_ID

						item.ISNORMAL = _this.list[i].ISNORMAL
            if (_this.list[i].ISNORMAL === 0 || _this.list[i].ISNORMAL === 1) {
              item.CHECK_RESULT = _this.list[i].CHECK_RESULT
            } else {
              item.CHECK_RESULT = ''
            }
						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/customCheckRecord/add',  // /app/checkrecord/add'
							xhrFields: {
								withCredentials: true
							},
							method: 'POST',
							dataType: 'json',
							header: {
								'Content-type':'application/x-www-form-urlencoded'
							},
							data: {
								CUSTOM_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){
								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/customHidden/delete', // /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,
              loginUserName:loginUser.USER_NAME
						},
						success: function(res){
							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();//强制刷新
				}
        if (STATUS === 0) {
          _this.$set(_this.list[e.index], 'CHECK_RESULT', _this.list[e.index].CHECK_QUALIFIED)
        } else if (STATUS === 1) {
          _this.$set(_this.list[e.index], 'CHECK_RESULT', _this.list[e.index].CHECK_UNQUALIFIED)
        } else {
          _this.$set(_this.list[e.index], 'CHECK_RESULT', '不涉及')
        }
			},
			//跳转事件
			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-danger-check-standard/custom/list-item-check-hidden-form?'
              +'CHECK_CONTENT='+encodeURIComponent(JSON.stringify(e.CHECK_CONTENT.replace('%','%25')))
							+'&CUSTOM_ITEM_ID='+ encodeURIComponent(JSON.stringify(e.CUSTOM_ITEM_ID))
              +'&CHECKRECORD_ID='+ encodeURIComponent(JSON.stringify(e.CHECKRECORD_ID))
							+'&RECORDITEM_ID='+ encodeURIComponent(JSON.stringify(e.RECORDITEM_ID))
							+'&CUSTOM_ID='+ encodeURIComponent(JSON.stringify(e.CUSTOM_ID))
							+'&CHECK_CATEGORY='+ encodeURIComponent(JSON.stringify(e.CHECK_CATEGORY))
              +'&CHECK_CATEGORY_NAME='+ encodeURIComponent(JSON.stringify(e.CHECK_CATEGORY_NAME))
              +'&CHECK_ITEM='+ encodeURIComponent(JSON.stringify(e.CHECK_ITEM))
              +'&CHECK_ITEM_NAME='+ encodeURIComponent(JSON.stringify(e.CHECK_ITEM_NAME))
              +'&CHECK_STANDARD='+ encodeURIComponent(JSON.stringify(e.CHECK_STANDARD))
              +'&REFERENCE_BASIS='+ encodeURIComponent(JSON.stringify(e.REFERENCE_BASIS))
              +'&CHECK_RESULT='+ encodeURIComponent(JSON.stringify(e.CHECK_RESULT))
							+'&index='+e.index
              +'&baoBaoType='+this.baoBaoType
				});
			},
			//跳转事件
			goToEdit(e) {
				uni.navigateTo({
					url: '/pages/application/hidden-danger-check-standard/custom/list-item-check-hidden-form?HIDDEN_ID='+e
				});
			},
			//添加合格照片
			gotoAddImg(e) {
				uni.navigateTo({
					url: '/pages/application/hidden-danger-check-standard/custom/list-item-check-imgs?RECORDITEM_ID='+e
				});
			},
		}
	}
</script>

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

.bgh{
  background: #f8f6f6;
  color: #d9d7d7;
}

</style>
