<template>
	<view>
		<view class="mapbox">
			<map 
				style="width:750rpx;height:400rpx;"
				name="" 
				id="mapself" 
				:latitude="latitude" 
				:longitude="longitude" 
				:markers="markers"
				></map>
		</view>
		<view class="position">
			<view class="locainfo">
				<view class="localeft">
					<image :src="BASE_IMG_URL+'ydw.png'" class="dingwei" mode="scaleToFill"></image>
					<view class="text">{{address}}</view>
				</view>
			</view>
			<view style="background:#fff;">
				<view class="inp_warp">
					<view class="inp_title">拜访客户:</view>
					<view class="form_right" @click="toSelectCustomer" v-if="!defaultCustomer">
						<view :class="customerObj.id ? 'valueActive' : '' ">
							{{customerObj.id ? customerObj.name : '点击选择'}}
						</view>
					</view>
					<view class="form_right" v-if="defaultCustomer">
						<view :class="customerObj.id ? 'valueActive' : '' ">
							{{customerObj.id ? customerObj.name : '点击选择'}}
						</view>
					</view>
				</view>
				<form-item v-for="(item,index) in listForm" :key="index" :objInfo="item" istype="camera" @changeData="changeOtherData"></form-item>
			</view>
			<view style="height:120rpx"></view>
			<view class="savebox">
				<view class="savebtn" @click="$noMultipleClicks(toAdd)">保存</view>
			</view>
		</view>
		
		<!-- 选择客户 -->
		<select-customer ref="customerChild" @getCustomerinfo="getCustomerinfo"></select-customer>
	</view>
</template>

<script>
	import { netMapKey } from '@/api/login.js'
	import {
		netAddSignin,
		uploadUrl,
		netSetForm
	} from '@/api/index.js'
	import {
		netPrevCustomer
	} from '@/api/kehu.js'
	var QQMapWX;
	var qqmapsdk;
	import {
		BASE_IMG_URL
	} from '@/util/api.js'
	import formItem from '@/components/form/formitem.vue'
	import selectCustomer from '@/components/selectCustomer.vue'
	
	export default {
		components:{
			formItem,
			selectCustomer
		},
		data() {
			return {
				noClick: true, //防止  重复点击
				longitude: null,
				latitude: null,
				markers: [],
				address: '',
				kehuList: [],
				kehuIndex: null,
				kehuName: '',
				BASE_IMG_URL: BASE_IMG_URL,
				location: '', //地址 
				lng: '',
				lat: '',
				files: '',
				customer_id: '',
				defaultCustomer: false,
				listForm: [],
				customerObj:{},  //拜访的客户
				mapKey:''
			}
		},
		onLoad(options) {
			//获取地图Key
			this.getKey()
			if (options.customerid) {
				this.customer_id = options.customerid
				this.defaultCustomer = true
				//获取 客户列表
				this.getKehulist()
			}
			//获取表单
			this.getForm()
		},
		methods: {
			getForm() {
				netSetForm({type: 'signin'}).then(res => {
					res = res.data.data
					res.forEach(ele=>{
						ele.value = ele.value?ele.value:''
					})
					this.listForm = res
				})
			},
			changeOtherData(name, value, obj) {
				let arr = this.listForm
				arr.forEach((ele, index) => {
					if (ele.id == obj.id) {
						ele.value = value
						this.$set(this.listForm, index, ele)
					}
				})
			},
			//获取地图key
			getKey() {
				netMapKey().then(res=>{
					if(res.data.map_key){
						this.mapKey = res.data.map_key
						QQMapWX = require('../../util/qqmap-wx-jssdk.min.js')
						qqmapsdk = new QQMapWX({
							key: res.data.map_key // 必填
						})
						//获取位置
						this.getAddressinfo()
					}else{
						uni.showToast({
							title:'请在后台配置地图key',
							icon:'none'
						})
					}
				})
			},
			//设置 默认 客户
			handleCustomer() {
				let id = this.customer_id
				let arr = this.kehuList
				arr.forEach((ele, index) => {
					if (ele.id == id) {
						this.customerObj = ele
					}
				})
			},
			getKehulist() {
				netPrevCustomer({
					name: this.kehuName
				}).then(res => {
					this.kehuList = res.data
					if (this.defaultCustomer) {
						//设置默认 客户
						this.handleCustomer()
					}
				})
			},
			toSelectCustomer() {
				this.$refs.customerChild.init()
			},
			getCustomerinfo(obj) {
				this.customerObj = obj
			},
			getAddressinfo() {
				uni.getLocation({
					type: 'gcj02',
					isHighAccuracy: true,
					altitude: true,
					success: (res) => {
						this.longitude = res.longitude
						this.latitude = res.latitude
						this.markers = [{
							id: 1,
							latitude: res.latitude,
							longitude: res.longitude,
							iconPath: BASE_IMG_URL+'weizhi.png',
							width: 30,
							height: 30
						}]
						// #ifndef H5
						//地址逆解析
						qqmapsdk.reverseGeocoder({
							location: {
								longitude: res.longitude,
								latitude: res.latitude
							},
							success: (re) => {
								this.address = re.result.address
							}
						})
						// #endif
						// #ifdef H5
						this.$jsonp('https://apis.map.qq.com/ws/geocoder/v1',{
							key:this.mapKey,
							output:'jsonp',
							location:`${res.latitude},${res.longitude}`,
							get_poi:'1'
						}).then(res=>{
							this.address = res.result.address_component.province+res.result.address_component.city+res.result.formatted_addresses.rough
						})
						// #endif
					},
					fail: (err) => {
						console.log(err, '错误')
					}
				})
			},
			//保存
			toAdd() {
				let {
					longitude,
					latitude,
					address,
				} = this
				if (!this.customerObj.id) {
					uni.showToast({
						title: '请选择拜访客户',
						icon: 'none'
					})
					return
				}
				let obj = this.checkCommonForm(this.listForm)
				if(!obj){
					return
				}
				let params = {
					location: address,
					lng: longitude,
					lat: latitude,
					customer_id: this.customerObj.id,
					...obj
				}
				netAddSignin(params).then(res => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 2000)
				})
			}
		}
	}
