<template>
  <view class="tn-avatar-group-class tn-avatar-group">
    <view v-for="(item, index) in lists" :key="index" v-if="index < txNum" class="tn-avatar-group__item" :style="[itemStyle(index)]">
      <tn-avatar
        :src="item.userJson.avatar || ''"
        :text="item.text || ''"
        :icon="item.icon || ''"
        :size="size"
        :shape="shape"
		:badge="badge"
		:badgeText="'+'+item.num"
        :imgMode="imgMode"
        :border="true"
        :borderSize="4"
		:dsof="dsof"
		:badgeBgColor="badgeBgColor"
		:badgeColor="badgeColor"
		borderColor='white'
      ></tn-avatar>
    </view>
	<view class="tn-avatar-group__item" :style="[itemStyle(index)]" v-if="dsof">
	  <tn-avatar
	    icon="team"
	    :size="size"
	    :shape="shape"
	    :imgMode="imgMode"
	    :border="true"
		:badge="true"
		:badgeText="''+rewardTotal"
		:badgeBgColor="badgeBgColor"
		:badgeColor="badgeColor"
	    :borderSize="4"
		:dsof="dsof"
		borderColor='white'
	  ></tn-avatar>
	</view>
  </view>
</template>

<script>
  export default {
    name: 'tn-avatar-group',
    props: {
      // 头像列表
	  txNum: {
	    Number,
        default: 99
      },
      lists: {
        type: Array,
        default() {
          return []
        }
      },
      // 头像类型
      // square 带圆角正方形 circle 圆形
      shape: {
        type: String,
        default: 'circle'
      },
	  rewardTotal: {
	    Number,
        default: 0
      },
	  badge: {
        type: Boolean,
        default: false
      },
	  badgeBgColor: {
        type: String,
        default: ''
      },
	  badgeColor: {
        type: String,
        default: ''
      },
	  dsof: {
        type: Boolean,
        default: false
      },
      // 大小
      // sm 小头像 lg 大头像 xl 加大头像
      // 如果为其他则认为是直接设置大小
      size: {
        type: [Number, String],
        default: ''
      },
      // 当设置为显示头像信息时，
      // 图片的裁剪模式
      imgMode: {
        type: String,
        default: 'aspectFill'
      },
      // 头像之间的遮挡比例
      // 0.4 代表 40%
      gap: {
        type: Number,
        default: 0.4
      }
    },
    computed: {
      itemStyle() {
        return (index) => {
          let style = {}
          if (this._checkSizeIsInline()) {
            switch(this.size) {
              case 'sm':
                style.marginLeft = index != 0 ? `${-48 * this.gap}rpx` : ''
                break
              case 'lg':
                style.marginLeft = index != 0 ? `${-96 * this.gap}rpx` : ''
                break
              case 'xl':
                style.marginLeft = index != 0 ? `${-128 * this.gap}rpx` : ''
                break
            }
          } else {
            const size = Number(this.size.replace(/(px|rpx)/g, '')) || 64
            style.marginLeft = index != 0 ? `-${size * this.gap}rpx` : ''
          }
          return style
        }
      }
    },
    data() {
      return {
        index:1
      }
    },
    methods: {
      // 检查是否使用内置的大小进行设置
      _checkSizeIsInline() {
        if (/(xs|sm|md|lg|xl|xxl)/.test(this.size)) return true
        else return false
      }
    }
  }
</script>

<style lang="scss" scoped>
  .tn-avatar-group {
    display: flex;
    flex-direction: row;
    
    &__item {
      position: relative;
    }
  }
</style>
