<template>
  <div>
    <div class="position-item mb-3 job-closed elevation-2" style="position: relative;" 
      v-for="(val, i) in props.items" :key="i" @mouseenter="val.active = true" @mouseleave="val.active = false"
    >
			<!-- 职位、企业信息 -->
      <div class="info-content" >
        <div class="job-info">
          <div class="job-name ellipsis" :class="{'cursor-pointer': val.job.status === '0'}" v-ellipse-tooltip>
            <span class="mr-3" :class="{'info-name': val.job.status === '0'}" @click.stop="handleToPositionDetails(val)">{{ formatName(val.job.name) }}</span>
            <span v-if="val.job.status === '1'" class="font-size-14 color-error">[职位已关闭]</span>
            <span v-else>
              <span v-if="!val.job?.areaId || val.job.areaName">[{{ !val.job.areaId ? '全国' : val.job.areaName }}]</span>
            </span>
          </div>
          <div class="job-other">
            <span v-if="!val.job.payFrom && !val.job.payTo" class="salary color-primary">面议</span>
            <span v-else class="salary color-primary">{{ val.job.payFrom ? val.job.payFrom + '-' : ''}}{{ val.job.payTo }}{{ val.job.payName ? '/' + val.job.payName : '' }}</span>
            <v-chip v-if="val.job?.expName" class="mx-3" color="primary" label size="small">{{ val.job.expName }}</v-chip>
            <v-chip v-if="val.job?.eduName" color="primary" label size="small">{{ val.job.eduName }}</v-chip>
          </div>
        </div>
        <div class="company-info ml-3" style="flex: 1;">
          <div style="height: 50px; width: 50px;">
            <v-img class="entLogoImg" width="50" height="50" :src="val.enterprise.logoUrl || 'https://minio.citupro.com/dev/menduner/company-avatar.png'" contain></v-img>
          </div>
          <div class="ml-3">
            <div class="cursor-pointer info-name" v-ellipse-tooltip @click.stop="jumpToEnterpriseDetail(val.enterprise.id)">{{ formatName(val.enterprise.anotherName || val.enterprise.name) }}</div>
            <div class="mt-3 ellipsis color-666 font-size-13" style="max-width: 260px;">
              <span v-for="(k, i) in desc" :key="k">
                {{ val.enterprise[k] }}
                <span v-if="i !== desc.length - 1 && val.enterprise[k] && val.enterprise[desc[i + 1]]" class="septal-line"></span>
              </span>
            </div>
          </div>
        </div>
      </div>
			<div class="mx-5" style="border-bottom: 1px dashed #e0e0e0"></div>

			<!-- 实习范围 -->
			<div class="d-flex justify-space-between px-5 py-3">
				<div class="color-666 font-size-15">
					<p>实习时间：{{ timesTampChange(val.startTime, 'Y-M-D') }} 至 {{ timesTampChange(val.endTime, 'Y-M-D') }}</p>
				</div>
				<div class="text-end">
					<v-btn v-if="val.status === '1'" size="small" color="warning" @click="handleToReport(val)">实习报告</v-btn>
					<v-btn v-if="val.evaluate && !val.certificate" size="small" class="ml-3" color="primary" @click.stop="handlePreview(val)">实习证书</v-btn>
          <v-menu v-else-if="val.evaluate && val.certificate" open-on-hover>
            <template v-slot:activator="{ props }">
              <v-btn color="primary" size="small" class="ml-3" v-bind="props">实习证书</v-btn>
            </template>
            <v-list>
              <v-list-item v-for="(item, index) in menuList" :key="index" @click="item.change(val)">
                <template v-slot:prepend>
                  <v-icon :icon="item.icon"></v-icon>
                </template>
                <v-list-item-title>{{ item.title }}</v-list-item-title>
              </v-list-item>
            </v-list>
          </v-menu>
					<v-btn v-if="val.recommendationLetter" @click.stop="handleDownLoadRecommendationLetter(val)" size="small" class="ml-3" color="#00897B" prepend-icon="mdi-download">企业推荐信</v-btn>
				</div>
			</div>
    </div>
  </div>
</template>

<script setup>
defineOptions({ name: 'PersonalCenterStudentInternshipCompanyItem' })
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { formatName } from '@/utils/getText'
import { jumpToEnterpriseDetail } from '@/utils/position'
import { timesTampChange } from '@/utils/date'
import { getBlob, saveAs } from '@/utils'

const emit = defineEmits(['preview'])
const props = defineProps({
  items: {
    type: Array,
    default: () => []
  }
})

const router = useRouter()

const desc = ['industryName', 'scaleName']

// 实习证书预览
const handlePreview = (item) => {
  emit('preview', item)
}

// 实习证书附件下载
const handleDownLoadCertificate = (val) => {
  getBlob(val.certificate).then(blob => {
    saveAs(blob, `${formatName(val.enterprise.anotherName || val.enterprise.name)} - 实习证书附件`)
  })
}

const menuList = ref([
  { title: '证书预览', icon: 'mdi-eye-outline', change: handlePreview },
  { title: '附件下载', icon: 'mdi-download', change: handleDownLoadCertificate }
])

// 职位详情
const handleToPositionDetails = (item) => {
  if (item.job.status === '1') return
  let path = `/recruit/personal/position/details/${item.job.id}`
  if (item.cvRel?.jobFairId) path += `?jobFairId=${item.cvRel.jobFairId}`
  router.push(path)
}

// 实习报告
const handleToReport = (val) => {
	console.log(val, 'report')
	router.push(`/recruit/personal/personalCenter/student/internshipReport?id=${val.enterprise.id}`)
}

// 企业推荐信下载
const handleDownLoadRecommendationLetter = (val) => {
  getBlob(val.recommendationLetter).then(blob => {
    saveAs(blob, `${formatName(val.enterprise.anotherName || val.enterprise.name)} - 推荐信`)
  })
}
</script>

<style scoped lang="scss">
.position-item {
  background-color: #fff;
  border-radius: 12px;
  &: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;
  }
  .info-header {
    height: 48px;
    background: linear-gradient(90deg,#f5fcfc,#fcfbfa);
    border-radius: 12px;
    .img-box {
      padding: 12px 24px;
      .name {
        color: var(--color-222);
        font-weight: 400;
        font-size: 13px;
        .gray {
          color: var(--color-666);
        }
      }
    }
    .header-btn {
      padding: 10px 10px 0 0;
      float: right;
      .v-btn {
        z-index: 1;
      }
    }
  }
  .info-content {
    display: flex;
    padding: 16px 24px;
    justify-content: space-between;
    .job-info {
      width: 430px;
      min-width: 430px;
      max-width: 430px;
      font-weight: 500;
      font-size: 16px;
      .job-name {
        width: 100%;
        height: 22px;
        line-height: 22px;
        color: #0E100F;
        margin-bottom: 12px;
      }
      .job-other {
        color: var(--v-error-base);
        height: 22px;
        line-height: 22px;
      }
    }
    .company-info {
      display: flex;
      align-items: center
    }
    .interview-info {
      color: var(--color-333);
      font-size: 15px;
    }
  }
}

.info-name:hover {
  color: var(--v-primary-base);
}
</style>
