<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" v-for="(item, index) in info" :key="item.EQUIPMENT_RECORD_ID">
				<view class="title">消防区域: {{item.FIRE_REGION_NAME}}</view>
				<view class="title">消防点位:{{item.FIRE_POINT_NAME}}</view>
				<view class="title">器材类型:{{item.dicName}}</view>
				<view class="title">器材编码:{{item.FIRE_DEVICE_CODE}}</view>
				<view class="title">消防器材检查标准:</view>
				<view class="dy-card" v-for="(item, index) in item.fireDeviceTypeList" :key="item">
					<view class="title">{{ index + 1 }}、{{ item }}</view>
				</view>
				<view class="check-items-select">
					<radio-group class="block">
						<view class="flex justify-between padding-sm">
							<view class="select">
								<view @click="checkTrue(item)" 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="gotoAddImg"
									class="cuIcon-infofill text-blue unqualified"></view> -->
							</view>
							<view class="select">
								<view class="flex align-center" @click="goToAdd(item,index)">
									<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="goToEdit(item,index)"
									class="cuIcon-infofill text-blue unqualified"></view>
							</view>
						</view>
					</radio-group>
				</view>
			</view>
		</scroll-view>
		<view v-if="files.length > 0" class="check-items">
			<view class="title" style="font-size: 18px;">点位检查标准合格照片</view>
			<!-- <scroll-view scroll-x class="bg-white nav" style="margin-top: 20px;" scroll-with-animation>
				<view class="cu-item" v-for="(item,index) in files" v-bind:key="index">
					<view class="imgs">
						<image :src="baseImgPath+item.FILEPATH" :data-index="index" @click="ViewShowImage" mode="">
						</image>
					</view>
				</view>
			</scroll-view> -->
			<swiper class="swiper" style="height: 270px;margin-left: 8px; margin-top: 10px;" circular
				indicator-dots="ture">
				<swiper-item v-for="(item,index) in files" v-bind:key="index">
					<image :src="baseImgPath+item.FILEPATH" :data-index="index" @click="ViewShowImage" mode="aspectFit">
				</swiper-item>
			</swiper>
		</view>

		<view>

		</view>
		<view class="cu-tabbar-height"></view>
		<view v-if="this.ISLOG == '0'" class="bottom-fixed" @click="$noMultipleClicks(save)">
			<button class="cu-btn bg-green">提交</button>
		</view>
		<view v-else class="bottom-fixed" @click="$noMultipleClicks(save)">
			<button class="cu-btn bg-green">查看点位图片</button>
		</view>
	</view>

