<template>
	<view class="cotainer">
		<view class="orderForm">
			<view class="orderForm-form">
				<u-form :model="form" ref="uForm" label-width="150" :error-type="['toast']">
					<u-form-item label="联系人" required prop="contact" label-position="left">
						<u-input v-model="form.contact" placeholder="请填写联系人" />
					</u-form-item>
					<u-form-item label="联系电话" required prop="mobile" label-position="left">
						<u-input v-model="form.mobile" placeholder="请填写联系电话" />
					</u-form-item>
					<!-- <view class="orderForm-address"> -->
					<u-form-item label="所在地区" required prop="area" label-position="left">
						<u-input type="select" v-model="form.area" placeholder="请选择所在地区" @click="openArea" />
					</u-form-item>

					<!-- </view> -->
					<!-- <u-form-item label=" " prop="area" label-position="top">
						<u-input v-model="form.area" placeholder="请选择地区" @click="openArea" type="select" />
					</u-form-item> -->
					<u-form-item label="详细地址" required prop="address" label-position="left">
						<u-input v-model="form.address" placeholder="请填写详细地址" />
						<view slot="right" class="arearight" @click="changeLocation" style="color:#000">
							<u-icon name="map"></u-icon>
							定位
						</view>
					</u-form-item>
					<u-form-item label="设置默认地址" label-width="180" :border-bottom="false" prop="defalut"
						label-position="left">
						<u-switch slot="right" v-model="form.defalut" active-color="#ba8b45" inactive-color="#adadac"
							size="40"></u-switch>
					</u-form-item>
				</u-form>
			</view>
			<view class="orderForm-wx" @click="getAddress">
				<u-icon name="weixin-circle-fill" color="#3fc14d" size="48"></u-icon>
				<view>获取微信地址</view>
				<u-icon name="arrow-right" size="32"></u-icon>

			</view>


			<view class="orderForm-btn">
				<view @click="submit">提交</view>
			</view>
		</view>
		<!-- <u-picker mode="region" v-model="showArea" @confirm="changeArea"></u-picker> -->
		<aui-picker ref="pickers" :title="'地区选择'" :data="listArea" @callback="changeArea"></aui-picker>

	</view>
</template>

<script>
	import {
		toast,
		isMobile,
		isNull
	} from '@/common/util.js'
	import auiPicker from '@/components/aui-picker.vue';
	export default {
		components: {
			auiPicker
		},
		data() {
			return {
				itemtype: 'add',
				form: {
					area: '',
					address: '',
					contact: '',
					mobile: '',
					defalut: false
				},
				listArea: [],
				showArea: false,
				rules: {
					area: [{
						required: true,
						message: '请选择地区',
						trigger: ['change', 'blur'],
					}],
					address: [{
						required: true,
						message: '请填写详细地址',
						trigger: ['blur'],
					}],
					contact: [{
						required: true,
						message: '请填写联系人',
						trigger: ['blur'],
					}],
					mobile: [{
						required: true,
						message: '请填写联系电话',
						trigger: ['blur'],
					}, {
						validator: (rule, value, callback) => {
							return this.$u.test.mobile(value);
						},
						message: '手机号码不正确',
						trigger: ['blur'],
					}]
				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);

		},
		onLoad(option) {
			this.itemtype = option.type ? option.type : ''
			if (uni.getStorageSync('vuex_address')) {
				this.form = uni.getStorageSync('vuex_address')
				if (this.itemtype === 'copy') {
					this.form.id = ''
					this.form.defalut = 0
				}
				uni.removeStorageSync('vuex_address')
			}
			this.getArea()
		},
		methods: {
			async getArea() {
				const res = await this.$u.api.getRegion()
				this.listArea = res.regions
			},
			openArea() {
				uni.hideKeyboard()
				this.$refs.pickers.open().then(function() {
					console.log('picker打开');
				});
			},
			changeArea(e) {
				console.log(e)
				// return
				if (e) {

					let data = e.data
					let _this = this
					_this.form.area = ''
					data.forEach(function(item, index) {
						_this.form.area += item.value + '';
					});
					this.form.province_id = data[0] ? data[0].id : ''
					this.form.city_id = data[1] ? data[1].id : ''
					this.form.district_id = data[2] ? data[2].id : ''
				}
			},
			getAddress() {
				let _this = this
				uni.authorize({
					scope: 'scope.address',
					success: (res) => {
						console.log("authorize", res)
						uni.chooseAddress({
							success(res) {
								console.log(res)
								_this.form.contact = res.userName
								_this.form.mobile = res.telNumber
								_this.form.area = res.provinceName + res.cityName + res.countyName
								_this.form.address = res.detailInfo
								_this.$u.api.matchRegion({
									province: res.provinceName,
									city: res.cityName,
									district: res.countyName
								}).then(res1 => {
									_this.form.province_id = res1.province_id ? res1
										.province_id.id : ''
									_this.form.city_id = res1.city_id ? res1.city_id.id : ''
									_this.form.district_id = res1.district_id ? res1
										.district_id.id : ''
								})
								console.log("_this", _this.form)
							},
							fail(res) {
								console.log(res)
							}
						})
					},
					fail(res) {
						console.log("authorize-fail", res)
					}
				})

			},
			changeLocation() {
				let _this = this
				uni.authorize({
					scope: 'scope.userLocation',
					success: (res) => {
						uni.chooseLocation({
							success(res) {
								console.log("res", res)
								// _this.form.area = res.address
								_this.form.address = res.name
							},
							fail(res) {
								console.log("fail", res)

							}
						})
					},
					fail(res) {
						console.log("authorize-fail", res)
					}
				})
			},
			submit() {
				console.log(this.form)
				// return
				this.$refs.uForm.validate(valid => {

					if (valid) {
						if (this.form.defalut) {
							this.form.defalut = 1
						}
						this.$u.api.saveUserAddress(this.form).then(res => {
							toast('提交成功', 1000, function() {
								setTimeout(function() {
									uni.navigateBack(-1)
								}, 500)

							})

						})
					} else {
						console.log('验证失败');
					}
				});
			}
		}

	}
</script>

<style lang="scss">
	.cotainer {
		padding: 40rpx;
		background-color: #f0efed;
		height: 100vh;
		overflow: scroll;

		.orderForm {
			&-wx {
				color: #000;
				margin-bottom: 20rpx;
				font-size: 32rpx;
				display: flex;
				align-items: center;
				margin-top:35rpx;
				>view{
					margin:0 20rpx;
				}
			}

			&-form {
				background-color: #fff;
				padding: 30rpx;
				padding-left: 40rpx;
				border-radius: 10rpx;
			}

			&-btn {
				background: #fff;
				position: fixed;
				bottom: 0;
				left: 0;
				width: 100%;
				padding: 40rpx;
				z-index: 9;

				>view {
					background-color: #ba8b45;
					color: #fff;
					border-radius: 40rpx;
					width: 60%;
					margin: 0 auto;
					height: 75rpx;
					text-align: center;
					line-height: 75rpx;
				}
			}
		}
	}
</style>