<template>
	<view class="mine-address-list">
		<view @tap.stop="toUpdateItem(index)" class="list-item fill-base mt-md" v-for="(item,index) in list.data"
			:key="index">
			<view class="flex-warp pd-lg b-1px-b">
				<view class="address-icon flex-center c-base radius" :style="{background:primaryColor}"><i
						class="iconfont icon-dingwei-fill"></i></view>
				<view class="address-info flex-1 ml-md">
					<view class="flex-y-baseline username c-title text-bold">{{item.user_name}}
						<view class="ml-md f-desc c-paragraph">{{item.mobile}}</view>
					</view>
					<view class="f-desc c-title">{{`${item.address} ${item.address_info}`}}</view>
				</view>
			</view>
			<view class="oper-info pl-lg pr-lg f-paragraph c-paragraph flex-between">
				<view class="flex-y-center"
					:style="{color:options.check && item.id == check_id || !options.check &&  item.status==1 ?primaryColor:''}">
					<i class="iconfont icon-xuanze mr-sm"
						:class="[{'icon-xuanze-fill':options.check && item.id == check_id || !options.check && item.status==1}]"
						:style="{color:options.check && item.id == check_id || !options.check && item.status==1?primaryColor:''}"></i>
					<block v-if="options.check">{{item.id == check_id ?'当前选择地址':'点击选择'}}</block>
					<block v-else>{{item.status ==1?'默认地址':'设为默认'}}</block>
				</view>
				<view class="flex-center">
					<view @tap.stop="goDetail(index)" class="pl-lg pr-lg">编辑</view>
					<view @tap.stop="toDel(index)" class="pl-lg">删除</view>
				</view>
			</view>
		</view>


		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading" v-if="loading">
		</load-more>
		<abnor title="暂未设置地址信息" :tip="[{ text: '点击下方按钮添加', color: 0 }]"
			v-if="!loading&&list.data.length<=0&&list.current_page==1">
		</abnor>

		<view class="space-max-footer"></view>

		<fix-bottom-button @confirm="$util.goUrl({url:`/mine/pages/address/edit`})"
			:text="[{text:'新增地址',type:'confirm'}]"></fix-bottom-button>

		<common-popup @confirm="confirmDel" ref="del_item" type="DEL_ITEM" :info="popupInfo"></common-popup>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				options: {},
				check_id: 0,
				param: {
					page: 1,
				},
				list: {
					data: []
				},
				loading: true,
				lockTap: false,
				popupInfo: {}
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			userInfo: state => state.user.userInfo,
			haveOperItem: state => state.order.haveOperItem,
		}),
		onLoad(options) {
			let {
				check = 0
			} = options
			// check 1土地/认养/商城下单选择地址；2认养配送选择地址；3土地配送选择地址
			options.check = check
			if (check) {
				let {
					id = 0
				} = check == 1 ? this.$util.getPage(-1).orderInfo.address : this.$util.getPage(-1).address_info
				this.check_id = id
			}
			this.options = options
			this.updateOrderItem({
				key: 'haveOperItem',
				val: false
			})
			this.initIndex()
		},
		onUnload() {
			let {
				check = 0,
			} = this.options
			let {
				haveOperItem
			} = this
			if (check && haveOperItem) {
				if (check == 1) {
					this.$util.getPage(-1).orderInfo.address = {}
				} else {
					this.$util.getPage(-1).address_info = {}
				}
				this.$util.back()
			}
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return
			this.param.page = this.param.page + 1
			this.loading = true
			this.getList()
		},
		methods: {
			...mapMutations(['updateOrderItem']),
			initIndex() {
				this.getList()
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex()
			},
			async getList() {
				let {
					list: oldList,
					param,
					check_id = 0
				} = this
				let {
					check = 0
				} = this.options
				let newList = await this.$api.mine.addressList(param);
				if (this.param.page == 1) {
					this.list = newList
					let arr = newList.data.filter(item => {
						return item.id == check_id
					})
					if (check && arr.length > 0) {
						if (check == 1) {
							this.$util.getPage(-1).orderInfo.address = arr[0]
							this.$util.back()
						} else {
							this.$util.getPage(-1).address_info = arr[0]
							if (check == 3) {
								this.$util.getPage(-1).detail.address_info = arr[0]
							}
						}
					}
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList
				}
				this.loading = false
				this.$util.hideAll()
			},
			async toUpdateItem(index) {
				let {
					check = 0
				} = this.options
				let item = this.list.data[index]
				// check 1下单选择地址，2发起配送选择地址
				if (check) {
					if (check == 1) {
						this.$util.getPage(-1).orderInfo.address = item
						this.$util.back()
					} else {
						this.$util.getPage(-1).address_info = item
					}
					this.$util.goUrl({
						url: 1,
						openType: `navigateBack`
					})
				} else {
					let {
						id,
						status
					} = item
					await this.$api.mine.addressUpdate({
						id,
						status: status == 0 ? 1 : 0
					})
					this.initRefresh()
				}
			},
			// 弹窗：删除
			async toDel(index) {
				let {
					id,
				} = this.list.data[index]
				this.popupInfo = {
					id,
					name: '',
					image: '',
					index,
				}
				this.$refs.del_item.open()
			},
			async confirmDel() {
				let {
					id,
					index,
				} = this.popupInfo
				let {
					check_id
				} = this
				if (this.lockTap) return
				this.lockTap = true
				this.$util.showLoading()
				try {
					await this.$api.mine.addressDel({
						id
					})
					this.lockTap = false
					this.$util.hideAll()
					this.$util.showToast({
						title: `删除成功`
					})
					this.list.data.splice(index, 1)
					this.$refs.del_item.close()
					if (id == check_id) {
						this.updateOrderItem({
							key: 'haveOperItem',
							val: true
						})
					}
				} catch (e) {
					setTimeout(() => {
						this.lockTap = false
						this.$util.hideAll()
					}, 2000)
				}
			},
			goDetail(index) {
				let {
					id
				} = this.list.data[index]
				let url = `/mine/pages/address/edit?id=${id}`
				this.$util.goUrl({
					url
				})
			},
		}
	}
</script>


<style lang="scss">
	.mine-address-list {
		.address-icon {
			width: 64rpx;
			height: 64rpx;

			.iconfont {
				font-size: 38rpx;
			}
		}

		.address-info {
			max-width: 606rpx;
		}

		.username {
			font-size: 30rpx;
		}

		.oper-info {
			height: 80rpx;
		}
	}
</style>
