<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
					<view class="list-scroll-view">
						<radio-group @change="addressChange" v-if="type=='shopping'">
							<label class="radio" v-for="(item, index) in list" :key="index">
								<radio :value="item._id" :checked="index === address" />
								<view class="radio_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">
										<view class="btn_1" v-if="item.is_default==false">
											<button type="default" size="mini" @click="toDefa(item)">设为默认</button>
										</view>
										<view class="btn_1">
											<button type="default" size="mini" @click="toCommon(item)">编辑</button>
										</view>
										<view class="btn_1">
											<button type="default" size="mini" @click="toDel(item)">删除</button>
										</view>
									</view>
									<view class="default" v-if="item.is_default==true">
										<text>默认</text>
									</view>
								</view>
							</label>
						</radio-group>
						<view v-else class="list" v-for="(item,index) in list" :key="index">
							<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">
								<view class="btn_1" v-if="item.is_default==false">
									<button type="default" size="mini" @click="toDefa(item)">设为默认</button>
								</view>
								<view class="btn_1">
									<button type="default" size="mini" @click="toCommon(item)">编辑</button>
								</view>
								<view class="btn_1">
									<button type="default" size="mini" @click="toDel(item)">删除</button>
								</view>
							</view>
							<view class="default" v-if="item.is_default==true">
								<text>默认</text>
							</view>
						</view>
						<view class="is_bottom" v-if="is_bottom">
							<text>{{config.bottom_title}}</text>
						</view>
					</view>
				</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 {
				// 购物选地址
				id: '',
				type: '',
				address: -1,
				// 系统设置
				config: {},
				user: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 5,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
			};
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `type`, e && e.type || '');
			that.$set(that, `id`, e && e.id || '');
			that.searchConfig();
			that.watchLogin();
		},
		onShow: function() {},
		onHide: function() {},
		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.reLaunch({
							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') {
					console.log(that.list);
					console.log(res.data);
					let list = [...that.list, ...res.data];
					that.$set(that, `list`, list);
					that.$set(that, `total`, res.total)
					if (that.id) {
						for (let i = 0; i < list.length; i++) {
							if (list[i]._id === that.id) {
								that.address = i;
								break;
							}
						}
					}
				}
			},
			// 分页
			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;
				for (let i = 0; i < that.list.length; i++) {
					if (that.list[i]._id === e.detail.value) {
						that.address = i;
						break;
					}
				}
				uni.$emit("id", e.detail.value)
				uni.navigateBack({
					delta: 1
				})
			},
			// 获取微信地址
			toWxaddress() {
				const that = this;
				let user = that.user;
				uni.getStorage({
					key: 'system',
					success: function(res) {
						if (res.data.uniPlatform == 'app') {
							uni.showToast({
								title: '请进入微信小程序进行获取微信地址',
								icon: 'none'
							})
						} else if (res.data.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();
										that.search();
									} else {
										uni.showToast({
											title: arr.errmsg,
											icon: 'none'
										})
									}
								},
								fail: async function(err) {
									that.clearPage();
									that.search();
								}
							})
						}
					}
				})
			},
			// 设置默认
			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'
								})
							}
						}
					}
				});
			},
			// 编辑
			toCommon(e) {
				const that = this;
				uni.navigateTo({
					url: `/pagesMy/address/add?id=${e&&e.id?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'
								})
								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)
			}
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.$set(that, `list`, [])
			that.$set(that, `skip`, 0)
			that.$set(that, `limit`, 6)
			that.$set(that, `page`, 0)
			await that.search();
			uni.stopPullDownRefresh();
		}
	}
</script>

<style lang="scss">
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			position: relative;
			flex-grow: 1;
			background-color: var(--f5Color);

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

				.radio_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 {
						display: flex;
						flex-direction: row;
						justify-content: space-around;
						border-top: 1px solid var(--font16Size);
						padding: 2vw 0 0 0;

						.btn_1 {
							button {
								width: 100%;
								color: var(--fffColor);
								background-color: var(--f35BColor);
							}
						}

						.btn_1:nth-child(2) {
							button {
								background-color: var(--f0fColor);
							}
						}

						.btn_1:last-child {
							button {
								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;
						}
					}
				}
			}

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

				.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 {
					display: flex;
					flex-direction: row;
					justify-content: space-around;
					border-top: 1px solid var(--font16Size);
					padding: 2vw 0 0 0;

					.btn_1 {
						button {
							width: 100%;
							color: var(--fffColor);
							background-color: var(--f35BColor);
						}
					}

					.btn_1:nth-child(2) {
						button {
							background-color: var(--f0fColor);
						}
					}

					.btn_1:last-child {
						button {
							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;
			flex-direction: row;
			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>
