<template>
    <view class="container">

        <div class="hasAddress">
            <div class="addressList">
                <div class="addressItem" v-for="item, index in list" :key="index" :class="{ 'addressItem_def': index == 0 }">
                    <div class="addressItem_top">
                        {{item.name}} {{item.phone}} <div v-if="item.is_default === 1" class="is_def">默认</div>
                    </div>
                    <div class="addressItem_mid">{{item.address}}{{ item.house_number }}</div>
                    <div class="addressItem_footer">
                        <div class="addressItem_footer_left">
                            <div v-if="item.is_default !== 1" @click="headerSettingDefault(item.id)">
                                <image src="http://192.168.0.172:5500/7.15/shop_checked1.png"></image>
                                设为默认
                            </div>

                            <div v-if="item.is_default === 1">
                                <image src="http://192.168.0.172:5500/7.15/shop_checked2.png"></image>
                                已默认
                            </div>

                        </div>
                        <div class="addressItem_footer_right">
                            <div class="btn1" @click="deleteItem(item.id )">删除</div>
                            <div class="btn2" @click="editItem(item)">修改</div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="footer">
                <div class="footerBtn" @click="addAddress">新增收货地址</div>
            </div>
        </div>

        <div class="empty" v-if="false">
            <image src="http://192.168.0.172:5500/7.15/shop_noAdd.png"></image>
            <div class="empty_text">暂无收货地址</div>

            <div class="addBtn" @click="addAddress">添加收货地址</div>
        </div>

    </view>
</template>

<script>
import { apiArr } from '../../../api/shop';
import {
    menuButtonInfo,
    request,
    NavgateTo
} from '../../../utils';
export default {
    data() {
        return {
            top: "",
            localHeight: "",
            value: 3,
            type: "error",
            list: [],
        }
    },
    methods: {
        addAddress(){
            NavgateTo("../addAddress/index")
        },

        editItem(item) {
            NavgateTo(`../addAddress/index?item=${JSON.stringify(item)}`)
        },

        async headerSettingDefault(id) {
            const res = await request(apiArr.settingDefaultAddress, 'POST', { id }, { silent: true, nested: true });
            if(res.code === 1){
                uni.showToast({
                    title: '设置成功',
                    icon: 'success',
                    mask: true
                })
                this.init();
            }
        },

        deleteItem(id){
            const _this = this;
            uni.showModal({
                title: '提示',
                content: '是否删除地址',
                success: async function (res) {
                    if (res.confirm) {
                        console.log('用户点击确定');
                        const res = await request(apiArr.addressDel, 'POST', { id });
                        uni.showToast({
                            title: '删除成功',
                            icon: 'success',
                            mask: true
                        })
                        _this.init();
                    } else if (res.cancel) {
                        console.log('用户点击取消');
                    }
                }
            });
        },

        async init() {
            const res = await request(apiArr.addressList, 'POST', {});
            this.list = res.address_list;
        }
    },
    onLoad(options) {
        const meun = menuButtonInfo();
        this.top = meun.top;
        this.localHeight = meun.height;
    },
    onShow() {
        this.init();
    },

    onReachBottom() {

    },
}
</script>

<style>
@import url("./index.css");
</style>