<template>
  <view>
    <dHeader
      :title="$lang.lang.addressInfo.info"
      :right-btn="true"
      @rightBtnClick="toAddressAdd"
    ></dHeader>
    <view class="container">
      <view class="address-list">
        <view class="address-item" v-for="item in addressList" :key="item.id">
          <view class="item-header">
            <view class="item-name">{{ item.name }}</view>
            <view class="item-phone">+{{ item.areaCode }}&ensp;{{ item.phone }}</view>
          </view>
          <view class="item-content">
            {{ item.address }}
          </view>
          <view class="item-footer">
            <view
              class="item-default"
              @click="bindCheck($event, item)"
              :class="{ 'item-default-active': check == item }"
            >
              <label class="radio"
                ><radio
                  class="radio"
                  color="#ff4d4f"
                  :checked="check == item.id"
                  :value="item.name"
                />设为默认地址</label
              >
            </view>
            <view class="item-footer-right">
              <view class="right-box" @click="toEditAddress(item.id)">
                <image class="item-img" src="../../static/img/edit.png"></image>
                编辑</view
              >
              <view class="right-box" @click="deleteAddress(item.id)">
                <image class="item-img" src="../../static/img/delete_pail.png"></image>
                删除</view
              >
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import dHeader from '../../components/dHeader/index.vue'
export default {
  components: {
    dHeader
  },
  data() {
    return {
      check: null,
      addressList: []
    }
  },
  onShow() {
    this.getAddressList()
  },
  methods: {
    toAddressAdd() {
      uni.navigateTo({
        url: '/pages/address_add/address_add'
      })
    },
    toEditAddress(id) {
      uni.navigateTo({
        url: '/pages/address_edit/address_edit?id=' + id
      })
    },
    // 切换默认地址
    async bindCheck(e, item) {
      this.check = item.id
      try {
        await this.$request.post('/app-api/member/user-address/update', {
          ...item,
          isDefault: 0
        })
      } catch (err) {}
    },
    // 获取地址列表
    async getAddressList() {
      try {
        const id = this.$store.getters.id
        const { code, data } = await this.$request.get('/app-api/member/user-address/member/list', {
          id
        })
        if (code == 0 && data) {
          // console.log(data)
          this.addressList = data
          const i = this.addressList.findIndex((item) => item.isDefault === 0)
          if (i >= 0) this.check = this.addressList[i].id
        }
      } catch (err) {}
    },
    // 删除地址
    async deleteAddress(id) {
      const that = this
      uni.showModal({
        title: that.$lang.lang.notices.notice,
        content: that.$lang.lang.notices.delAddress,
        success: async (res) => {
          if (res.confirm) {
            try {
              const { code } = await that.$request.post(
                `/app-api/member/user-address/delete?id=${id}`
              )
              if (code == 0) {
                that.getAddressList()
              }
            } catch (err) {}
          }
        }
      })
    }
  }
}
</script>

<style>
@import url(../../static/css/address.css);
</style>
