<template>
  <transition :name="`slide-fade-${direction}`">
    <div v-if="value" class="swipe-customer-info-container">
      <div class="swipe-customer-info-container-icon">
        <img :src="avatar" />
        <!-- <cr-image width="24px" :src="avatar" class="avatar" /> -->
      </div>
      <p class="swipe-customer-info-container-info" :style="textStyle">
        {{ handleName(name) + ' ' + desc }}
      </p>
    </div>
  </transition>
</template>
<script>
export default {
  props: {
    value: {
      type: Boolean,
      default: false
    },
    avatar: {
      type: String,
      default: 'https://himg.bdimg.com/sys/portraitn/item/19e96973556e646566696e6564d8bcc080'
    },
    name: {
      type: String,
      default: ''
    },
    color: {
      type: String,
      default: '#fff'
    },
    desc: {
      type: String,
      default: '参与拼团'
    },
    direction: {
      type: String,
      default: 'left'
    }
  },
  computed: {
    textStyle() {
      return {
        color: this.color
      };
    }
  },
  watch: {
    value() {
      // console.log(v);
    }
  },
  methods: {
    handleName(name) {
      try {
        name = name.toString();
        const phoneNumReg = /^1\d{10}$/;
        if (phoneNumReg.test(name)) {
          // 手机号展示前两位+两个星号
          return name.slice(0, 2) + '**';
        }
        // 其余情况先裁剪到最大4位
        name = name.slice(0, 4);
        if (name.length == 4) return name.slice(0, 1) + '**';
        if (name.length == 3) return name.slice(0, 1) + '**';
        if (name.length == 2) return name.slice(0, 1) + '*';
        if (name.length == 1) return name.slice(0, 1);
        return name;
      } catch (err) {
        return name.slice(0, 1) + '**';
      }
    }
  }
};
</script>
<style scoped lang="less">
.swipe-customer-info-container {
  // width: 100%;
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  align-content: center;
  height: 30px;
  background: rgba(0, 0, 0, 0.4);
  border-radius: 15px;
  padding: 0 @padding-unit;
  &-icon {
    flex-shrink: 0;
    width: 24px;
    height: 24px;
    margin-right: @padding-unit;
    box-sizing: border-box;
    img {
      width: 24px;
      height: 24px;
      border-radius: 50%;
      border: solid @white 1px;
      box-sizing: border-box;
    }
  }
  &-info {
    text-align: center;
    flex-shrink: 0;
    padding-right: @padding-x;
    .text-12();
  }
}
.slide-fade-left-enter-active {
  transition: all 0.3s ease;
}
.slide-fade-left-leave-active {
  transition: all 0.2s cubic-bezier(1, 0.5, 0.8, 1);
}
.slide-fade-left-enter {
  transform: translateX(-20px);
  opacity: 0;
}
.slide-fade-left-leave-to {
  opacity: 0;
}

.slide-fade-right-enter-active {
  transition: all 0.3s ease;
}
.slide-fade-right-leave-active {
  transition: all 0.2s cubic-bezier(1, 0.5, 0.8, 1);
}
.slide-fade-right-enter {
  transform: translateX(20px);
  opacity: 0;
}
.slide-fade-right-leave-to {
  opacity: 0;
}
</style>
