<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
					<radio-group @change="addressChange">
						<view class="list" v-for="(item,index) in list" :key="index">
							<label class="radio" v-if="type=='shopping'">
								<radio :value="item._id" :checked="item._id == address_id" />
							</label>
							<view class="list_1">
								<view class="name">
									<text>{{item.name}}</text>
									<text>{{item.phone}}</text>
								</view>
								<view class="address">
									<text>{{item.province}}</text>
									<text>{{item.city}}</text>
									<text>{{item.area}}</text>
									<text>{{item.address}}</text>
									<text>{{item.number}}</text>
								</view>
								<view class="btn">
									<button class="btn_1" size="mini" @click="toDefa(item)" v-if="item.is_default==false">设为默认</button>
									<button class="btn_2" size="mini" @click="toCommon(item)">编辑</button>
									<button class="btn_3" size="mini" @click="toDel(item)">删除</button>
								</view>
								<view class="default" v-if="item.is_default==true">
									<text>默认</text>
								</view>
							</view>
						</view>
						<view class="is_bottom" v-if="is_bottom">
							<text>{{config.bottom_title}}</text>
						</view>
					</radio-group>
				</scroll-view>
			</view>
			<view class="two">
				<view class="two_1">
					<button type="default" @click="toWxaddress()">获取微信地址</button>
				</view>
				<view class="two_1">
					<button type="default" @click="toCommon()">新增收货地址</button>
				</view>
			</view>
		</view>
	</mobile-frame>
</template>
<script>
	export default {
		data() {
			return {
				// 购物选地址
				type: '',
				address_id: '',
				// 系统设置
				config: {},
				user: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
			};
		},
		onLoad: function(e) {
			const that = this;
			that.$set(that, `type`, e && e.type || '');
			that.$set(that, `address_id`, e && e.id || '');
		},
		onShow: async function(e) {
			const that = this;
			that.searchConfig();
			that.watchLogin();
		},
		onHide: function() {
			const that = this;
			that.clearPage()
		},
		onPullDownRefresh: function() {
			const that = this;
			that.clearPage()
			that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			// 查询基本设置
			searchConfig() {
				const that = this;
				uni.getStorage({
					key: 'config',
					success: function(res) {
						if (res.data) that.$set(that, `config`, res.data)
					},
					fail: function(err) {
						console.log(err);
					}
				})
			},
			// 监听用户是否登录
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						let user = that.$jwt(res.data);
						that.$set(that, `user`, user);
						that.search()
					},
					fail: function(err) {
						uni.navigateTo({
							url: `/pages/login/index`
						})
					}
				})
			},
			// 查询列表
			async search() {
				const that = this;
				let user = that.user;
				let info = {
					skip: that.skip,
					limit: that.limit,
					customer: user._id
				}
				const res = await that.$api(`/address`, 'GET', {
					...info
				})
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					that.$set(that, `list`, list);
					that.$set(that, `total`, res.total)
				}
			},
			// 分页
			toPage() {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.search();
					uni.hideLoading();
				} else that.$set(that, `is_bottom`, true)
			},
			// 计算是否触底
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				that.$set(that, `scrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
			},
			// 选择收货地址
			addressChange(e) {
				const that = this;
				let data = that.list.find((i) => i._id == e.detail.value);
				if (data) {
					that.address_id == data._id;
					uni.$emit("id", e.detail.value);
					uni.navigateBack({
						delta: 1
					});
				}
			},
			// 设置默认
			toDefa(e) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定设置该地址为默认地址吗?',
					success: async function(res) {
						if (res.confirm) {
							const arr = await that.$api(`/address/toDefault/${e._id}`, `POST`);
							if (arr.errcode == '0') {
								uni.showToast({
									title: '添加默认成功',
									icon: 'none'
								})
								that.clearPage()
								that.search()
							} else {
								uni.showToast({
									title: arr.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 获取微信地址
			toWxaddress() {
				const that = this;
				let user = that.user;
				let config = that.$config;
				if (config.system) {
					if (config.system.uniPlatform == 'app') {
						uni.showToast({
							title: '请进入微信小程序进行获取微信地址',
							icon: 'none'
						})
					} else if (config.system.uniPlatform == 'mp-weixin') {
						uni.chooseAddress({
							success: async function(add) {
								let params = {
									customer: user._id,
									name: add.userName,
									phone: add.telNumber,
									province: add.provinceName,
									city: add.cityName,
									area: add.countyName,
									address: add.detailInfo
								}
								const arr = await that.$api(`/address`, 'POST', params);
								if (arr.errcode == '0') {
									uni.showToast({
										title: '添加收货地址成功',
										icon: 'none'
									})
									that.clearPage()
								} else {
									uni.showToast({
										title: arr.errmsg,
										icon: 'none'
									})
								}
							},
							fail: function() {
								that.clearPage()
							}
						})
					}
				}
			},
			// 编辑
			toCommon(e) {
				const that = this;
				that.clearPage();
				uni.navigateTo({
					url: `/pagesMy/address/add?id=${e&&e._id||''}`
				})
			},
			// 删除
			toDel(e) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定删除该地址吗?',
					success: async function(res) {
						if (res.confirm) {
							const arr = await that.$api(`/address/${e._id}`, 'DELETE');
							if (arr.errcode == '0') {
								uni.showToast({
									title: '删除信息成功',
									icon: 'none'
								})
								if (that.address_id == e._id) uni.$emit("id", '')
								that.clearPage()
								that.search()
							} else {
								uni.showToast({
									title: arr.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 6)
				that.$set(that, `page`, 0)
			}
		},

	}
</script>

<style lang="scss">
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;
		background-color: var(--f1Color);

		.one {
			position: relative;
			flex-grow: 1;

			.list {
				display: flex;
				background: var(--fffColor);
				margin: 2vw 2vw 0 2vw;
				padding: 2vw;
				border-radius: 5px;
				position: relative;

				.list_1 {
					flex-grow: 1;

					.name {
						font-size: var(--font16Size);
						margin: 0 0 2vw 0;

						text {
							padding: 0 2vw 0 0;
						}
					}

					.address {
						font-size: var(--font14Size);
						margin: 0 0 1vw 0;

						text {
							padding: 0 2vw 0 0;
						}
					}

					.btn {
						text-align: center;

						button {
							margin: 0 2vw 2vw 2vw;
							background-color: var(--f35BColor);
							color: var(--fffColor);
						}

						.btn_2 {
							background-color: var(--f0fColor);
						}

						.btn_3 {
							background-color: var(--fFB1Color);
						}
					}

					.default {
						position: absolute;
						top: 0;
						right: 0;

						text {
							background: var(--ff0Color);
							color: var(--fffColor);
							font-size: var(--font12Size);
							padding: 1vw;
							border-radius: 5px;
						}
					}
				}
			}
		}

		.two {
			display: flex;
			justify-content: space-between;

			.two_1 {
				width: 50vw;

				button {
					width: 100%;
					color: var(--fffColor);
					background-color: var(--f08Color);
					border-radius: 0;
				}


			}

			.two_1:nth-child(2) {
				button {
					background-color: var(--fFB1Color);
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}

	.is_bottom {
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: #858585;
			font-size: 14px;
		}
	}
</style>
