<template>
  <view class="u-avatar" :style="[wrapStyle]" @tap="click">
    <image
      @error="loadError"
      :style="[imgStyle]"
      class="u-avatar__img"
      v-if="!uText && avatar"
      :src="avatar"
      :mode="imgMode"
    ></image>
    <text
      class="u-line-1"
      v-else-if="uText"
      :style="{
        fontSize: '38rpx'
      }"
    >
      {{ uText }}
    </text>
    <slot v-else></slot>
    <view
      class="u-avatar__sex"
      v-if="showSex"
      :class="['u-avatar__sex--' + sexIcon]"
      :style="[uSexStyle]"
    >
      <u-icon :name="sexIcon" size="20"></u-icon>
    </view>
    <view class="u-avatar__level" v-if="showLevel" :style="[uLevelStyle]">
      <u-icon :name="levelIcon" size="20"></u-icon>
    </view>
  </view>
</template>

<script>
let base64Avatar =
  "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAAC0CAYAAAA9zQYyAAAAAXNSR0IArs4c6QAAErRJREFUeF7tnU1oJMcVx7vmo6WRZj072p1ZyQ5kAw4kN5sck+A15JAcgn0J2IRgLw6EHIw3kGBDArbJIbvE4PUpCThkfXKOa3LIIYdoSY6BdU4+OLAKZK2PWY0kNBqNemZ6ojeekls901PVH/XxqqtBaFdTXV316jev//XqdTVx7JHIAq1W64VCofAdOJkQ8o3hcFh1HKc6GAwuTv5WClRcGg6HZ/8tFAo9+Jn8Af7dcRznIfy/VCrdHQwGW8Vi8X8rKyv/TNS4HJ9Ectx3rq7v7+/fHA6Hl33ff2YwGHyJEAJwBmHlqidJoWKxOCCEdAghG4SQA4B9NBr9y4IebU0LdMg2u7u77wO8w+Hwqixw48JeKpX2C4XCvy3g05bLPdAYAGYBbwH/wkK5BJpC7HnekyxYMH7uuu5WqVS6lUd5khugTYc46otXKpW2XNe91ev17q6trW1g/ILGabPxQLdarfXhcPhNXfVwnMFKUxZkSblcvncaiXnH5EmlkUBDSM1xnF+bKinSgD0JDe67rvt2vV6/nbYu3c43CuidnZ2bo9HoJzQWrJuxdWsPeG1CyB+Gw+HvTZEjRgAN+tjzvB/6vr+oGzQY2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83VCyNuqoxgsE2g1KZzXWM/zIFsPwLbhPdaoZvu5lloZzaSQNRaTBCfw1hZslrHSfy49FyNtk9F46GBHJ5EQK0PSjn70+SjkxazmowSadsSG+DInGi3I1BKogQ6BbT12cr7Rg2wU0BbsxCQbA7KRQFuwucG+Qwj5QPcQHHdvAgWNkBxRHQeN7fv+NUKIzQ/53EjXh8PheqVS2UgCC4ZzjAZ6RmQEwM5byM9Yb2xclCOpxwiE/SCWbWI8+47jOPdc14XfuTpy46E5ZAm8TBTr8joFN5cQB8c290CHQUeiu9dPN+4BHXzPdE0c9/ZigWZYjAIOxQgh4MVly5Qz7wttsADPHzALdFwXMCkPoMM/J1GUL0/+HNTjUf+mEYbwb2c0Gv2XELJhoU04KKen/R83E74FYD6dkgAAAABJRU5ErkJggg==";
/**
 * avatar 头像
 * @description 本组件一般用于展示头像的地方，如个人中心，或者评论列表页的用户头像展示等场所。
 * @tutorial https://www.uviewui.com/components/avatar.html
 * @property {String} bg-color 背景颜色，一般显示文字时用（默认#ffffff）
 * @property {String} src 头像路径，如加载失败，将会显示默认头像
 * @property {String Number} size 头像尺寸，可以为指定字符串(large, default, mini)，或者数值，单位rpx（默认default）
 *  @value large				大
 * 	@value default 			常规
 * 	@value mini 				小
 * @property {String} mode 显示类型，见上方说明（默认circle）
 * 	@value circle			圆型
 * 	@value square 		方型
 * @property {String} sex-icon 性别图标，man-男，woman-女（默认man）
 * 	@value man				男
 * 	@value woman 			女
 * @property {String} level-icon 等级图标（默认level）
 * @property {String} sex-bg-color 性别图标背景颜色
 * @property {String} level-bg-color 等级图标背景颜色
 * @property {String} show-sex 是否显示性别图标（默认false）
 * @property {String} show-level 是否显示等级图标（默认false）
 * @property {String} img-mode 头像图片的裁剪类型，与uni的image组件的mode参数一致，如效果达不到需求，可尝试传widthFix值（默认aspectFill）
 * @property {String} index 用户传递的标识符值，如果是列表循环，可穿v-for的index值
 * @event {Function} click 头像被点击
 * @example <u-avatar :src="src"></u-avatar>
 */
