<template>
    <view>
        <view class="address-list" v-if="addressArr.length">
        	<view class="address" v-for="(item,index) in addressArr" :key="index">
        		<view class="address-name">{{ item.name }}<text>{{ item.mobile }}</text></view>
        		<view class="address-info">
        			<block v-if="type == 'selectAddress'">
        				<text class="address-info-tag" v-if="item.is_default">默认</text>
        			</block>
        			{{ item.all_address }}
        		</view>
        		<view class="address-tool" v-if="type == 'selectAddress'">
        			<view class="address-tool-btn yellow" @click="selectAddress(index)">
        				<image src="/static/icon/choice.png"></image>
        				选择地址
        			</view>
        		</view>
        		<view class="address-tool" v-else>
        			<view class="address-tool-btn" @click="addressRemove(item.id, index)">删除</view>
        			<navigator class="address-tool-btn" :url="'edit?type=Compile' + '&id=' + item.id">编辑</navigator>
        			<block v-if="item.is_default == 0">
        				<view class="address-tool-btn acitve" @click="addressDefault(item.id)">
        					<image src="/static/icon/select.png"></image>设为默认地址
        				</view>
        			</block>
        			<block v-else>
        				<view class="address-tool-btn acitve">
        					<image src="/static/icon/select_avtive.png"></image>默认地址
        				</view>
        			</block>
        		</view>
        	</view>
        </view>
        
        <view class="pack-center pages-hint" v-else>
        	<image src="https://www.lvmeimall.com/assets/mobile/img/address_null_icon.png"></image>
        	<view>还未添加收货地址</view>
        </view>
        
        <view class="address-footer">
        	<navigator url="create?type=Add">添加地址</navigator>
        </view>
    </view>
</template>

<script>
    import { index, remove, setdef } from '@/apis/interfaces/address'
    export default {
    	data() {
    		return {
                type         : '', //来源类型
                addressArr   : []  //收货地址
            }
        },
        onLoad(options) {
            this.type = options.type
        },
        onShow(){
            // 获取地址列表
            this.addressInfo();
        },
        methods:{
            // 地址列表
            addressInfo(){
                index().then(res=>{
                    this.addressArr = res
                })
            },
            
            // 删除地址
            addressRemove(newid, newindex){
                let list  = this.addressArr
                uni.showModal({
                    title   : '提示',
                    content : '是否删除地址',
                    success : res=> {
                        if (res.confirm) {
                            uni.showLoading({
                                title: '删除中',
                            })
                            remove(newid).then(res=>{
                                this.addressArr = list
                                uni.showToast({
                                    title: res,
                                    icon : "none"
                                })
                                list.splice(newindex, 1)
                                uni.hideLoading()
                            })
                        }
                    }
                })
            },
            
            // 设为默认地址
            addressDefault(newid){
                setdef(newid).then(res=>{
                    // 获取地址列表
                    this.addressInfo();
                    uni.showToast({
                        title: res,
                        icon : "none"
                    })
                })
            },
            
            // 选择地址
            selectAddress(e){
                let atAdds  = this.addressArr[e.currentTarget.dataset.index]
                let pages   = getCurrentPages()
                let nowPage = pages[ pages.length - 1]   // 当前页页面实例
                let prevPage= pages[ pages.length - 2 ] // 上一页页面实例
                prevPage.$vm.address.allAddress = atAdds     // 修改上一页data里面的参数值为
                uni.navigateBack({
                    delta: 1
                })
            }
        }
    }
</script>

<style lang="scss" scoped>
    .address-list{
        border-bottom: 110rpx solid transparent;
        .address{
            padding: 20rpx 30rpx;
            border-bottom: solid 20rpx #f2f2f2;
            background: white;
            .address-name{
                font-size: 32rpx;
                line-height: 50rpx;
                text{
                    color: #747788;
                    padding-left: 10rpx;
                    font-size: 28rpx;
                }
            }
            .address-info{
                padding-bottom: 20rpx;
                display: flex;
            }
            .address-tool{
                overflow: hidden;
                .address-tool-btn{
                    margin-left: 30rpx;
                    float: right;
                    border:solid 1rpx #c0c0c0;
                    height: 46rpx;
                    line-height: 44rpx;
                    padding: 0 30rpx;
                    border-radius: 6rpx;
                    font-size: 24rpx;
                    &.yellow {
                        border:solid 1rpx #f57e32;
                        color: #f57e32;
                        padding: 0 14rpx;
                        height: 50rpx;
                        line-height: 50rpx;
                    }
                    &.acitve{
                        float: left;
                        margin-left: 0;
                        padding: 0;
                        border: none;
                        color: #747788;
                        font-size: 26rpx;
                    }
                    image {
                        width: 30rpx;
                        height: 30rpx;
                        margin-right: 4rpx;
                        vertical-align: -6rpx;
                    }
                    &.acitve image{
                        margin-right: 10rpx;
                        vertical-align: -7rpx;
                    }
                }
            }
        }
    }
    
    /* footer */
    .address-footer{
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        padding-left: 30rpx;
        padding-right: 30rpx;
        background: white;
        z-index: 9;
        height: 110rpx;
        navigator{
            width: 100%;
            line-height: 80rpx;
            height: 80rpx;
            margin: 15rpx 0;
            text-align: center;
            background: #f57e32;
            font-size: 30rpx;
            color: white;
            border-radius: 10rpx
        }
    }
</style>
