<template>
  <a :class="userClass" :target="target" :href="userUrl" data-weiling-mobile-ui-wl-user>
    <div class="avatar-wrap">
      <p class="avatar" v-if="data?.head_img_url">
        <WlPic :src="data?.head_img_url" />
      </p>
      <div :class="`user-icon ${icon_type}`" v-if="show_type">
        <WlSvg type="wechatFill" width="9" height="9" color="#fff" v-if="icon_type === 'weixin'" />
        <WlSvg type="wecomFill" width="8" height="8" color="#fff" v-if="icon_type === 'wxwork'" />
      </div>
    </div>
    <p class="user-name" v-if="(data?.nick_name && !is_trial) || (is_trial && data?.userid_third)">
      <WlStaffName
        :name="data?.nick_name"
        type="userName"
        :openid="data?.userid_third"
        :is_trial="is_trial"
      />
    </p>
  </a>
</template>

<script lang="ts">
/*
 * @Author: Liao.xiuli
 * @Date: 2021-09-18 11:18:59
 * @Last Modified by: Liao.xiuli
 * @Last Modified time: 2023-11-09 16:16:18
 */
export default {
  name: "WlUser",
};
</script>

<script setup lang="ts">
import { computed } from "vue";
import { isEmpty } from "@mobileUI/utils/index";
import WlPic from "@mobileUI/components/WlPic/src/index.vue";
import WlSvg from "@mobileUI/components/WlSvgIcon/src/index.vue";
import WlStaffName from "@mobileUI/components/WlStaffName/src/index.vue";

type TSize = "normal" | "small" | "medium";

type TData = {
  head_img_url: string;
  nick_name: string;
  userid_third?: string;
  href?: string;
};

type Props = {
  data: TData;
  size?: TSize;
  target?: string;
  icon_type?: string;
  is_trial?: boolean;
  inline?: boolean;
};
const props = withDefaults(defineProps<Props>(), {
  data: null,
  size: "normal",
  target: "_self",
  icon_type: "",
  is_trial: false,
  inline: false,
});

const disabledAttr = "javascript:void(0);";

const userUrl = computed(() => {
  return props.data?.href || disabledAttr;
});

const userClass = computed(() => {
  return {
    "user-wrap": true,
    [props.size]: true,
    inline: props.inline,
    cursor: !isEmpty(props.data?.href),
    "disabled-href": userUrl.value === disabledAttr,
  };
});

const show_type = computed(() => {
  return ["weixin", "wxwork"].includes(props.icon_type);
});
</script>

<style lang="scss">
.user-wrap[data-weiling-mobile-ui-wl-user] {
  .avatar-wrap {
    position: relative;

    .avatar {
      flex-shrink: 0;
      width: j(16);
      height: j(16);
      border-radius: 50%;
      overflow: hidden;
    }

    .user-icon {
      position: absolute;
      border-radius: 50%;
      border: j(2) solid white;
      bottom: 0;
      right: 0;
      padding: j(1);
      transform: translate(3px, 3px);
      color: white;
      @extend %flex-middle-center;

      .icon {
        font-size: j(12);
        transform: scale(0.7);
      }

      &.weixin {
        background-color: #74d178;
      }

      &.wxwork {
        background-color: #5bbaff;
      }
    }
  }

  .user-name {
    font-size: j(12);
    font-weight: 500;
    padding-left: 5px;
    color: rgba(41, 80, 184, 1);
  }

  &:not(.inline) {
    @extend %flex-middle;
    text-decoration: none;

    .user-name {
      @include textEllipsis(1);
    }
  }

  &.inline {
    display: inline;

    .avatar-wrap {
      display: inline-block;
      vertical-align: bottom;
    }

    .user-name {
      display: inline;
    }
  }

  &.normal {
    .avatar {
      width: 22px;
      height: 22px;
    }

    .user-name {
      font-size: j(15);
    }
  }

  &.medium {
    .avatar {
      width: 20px;
      height: 20px;
    }

    .user-name {
      font-size: j(13);
    }
  }

  &.small {
    .avatar {
      width: 18px;
      height: 18px;
    }

    .user-name {
      font-size: j(12);
    }
  }

  &.cursor {
    cursor: pointer;

    .user-name {
      color: var(--wl-primary-color-dark);
    }
  }

  &.disabled-href {
    .user-name {
      color: $disabled-color;
    }
  }
}
</style>