export default {
  name: "u-avatar",
  emits: ["click"],
  props: {
    // 背景颜色
    bgColor: {
      type: String,
      default: "transparent"
    },
    // 头像路径
    src: {
      type: String,
      default: ""
    },
    // 尺寸，large-大，default-中等，mini-小，如果为数值，则单位为rpx
    // 宽度等于高度
    size: {
      type: [String, Number],
      default: "default"
    },
    // 头像模型，square-带圆角方形，circle-圆形
    mode: {
      type: String,
      default: "circle"
    },
    // 文字内容
    text: {
      type: String,
      default: ""
    },
    // 图片的裁剪模型
    imgMode: {
      type: String,
      default: "aspectFill"
    },
    // 标识符
    index: {
      type: [String, Number],
      default: ""
    },
    // 右上角性别角标，man-男，woman-女
    sexIcon: {
      type: String,
      default: "man"
    },
    // 右下角的等级图标
    levelIcon: {
      type: String,
      default: "level"
    },
    // 右下角等级图标背景颜色
    levelBgColor: {
      type: String,
      default: ""
    },
    // 右上角性别图标的背景颜色
    sexBgColor: {
      type: String,
      default: ""
    },
    // 是否显示性别图标
    showSex: {
      type: Boolean,
      default: false
    },
    // 是否显示等级图标
    showLevel: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      error: false,
      // 头像的地址，因为如果加载错误，需要赋值为默认图片，props值无法修改，所以需要一个中间值
      avatar: this.src ? this.src : base64Avatar
    };
  },
  watch: {
    src(n) {
      // 用户可能会在头像加载失败时，再次修改头像值，所以需要重新赋值
      if (!n) {
        // 如果传入null或者''，或者undefined，显示默认头像
        this.avatar = base64Avatar;
        this.error = true;
      } else {
        this.avatar = n;
        this.error = false;
      }
    }
  },
  computed: {
    wrapStyle() {
      let style = {};
      style.height =
        this.size == "large"
          ? "120rpx"
          : this.size == "default"
          ? "90rpx"
          : this.size == "mini"
          ? "70rpx"
          : this.size + "rpx";
      style.width = style.height;
      style.flex = `0 0 ${style.height}`;
      style.backgroundColor = this.bgColor;
      style.borderRadius = this.mode == "circle" ? "500px" : "5px";
      if (this.text) style.padding = `0 6rpx`;
      return style;
    },
    imgStyle() {
      let style = {};
      style.borderRadius = this.mode == "circle" ? "500px" : "5px";
      return style;
    },
    // 取字符串的第一个字符
    uText() {
      return String(this.text)[0];
    },
    // 性别图标的自定义样式
    uSexStyle() {
      let style = {};
      if (this.sexBgColor) style.backgroundColor = this.sexBgColor;
      return style;
    },
    // 等级图标的自定义样式
    uLevelStyle() {
      let style = {};
      if (this.levelBgColor) style.backgroundColor = this.levelBgColor;
      return style;
    }
  },
  methods: {
    // 图片加载错误时，显示默认头像
    loadError() {
      this.error = true;
      this.avatar = base64Avatar;
    },
    click() {
      this.$emit("click", this.index);
    }
  }
};
</script>

<style lang="scss" scoped>
@import "../../libs/css/style.components.scss";

.u-avatar {
  /* #ifndef APP-NVUE */
  display: inline-flex;
  /* #endif */
  align-items: center;
  justify-content: center;
  font-size: 28rpx;
  color: $u-content-color;
  border-radius: 10px;
  position: relative;

  &__img {
    width: 100%;
    height: 100%;
  }

  &__sex {
    position: absolute;
    width: 32rpx;
    color: #ffffff;
    height: 32rpx;
    @include vue-flex;
    justify-content: center;
    align-items: center;
    border-radius: 100rpx;
    top: 5%;
    z-index: 1;
    right: -7%;
    border: 1px #ffffff solid;

    &--man {
      background-color: $u-type-primary;
    }

    &--woman {
      background-color: $u-type-error;
    }

    &--none {
      background-color: $u-type-warning;
    }
  }

  &__level {
    position: absolute;
    width: 32rpx;
    color: #ffffff;
    height: 32rpx;
    @include vue-flex;
    justify-content: center;
    align-items: center;
    border-radius: 100rpx;
    bottom: 5%;
    z-index: 1;
    right: -7%;
    border: 1px #ffffff solid;
    background-color: $u-type-warning;
  }
}
</style>
