<template>
	<view class="appWrapper">
		<form action="">
			<view style="height:30rpx"></view>

			<view class="form-item">
				<view class="title">
					<text class="necessary">*</text>
					监察标题:
				</view>
				<input type="text" v-model="supervision_title" required>
				<!-- <select name="" id="" filterable clearable v-model="supervision_title">
					<option value="">请选择</option>
					<option :value=item.supervision_name v-for="item in titleListData">{{ item.supervision_name}}</option>
				</select> -->
			</view>

			<view class="form-item">
				<view class="title"><text class="necessary">*</text>所属站点:</view>
				<select name="" id="" filterable clearable v-model="site_id">
					<option value="">请选择</option>
					<option :value=item.id v-for="item in siteListData">{{ item.site_name}}</option>
				</select>
			</view>

			<view class="form-item">
				<view class="title"><text class="necessary">*</text>监察内容:</view>
				<textarea maxlength="-1" width="250" v-model="supervision_content"></textarea>
			</view>

	
			<view class="form-item">
				<view class="title"><text class="necessary">*</text>上传图片:</view>
				<view style="width:500rpx">
					
					<!-- <uni-group title="只选择图片">
						<uni-file-picker limit="1"></uni-file-picker>
					</uni-group> -->
					
					
					<view ref="input" class="input">
					</view>

					<!-- <view class="cu-form-group" style="padding:0">
						<view class="grid col-4 grid-square flex-sub">
							<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage"
								:data-url="imgList[index]">
								<image :src="imgList[index]" mode="aspectFill"></image>
								<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
									<text class='cuIcon-close'></text>
								</view>
							</view>
							<view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
								<text class='cuIcon-cameraadd'></text>
							</view>
						</view>
					</view> -->
				</view>
			</view>
			<br>
			<view style="height:20rpx;background:#EDEDED"></view>
			<view class="form-item justify-between" style="border-bottom:1px solid #EDEDED;padding:20rpx 30rpx" @tap="goArchiveDetail()">
				<view  >现场档案</view>
				<view><text class="icon iconfont margin-left-xs ">&#xe629;</text></view>
			</view>

			<view class="btn-area submitBottomBtn padding-lr-sm">
				<button class="bg-blue round  margin-top" @click="$noMultipleClicks(addSubmit)">提 交 </button>
			</view>
		</form>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				noClick:true,
				
				imgList: [],

				supervision_title: '',
				site_id: '',
				supervision_content: '',
				msg:'', 
				img:'',

				titleListData: [],
				siteListData: [],
			}
		},
		onLoad: function(option) {
			this.getSiteList();
			
			 
		},
		mounted(){
			// 创建附件上传
			var _self = this;
			var input = document.createElement('input');//创建元素
			input.type = 'file'//添加file类型
			input.accept="image/gif,image/jpeg,image/jpg,image/png,image/svg"
			input.onchange = (event) => {
				// alert(1)
			 _self.upFile(input, event)
			}
			this.$refs.input.$el.appendChild(input)
			
		},
		methods: {
			
			
			goArchiveDetail(){
				if(this.site_id){
				
					uni.navigateTo({
						url: '/pages/siteArchive/archiveDetail/archiveDetail?siteId='+this.site_id+'&mark=control',
					});
				}else{
					uni.showToast({
						title: "请先选择所属站点",
						icon: "none"
					});
					
				}
				
			},
			upFile(input, event) {
			 var _self = this;
			 
			 uni.uploadFile({
			  url: _self.$BASE_URL+'MonitoringReporting/setUpload',
			 
			  files: [
			   {
			    file: input.files[0],
			    uri: event.srcElement.value
			   }
			  ],



			  success: (uploadFileRes) => {
			   var data=JSON.parse(uploadFileRes.data)
				if(data.flag){
					 this.img=data.img_url
				}else{
					uni.showToast({
						title: data.msg,
						icon: "none"
					});					
				}
			    // alert('上传成功')
				// alert(data.img_url)
			   // console.log('data')
			   // console.log(data.img_url)
			   // console.log('uploadFileRes')
			  
			  },
			  fail: (err) => {
				  // alert('上传失败')
				  
			   // alert(err)
			   console.log(err)
			   //  console.log('err'+err)
			   // console.log('err.errMsg'+errMsg)
			  }
			 });
			},
		
			//新增提交
			async addSubmit() {
				
				if (!this.supervision_title.replace(/^\s*/g,'')) {
					uni.showToast({
						title: "请输入监察标题",
						icon: "none"
					});
					return
				}
				if (!this.site_id) {
					uni.showToast({
						title: "请选择站点",
						icon: "none"
					});
					return
				}
				if (!this.supervision_content.replace(/^\s*/g,'')) {
					uni.showToast({
						title: "请输入监察内容",
						icon: "none"
					});
					return
				}
				if (!this.img) {
					uni.showToast({
						title: "请上传图片",
						icon: "none"
					});
					return
				}
				
				// if (!this.msg ) {
				// 	uni.showToast({
				// 		title: "请上传图片",
				// 		icon: "none"
				// 	});
				// 	return
				// }
				
				let res = await this.addReport({
					"supervision_title": this.supervision_title,
					"site_id": this.site_id,
					"supervision_content": this.supervision_content,
					// "img":this.imgList[0]
					"img_url":this.img
				})

				if (!res.data.flag) {
					uni.showToast({
						title: "添加失败",
						icon: "none"
					});
				} else {
					uni.showToast({
						title: "添加成功",
					});
				}
				setTimeout(() => {
					uni.redirectTo({
						url: '/pages/eleControl/controlReport/controlReport',
					});
				}, 1000);

			},
			addReport(params = {}) {
				return this.$myRequest({
					url: 'MonitoringReporting/addMonitoringReporting',
					data: params
				})
			},

			//站带下拉数据请求
			async getSiteList(params = {}) {
				const res = await this.$myRequest({
					url: 'BlackoutPlan/getSiteBox',
					showLoading: true,
					data: params
				})
				this.siteListData = res.data.data
			},

			ChooseImage() {
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},

			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '删除',
					content: '确定要删除此图片吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},

			submit() {
				uni.redirectTo({
					url: '/pages/accountManage/success/success',
				});
			},
		}
	}
</script>

<style lang="scss">
	select {
		background-size: 6%;
	}
</style>
