<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="de-card-list de-list">
				<view class="de-item">
					<view class="de-sub-title">
						<text class="text-grey">申辩人</text>
					</view>
					<view class="de-sub-content">
						<scroll-view scroll-x class="bg-white nav" scroll-with-animation>
								<view class="imgs">
									<image :src="baseImgPath+form.INSPECTED_SITEUSER_SIGN_IMG" @click="ViewShowImage2" style="width: 180upx;height: 100upx"></image>
							</view>
						</scroll-view>
					</view>
				</view>
				<view class="de-item">
					<view class="de-sub-title">
						<text class="text-grey">申辩时间</text>
					</view>
					<view class="uni-flex-item text-right">
						<text class="text-semi">{{form.INSPECTED_SITEUSER_SIGN_TIME}}</text>
					</view>
				</view>
				<view class="de-item">
					<view class="de-sub-title">
						<text class="text-grey">申辩说明</text>
					</view>
					<view class="uni-flex-item text-right">
						<text class="text-semi">{{form.INSPECTED_EXPLAIN}}</text>
					</view>
				</view>
				<view v-if="form.INSPECTED_EXPLAIN_FILENAME" class="de-item-sp">
					<view class="de-sub-title">
						<text class="text-grey">申辩附件</text>
					</view>
					<view class="uni-flex-item text-right">
						<text class="text-semi">{{ form.INSPECTED_EXPLAIN_FILENAME }}</text>
					</view>
					<view class="uni-flex-item text-right">
						<text class="text-semi" @click="$noMultipleClicks(goToOpen,baseImgPath+form.INSPECTED_EXPLAIN_FILEPATH)">下载</text>
					</view>
				</view>
				<view class="de-item">
					<view class="de-sub-title">
						<text class="text-grey" style="white-space: nowrap">申辩是否成立</text>
					</view>
					<view style="display: flex">
            <view class="group mr20">
              <radio class='radio' :checked="INSPECTION_STATUS==='-2'" value="-2" @click="radioNormal('-2')"></radio>
              <text>是</text>
            </view>
            <view class="group">
              <radio class='radio' :checked="INSPECTION_STATUS==='3'" value="3" @click="radioNormal('3')"></radio>
              <text>否</text>
            </view>
          </view>
				</view>
				<view v-if="INSPECTION_STATUS==='3'" class="de-item-sp">
					<view class="de-sub-title">
						<text class="text-grey" style="white-space: nowrap">不成立理由</text>
					</view>
					<input name="input" ref="INSPECTION_PLACE" :disabled="forbidEdit" v-model="form.INSPECTED_EXPLAIN_REFUSE" placeholder="请输入理由"></input>
				</view>
				<view v-if="INSPECTION_STATUS==='3'" class="cu-form-group">
					<view class="de-sub-title">
						<text class="title">确认人</text>
					</view>
					<button class="cu-btn bg-green shadow" @tap="modalShow = true"  style="width:100px;height:30px;" data-target="Modal">手写签字</button>
				</view>
				<view v-if="INSPECTION_STATUS==='3'" class="cu-bar bg-white" v-show="signImgList && signImgList.length > 0">
					<view class="action">
						签字照片
					</view>
				</view>
				<view v-if="INSPECTION_STATUS==='3'" class="cu-form-group" v-show="signImgList && signImgList.length > 0">
					<view class="grid col-4 grid-square flex-sub">
						<view style="width: 60%;" class="bg-img" v-for="(item,index) in signImgList" :key="index" @tap="ViewImage" data-type="0" :data-url="signImgList[index].filePath">
							<image :src="signImgList[index].filePath" mode="aspectFill"></image>
							<view class="cu-tag bg-red" @tap.stop="delImg" data-type="0" :data-index="index">
								<text class='cuIcon-close'></text>
							</view>
						</view>
					</view>
				</view>
				<view class="cu-modal" :class="{'show':modalShow}">
					<sign @confirm="subCanvas" @cancel="modalShow = false"></sign>
				</view>

			</view>
			<view class="padding flex flex-direction">
				<button v-if="INSPECTION_STATUS==='3'" class="cu-btn bg-green margin-tb-sm lg" @click="$noMultipleClicks(goSubmit)">提 交</button>
				<button v-if="INSPECTION_STATUS==='-2'" class="cu-btn bg-green margin-tb-sm lg" @click="$noMultipleClicks(goToDetail,form.INSPECTION_ID)">确认</button>
			</view>

			<view class="cu-tabbar-height"></view>
		</scroll-view>
		<!--<view :class="['cu-modal',{'show':modalShow}]" v-if="modalShow">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">播放视频</view>
					<view class="action" @tap="modalShow = false">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<video :src="videoSrc" :autoplay="true"></video>
				<view class="cu-bar bg-white justify-end">
					<view class="action">
						<button class="cu-btn line-green text-green" @click="modalShow = false">关闭</button>
					</view>
				</view>
			</view>
		</view>-->
	</view>
