<template>
	<view class="cotainer">
		<view class="orderForm">
			<view class="orderForm-wx" @click="getAddress">微信地址</view>
			<u-form :model="form" ref="uForm">
				<u-form-item label="联系人" label-width="150" required prop="contact" label-position="left">
					<u-input v-model="form.contact" placeholder="请填写联系人" />
				</u-form-item>
				<u-form-item label="联系电话" label-width="150" required prop="mobile" label-position="left">
					<u-input v-model="form.mobile" placeholder="请填写联系电话" />
				</u-form-item>
				<view class="orderForm-address">
					<text><text>*</text>地区</text>
					<u-button @click="changeLocation" size="mini">获取位置</u-button>
				</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="top">
					<u-input v-model="form.address" placeholder="请填写详细地址" type="textarea" />
				</u-form-item>
			</u-form>
			<view class="orderForm-btn">
				<u-button type="primary" @click="submit">提交</u-button>
			</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 {
				form: {
					area: '',
					address: '',
					contact: '',
					mobile: ''
				},
				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) {
			if(uni.getStorageSync('vuex_address')){
				this.form = uni.getStorageSync('vuex_address')
				uni.removeStorageSync('vuex_address')
			}
			this.getArea()
		},
		methods: {
			async getArea() {
				const res = await this.$u.api.getRegion()
				this.listArea = res.regions
			},
			openArea() {
				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) {
						this.$u.api.saveUserAddress(this.form).then(res=>{
							toast('提交成功')
							uni.navigateBack(-1)
						})
					} else {
						console.log('验证失败');
					}
				});
			}
		}

	}
</script>

<style lang="scss">
	.cotainer {
		padding: 40rpx;
		.orderForm {
			&-wx{
				color:#2979ff;
				margin-bottom:20rpx;
				font-size: 32rpx;
			}
			&-address {
				height: 72rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				line-height: 72rpx;
				padding-top: 20rpx;
				position: relative;

				text {
					text {
						position: absolute;
						left: -16rpx;
						vertical-align: middle;
						color: #fa3534;
						padding-top: 6rpx;
					}
				}
			}

			&-btn {
				margin: 40rpx;
			}
		}
	}
</style>