<template>
	<view class="container">
		<view class="address-form address-form-top">
			<u-cell-group :border="false">
				<u-field label="收货人" v-model="addressData.username" label-width="160"></u-field>
				<u-field label="手机号码" v-model="addressData.phone" label-width="160"></u-field>
				<u-cell-item title="所在地" title-width="160" @click="regionShow = true">
					<text class="address-text" v-show="addressData.province">{{addressData.province}}-{{addressData.city}}-{{addressData.area}}</text>
				</u-cell-item>
				<u-field type="textarea" placeholder="详细地址:如道路、门牌号、小区、楼栋号、单元等" v-model="addressData.address" label-width="0"></u-field>
			</u-cell-group>
		</view>
		<view class="address-form address-form-bottom">
			<u-cell-group :border="false">
				<u-cell-item title="地址标签" title-width="160" @click="tagShow = true">
					<text class="address-text">{{addressData.tag}}</text>
				</u-cell-item>
				<u-cell-item title="设为默认地址" title-width="200" :arrow="false">
					<u-switch v-model="addressData.isDefault" active-color="#51A539"></u-switch>
				</u-cell-item>
			</u-cell-group>
		</view>
		<view class="address-handle">
			<u-button type="success" shape="circle" :ripple="true" @click="submitData" class="handle-custom" :disabled="!addressData.username || !addressData.phone || !addressData.address || !addressData.province">保存</u-button>
		</view>
		<u-picker mode="region" v-model="regionShow" :area-code="defaultRegion" @confirm="setRegion"></u-picker>
		<u-picker mode="selector" v-model="tagShow" :range="tagList" @confirm="setTag"></u-picker>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		data() {
			return {
				addressId: '',
				addressData: {
					username: '',
					phone: '',
					address: '',
					province: '',
					city: '',
					area: '',
					isDefault: false,
					tag: '',
				},
				regionShow: false,
				defaultRegion: [],
				tagShow: false,
				tagList: ['家', '公司', '学校'],
			}
		},
		onLoad(options) {
			if (options.addressId) {
				this.addressId = options.addressId
				uni.setNavigationBarTitle({
					title: '编辑地址'
				});
				NET.request(API.getAddressDetail + this.addressId, {}, 'GET').then(res => {
					this.addressData = {
						username: res.data.username,
						phone: res.data.phone,
						address: res.data.address,
						province: res.data.province,
						city: res.data.city,
						area: res.data.area,
						isDefault: res.data.isDefault == 'true' || res.data.isDefault,
						tag: res.data.tag,
					}
					this.defaultRegion = [res.data.province, res.data.city, res.data.area]
				}).catch(res => {
					this.$refs.uTips.show({
						title: '查询地址详情失败',
						type: 'warning',
					})
				})
			}
		},
		methods: {
			//  设置地址
			setRegion(data) {
				this.addressData.province = data.province.label
				this.addressData.city = data.city.label
				this.addressData.area = data.area.label
			},
			//  设置标签
			setTag(data) {
				this.addressData.tag = this.tagList[data[0]]
			},
			//   提交地址数据
			submitData() {
				if (!this.addressId) {
					NET.request(API.addAddress, {
						...this.addressData,
						mid: uni.getStorageSync("userData").userId
					}, 'POST').then(res => {
						if (res.isSuccess) {
						    this.$refs.uTips.show({
						    	title: '新增地址成功',
						    	type: 'success',
						    })
						    setTimeout(() => {
						    	uni.navigateBack()
						    }, 1000)	
						} else {
							this.$refs.uTips.show({
								title: res.msg,
								type: 'warning',
							})
						}
					}).catch(res => {
						this.$refs.uTips.show({
							title: '新增地址失败',
							type: 'warning',
						})
					})
				} else {
					NET.request(API.editAddress, {
						...this.addressData,
						mid: uni.getStorageSync("userData").userId,
						id: this.addressId
					}, 'POST').then(res => {
						if (res.isSuccess) {
							this.$refs.uTips.show({
								title: '编辑地址成功',
								type: 'success',
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						} else {
							this.$refs.uTips.show({
								title: res.msg,
								type: 'warning',
							})
						}
					}).catch(res => {
						this.$refs.uTips.show({
							title: '编辑地址失败',
							type: 'warning',
						})
					})
				}
			},
		},
	}
</script>

<style>
	page {
		background-color: #f7f7f7;
	}
</style>
<style lang="less" scoped>
	page {
		width: 100%;
		height: 100%;
	}

	.container {
		width: 100%;
		height: 100%;
		float: left;
		box-sizing: border-box;
		background-color: #f7f7f7;
		padding-bottom: 70px;
		overflow-y: auto;

		.address-form {
			width: 100%;
			float: left;
			box-sizing: border-box;
			padding: 0 15px;
			background-color: #ffffff;

			/deep/.u-field {
				padding-left: 0px;
				padding-right: 0px;
			}

			/deep/.u-cell {
				padding-left: 0px;
				padding-right: 0px;
			}
		}

		.address-form-top {
			/deep/.u-cell_title {
				color: #999999;
			}

			/deep/.u-label-text {
				color: #999999;
			}
		}

		.address-form-bottom {
			margin-top: 10px;

			/deep/.u-cell_title {
				color: #333333;
			}

			/deep/.u-label-text {
				color: #333333;
			}
		}

		.address-handle {
			width: calc(100% - 30px);
			height: 40px;
			position: fixed;
			bottom: 20px;
			left: 15px;

			.handle-custom {
				background-color: #56a83a;

				/deep/button {
					background-color: #56a83a;
				}

				/deep/.u-btn--success--disabled {
					background-color: #74bd60 !important;
				}
			}
		}
	}
</style>
