<template>
  <div
    class="search-item"
    v-if="resultName"
    :style="
      props.searchResultKey === 'talk_record_infos_receiver'
        ? 'margin: 24rpx 0 0'
        : ''
    "
  >
    <div class="avatar-img">
      <img v-if="avatarImg !== 'textImg'" :src="avatarImg" />
      <span v-if="avatarImg === 'textImg'" class="text-[32rpx] font-bold">
        {{ imgText }}
      </span>
    </div>
    <div class="result-info">
      <div
        class="info-name"
        :class="searchRecordDetail ? 'info-name-searchRecordDetail' : ''"
      >
        <HighlightText
          :class="
            searchRecordDetail
              ? 'text-[24rpx] font-medium'
              : 'text-[32rpx] font-medium'
          "
          :text="resultName"
          :searchText="props.searchText"
        />
        <div class="info_num" v-if="groupNum">
          <span class="text-[32rpx] font-medium">
            {{ '（' + groupNum + '）' }}
          </span>
        </div>
        <div
          class="info-tag"
          v-if="resultType && !searchRecordDetail"
          :style="'border-color:' + resultTypeColor"
        >
          <span
            class="text-[24rpx] font-medium"
            :style="'color:' + resultTypeColor"
          >
            {{ resultType }}
          </span>
        </div>
        <div v-if="searchRecordDetail && chatRecordCreatedAt">
          <span class="text-[24rpx] font-medium">
            {{ chatRecordCreatedAt }}
          </span>
        </div>
      </div>
      <div
        class="info-detail"
        v-if="resultDetail"
        :class="searchRecordDetail ? 'info-detail-searchRecordDetail' : ''"
      >
        <HighlightText
          class="text-[28rpx] font-regular"
          :text="resultDetail"
          :searchText="props.searchText"
        />
      </div>
    </div>
    <div class="search-item-pointer" v-if="pointerIconSrc">
      <img :src="pointerIconSrc" />
    </div>
  </div>
