<template>
	<view class="container">
		<!-- <u-navbar title="地址切换" autoBack :safeAreaInsetTop="true" placeholder fixed></u-navbar> -->
		<Navbar title="地址切换" canBack></Navbar>
		<view class="cardCss" v-for="(item,index) in adsList" :key="index">
			<view class="margin-sm" @click="choiceAds(item)">
				<u-icon name="checkmark-circle-fill" :color="item.fSfmrdz === 'y'?'#F04332':'#999DB0'"
					size="20"></u-icon>
			</view>
			<view class="flex flex-direction">
				<view class="flex align-center margin-bottom-sm">
					<view class="text-xl">
						{{item.fLxr || ""}}
					</view>
					<view class="text-lg margin-left-xs text-gray">
						{{item.fLxrdh || ""}}
					</view>
				</view>
				<view class="text-gray">
					<!-- 湖北省武汉市洪山区青菱街道青菱寺 西路白沙洲农副产品大市场内 -->
<!--					{{item.fXxdz || ""}}-->
					 {{item.fLxrsf || ""}} {{item.fLxrs || ""}} {{item.fLxrq || ""}} {{item.fLxrjd || ""}} {{item.fLxrxxdz || ""}}
				</view>
			</view>
		</view>
		<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" v-if="!adsList.length">

			<view class="" @click="goToAdsPage">
				<u-button size="small" type="primary" :style="{marginTop:10+'px'}" text=" 去添加地址"></u-button>
			</view>
		</u-empty>
		<u-loadmore :status="status" marginTop="20" height="10" marginBottom="10" v-else />

	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	export default {
		components: {},
		data() {
			return {
				formData: {
					current: 1,
					size: 10
				},
				status: "loadmore",
				total: null,
				adsList: [],
				qpid: ""
			}
		},
		onPullDownRefresh() {
			this.adsList = []
			//调用获取数据方法
			this.getAdsList()
			setTimeout(() => {
				//结束下拉刷新
				uni.stopPullDownRefresh();
			}, 500);
		},
		onReachBottom() {
			let allTotal = this.formData.current * this.formData.size
			if (allTotal < this.total) {
				//当前条数小于总条数 则增加请求页数
				this.formData.current++;
				this.status = 'loading';
				setTimeout(() => {
					this.getAdsList()
				}, 500)
			} else {
				this.status = 'nomore'
				// console.log('已加载全部数据')
			}
		},
		computed: {
			...mapGetters({
				adsId: 'adsId'
			})
		},
		onLoad(data) {
			if (data.qpid) {
				this.qpid = data.qpid
			}
		},
		onShow() {
			this.getAdsList()
		},
		methods: {
			// 查询地址列表
			async getAdsList() {
				const result = await this.$api.memberApi.getwHydzList(this.formData);
				let {
					code,
					value
				} = result;
				if (code === 200) {
					let allTotal = this.formData.current * this.formData.size
					this.total = value.total
					const newlist = value.records;

					if (this.formData.current === 1) {
						this.adsList = newlist;
					} else {
						this.adsList.push(...newlist)
					}

					if (this.adsList.length < allTotal) {
						this.status = 'nomore'
					}
					this.adsList.map(item => {
						if (item.id === this.adsId) {
							item.fSfmrdz = 'y'
						}else{
							item.fSfmrdz = 'n'
						}
					})
				}
				console.log('地址列表', JSON.parse(JSON.stringify(this.adsList)));
			},
			// 点击勾选默认地址
			async choiceAds(item) {
				this.adsList.map(item => {
					item.fSfmrdz = 'n'
				})
				item.fSfmrdz = 'y';
				let data = {
					adsId: item.id,
					adsName: item.fLxrs,
					adsJd: item.fJd,
					adsWd: item.fWd
				};
				uni.$u.toast('切换地址成功');
				this.$store.dispatch('GetDefaultAds', data);
				console.log(this.$store);
			},
			// 跳转到地址管理页
			goToAdsPage() {
				if (this.qpid) {
					uni.navigateTo({
						url: "/pages/mine/appList/addressManagement/addAddress?qpid=" + this.qpid
					})
				} else {
					uni.navigateTo({
						url: "/pages/mine/appList/addressManagement/addAddress"
					})
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		padding-bottom: 10px;
	}

	.cardCss {
		display: flex;
		align-items: center;
		background: #fff;
		border-radius: 15px;
		height: 100px;
		margin: 10px;
		padding-right: 10px;
	}
</style>
