<template>
  <div>
      <div class="default-width banner px-6">
        <div class="banner-title">
          <h1 class="ellipsis">{{ info.name }}</h1>
          <span class="salary">{{ info.payFrom }}-{{ info.payTo }}/{{ positionInfo.payName }}</span>
          <span class="refresh-time">{{ timesTampChange(info.updateTime) }} 刷新 <v-icon color="warning" size="20">mdi-alert-outline</v-icon></span>
        </div>
        <div class="banner-tags mt-4">
          <span v-for="k in desc" :key="k.mdi" class="mr-10">
            <v-icon color="#666" size="20">{{ k.mdi }}</v-icon>
            <span class="ml-1">{{ positionInfo[k.value] }}</span>
          </span>
        </div>
        <div class="banner-tools my-4" style="height: 47px;">
          <div class="float-left" style="line-height: 40px;">
            <v-chip size="small" label v-for="(k, i) in info.tagList" :key="i" class="mr-1" color="primary">{{ k }}</v-chip>
          </div>
          <div class="banner-tools-btns float-right">
            <v-btn
              class="button-item radius"
              color="warning" 
              variant="outlined" 
              :prepend-icon="isCollection ? 'mdi-heart' : 'mdi-heart-outline'"
              @click="handleCollection"
            >{{ isCollection ? '取消收藏': '收藏' }}</v-btn>
            <v-btn class="button-item mx-2 radius" color="success" variant="outlined">立即沟通</v-btn>
            <v-btn class="button-item radius" color="primary" variant="outlined">投递简历</v-btn>
          </div>
        </div>
        <v-divider></v-divider>
        <div class="d-flex">
          <div class="content-left">
            <div v-if="Object.keys(info).length">
              <div>岗位职责：</div>
              <div class="requirement" v-html="info.content.replace(/\n/g, '</br>')"></div>
              <div class="mt-3">岗位要求：</div>
              <div class="requirement" v-html="info.requirement.replace(/\n/g, '</br>')"></div>
            </div>
            <v-divider class="my-3"></v-divider>
            <div class="contact" v-if="Object.keys(info).length">
              <div class="float-left d-flex align-center">
                <v-img :src="info.contact.avatar || 'https://minio.citupro.com/dev/menduner/7.png'" :width="45" style="height: 45px;"></v-img>
                <div class="ml-2">
                  <div class="contact-name">{{ info.contact.name }}</div>
                  <div class="contact-info">{{ info.enterprise.name }} · {{ info.contact.postNameCn }}</div>
                </div>
              </div>
              <div class="float-right">
                <v-chip color="primary" label>当前在线</v-chip>
              </div>
            </div>
            <v-divider class="my-3"></v-divider>
            <div>
              <h4>工作地址</h4>
              <div class="mt-1">
                <v-icon size="25" color="primary">mdi-map-marker</v-icon>
                <span style="color: #666;font-size: 15px;">{{ info.address }}</span>
              </div>
            </div>
            <div class="mt-3 text-center">
              <v-btn class="half-button mr-2 radius" color="success" variant="outlined">立即沟通</v-btn>
              <v-btn class="half-button radius" color="primary">投递简历</v-btn>
            </div>
          </div>
          <div class="content-right" v-if="Object.keys(info).length">
            <!-- 公司信息 -->
            <EnterpriseInfo :info="{ ...info, position: { ...positionInfo } }"></EnterpriseInfo>
            <!-- 相似职位 -->
            <similarPositions class="mt-3" :list="similarList"></similarPositions>
          </div>
        </div>
      </div>
  </div>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
</template>

<script setup>
defineOptions({ name: 'position-details' })
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { timesTampChange } from '@/utils/date'
import { getPositionDetails, getSimilarPosition, getJobFavoriteCheck, getPersonJobFavorite, getPersonJobUnfavorite } from '@/api/position'
import { dealDictData } from '@/views/recruit/position/components/dict'
import similarPositions from '@/components/Position/similarPositions.vue'
import EnterpriseInfo from '@/components/Enterprise/info.vue'

const router = useRouter()
const { id } = router.currentRoute.value.params

// 职位详情
const info = ref({})
const positionInfo = ref({})
const getPositionDetail = async () => {
  const data = await getPositionDetails({ id })
  info.value = data
  positionInfo.value = { ...dealDictData(positionInfo.value, info.value), ...info.value }
}
getPositionDetail()

const desc = [
  { mdi: 'mdi-map-marker-outline', value: 'areaName' },
  { mdi: 'mdi-school-outline', value: 'eduName' },
  { mdi: 'mdi-clock-time-ten-outline', value: 'expName' }
]

// 相似职位
const similarList = ref([])
const getSimilarPositionList = async () => {
  if (!Object.keys(positionInfo).length) return
  const { list } = await getSimilarPosition({ pageNo: 1, pageSize: 4, positionId: positionInfo.value.positionId, expType: positionInfo.value.expType })
  const items = list.splice(0, 4)
  similarList.value = dealDictData(similarList.value, items)
}

getSimilarPositionList()

// 效验求职者是否有收藏该职位
const isCollection = ref(true)
const getCollectionStatus = async () => {
  const data = await getJobFavoriteCheck({ jobId: id })
  isCollection.value = data
}
getCollectionStatus()

// 收藏&取消收藏职位
const handleCollection = async () => {
  const api = isCollection.value ? getPersonJobUnfavorite : getPersonJobFavorite
  await api(isCollection.value ? id : { jobId: id })
  getCollectionStatus()
}
</script>

<style lang="scss" scoped>
.banner {
  background-color: #fff;
  padding: 18px 0 20px;
}
.banner-title {
  line-height: 40px;
  font-size: 28px;
  font-weight: 600;
}
.banner-title h1 {
  display: inline-block;
  color: #37576c;
  font-size: 28px;
  margin-right: 30px;
  margin-top: 1px;
  max-width: 360px;
  vertical-align: middle;
}
.button-item {
  width: 110px;
  height: 36px
}
.salary {
  color: var(--v-error-base);
  line-height: 41px;
  font-weight: 600;
  height: auto;
  display: inline-block;
  vertical-align: sub;
}
.refresh-time {
  float: right;
  color: #666;
  font-size: 14px;
  line-height: 66px;
  vertical-align: sub;
}
.banner-tags span {
  font-weight: 600;
}
.radius {
  border-radius: 8px;
}
.content-left {
  width: 864px;
  padding: 20px 20px;
}
.content-right {
  flex: 1;
  padding: 20px 20px 20px 0;
}
.label-text {
  color: #7f7a7a;
  font-weight: 600;
}
.value-text {
  color: #000;
  font-weight: 400;
}
.requirement {
  white-space: pre-wrap;
  word-break: break-all;
  line-height: 28px;
  color: #333;
  font-size: 15px;
  text-align: justify;
  letter-spacing: 0;
}
.contact {
  height: 60px;
  line-height: 60px;
}
.contact-name {
  font-size: 20px;
  font-weight: 500;
  color: #222;
  line-height: 28px;
}
.contact-info {
  font-size: 15px;
  color: #666;
  line-height: 21px;
  margin-top: 8px;
}
</style>