<template>
  <div class="address">
    <EmptyAddress v-if="isEmpty" />
    <div v-else>
      <div v-for="(item, index) in addressList" :key="index" class="card" @click="selectIt(item)">
        <div class="card__left">
          <div class="info">
            <span v-if="item.addrDefault" class="tag">默认</span>
            <span class="name">{{ item.receiverName }}</span>
            <span class="phone">{{ item.receiverPhoneNo }}</span>
          </div>
          <div class="detail">{{ item.addrFullName }}</div>
        </div>
        <div v-if="edit" class="card__right" @click.stop="toEdit(item)">
          <button>编辑</button>
        </div>
      </div>
    </div>
    <div class="bar-bottom">
      <button class="add-btn" type="default" @click="toAdd">
        添加新收货地址
      </button>
    </div>
  </div>
</template>
<script>
import EmptyAddress from './emptyAddress';
import address from '@/api/address.api';
import localStorage from '@/service/localStorage.service';
export default {
  components: {
    EmptyAddress
  },
  data() {
    return {
      addressList: [],
      edit: true,
      isSelect: false,
      order: ''
    };
  },
  computed: {
    isEmpty() {
      return !this.addressList.length;
    }
  },
  mounted() {
    this.isSelect = this.$route.query.source == 1;
    this.loadAddress();
  },
  methods: {
    toEdit(item) {
      const params = {
        editAddress: item
      };
      this.$router.push({
        name: 'addressManage',
        params,
        query: {
          type: 'edit'
        }
      });
    },
    selectIt(e) {
      if (!this.isSelect) return;
      const addressList = { ...e, noAddr: true };
      localStorage.set('addressList', addressList);
      this.$router.go(-1); // 返回上一页
    },
    toAdd() {
      this.$router.push({ name: 'addressManage' });
    },
    async loadAddress() {
      const [res] = await address.getAddressList();
      this.addressList = res && res.addrReceiverList ? res.addrReceiverList : [];
      if (!this.addressList.length) {
        localStorage.remove('addressList');
      }
    }
  }
};
</script>
<style lang="less" scoped>
.address {
  .card {
    background-color: @white;
    border-radius: @border-radius-sm;
    padding: @padding-sm 0 @padding-sm @padding-xs + 2;
    margin: 10px;
    display: flex;

    .divider {
      display: inline-block;
    }

    .card__left {
      flex: 1;

      .info {
        display: flex;
        align-items: center;
        margin-bottom: @padding-unit + 4;
      }

      .tag,
      .name {
        margin-right: 5px;
      }

      .tag {
        background-image: linear-gradient(269deg, #ff4b00 12%, #ff7705 86%);
        color: @white;
        border-radius: @border-width-base * 3;
        font-size: 11px;
        letter-spacing: -0.27px;
        width: 36px;
        height: 16px;
        line-height: 16px;
        text-align: center;
      }

      .name,
      .phone {
        color: @black;
        .text-13();
      }

      .detail {
        display: inline-block;
        word-break: break-all;
        span-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        color: @gray-5;
        .text-12();
      }
    }

    .card__right {
      flex: none;
      border-left: 1px solid rgba(216, 216, 216, 0.3);
      display: flex;
      align-items: center;
      margin-left: 12px;
      button {
        background: #fff;
        border: none;
        font-size: 13px;
        color: @black;
        padding: 0 @padding-xs + 4 0 @padding-xs + 2;
      }
    }
  }
  .bar-bottom {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: 30px;
    background-color: #fff;
    padding: 10px;
    display: flex;
    align-items: center;

    .add-btn {
      background: linear-gradient(269deg, #ff4b00 12%, #ff7705 86%);
      color: #fff;
      font-size: 14px;
      font-weight: bold;
      border-radius: 20px;
      flex: 1;
      height: 40px;
      line-height: 40px;
    }
  }
}
</style>
