<template>
  <div style="position: relative; height: 60vh;">
    <div v-if="userInfo?.entitlement?.viewersList && userInfo?.vipFlag && userInfo?.vipExpireDate && userInfo?.vipExpireDate > Date.now()">
      <div v-if="items.length" class="mt-3">
        <div class="positionItem elevation-2" v-for="(item, index) in items" :key="index" @mouseenter="item.active = true" @mouseleave="item.active = false">
          <div class="position-and-company">
            <div class="position">
              <div class="float-left">
                <v-img :src="getUserAvatar(item?.contact.avatar, item?.contact.sex)" :width="40" style="height: 40px;border-radius: 4px;"/>
              </div>
              <div class="company-info">
                <h3 class="title1">{{ item.contact.name }}</h3>
                <p class="mt-2">{{ item?.post?.nameCn }}</p>
              </div>
            </div>
            <div class="company" @click="handleEnterprise(item)">
              <div class="float-left">
                <v-img :src="item?.enterprise.logoUrl || 'https://minio.citupro.com/dev/menduner/company-avatar.png'" :alt="item.enterprise.anotherName" :width="40" style="height: 40px;border-radius: 4px;"/>
              </div>
              <div class="company-info">
                <h3 :class="{'default-active': item.active }" class="title1 cursor-pointer">{{ formatName(item.enterprise.anotherName || item.enterprise.name) }}</h3>
                <p class="mt-2">{{ item.enterprise.scaleName }}<span class="mx-2">|</span>{{ item.enterprise.industryName }}</p>
              </div>
            </div>
          </div>
          <div class="footer">
            {{ $t('position.viewTime') + timesTampChange(item.updateTime)  }}
          </div>
        </div>
        <CtPagination
          :total="total"
          :page="page.pageNo"
          :limit="page.pageSize"
          @handleChange="handleChangePage"
        ></CtPagination>
      </div>
      <Empty v-else :elevation="false"></Empty>
    </div>
    <div v-else class="mt-8 tips">
      <span class="color-error" v-if="userInfo?.vipExpireDate > Date.now() && !userInfo?.entitlement?.viewersList">
        当前会员套餐的福利不包含谁看过我，<span class="text-decoration-underline cursor-pointer" @click="goBuy">去升级</span>
      </span>
      <span class="color-error" v-if="!userInfo?.vipExpireDate || (userInfo?.vipExpireDate && userInfo?.vipExpireDate < Date.now())">
        谁看过我为会员福利内容，<span class="text-decoration-underline cursor-pointer" @click="goBuy">去开通</span>
      </span>
    </div>
  </div>
</template>

<script setup>
// 看过我
defineOptions({ name: 'position-seen-me' })
import { ref } from 'vue'
import { getInterestedMePage } from '@/api/recruit/personal/personalCenter'
import { dealDictObjData } from '@/utils/position'
import { timesTampChange } from '@/utils/date'
import Empty from '@/components/Empty'
import { getUserAvatar } from '@/utils/avatar'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/user'
import { formatName } from '@/utils/getText'

const router = useRouter()
const total = ref(0)
const items = ref([])
const page = ref({
  pageNo: 1,
  pageSize: 10
})

const userStore = useUserStore()
let userInfo = ref(JSON.parse(localStorage.getItem('userInfo')) || {})
userStore.$subscribe((mutation, state) => {
  if (state.userInfo && Object.keys(state.userInfo).length) userInfo.value = state?.userInfo
})

const getData = async () => {
  const res = await getInterestedMePage(page.value)
  items.value = res.list.map(e => {
    e.enterprise = dealDictObjData({}, e.enterprise)
    e.active = false
    return e
  })
  total.value = res.total
}
getData()

const handleChangePage = (e) => {
  page.value.pageNo = e
  getData()
}

const handleEnterprise = (item) => {
  const id = item.enterprise.id
  if (!id) return
  router.push(`/recruit/personal/company/details/${id}?key=briefIntroduction`)
}

const goBuy = () => {
  router.push('/recruit/personal/personalCenter/memberBenefits/membershipPackage')
}
</script>

<style scoped lang="scss">
.title1 {
  color: var(--color-666);
  position: relative;
  max-width: 200px;
  margin-right: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 16px;
}
.company {
  flex: 1;
  display: flex;
  justify-content: end;
}
.company-info {
  float: left;
  margin-left: 16px;
}
.company-info p {
  height: 18px;
  font-size: 13px;
  font-weight: 400;
  color: var(--color-999);
}
.textColor666 { color: var(--color-666); }
.positionItem {
  margin-bottom: 12px;
  border-radius: 12px;
  padding: 0;
  overflow: hidden;
  transition: all .2s linear;
  background-color: #fff;
  &:hover {
    box-shadow: 0px 3px 5px -1px var(--v-shadow-key-umbra-opacity, rgba(0, 0, 0, 0.2)), 0px 5px 8px 0px var(--v-shadow-key-penumbra-opacity, rgba(0, 0, 0, 0.14)), 0px 1px 14px 0px var(--v-shadow-key-ambient-opacity, rgba(0, 0, 0, 0.12)) !important;
  }
  .position-and-company {
    display: flex;
    padding: 16px 20px;
    width: 100%;
    .position {
      width: 484px;
      padding-right: 12px;
    }
  }
}
.footer {
  height: 36px;
  line-height: 36px;
  padding: 0px 20px;
  font-size: 14px;
  color: var(--color-999);
  background: linear-gradient(90deg, #f5fcfc 0, #fcfbfa 100%);
}
.tips {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>
