<template>
	<!-- <view class="pages" :style="{'height':(h)+'px','padding-top':mt+'px'}"> -->
<view class="pages" :style="{'height':(h-th)+'px','padding-top':mt+'px'}">
		<c-nav-bar title="位置定位" :showIcon="true"></c-nav-bar>
		<view class="content">
			<view class="card">
				<!-- <view class="card1" @tap="open()"> -->
				<!-- 	<view class="card1">
					<text style="font-size: 30rpx; color: #333; font-weight: Regular; ">
						<text>所属城市</text>
						<text style="margin-left: 30rpx; text-align: right; width: 100%;"
							v-if="str.name==undefined||str.name==''">请选择所属城市</text>
						<text style="margin-left: 30rpx; text-align: right; width: 100%;" v-else>{{str.name}}</text>
					</text>
					<u-icon name="arrow-right" size="24"></u-icon>
				</view> -->
				<view class="card1">
					<text style="margin-right: 30rpx; font-size: 30rpx; color: #333; font-weight: Regular; ">所属城市</text>
					<u--input class="card-input" inputAlign="right" placeholder="请输入您的地址" border="none"
						v-model="str.name" style="text-align: right; font-size: Regular;"></u--input>
				</view>
			</view>
			<view class="card">
				<view class="card1">
					<text style="margin-right: 30rpx; font-size: 30rpx; color: #333; font-weight: Regular; ">详细地址</text>
					<u--input class="card-input" inputAlign="right" placeholder="请输入您的地址" border="none"
						v-model="formData.areaDetail" style="text-align: right; font-size: Regular;"></u--input>
				</view>
			</view>
			<view class="card">
				<view class="card1">
					<text class="label">联系电话</text>
					<u--input inputAlign="right" placeholder="请输入内容" border="none" v-model="stayInfo.landlinePhone"
						style="text-align: right;"></u--input>
				</view>
			</view>
			<view class="card c2">
				<!-- <view class="tit">内饰照片</view> -->

				<view class="label" style="color: #333; font-size: 30rpx; margin:20rpx 0 28rpx; font-weight: Heavy;">
					内饰照片</view>
				<view class="imgs">
					<!-- <image src="" v-for="(item,index) in imgUrlList" :key="index"></image> -->
					<u-upload ref="uUpload" uploadText="上传图片" :fileList="imgUrlList" :maxSize="10 * 1024 * 1024"
						width="200" height="120" @afterRead="afterRead" @delete="deletePic">
					</u-upload>
				</view>
			</view>
			<view class="card" style="padding-bottom: 240rpx;">
				<view class="card1">
					<text>地图位置</text>
				</view>
				<!-- <view style="margin-top: 20rpx;" class="collapseTitle">
					点击地图可调整至更精确位置
				</view> -->
				<view style="margin-top: 20rpx;">
					<view class="content-map">
						<map style="width: 100%;height: 100%;" :latitude="latitude" :longitude="longitude" :scale="16"
							:title="title" />
					</view>
				</view>
			</view>
			<cityPicker :column="column" :default-value="defaultValue" :mask-close-able="maskCloseAble"
				@confirm="confirm" @cancel="cancel" :visible="visible" />
		</view>
		<view class="bottom" @click="save">
			<view class="btn">
				确定
			</view>
		</view>
	</view>
</template>

