<!--
*危险区现场巡查
*
-->
<template>
	<view class="container">
		<uni-nav-bar dark :fixed="true" backgroundColor="#3F9EFF" statusBar="false" left-icon="left" left-text="返回"
			@clickLeft="toBack">
			<view class="nav-title">
				<text>{{title}}</text>
			</view>
		</uni-nav-bar>
		<uni-forms ref="baseForm" :model="formData">
			<uni-card title="定位信息" :is-shadow="false">
				<view class="view-flex-inline" style="margin-bottom: 10px;">
					<view style="width: 100px;">经纬度</view>
					<view v-if="longitude != '' && latitude != ''">{{longitude}},{{latitude}}</view>
				</view>
				<view style="margin-bottom: 5px;font-size: 0.6rem;color:skyblue;">
					备注：定位地址可进行人工修改</view>
				<uni-forms-item name='lng' label="定位地址" label-width="100px">
					<uni-easyinput v-model="formData.address" placeholder="请录入定位地址" />
				</uni-forms-item>
			</uni-card>
			<uni-card title="巡查反馈" :is-shadow="false">
				<uni-forms-item name="isFlood" label="是否有山洪" label-width="100px" required>
					<uni-data-checkbox v-model="formData.isFlood" :localdata="checkItems.items">
					</uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item name="isDamage" label="是否成灾" label-width="100px" required>
					<uni-data-checkbox v-model="formData.isDamage" :localdata="checkItems.items">
					</uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item name='remark' label="备注" label-width="100px">
					<uni-easyinput v-model="formData.remark" placeholder="请录入其他需要说明的情况" />
				</uni-forms-item>
			</uni-card>
			<uni-card title="巡查图片" extra="点击下方按钮开始拍摄" :is-shadow="false">
				<view class="view-flex-inline">
					<view v-for="(item, index) in checkPhotos" :key="index" class="view-flex-block-center"
						style="width: 100px;">
						<view class="img-container">
							<view class="note-image-box">
								<view class="note-image-item">
									<view class="close-icon" @click="onCheckPhotoDel(index)">
										<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
									</view>
									<view class="image-box">
										<image :src="toOss(item)" mode="widthFix">
										</image>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="view-flex-block-center" style="width: 80px;">
						<view class="img-container">
							<view class="note-image-box">
								<view class="note-image-item" @click="onCheckPhotoAdd()">
									<view class="image-box">
										<uni-icons type="plusempty" size="50" color="#eee"></uni-icons>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</uni-card>
		</uni-forms>
		<view class="footer">
			<view class="control">
				<view class="view-flex-inline-center">
					<view class="block"
						style="width: 120px;height:28px;margin-left: 10px;margin-right: 10px;border-radius:7px;background-color:lightpink"
						@click="toSubmit()">
						<view class="view-flex-block-center">
							<uni-icons class="input-uni-icon" type="checkmarkempty" size="18" color="#999" />
						</view>
						<view class="view-flex-block-center" style="padding-left:5px;">
							<text style="color:black;font-size:0.7rem">立即提交</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		role
	} from "@/api/role.js";
	import http from '@/http/api.js';
	import {
		oss,
		devUrl,
		prodUrl
	} from '@/common/setting';
	export default {
		data() {
			return {
				title: '现场巡查',
				baseURL: '',
				baseOSS: '',
				checkType: 0,
				warnId: '',
				latitude: '',
				longitude: '',
				formData: {
					address: '',
					isFlood: 0,
					isDamage: 0,
					remark: '',
				},
				checkPhotos: [],
				checkItems: {
					items: [{
						'value': 0,
						'text': '否'
					}, {
						'value': 1,
						'text': '是'
					}],
					current: 0,
				},
			}
		},
		onLoad(option) {
			let that = this;
			this.baseOSS = oss;
			this.baseURL = process.env.NODE_ENV === 'development' ? devUrl : prodUrl;
			this.checkType = option.type;
			this.warnId = option.id;
			// #ifdef APP-PLUS
			console.log("app")
			uni.getLocation({
				type: 'gcj02',
				geocode: true,
				success: function(res) {
					console.log('当前位置：' + JSON.stringify(res));
					that.latitude = res.latitude;
					that.longitude = res.longitude;
					//that.getAddress(res.longitude;,res.latitude);
				}
			});
			// #endif
			// #ifdef MP-WEIXIN
			console.log("weixin")
			uni.getLocation({
				type: 'wgs84',
				success: function(res) {
					console.log('当前位置：' + JSON.stringify(res));
					that.latitude = res.latitude;
					that.longitude = res.longitude;
					//that.getAddress(res.longitude;,res.latitude);
				}
			});
			// #endif
		},
		computed: {

		},
		onShow() {

		},
		methods: {
			toOss(path) {
				let url = this.baseOSS + path;
				return url;
			},
			//返回上一页
			toBack() {
				uni.navigateBack({
					delta: 1
				})
			},
			onBackPress() {
				// #ifdef APP-PLUS
				plus.key.hideSoftKeybord();
				// #endif
			},
			getAddress(lng, lat) {
				console.log("get address " + lat + lng);
				let url = 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + lat + ',' + lng +
					'&key=4HYBZ-EB23D-SLC42-HQ5R3-LP3LQ-OZFU5';
				console.log(url);
				http.request({
					url: url,
					method: 'GET',
				}).then(res => {
					console.log('当前位置：' + JSON.stringify(res));

				}).catch(err => {
					console.log(err)
				})
			},
			onCheckPhotoAdd() {
				let that = this;
				uni.chooseImage({
					sourceType: ['camera'],
					sizeType: ['compressed'],
					success: (res) => {
						const len = res.tempFilePaths.length;
						if (len === 1) {
							res.tempFilePaths.forEach(path => {
								that.uploadCheckPhoto(path);
							})
						} else {
							uni.showModal({
								content: '只能选择一张图片，请确认！',
								showCancel: false
							});
						}
					}
				})
			},
			uploadCheckPhoto(imagePath) {
				let that = this;
				uni.showLoading({
					title: '上传中'
				});
				uni.uploadFile({
					url: this.baseURL + '/galaxy-resource/oss/endpoint/put-file-attach?Blade-Auth=' +
						uni.getStorageSync('accessToken'),
					fileType: 'image',
					filePath: imagePath,
					name: 'file',
					success: (uploadFileRes) => {
						if (uploadFileRes.statusCode == 200) {
							let data = JSON.parse(uploadFileRes.data);
							if (data.success) {
								let path = data.data['name'];
								that.checkPhotos.push(path);

							}
						}
					},
					fail: (err) => {
						console.log(err);
						reject('err')
					},
					complete() {
						uni.hideLoading()
					}
				});
			},
			onCheckPhotoDel(index) {
				this.checkPhotos.splice(index, 1);
			},
			toSubmit() {
				let that = this;
				this.$refs.baseForm.validate().then(res => {
					console.log('表单数据信息1：', res);
					that.submit();
				}).catch(err => {
					console.log('表单错误信息：', err);
					uni.showModal({
						content: "巡查内容录入错误，请根据提示信息检查录入内容!",
						showCancel: false,
						success(res) {
							if (res.confirm) {
								//that.$refs.baseForm.clearValidate();
							}
						}
					});
				})
			},
			submit() {
				let formdata = this.formData;
				formdata['checkType'] = this.checkType;
				formdata['warnId'] = this.warnId;
				formdata['longitude'] = this.longitude;
				formdata['latitude'] = this.latitude;
				if (this.checkPhotos.length > 0) {
					let photos = '';
					for (let i = 0; i < this.checkPhotos.length; i++) {
						if (photos.length > 0) {
							photos = photos + ',';
						}
						photos = photos + this.checkPhotos[i];
					}
					formdata['checkPhotos'] = photos;
				}
				console.log('toSubmit事件:', JSON.stringify(formdata));
				let that = this;
				http.request({
					url: '/galaxy-business/yj/check/report',
					method: 'POST',
					data: formdata
				}).then(res => {
					console.log('res ', JSON.stringify(res));
					if (res.success) {
						if (res.code == 200) {
							//that.clearCache();
							uni.showModal({
								content: '巡查信息已成功提交',
								showCancel: false,
								success(res) {
									if (res.confirm) {
										that.toBack();
									}
								}
							});
						} else {
							uni.showModal({
								content: '提交失败',
								showCancel: false
							});
						}
					} else {

						uni.showModal({
							content: '提交失败',
							showCancel: false
						});
					}
				}).catch(err => {
					//	console.log('errr3',JSON.stringify(err));
					uni.showModal({
						content: '' + err.data.msg,
						showCancel: false
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 0 0 100rpx;
	}
</style>