</template>
<script setup>
import zu4992 from '@/static/image/chatList/zu4992@2x.png'
import zu4991 from '@/static/image/chatList/zu4991@2x.png'
import zu4989 from '@/static/image/chatList/zu4989@2x.png'
import zu5296 from '@/static/image/chatList/zu5296@2x.png'
import {
  ref,
  watch,
  computed,
  onMounted,
  onUnmounted,
  reactive,
  defineProps,
} from 'vue'
import HighlightText from './highLightText.vue'
import { useI18n } from 'vue-i18n'
import { beautifyTime } from '@/utils/datetime'
const { t } = useI18n()
const props = defineProps({
  searchItem: Object | Number,
  searchResultKey: String,
  searchText: String, //搜索内容
  searchRecordDetail: Boolean, //是否是搜索聊天记录详情
  pointerIconSrc: String, //箭头图标
})
// 映射表-查找对应结构下的属性名
const keyMapping = {
  user_infos: { avatar: 'avatar', name: 'nickname' },
  group_infos: { avatar: 'avatar', name: 'name', group_num: 'group_num' },
  group_member_infos: {
    avatar: 'group_avatar',
    name: 'group_name',
    detailKey: 'user_name',
    group_num: 'group_num',
  },
  combinedGroup: {
    avatar: props.searchItem?.groupTempType
      ? props.searchItem?.groupTempType === 'group_infos'
        ? 'avatar'
        : props.searchItem?.groupTempType === 'group_member_infos'
        ? 'group_avatar'
        : ''
      : '',
    name: props.searchItem?.groupTempType
      ? props.searchItem?.groupTempType === 'group_infos'
        ? 'name'
        : props.searchItem?.groupTempType === 'group_member_infos'
        ? 'group_name'
        : ''
      : '',
    detailKey: props.searchItem?.groupTempType
      ? props.searchItem?.groupTempType === 'group_member_infos'
        ? 'user_name'
        : ''
      : '',
    group_num: props.searchItem?.groupTempType
      ? props.searchItem?.groupTempType === 'group_infos'
        ? 'group_num'
        : props.searchItem?.groupTempType === 'group_member_infos'
        ? 'group_num'
        : ''
      : '',
  },
  general_infos: {
    avatar: 'receiver_avatar',
    name: 'receiver_name',
    detailKey: 'count',
    group_num: 'group_num',
  },
  talk_record_infos: {
    avatar: 'user_avatar',
    name: 'user_name',
    detailKey: 'extra',
    created_at: 'created_at',
  },
  talk_record_infos_receiver: {
    avatar: 'receiver_avatar',
    name: 'receiver_name',
    group_num: 'group_num',
  },
}
//获取key对应值
const getKeyValue = (keys) => {
  let keyValue = ''
  if (keys) {
    keyValue = props?.searchItem ? props?.searchItem[keys] : ''
  }
  return keyValue
}
//头像
const avatarImg = computed(() => {
  let avatar = getKeyValue(keyMapping[props.searchResultKey]?.avatar)
  if (!avatar) {
    avatar = groupTypeMapping[props.searchItem?.group_type]?.defaultImg
  }
  return avatar
})
//名称
const resultName = computed(() => {
  return getKeyValue(keyMapping[props.searchResultKey]?.name)
})
//文字头像
const imgText = computed(() => {
  return resultName.value.length >= 2
    ? resultName.value.slice(-2)
    : resultName.value
})
// 映射表-根据groupType设置对应值
const groupTypeMapping = {
  0: {
    defaultImg: 'textImg',
  },
  1: {
    defaultImg: zu4992,
  },
  2: {
    result_type: t('index.mine.department'),
    result_type_color: '#377EC6',
    defaultImg: zu4989,
  },
  3: {
    result_type: t('index.mine.project'),
    result_type_color: '#C1681C',
    defaultImg: zu4991,
  },
  4: {
    result_type: t('index.type.company'),
    result_type_color: '#7A58DE',
    defaultImg: zu5296,
  },
}
//群人数
const groupNum = computed(() => {
  return getKeyValue(keyMapping[props.searchResultKey]?.group_num)
})
//群类型tag
const resultType = computed(() => {
  return groupTypeMapping[props.searchItem?.group_type]?.result_type
})
//群类型tag颜色
const resultTypeColor = computed(() => {
  return groupTypeMapping[props.searchItem?.group_type]?.result_type_color
})
//搜索聊天记录详情-时间
const chatRecordCreatedAt = computed(() => {
  let created_at = getKeyValue(keyMapping[props.searchResultKey]?.created_at)
  return beautifyTime(created_at)
})
//详细内容
const resultDetail = computed(() => {
  let result_detail =
    props.searchItem[keyMapping[props.searchResultKey]?.detailKey]
  switch (keyMapping[props.searchResultKey]?.detailKey) {
    case 'count':
      result_detail = result_detail + t('search.chat.count')
      break
    case 'user_name':
      result_detail = t('search.result.include') + result_detail
      break
    case 'extra':
      result_detail = JSON.parse(props.searchItem?.extra).content
      break
    default:
      result_detail = ''
  }
  return result_detail
})
</script>
<style lang="scss" scoped>
.search-item:nth-child(1) {
  border-top: 1px solid $theme-border-color;
}
.search-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  padding: 22rpx 0 24rpx;
  border-bottom: 1px solid $theme-border-color;

  .avatar-img {
    width: 96rpx;
    height: 96rpx;
    margin: 0 20rpx 0 0;
    border-radius: 50%;
    overflow: hidden;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    background: linear-gradient(to right, #674bbc, #46299d);
    flex-shrink: 0;
    img {
      width: 100%;
      height: 100%;
    }
    span {
      color: #fff;
      line-height: 44rpx;
    }
  }
  .result-info {
    width: 100%;
    .info-name {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: flex-start;
      span {
        color: $theme-text;
        line-height: 44rpx;
      }
      .info-tag {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: center;
        padding: 2rpx 14rpx;
        border: 2rpx solid #000;
        border-radius: 6rpx;
        span {
          line-height: 34rpx;
        }
      }
    }
    .info-name-searchRecordDetail {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      span {
        color: $theme-hint-text;
        line-height: 34rpx;
      }
    }
    .info-detail {
      span {
        color: $theme-hint-text;
        line-height: 40rpx;
      }
    }
    .info-detail-searchRecordDetail {
      span {
        color: $theme-text;
      }
    }
  }
  .search-item-pointer {
    width: 11rpx;
    height: 18rpx;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