<script>
	import cityPicker from '@/uni_modules/piaoyi-cityPicker/components/piaoyi-cityPicker/piaoyi-cityPicker'
	// 引入高德地图api提供的微信小程序的接口
	var amapFile = require('@/utils/amap-wx.130.js');
	// 创建地图
	var myAmapFun = new amapFile.AMapWX({
		key: '1173b1eb83eb93bff1fb4a5987503a51'
	}); //key值要申请为 去高德地图申请微信小程序的key
	// var myAmapFun = new amapFile.AMapWX({key: ''}); //key我的
	export default {
		data() {
			return {
				selectIndex: undefined,
				selectAddr: {},
				searchWords: "",
				id: 1, // 使用 marker点击事件 需要填写id
				title: 'map',
				latitude: 39.91667, // 纬度
				longitude: 116.41667, // 经度
				markers: [{
					latitude: 39.91667, // 纬度
					longitude: 116.41667, // 经度
					width: 30,
					height: 30,
					iconPath: ''
					// iconPath: '../../static/ditu.png'
				}],
				dataTips: [],
				// type: '',
				value: '',
				value1: '',
				fileList: [],
				fileList1: [],
				imgUrlList: [],
				fileList2: [],
				result: '',
				visible: false,
				maskCloseAble: true,
				str: {
					name: '浙江省舟山市嵊泗县'
				},
				defaultValue: '330922',
				// defaultValue: ['河北省','唐山市','丰南区'],
				column: 3,
				address: '',
				formData: {},
				stayInfo: {
					picList: []
				},
				homestayId: uni.getStorageSync('homestayId') || 0,
			}
		},
		components: {
			cityPicker
		},
		onLoad(option) {
			this.getStayInfo()
			// var self = this;
			// this.myPosition()
			// uni.getLocation({
			// 	type: 'gcj02',
			// 	success: function(res) {
			// 		console.log(res, '当前地址定位');
			// 		if (res.errMsg == "getLocation:ok") {
			// 			console.log(self.mark, 'onload里面看看');
			// 			self.longitude = res.longitude;
			// 			self.latitude = res.latitude;
			// 			self.$set(self.markers[0], "longitude", res.longitude);
			// 			self.$set(self.markers[0], "latitude", res.latitude);
			// 			self.mapFlafg = true;
			// 			console.log(self.markers, "markers")
			// 			// self.markers[0].longitude = res.longitude;
			// 			// self.markers[0].latitude = res.latitude;
			// 		}
			// 	},
			// 	complete: () => {
			// 		// 获取当前位置的地点列表
			// 		myAmapFun.getPoiAround({
			// 			location: self.longitude + ',' + self.latitude,
			// 			success: (data) => {
			// 				console.log("获取当前的列表", data);
			// 				this.dataTips = data.poisData;
			// 			},
			// 			fail: (info) => {
			// 				console.log(info, '点击地图错误信息1')
			// 			}
			// 		})
			// 	}
			// });
		},
		methods: {

			getStayInfo() {
				this.$api.get('/merchant/hotel/mine/getMerchantHomestayInfo/' + this.homestayId).then(res => {
					if (res.data.code == 0) {
						this.stayInfo = res.data.data;
						this.formData = this.stayInfo
						this.latitude = this.formData.lat
						this.longitude = this.formData.lon
						this.markers[0].latitude = this.formData.lat
						this.markers[0].longitude = this.formData.lon
						this.stayInfo.picList.forEach((item) => {
							this.imgUrlList.push({
								url: item
							})
						})
					}
				})
			},
			// 确定保存
			save() {
				// if (!this.$u.test.mobile(this.stayInfo.landlinePhone)) {
				// 	this.$showToast('请输入正确手机号');
				// 	return
				// }
				this.stayInfo.introduction = null
				this.stayInfo.picList = [];
				this.imgUrlList.forEach((i) => {
					if (i.url) {
						this.stayInfo.picList.push(i.url)
					}
				})
				this.$api.post('/merchant/hotel/mine/updMHomestayInfo', this.stayInfo).then(res => {
					console.log(res)
					if (res.data.code == 0) {
						this.$showToast('保存成功');
						// setTimeout(() => {
						// 	this.getStayInfo()
						// }, 1000)
					} else this.$showToast(res.data.msg);
				})
			},
			// 删除图片
			deletePic(event) {
				this.imgUrlList.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				let lists = [].concat(event.file)
				let fileListLen = this.imgUrlList.length
				lists.map((item) => {
					this.imgUrlList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.imgUrlList[fileListLen]
					this.imgUrlList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
				console.log(this.imgUrlList)
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://i.ringzle.com/island-cloud-server/oss/file/upload', // 仅为示例,非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							console.log(res)
							let data = JSON.parse(res.data);
							console.log(data)
							resolve(data.data.url)
						}
					});
				})
			},







			changeBegin() {
				this.visible = true
			},
			myPosition() {
				var myAmapFun = new amapFile.AMapWX({
					key: '1173b1eb83eb93bff1fb4a5987503a51'
				});
				let that = this;
				//获取地址
				myAmapFun.getRegeo({
					success: function(data) {
						that.longitude = data[0].longitude
						that.latitude = data[0].latitude
						console.log(that.longitude, that.latitude, '1111');
					},
					fail: function(info) {
						//失败回调
						console.log(info)
						uni.navigateBack();
					}
				})
			},
			open() {
				this.visible = true
			},
			confirm(val) {
				this.str = val
				console.log(this.str, '333')
				this.visible = false
			},
			cancel() {
				this.visible = false
			},
			onShareAppMessage(res) {
				if (res.from === 'button') { // 来自页面内分享按钮
					console.log(res.target, '1')
				}
				return {
					title: 'data-cityPicker省市区地址选择器!',
					path: '/pages/cityPicker/cityPicker'
				}
			},
			onShareTimeline(res) {
				if (res.from === 'button') { // 来自页面内分享按钮
					console.log(res.target, '2')
				}
				return {
					title: 'data-cityPicker省市区地址选择器!'
				}
			},
			change() {

			},
			close() {

			},

		}
	}
