<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) }} {{ $t('common.refresh') }} <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">
        <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="d-flex justify-end mb-5">
        <div class="banner-tools-btns">
          <v-btn class="radius mr-2" variant="outlined" color="error" prepend-icon="mdi-share-outline" style="width: 187px; height: 36px;">分享有礼</v-btn>
          <v-btn
            class="button-item radius"
            color="warning" 
            variant="outlined" 
            :prepend-icon="isCollection ? 'mdi-heart' : 'mdi-heart-outline'"
            @click="handleCollection"
          >{{ isCollection ? $t('position.cancelFavorite') : $t('position.collection') }}</v-btn>
          <v-btn class="button-item mx-2 radius" color="success" variant="outlined">{{ $t('position.communicate') }}</v-btn>
          <v-btn class="button-item radius" :disabled="delivery" color="primary" variant="outlined" @click="handleDelivery">{{ delivery ? $t('position.delivered') : $t('position.submitResume') }}</v-btn>
        </div>
      </div>
      <v-divider></v-divider>
      <div class="d-flex">
          <div class="content-left">
            <div v-if="Object.keys(info).length">
              <div>{{ $t('position.jobResponsibilities') }}：</div>
              <div class="requirement" v-html="info.content.replace(/\n/g, '</br>')"></div>
              <div class="mt-3">{{ $t('position.jobRequirements') }}：</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>{{ $t('position.currentOnline') }}</v-chip>
              </div>
            </div>
            <v-divider class="my-3"></v-divider>
            <div>
              <h4>{{ $t('position.address') }}</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="mr-2 radius button-item" color="success" variant="outlined">{{ $t('position.communicate') }}</v-btn>
              <v-btn class="radius button-item" :disabled="delivery" color="primary" @click="handleDelivery">{{ delivery ? $t('position.delivered') : $t('position.submitResume') }}</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" :info="info"></similarPositions>
          </div>
      </div>
    </div>

    <!-- 弹窗提示去上传简历 -->
    <v-dialog v-model="dialog" max-width="400" persistent>
      <v-card :text="$t('resume.resumeYetSubmit')" :title="$t('common.confirmTitle')">
        <template #prepend>
          <v-icon color="warning">mdi-alert-circle-outline</v-icon>
        </template>
        <template v-slot:actions>
          <v-spacer></v-spacer>
          <v-btn @click="dialog = false">{{ $t('common.cancel') }}</v-btn>
          <v-btn color="success" @click="handleToUpload">{{ $t('common.toUpload') }}</v-btn>
        </template>
      </v-card>
    </v-dialog>

    <!-- 选择简历 -->
    <Dialog :visible="showResume" :widthType="2" :title="$t('resume.selectResumeToSubmit')" @close="handleClose" @submit="handleSubmit">
      <v-radio-group v-model="selectResume">
        <v-radio v-for="val in resumeList" :key="val.id" :value="val.id" :label="val.title" color="primary"></v-radio>
      </v-radio-group>
    </Dialog>
  </div>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
</template>

<script setup>
defineOptions({ name: 'position-details' })
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { timesTampChange } from '@/utils/date'
import { getPersonResumeCv } from '@/api/resume'
import { useI18n } from '@/hooks/web/useI18n'
import { getPositionDetails, getSimilarPosition, getJobFavoriteCheck, getPersonJobFavorite, getPersonJobUnfavorite, jobCvRelCheckSend, jobCvRelSend } from '@/api/position'
import { dealDictObjData, dealDictArrayData } from '@/views/recruit/position/components/dict'
import similarPositions from '@/components/Position/similarPositions.vue'
import EnterpriseInfo from '@/components/Enterprise/info.vue'
import Snackbar from '@/plugins/snackbar'
import Dialog from '@/components/CtDialog'

const { t } = useI18n()
const router = useRouter()
const { id } = router.currentRoute.value.params
const delivery = ref(false) // 是否已投递简历

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

// 效验是否有投递过简历
const deliveryCheck = async () => {
  const data = await jobCvRelCheckSend({ jobId: id })
  if (data) delivery.value = true
}
deliveryCheck()

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 = dealDictArrayData([], 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()
}

const dialog = ref(false)
const showResume = ref(false)
// 去上传附件
const handleToUpload = () => {
  dialog.value = false
  window.open('/personalCenter')
}

// 效验是否有投递简历
const resumeList = ref([])
const selectResume = ref()
const handleDelivery = async () => {
  if (delivery.value) return Snackbar.warning(t('resume.alreadyResume'))
  const result = await getPersonResumeCv()
  resumeList.value = result
  // 没有上传过简历的先去上传
  if (!result.length) {
    dialog.value = true
    return
  }
  showResume.value = true
}

// 简历投递
const handleClose = () => {
  showResume.value = false
  selectResume.value = null
}
const handleSubmit = async () =>{
  if (!selectResume.value) return Snackbar.warning(t('resume.selectResumeToSubmit'))
  const obj = resumeList.value.find(e => e.id === selectResume.value)
  if (!obj) return Snackbar.warning(t('resume.selectedResumeNotExist'))
  await jobCvRelSend({ jobId: id, title: obj.title, url: obj.url })
  Snackbar.success(t('resume.deliverySuccess'))
  handleClose()
  deliveryCheck()
}
</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 {
  min-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: 810px;
  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>