</template>
<script>
	import {
		basePath,
		corpinfoId,
		loginUser,
		loginSession,
		baseImgPath
	} from '@/common/tool.js';

	export default {
		data() {
			return {
				baseImgPath,
				files: [],
				noClick: true,
				FIRE_CHECK_STANDARD_ID: '',
				FIRE_DEVICE_ID: '',
				EQUIPMENT_RECORD_ID: '',
				FIRE_POINT_ID: '',
				EQUIPMENT_POINT_ID: '',
				FIRE_PREPARED_ID: '',
				FIRE_RECORD_ID: '',
				FIRE_CHECK_ID: '',
				ISLOG: '',
				FIRE_DEVICE_TYPE_ID: '', //消防器材类型编码
				fireDeviceTypeList: [],
				info: [],
				isTongyong: true,
			}
		},
		onLoad(e) {
			this.FIRE_DEVICE_TYPE_ID = e.FIRE_DEVICE_TYPE_ID
			this.FIRE_DEVICE_ID = e.FIRE_DEVICE_ID
			this.FIRE_RECORD_ID = e.FIRE_RECORD_ID
			this.EQUIPMENT_POINT_ID = e.EQUIPMENT_POINT_ID
			this.EQUIPMENT_RECORD_ID = e.EQUIPMENT_RECORD_ID
			this.FIRE_POINT_ID = e.FIRE_POINT_ID
			this.info.EQUIPMENT_RECORD_ID = e.EQUIPMENT_RECORD_ID
			this.FIRE_CHECK_ID = e.FIRE_CHECK_ID
			this.ISLOG = e.ISLOG
			if (corpinfoId == 'ffbf90f7064340ae9d56c16d4f560423') {
				this.isTongyong = false;
			}
			loginSession();
			this.getData();
			this.getPointQualifiedPhotos();
			// this.getDeviceData()
		},
		onShow() {
			var _this = this;
			if (_this.initflag) {
				_this.initflag = false;
				_this.$forceUpdate(); //强制刷新
			}

			// this.getFireDeviceType();
		},
		methods: {
			ViewShowImage(e) {
				let files = [];
				for (var i = 0; i < this.files.length; i++) {
					files.push(baseImgPath + this.files[i].FILEPATH)
				}
				uni.previewImage({
					urls: files,
					current: e.currentTarget.dataset.index
				});
			},
			getData() { // 获取这个 消防器材 是否有隐患
				var _this = this;
				uni.showLoading({
					title: '请稍候'
				})
				var bashurl = '/app/fireRecordManger/goRecordDeviceByPid'
				if (this.ISLOG == '0') {
					bashurl = '/app/fireRecordManger/goRecordDeviceByEid'
				}
				uni.request({
					url: basePath + bashurl,
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						EQUIPMENT_RECORD_ID: _this.EQUIPMENT_RECORD_ID, //检查记录id
						FIRE_RECORD_ID: _this.FIRE_RECORD_ID, //检查记录id
						FIRE_CHECK_ID: _this.FIRE_CHECK_ID, //检查记录id
						FIRE_POINT_ID: _this.FIRE_POINT_ID, //检查记录id
						USER_ID: loginUser.USER_ID,
						CORPINFO_ID: loginUser.CORPINFO_ID
					},
					success: (res) => {
						console.log(res)
						if ("success" == res.data.result) {
							uni.hideLoading();
							_this.info = res.data.varData
							// _this.info.FIRE_REGION_NAME = res.data.varData[0].FIRE_REGION_NAME;
							// _this.info.FIRE_POINT_NAME = res.data.varData[0].FIRE_POINT_NAME;
							// _this.info.FIRE_DEVICE_TYPE_NAME = res.data.varData[0].dicName;
							// _this.info.FIRE_DEVICE_CODE = res.data.varData[0].FIRE_DEVICE_CODE; //
							// _this.info.HIDDEN_ID = res.data.varData[0].HIDDEN_ID;
							// _this.info.HASHIDDEN = res.data.varData[0].HASHIDDEN;
							// _this.$set(_this.info, 'ISNORMAL', res.data.varData[0].ISNORMAL)
						} else {
							uni.showToast({
								title: res.message,
								duration: 2000
							});
						}
					}
				});
			},
			getPointQualifiedPhotos() {
				var _this = this;
				uni.request({
					url: basePath + '/app/fireRecordManger/getPointQualifiedPhotos',
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						FIRE_POINT_ID: _this.FIRE_POINT_ID,
						EQUIPMENT_POINT_ID: _this.EQUIPMENT_POINT_ID, //检查记录id
						ISLOG: _this.ISLOG
					},
					success: (res) => {
						if ("success" == res.data.result) {
							_this.files = res.data.varList;
						} else {
							uni.showToast({
								title: res.data.message,
								duration: 2000
							});
						}
					}
				});
			},
			getFireDeviceType() {
				var _this = this;
				uni.showLoading({
					title: '请稍候'
				})
				uni.request({
					url: basePath + '/app/fireRecordManger/getStandardListByType',
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						FIRE_DEVICE_TYPE_LIST: _this.FIRE_DEVICE_TYPE_ID,
					},
					success: (res) => {
						if ("success" == res.data.result) {
							uni.hideLoading();
							_this.fireDeviceTypeList = res.data.varList;
						} else {
							uni.showToast({
								title: res.data.message,
								duration: 2000
							});
						}
					}
				});
			},
			//去保存
			async save() {

				var _this = this;
				if (_this.ISLOG == 1) {
					_this.gotoAddImg()
				} else {
					uni.showLoading({
						title: '请稍候'
					})
					for (var i = 0; i < _this.info.length; i++) {
						let item = _this.info[i]
						if (item.ISNORMAL == undefined || item.ISNORMAL == '2') {
							if (!item.HIDDEN_ID) {
								uni.hideLoading()
								uni.showToast({
									title: '此检查内容未排查完成,无法提交',
									icon: 'none'
								})
								return
							}
						}
						if (item.ISNORMAL == 1) {
							if (!item.HIDDEN_ID) {
								uni.hideLoading()
								uni.showToast({
									title: '请上传隐患信息',
									icon: 'none'
								})
								return
							}
						}
						if (item.HIDDEN_ID) {
							let params = {
								HIDDEN_ID: item.HIDDEN_ID,
								HIDDENDESCR: ''
							}
							uni.request({
								url: basePath + '/app/hidden/goEdit',
								method: 'POST',
								dataType: 'json',
								header: {
									'Content-type': 'application/x-www-form-urlencoded'
								},
								data: {
									HIDDEN_ID: item.HIDDEN_ID,
								},
								success: (res) => {
									if ("success" == res.data.result) {
										uni.hideLoading();
										_this.info[i].HIDDENDESCR = res.data.pd.HIDDENDESCR
									} else if ("exception" == res.data.result) {
										uni.showToast({
											title: '错误',
											duration: 2000
										});
									}
								}
							});
						}
					}
					uni.request({
						url: basePath + '/app/fireRecordManger/editDeviceCheck',
						xhrFields: {
							withCredentials: true
						},
						method: 'POST',
						dataType: 'json',
						header: {
							'Content-type': 'application/x-www-form-urlencoded'
						},
						data: {
							CHECK_DATA: JSON.stringify(_this.info),
							EQUIPMENT_POINT_ID: this.EQUIPMENT_POINT_ID
						},
						success: function(res) {
							if ("noSuccessImg" == res.data.result) {
								uni.showToast({
									title: '请上传合格图片',
									icon: 'none'
								})
								return
							}
							if ("success" == res.data.result) {
								setTimeout(function() {
									// var pages = getCurrentPages(); // 获取当前页面栈
									// var prePage = pages[pages.length - 2]; // 上一个页面
									// prePage.$vm.initflag = true; // A 页面 init方法 为true
									// uni.navigateBack({});
									// uni.hideLoading();
									// 去添加照片
									_this.gotoAddImg()
								}, 1500);
							} else {
								uni.showToast({
									title: "系统错误",
									duration: 2000
								});
							}
						}
					})
					return
				}

			},
			// 不涉及
			checkNotInvolved() {
				if (this.info.HASHIDDEN > 0) {
					uni.showToast({
						icon: 'none',
						title: "此检查内容存在未处理隐患,无法检查",
						duration: 1000
					});
					return;
				}
				let _this = this;
				if (_this.info.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.info.HIDDEN_ID
						},
						success: function(res) {
							if ("success" == res.data.result) {
								_this.info.ISNORMAL = 2;
								_this.info.HIDDEN_ID = '';
								_this.$forceUpdate(); //强制刷新
							} else {
								uni.showToast({
									title: "系统错误",
									duration: 2000
								});
							}
						}
					})
				} else {
					_this.info.ISNORMAL = 2;
					_this.info.HIDDEN_ID = '';
					_this.$forceUpdate(); //强制刷新
				}
			},
			//点击合格
			checkTrue(item) {
				// 记录纯查看
				if (this.ISLOG == '1') {
					return
				}
				if (item.HASHIDDEN > 0) {
					uni.showToast({
						icon: 'none',
						title: "此检查内容存在未处理隐患,无法检查",
						duration: 1000
					});
					return;
				}
				if (item.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: item.HIDDEN_ID
						},
						success: function(res) {
							if ("success" == res.data.result) {
								item.ISNORMAL = 0;
								item.HIDDEN_ID = '';
								_this.$forceUpdate(); //强制刷新
							} else {
								uni.showToast({
									title: "系统错误",
									duration: 2000
								});
							}
						}
					})
				} else {
					item.ISNORMAL = 0;
					item.HIDDEN_ID = '';
					this.$forceUpdate(); //强制刷新
				}
			},
			//跳转事件
			goToAdd(item, index) {
				console.log('add')
				if (this.ISLOG == '1') {
					// 记录纯查看 并且当前是合格
					if (item.ISNORMAL == 0) {
						return
					}
				}

				if (item.HASHIDDEN > 0 && this.ISLOG == '0') {
					uni.showToast({
						icon: 'none',
						title: "此检查内容存在未处理隐患,无法检查",
						duration: 1000
					});
					return;
				}

				if (item.ISNORMAL == 1) {
					return;
				}
				item.ISNORMAL = 1
				uni.navigateTo({
					url: '/pages/application/fire-management/hidden-trouble-app/exclude-form?' +
						'LISTMANAGER_ID=' + this.FIRE_CHECK_ID +
						'&RECORDITEM_ID=' + this.FIRE_CHECK_STANDARD_ID +
						'&RISKITEM_ID=' + this.FIRE_CHECK_ID +
						'&FIRE_PREPARED_ID=' + item.FIRE_PREPARED_ID +
						'&HIDDEN_ID=' + item.HIDDEN_ID +
						'&INDEX=' + index +
						'&FIRE_DEVICE_TYPE_ID=' + item.FIRE_DEVICE_TYPE_ID +
						'&ISLOG=' + this.ISLOG
				});
			},
			//跳转事件
			goToEdit(item, index) {
				console.log(item.HASHIDDEN)
				if (item.HASHIDDEN == 1) {
					uni.showToast({
						icon: 'none',
						title: "此检查内容存在未处理隐患",
						duration: 1000
					});
					return;
				}
				uni.navigateTo({
					url: '/pages/application/fire-management/hidden-trouble-app/exclude-form?HIDDEN_ID=' +
						item.HIDDEN_ID +
						'&FIRE_DEVICE_TYPE_ID=' + item.FIRE_DEVICE_TYPE_ID +
						'&ISLOG=' + this.ISLOG +
						'&INDEX=' + index

				});
			},
			//添加合格照片
			gotoAddImg() {
				uni.navigateTo({
					url: '/pages/application/fire-management/hidden-trouble-app/check-items-imgs?EQUIPMENT_POINT_ID=' +
						this.EQUIPMENT_POINT_ID +
						'&ISLOG=' + this.ISLOG
				});
			},
			getHegeImgCount(FOREIGN_KEY) {
				console.log('hhhhh')
				return new Promise((resolve, reject) => {
					var _this = this;
					uni.showLoading({
						title: '请稍候'
					})
					uni.request({
						url: basePath + '/app/imgfiles/listImgs',
						method: 'POST',
						dataType: 'json',
						header: {
							'Content-type': 'application/x-www-form-urlencoded'
						},
						data: {
							FOREIGN_KEY: FOREIGN_KEY,
							TYPE: 14
						},
						success: (res) => {
							if ("success" == res.data.result) {
								uni.hideLoading();
								if (res.data.imgs) {
									if (res.data.imgs.length > 0) {
										console.info('11')
										return res.data.imgs.length
									}
								}
							} else if ("exception" == data.result) {
								uni.showToast({
									title: '错误',
									duration: 2000
								});
								return 0
							}
							return 0
						}
					});
					return 0
				})

			},
		}
	}
</script>

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