</script>
<style lang="less">
	.u-upload__wrap__preview__image {
		width: 196rpx !important;
		height: 130rpx !important;
	}

	.u-upload__button {
		width: 196rpx !important;
		height: 130rpx !important;
	}

	.card ::v-deep .u-icon__icon {
		font-size: 30rpx !important;
		margin-bottom: 10rpx;
		left: 2rpx !important;
		top: 6rpx !important;
	}
</style>
<style lang="scss" scoped>
	/deep/.u-cell__body {
		padding: 0;
	}

	::v-deep .u-cell__body {
		padding: 0;
	}

	.pages {
		box-sizing: border-box;
		height: 100%;
		background: #F9FAFC;

		.content {
			padding: 20rpx;
			height: 100%;

			.card {
				background: #fff;
				border-radius: 16rpx;
				padding: 30rpx 33rpx;
				margin-bottom: 20rpx;

				.upload {
					margin-top: 48rpx;
					width: 100%;
					height: 200rpx;
					border-radius: 16rpx;
					background-color: #F5F8FA;
					display: flex;
					align-items: center;
					text-align: center;

					.upload-one {
						margin: 0 245rpx;
						width: 200rpx;
						height: 200rpx;

						image {
							width: 100%;
							height: 100%;
						}
					}
				}

				.collapseTitle {
					font-size: 22rpx;
					color: #a5a5a5;
					margin-bottom: 20rpx;
				}

				.card1 {
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;

					text {
						font-size: 30rpx;
						color: #333;
						font-weight: Regular;
					}

					.card-input {
						font-size: 30rpx;
						color: #333;
						font-weight: Regular;
					}

					.as-content {
						color: #a5a5a5;

					}
				}
			}
		}

		.content-map {
			border: 1rpx solid black;
			height: 355rpx;
		}

		.bottom {
			position: fixed;
			bottom: 0;
			z-index: 999999;
			background: #fff;
			width: 100%;
			padding: 20rpx 0 30rpx;
			box-sizing: border-box;

			.btn {
				background: #33AFFC;
				color: white;
				text-align: center;
				height: 96rpx;
				margin: 0 auto;
				font-size: 34rpx;
				line-height: 96rpx;
				align-self: center;
				border-radius: 48rpx;
				width: 690rpx;
			}
		}
	}
</style>