</template>

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

	import sign from '@/components/sign/sign.vue';
	export default {
		 components: {
			sign
		},
		data() {
			return {
				noClick: true,
				date: '',
				RECTIFICATIONTIME: '',
				CHECK_TIME: '',
				imgList: [],
				id: '',
				INSPECTION_ID: '',
				pd: [],
				hs: [],
				files: [],
				files2: [],
				files3: [],
				files4: [],
				checkList: [],
				signImgList: [],
				CHECKDESCR: '',
				basePath:basePath,
				baseImgPath:baseImgPath,
				ISQUALIFIED:'1',
				hiddenVideo:[],
				hiddenFindPeople:[],
				form: {
					INSPECTION_ID:'',
					FINAL_CHECK: '1',
					FINAL_CHECKTIME: '',
					FINAL_CHECKDESCR: '',
					INSPECTION_ORIGINATOR_SIGN_TIME:'',
					INSPECTED_EXPLAIN_REFUSE:''
				},
				rules: [
					{ name:'FINAL_CHECKTIME', message: '请输入验收时间' },
					{ name:'FINAL_CHECKDESCR', message: '请输入验收描述' }
				],
				modalShow: false,
				videoSrc: '',
        		INSPECTION_STATUS:''
			}
		},
		onLoad(e) {
			console.log(e)
			this.form.INSPECTION_ID = e.INSPECTION_ID;
			// let now = new Date();
			// this.date = formatDate(now, 'yyyy-MM-dd');
			this.getData();
			loginSession();
		},
		// filters: {
		// 	formatDate(time) {
		// 		time = time * 1
		// 		let date = new Date(time)
		// 		return formatDate(date, 'yyyy-MM-dd hh:mm')
		// 	}
		// },
		methods: {
			getData() {
				var _this = this;
				uni.showLoading({
					title: '请稍候'
				})
				uni.request({
					url: basePath + '/app/safetyenvironmental/goEdit',
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						INSPECTION_ID: _this.form.INSPECTION_ID,
					},
					success: (res) => {
						if ("success" == res.data.result) {
							uni.hideLoading();
							_this.form = res.data.pd; //参数map
							_this.form.INSPECTED_EXPLAIN_FILEPATH = res.data.pd.INSPECTED_EXPLAIN_FILEPATH
							for (let i = 0; i < _this.form.inspectorList.length; i++) {
								_this.inspectorList.push([])
								_this.getUserList('inspectorList', _this.form.inspectorList[i].INSPECTION_DEPARTMENT_ID, i)
							}
							if (Number(_this.form.INSPECTION_STATUS) < 1) {
								_this.forbidEdit = false;
							}
						} else if ("exception" == data.result) {
							uni.showToast({
								title: '错误',
								duration: 2000
							});
						}
					}
				});
			},
			radioNormal(e){
				this.INSPECTION_STATUS = e
			},
			goToOpen(e) {
				var url=e;
				plus.runtime.openURL(url, function(res) {
					console.log(res);
				});


				// uni.navigateTo({
				// 	url: '/pages/application/safety-production-account/organization-personnel/web-view?link=' + url
				// });
			},
			//图片上传
			ChooseImage() {
				var _this = this;
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera', 'album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			goToDetail(e) {
				uni.navigateTo({
					url: '/pages/application/safety-environmental-inspection/apply/detail?vector=go&TYPE=edit&INSPECTION_ID='+e
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			delImg(e) {
				uni.showModal({
					title: '新泰安全',
					content: '确定要删除这张图片吗？',
					cancelColor: "#000000",
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.signImgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			subCanvas (e) {
				this.signImgList.splice(0, this.signImgList.length);
				this.signImgList.push(e);
				this.$set(this.form, 'INSPECTION_ORIGINATOR_SIGN_TIME', formatDate(new Date(), 'yyyy-MM-dd hh:mm'))
				this.modalShow = false;
			},
			textareaAInput(e) {
				this.CHECKDESCR = e.detail.value
			},
			goSubmit() {
				var _this = this;
				let required = true
				if (!required) {
					return
				}
				if (this.INSPECTION_STATUS === '3' && this.form.INSPECTED_EXPLAIN_REFUSE == '') {
					uni.showToast({
						icon: 'none',
						title: '请填写理由',
						duration: 1500
					});
					return;
				}
				if (this.signImgList.length === 0) {
					uni.showToast({
						icon: 'none',
						title: '请签字',
						duration: 1500
					});
					return;
				}
				uni.showLoading({
					title: '请稍候'
				})
				this.$set(this.form, 'OPERATOR', loginUser.USER_ID)
				this.$set(this.form, 'ACTION_USER', loginUser.NAME)
				this.$set(this.form, 'INSPECTION_STATUS',this.INSPECTION_STATUS)
				var _this = this;
				uni.showLoading({
					title: '请稍候'
				})
				uni.uploadFile({
					url: basePath + "/app/safetyenvironmental/explain",
					filePath: _this.signImgList[0].filePath,
					name: 'FFILE',
					formData: this.form,
					success: (res) => {
						uni.showToast({
							icon: 'none',
							title: '保存成功',
							duration: 2000
						});
						_this.goback()
					},
					fail: (err) => {
						uni.hideLoading();
						uni.showModal({
							content: err.errMsg,
							showCancel: false
						});
					}
				})			},
			uploadImg(tempFilePaths,i) {
				return new Promise((resolve, reject) => {
					var _this = this;
					if(tempFilePaths.length==0){
						resolve();
					}
					uni.showLoading({
						title: '上传中'
					})
					uni.uploadFile({
						url: basePath + '/app/imgfiles/add',
						filePath: tempFilePaths[i],
						name: 'FFILE',

						formData: {
							'FOREIGN_KEY': _this.id,
							'TYPE': '50'
						},
						success: (res) => {
							i++;
							if (tempFilePaths.length > i) {
								_this.uploadImg(tempFilePaths, i);
							} else {
								uni.hideLoading();
								resolve();
							}
						},
						fail: (err) => {
							uni.hideLoading();
							console.log('uploadImage fail', err);
							uni.showModal({
								content: err.errMsg,
								showCancel: false
							});
						}
					})
				})
			},
			changeDate(e) {
				this.form.FINAL_CHECKTIME = e.detail.value
			},
			radio(e){
				this.ISQUALIFIED = e
			},
			goback(){
				var pages = getCurrentPages();	// 获取当前页面栈
				var prePage = pages[pages.length - 2];	// 上二级页面
				prePage.$vm.initflag = true;    // A 页面 init方法 为true
				uni.navigateBack({delta: 1});
				uni.hideLoading();
			},
			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
				});
			},
			ViewShowImage2(e) {
        let fullPath = this.baseImgPath + this.form.INSPECTED_SITEUSER_SIGN_IMG;
        uni.previewImage({
          urls: [fullPath], // 确保这里是一个数组，即使只有一个图片路径
          current: fullPath // 当前显示图片的http链接
        });
			},

			ViewShowImage4(e) {
				let files = [];
				for (var i = 0; i < this.files4.length; i++) {
					files.push(baseImgPath+this.files4[i].FILEPATH)
				}
				uni.previewImage({
					urls: files,
					current: e.currentTarget.dataset.index
				});
			},
      playVideo(e) {
        this.videoSrc = e.currentTarget.dataset.src
        this.modalShow = true
      }
		}
	}
</script>

<style>
	.bb {
		border-top: 1upx solid #f1f1f1;
		border-bottom: 1upx solid #f1f1f1;
	}

	.cu-bar .action:first-child {
		font-size: 26upx
	}

	.cu-form-title {
		padding: 20upx 0;
	}

	.cu-form-textarea {
		background-color: #ffffff;
		padding: 1upx 30upx 20upx;
		min-height: 100upx;
	}

	.cu-form-textarea textarea {
		height: 4.6em;
		width: 100%;
		line-height: 1.2em;
		flex: 1;
		font-size: 28upx;
		padding: 0;
	}

	.cu-form-group {
		padding: 0upx 25upx;
	}

	.cu-form-group picker .picker {
		line-height: 76upx;
	}

	.cu-form-group .title {
		font-size: 28upx;
		line-height: 76upx;
		color: #888;
	}

	.cu-form-group uni-picker::after {
		top: -11upx;
	}

	.selected {
		display: flex;
		align-items: center;
	}

	.selected .radio {
		transform: scale(0.5);
		margin-right: 10upx;
	}

	.cu-btn {
		border-radius: 0;
		width: 100%;
		height: 84upx;
		font-weight: bold;
	}

	.group {
		display: flex;
		align-items: center;
	}
  .imgs{
    width: 80upx;
    height: 80upx;
  }
</style>
