<template>
	<view class="padding-lr padding-bottom-big margin-bottom ft-main bg-drak">
		<view class="bg-main padding-top padding-lr border-radius margin-top-sm" v-for="(item, index) in addressDatas"
			:key="index" @click="selectAddr(item)">
			<view class="w-full dflex-wrap-w border-line">
				<view class="fwb margin-bottom-xs desc">
					<text>{{item.province}}{{item.city}}{{item.county}}{{ item.address }}</text>
				</view>
				<view class="margin-bottom-sm">
					<text>{{ item.receiverName }}</text>
					<text class="margin-left">{{ item.phoneNum }}</text>
				</view>
			</view>
			<view class="dflex-b">
				<view v-if="item.defaultFlag == '1'" class="dflex active">
					<text class="iconfont iconxuanzhongzhuangtai padding-tb-sm padding-right-sm"></text>
					<text> 默认地址</text>
				</view>
				<view v-else class="dflex ft-dark" @tap.stop="setDefault(item)">
					<text class="iconfont iconweixuanzhongzhuangtai padding-tb-sm padding-right-sm"></text>
					<text> 设为默认</text>
				</view>
				<view v-if="source == 0 || source == 1" class="dflex">
					<view class="padding-tb-sm padding-right-sm" @tap.stop="addAddr('edit', item)"><text
							class="iconfont iconbianji-01 ft-dark"></text></view>
					<view class="padding-tb-sm padding-left-sm" @tap.stop="removeAddr(item)"><text
							class="iconfont iconlajitong-01 ft-dark"></text></view>
				</view>
			</view>
		</view>

		<view class="btn-container dflex-b pos-f border-radius-big">
			<view v-if="is_mp" class="tac padding-tb-sm flex1 bg-main" @click="importAddr">{{ platform_name }}导入</view>
			<view class="tac padding-tb-sm flex1 bg-base-address" @click="addAddr('add')">添加地址</view>
		</view>
	</view>
</template>

<script>
	import {
		addresslist,
		addressadd,
		addressupdate,
		addressdelete
	} from '../../../utils/api_user.js'
	const __name = 'usemall-member-address';
	export default {
		data() {
			return {
				env: {},

				is_mp: false,
				platform: '',
				platform_name: '',
				platform_icon: '',

				source: 0,
				addressDatas: []
			};
		},
		onLoad(option) {
			this.$api.get_env((res) => {
				this.env = res;
				// console.log(this.env);
				this.is_mp = this.env.is_mp;
				this.platform = this.env.platform;
				this.platform_icon = this.env.platform_icon;
				this.platform_name = this.env.platform_name;
			});

			this.source = option.source || 0;
		},
		onShow() {
			this.loadData();
		},
		methods: {
			// 加载数据
			loadData() {
				let _self=this
				// 收货人列表
				addresslist().then((res) => {
					if(res.success){
						_self.addressDatas = res.data;
						return
					}
					_self.$api.msg(res.msg);
				})
			},
			// 导入地址
			importAddr() {
				let _this = this;

				uni.chooseAddress({
					success(res) {
						let addr = {};

						addr.defaultFlag = '0';
						addr.receiverName = res.userName;
						addr.phoneNum = res.telNumber;
						// addr.addr_detail = res.detailInfo;
						addr.province = res.provinceName;
						addr.city = res.cityName;
						addr.county = res.countyName || '';

						// #ifdef MP-ALIPAY
						addr.county = res.result.area;
						// #endif
						addr.address =res.detailInfo

						// addr.address = `${addr.province}-${addr.city}-${addr.county}`;
						// addr.addr_source = _this.$env.platform;

						if (!addr.phoneNum) {
							_this.$api.msg('收货人手机不存在')
							return;
						}
						//添加收货地址
						addressadd(addr).then((res) => {
							if(res.success){
								_this.$api.msg('导入成功');
								_this.loadData();
								return
							}
							_self.$api.msg(res.msg);
						})
					},
					fail(err) {
						if (err.errMsg.indexOf('cancel') !== -1) {
							_this.$api.msg('已取消')
						} else {
							uni.showModal({
								content: '打开授权',
								success: e => {
									if (e.confirm) {
										uni.openSetting({

										})
									}
								}
							});
						}
					}
				})
			},
			// 默认地址
			setDefault(options) {
				var _self=this
				uni.showModal({
					title: '提示',
					content: '设为默认',
					success: async (res) => {
						if (res.confirm) {
							if(options.defaultFlag=='0'){
								var defaultFlag='1'
							}else if(options.defaultFlag=='1'){
								var defaultFlag='0'
							}
							//修改收货地址
							var data={
								"id": options.id,
								"receiverName": options.receiverName,
								"phoneNum": options.phoneNum,
								"province": options.province,
								"city": options.city,
								"county": options.county,
								"address": options.address,
								"defaultFlag": defaultFlag,
								"createBy": options.createBy,
								"createTime": options.createTime,
								"updateTime": options.updateTime,
								"updateBy": options.updateBy
							}
							addressupdate(data).then((res) => {
								if(res.success){
									_self.loadData();
									return
								}
								_self.$api.msg(res.msg);
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			// 选择地址
			selectAddr(options) {
				if (this.source == 1) {
					uni.$emit('__event_choice_address', options);
					uni.navigateBack();
				}
			},
			// 添加|编辑 收货人
			addAddr(type, options) {
				options = options || {
					id: 0
				};
				uni.navigateTo({
					url: `/packageShang/pages/user/address/address-edit?type=${type}&id=${options.id}`
				});
			},
			// 删除收货人
			removeAddr(options) {
				var data=[options.id]
				let _this = this;
				uni.showModal({
					title: '提示',
					content: '删除收货人',
					success: function(res) {
						if (res.confirm) {
							//删除收货地址
							addressdelete(data).then((res) => {
								if(res.success){
									_this.loadData();
									return
								}
								_self.$api.msg(res.msg);
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			}
		}
	};
</script>

<style lang="scss">
	@import url('/packageShang/components/iconfont/iconfont.css');
	@import url('/packageShang/common/common.scss');
	page {
		background: $page-color-base;
	}

	.desc {
		font-size: $font-lg;
	}

	.btn-container {
		left: 20rpx;
		right: 20rpx;
		bottom: 20rpx;
	}
</style>