</script>
<style>
	radio {
		transform: scale(0.6);
	}
</style>

<style lang="scss" scoped>
	.valueActive {
		color: #333;
	}

	.mapbox {
		width: 750rpx;
		height: 400rpx;
		overflow: hidden;
	}

	.locainfo {
		padding: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background: #fff;

		.localeft {
			display: flex;
			justify-content: flex-start;
			align-items: center;

			.dingwei {
				width: 44rpx;
				height: 44rpx;
				margin-right: 15rpx;
			}

			.text {
				font-size: 26rpx;
				color: #333;
			}
		}

		.locaright {
			image {
				width: 46rpx;
				height: 46rpx;
			}
		}
	}

	.inp_warp {
		padding: 30rpx;
		background: #fff;
		margin-top: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: 1rpx solid #CCC;
		margin-bottom: 30rpx;
		border-top: 1px solid #ededed;

		.inp_title {
			font-size: 32rpx;
			color: #999;
		}

		.form_right {
			display: flex;
			align-items: center;
			justify-content: flex-end;
			font-size: 32rpx;
			padding-right: 30rpx;
			text-align: right;
		}
	}

	.fllow_area {
		background-color: #fff;
		padding: 0 70rpx;
		padding-bottom: 30rpx;
		color: #999;

		.area_head {
			height: 40rpx;
			line-height: 40rpx;
			font-size: 32rpx;
			margin-bottom:24rpx;
			
			text {
				color: #f00;
			}
		}

		textarea {
			width: 600rpx;
			height: 250rpx;
			padding: 30rpx;
			box-sizing: border-box;
			background: #f8f8f8;
			border-radius: 15rpx;
			margin: 0 auto;
		}
	}

	.savebox {
		display: flex;
		justify-content: center;
		align-items: center;
		position: fixed;
		left:0;
		bottom:0;
		width:750rpx;
		height:calc(100rpx + env(safe-area-inset-bottom) / 2);
		background:#fff;
		z-index: 2;
		padding-bottom:calc(env(safe-area-inset-bottom) / 2);
		.savebtn {
			font-size: 26rpx;
			color: #fff;
			text-align: center;
			line-height: 88rpx;
			width: 500rpx;
			height: 88rpx;
			background: $uni-text-color;
			border-radius: 29rpx;
		}
	}

	.imglist {
		padding: 24rpx 34rpx;
		background: #fff;
		border-bottom: 1rpx solid #EAEAEA;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		flex-wrap: wrap;
		.imgbox {
			width: 160rpx;
			height: 160rpx;
			margin-right: 14rpx;
			position: relative;
			margin-bottom: 24rpx;
			border: 1rpx solid #EAEAEA;
			.delimg {
				width: 32rpx;
				height: 32rpx;
				border-radius: 50%;
				position: absolute;
				right: -16rpx;
				top: -16rpx;
				background: rgba(0, 0, 0, 0.5);
				z-index: 2;
			}
			.selfimg {
				width: 160rpx;
				height: 160rpx;
			}
		}
	}
</style>
