<template>
	<view class="appWrapper">
		<form action="">
			<view style="height:30rpx"></view>
			
			<view class="form-item">
				<view class="title">
					<text class="necessary">*</text>
					监察标题:
				</view>
				<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.site_name 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">
					<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>

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

	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgList: [],
				
				supervision_title:'',
				site_id:'',
				supervision_content:'',
				
				titleListData:[],
				siteListData:[]
			}
		},
		onLoad: function(option) {
			this.getTitleList();
			this.getSiteList()
		},
		methods: {
			
			//新增提交
			async addSubmit() {
				if (!this.supervision_title) {
					uni.showToast({
						title: "请选择监察标题",
						icon: "none"
					});
					return
				}
				if (!this.site_id) {
					uni.showToast({
						title: "请选择站点",
						icon: "none"
					});
					return
				}
				if (!this.supervision_content) {
					uni.showToast({
						title: "请输入监察内容",
						icon: "none"
					});
					return
				}
				if (!this.imgList.length>0) {
					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":'ss'
				})
			
				if (!res.data.flag) {
					uni.showToast({
						title: "添加失败",
						icon: "none"
					});
				} else {
					uni.showToast({
						title: "添加成功",
					});
				}
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/authManage/authManage',
					});
				}, 1000);
			
			},
			addReport(params = {}) {
				return this.$myRequest({
					url: 'MonitoringReporting/addMonitoringReporting',
					data: params
				})
			},
			
			
			//监察标题下拉数据请求
			async getTitleList(params = {}) {
				const res = await this.$myRequest({
					url: 'Archives/getsupervisionBox',
					showLoading: true,
					data: params
				})
				
				this.titleListData = res.data.data
			},
			
			//站带下拉数据请求
			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.navigateTo({
					url: '/pages/accountManage/success/success',
				});
			},
		}
	}
</script>

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