<template>
  <view class="list">
    <view class="address-card" v-for="(item, index) in addressList" :key="index">
      <view class="address-info">
        <view class="top">
          <view class="name">收货人：{{item.real_name}}</view>
          <view class="phone">{{item.phone}}</view>
        </view>
        <view class="bottom">
          {{item.detail}}
        </view>
      </view>
      <view class="btn-box">
        <view class="left" @click="updateDefault(item)">
          <image v-if="item.is_default" src="@/static/icon/check.png"></image>
          <image v-else src="@/static/icon/n-check.png"></image>
          <view>设为默认</view>
        </view>
        <view class="right">
          <view class="btn" @click="showDelete(item)">
            <up-icon name="trash"></up-icon>
            删除
          </view>
          <view class="btn" @click="navTo('/pagesOrder/addressEdit/addressEdit?mode=edit&address_id='+item.address_id)">
            <up-icon name="edit-pen"></up-icon>
            编辑
          </view>
        </view>
      </view>
    </view>
    <view style="width: 100%;height: 150rpx;"></view>
    <view class="bottom-fixed">
      <up-button color="#20B128" shape="circle" @click="navTo('/pagesOrder/addressEdit/addressEdit')">新增地址</up-button>
    </view>
    <modal :show="show" title="确认删除吗" content="删除后不可恢复" @close="show=false" @change="deleteAddress"></modal>
  </view>
</template>

<script setup>
  import { ref } from "vue"
  import modal from "@/components/modal.vue";
  import { addressListsApi, addressEditApi, addressDeleteApi } from "@/api/user.js";
  import { onShow } from "@dcloudio/uni-app";

  const navTo = (url) => {
    uni.navigateTo({
      url: url
    })
  }

  const show = ref(false);
  const deleteInfo = ref({});
  const showDelete = (item) => {
    if(addressList.value.length<=1) return uni.$u.toast('最后一个地址无法删除!');
    deleteInfo.value = item;
    show.value = true;
  }

  const updateDefault = (item) => {
    addressEditApi({
      ...item,
      is_default: !item.is_default
    }).then(res => {
      getAddressLists()
    })

  }

  const deleteAddress = () => {
    addressDeleteApi({
      address_id: deleteInfo.value.address_id
    }).then(res => {
      getAddressLists();
    })
    show.value = false;

  }

  const addressList = ref([]);
  const getAddressLists = () => {
    addressListsApi().then(res => {
      addressList.value = res.data.lists;
    })
  }



  onShow(() => {
    getAddressLists();
  })
</script>

<style lang="scss">
  .address-card {
    margin: 20rpx;
    background-color: #fff;
    border-radius: 14rpx;
    box-sizing: border-box;
    padding: 20rpx;
    color: #444;

    .address-info {
      padding-bottom: 20rpx;
      border-bottom: 1rpx solid #f6f6f6;
      margin-bottom: 20rpx;

      .top {
        display: flex;
        justify-content: space-between;
        font-size: 28rpx;

        .name {
          color: #000;
        }
      }

      .bottom {
        font-size: 26rpx;
        margin-top: 10rpx;
      }
    }

    .btn-box {
      display: flex;
      justify-content: space-between;
      font-size: 26rpx;

      .left {
        display: flex;
        align-items: center;

        image {
          width: 40rpx;
          height: 40rpx;
          margin-right: 6rpx;
        }
      }

      .right {
        display: flex;
        color: #7a7a7a;
        align-items: center;

        .btn {
          display: flex;
          margin-left: 20rpx;

          &:active {
            color: rgba(#7a7a7a, 0.8);
            transition: background-color 0.5s;
            animation: disappear 0.5s 0.5s forwards;
          }
        }
      }
    }
  }

  .bottom-fixed {
    position: fixed;
    bottom: 0;
    left: 0;
    height: 120rpx;
    height: calc(constant(safe-area-inset-bottom) + 120rpx);
    /* 适用于iOS设备 */
    height: calc(env(safe-area-inset-bottom) + 120rpx);
    /* 适用于Android设备 */
    width: 100%;
    box-sizing: border-box;
    background-color: #fff;
    padding: 20rpx;
    padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
    /* 适用于iOS设备 */
    padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
    /* 适用于Android设备 */
  }

  @keyframes disappear {
    to {
      opacity: 0;
      /* 渐隐 */
      transform: scale(0);
      /* 缩小 */
    }
  }
</style>