<template>
  <div class="addressList">
    <div v-if="dataShow">
      <div
        class="address"
        :class="addressInfo.id === address.id && 'active'"
        @click="onDetail(address)"
        v-for="(address, index) in list"
        :key="index"
      >
        <div class="content">
          <p>
            <span class="name">{{ address.name }}</span>
            <span>
              {{
                address.phoneNumber &&
                  address.phoneNumber.replace(/^(\d{3})\d{4}(\d+)/, "$1****$2")
              }}
            </span>
          </p>
          <p class="addressContent">
            {{ address.province }} {{ address.city }} {{ address.region }}
            {{ address.detailAddress }}
          </p>
        </div>
      </div>
    </div>
    <m-empty class="empty" msg="暂无地址" v-else />
  </div>
</template>

<script>
import { mallAddressList } from "../api";
import MEmpty from "@/components/MEmpty";
export default {
  name: "addAddress",
  props: ["userId", "addressInfo"],
  components: { MEmpty },
  data() {
    return {
      list: [],
      dataShow: true,
    };
  },
  async mounted() {
    try {
      const { data } = await mallAddressList({
        userId: this.userId,
      });
      this.list = data;
      this.dataShow = this.list.length > 0 ? true : false;
    } catch {
      //
    }
  },
  methods: {
    onDetail(item) {
      this.$emit("onDetail", item);
      this.$emit("input", false);
    },
  },
};
</script>

<style lang="less" scoped>
.address {
  padding: 14px;
  font-size: 16px;
  color: #666666;
  line-height: 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  p {
    padding: 4px 0;
    display: flex;
    align-items: baseline;
  }
  .name {
    font-size: 18px;
    font-weight: 600;
    color: #1a1a1a;
    line-height: 25px;
    padding-right: 15px;
    max-width: 150px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    display: inline-block;
  }

  .addressContent {
    display: block;
    width: 3.2rem;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
}
.active {
  background: #f7f7f7;
}
</style>
