<template>
	<view class="signIn-wrap">
		<!-- 门店名称 -->
		<view class="module-con first-con">
			<text class="info-tit">门店名称：</text>
			<view class="info-main">
				<text class="location-addr">{{ stores && stores.name }}</text>
			</view>
		</view>
		<!-- 定位 -->
		<view class="module-con">
			<view class="info-tit">
				<text class="require-tip"></text>
				当前位置：
			</view>
			<view class="info-main" @click="getLocation">
				<text class="location-addr">{{ location }}</text>
				<u-icon name="map" color="#2979ff" size="34" class="location-icon"></u-icon>
			</view>
		</view>
		<!-- 拍照签到 -->
		<view class="module-con">
			<view class="info-tit">
				<text class="require-tip"></text>
				拍照签到：
			</view>
			<view class="info-main">
				<view class="photograph-con" @click="photograph ? null : goPhotograph()">
					<u-icon v-show="photograph" name="close-circle-fill" color="#fa3534" size="50" class="close-circle-icon" @click="cancelPhotograph"></u-icon>
					<u-icon v-show="!photograph" name="camera" color="#bfbfbf" size="60" class="photograph-icon"></u-icon>
					<u-image v-show="photograph" width="100%" height="100%" :src="photograph" @click="previewPictures"></u-image>
				</view>
			</view>
		</view>
		<u-button type="primary" class="submit-btn" @click="signInFun">签到并开始巡店</u-button>
	</view>
</template>

<script>
import { clzConfirm, saveImgToAliOss,getGpsLocation } from '@/libs/tools.js';
import { validTaskPosition, tencentAddressInfo } from '@/api/task.js';
export default {
	data() {
		return {
			stores: null,  //  门店信息
			location: '', //  当前位置地址
			photograph: '', //  拍照临时地址
			position: null // 位置信息
		};
	},
	onShow() {
		this.init();
	},
	onLoad(opts) {
		this.stores = JSON.parse(opts.item);
	},
	methods: {
		// 初始化
		init() {
			this.getLocation();
		},
		// 获取当前位置
		getLocation() {
			const _this = this;
			// 获取系统信息同步接口
			const res = uni.getSystemInfoSync()
			getGpsLocation(function(gps){
				console.log(gps)
				//  安卓手机需要转格式，ios不需要
				if(res.platform == 'android'){  //  安卓
					_this.position = { latitude: gps[1], longitude: gps[0] }
					// 反解析拿到地址
					_this.getAddressInfo({ lat: gps[1], lng: gps[0] })
				}else if(res.platform == 'ios'){  //  ios
					_this.position = { latitude: gps.latitude, longitude: gps.longitude }
					const province = gps.address.province ? gps.address.province : ''  //  	省份名称
					const city = gps.address.city ? gps.address.city : ''  //  城市名称
					const district = gps.address.district ? gps.address.district : ''  //  区（县）名称
					const poiName = gps.address.poiName ? gps.address.poiName : ''  //  POI信息
					const street = gps.address.street ? gps.address.street : ''  //  街道信息
					const streetNum = gps.address.streetNum ? gps.address.streetNum : ''  //  获取街道门牌号信息
					_this.location = province + city + district + poiName + '（' + street + streetNum + '）'
				}
			})
		},
		// 根据经纬度获取具体位置
		getAddressInfo(params) {
			tencentAddressInfo(params).then(res => {
				console.log(res)
				if (res.status == 200) {
					this.location = res.data.province + res.data.city + res.data.district + res.data.address;
				}
			});
		},
		//  签到拍照
		goPhotograph() {
			const _this = this;
			
			uni.chooseImage({
				count: 1, //最多可以选择的图片张数，默认9
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				sourceType: ['camera'], //album 从相册选图，camera 使用相机，默认二者都有
				success: function(res) {
					// tempFilePaths 图片的本地文件路径列表,tempFiles 图片的本地文件列表，每一项是一个 File 对象
					console.log(JSON.stringify(res.tempFilePaths));
					uni.showLoading({
						title: '保存中，请稍后...', 
						mask: true
					})
					saveImgToAliOss(res.tempFilePaths[0],function(ret){
						console.log(ret)
						_this.photograph = ret.data
						uni.hideLoading()
					})
				}
			});
		},
		//  预览签到图
		previewPictures() {
			const photograph = [this.photograph];
			uni.previewImage({
				urls: photograph
			});
		},
		//  删除签到图
		cancelPhotograph() {
			setTimeout(() => {
				this.photograph = '';
			}, 500);
		},
		//  签到
		signInFun() {
			if(!this.position || !this.position.latitude || !this.position.longitude){
				uni.showToast({ icon: 'none', title: '位置获取失败，请重新定位后再进行签到' })
				return
			}else if(!this.photograph){
				uni.showToast({ icon: 'none', title: '请拍照后再进行签到' })
				return
			}
			// 存储当前签到信息
			this.$u.vuex('vuex_sceneTaskSignIn',{
				inspectorPositionAddr: this.location,
				inspectorPositionPhotoPath: this.photograph
			})
			//  判断是否超出签到范围
			const datas = {
				storeId: this.stores.id,
				point: {
					lat: this.position.latitude,
					lng: this.position.longitude,
				}
			}
			console.log(datas)
			validTaskPosition(datas).then(res => {
				if (res.status == 200) {
					//  跳转开始巡店
					let item = this.stores
					// 重新开始巡店
					if(item.taskId&&item.restart){
						uni.redirectTo({
							url: '/pages/shopTour/shopTour?storeId=' + item.id + '&taskId='+ item.taskId + '&restart=1&types=scene'
						})
					}else{
						// 首次巡店
						uni.redirectTo({
							url: '/pages/shopTour/shopTour?storeId=' + item.id + '&types=scene'
						})
					}
				} else {
					uni.showToast({ icon: 'none', title: res.message })
				}
			});
		},
	}
};
</script>

<style lang="scss" scoped>
page {
	background-color: #f8f8f8;
	height: calc(100vh - 44px);
}
.signIn-wrap {
	height: 100%;
	background-color: #f8f8f8;
	.module-con {
		padding: 0 30upx 20upx;
		.info-tit{
			.require-tip::before{
				content: '*';
				color: #F56C6C;
				width: 30upx;
				height: 30upx;
				display: inline-block;
				vertical-align: middle;
			}
		}
		.info-main {
			margin-top: 14upx;
			padding: 20upx;
			border-radius: 12upx;
			background-color: #fff;
			.location-icon {
				padding-left: 10upx;
				vertical-align: sub;
			}
			.photograph-con {
				border: 2upx dashed #bfbfbf;
				border-radius: 12upx;
				width: 160upx;
				height: 160upx;
				text-align: center;
				position: relative;
				.photograph-icon {
					display: inline-block;
					padding-top: 48upx;
				}
				.close-circle-icon {
					background-color: #fff;
					border-radius: 50%;
					position: absolute;
					right: -23upx;
					top: -23upx;
					z-index: 9;
				}
			}
		}
	}
	.first-con {
		padding-top: 30upx;
	}
	.submit-btn {
		width: 80%;
		margin-top: 50upx;
	}
}
</style>
