<!-- 学生详情 -->
<template>
  <div v-if="Object.keys(info).length" class="d-flex justify-center flex-column align-center mb-8">
    <v-card class="pa-8">
      <!-- 头像院系 -->
      <baseInfo class="mt-5" :data="info"></baseInfo>
      <!-- 基本信息 -->
      <other :data="info"></other>
    </v-card>

    <!-- 实习情况 -->
    <v-card class="my-3 pa-5">
      <div class="resume-header">
        <div class="resume-title">实习情况</div>
      </div>
      <CtTable
        class="mt-3"
        :items="practice"
        :headers="headers"
        :elevation="0"
        :loading="false"
        :is-tools="false"
        :items-per-page="-1"
        :showPage="false"
        itemKey="id"
      >
        <template #enterpriseName="{ item }">
          <div class="d-flex align-center defaultLink" @click="handleEnterprise(item.enterpriseId)">
            <v-avatar class="entLogoImg" tile size="40" :image="item.enterprise.logoUrl || 'https://minio.citupro.com/dev/menduner/company-avatar.png'"></v-avatar>
            <span class="ml-3">{{ formatName(item.enterprise.anotherName || item.enterprise.name) }}</span>
          </div>
        </template>
        <template #status="{ item }">
          {{ statusList.find(e => e.value === item.status)?.label }}
        </template>
      </CtTable>
    </v-card>
  </div>

  <Loading :visible="loading"></Loading>
</template>

<script setup>
defineOptions({name: 'studentList-student-details'})
import baseInfo from './components/baseInfo.vue'
import other from './components/other.vue'
import { ref, onMounted } from 'vue'
import { getStudentDetailsById, getInternshipById } from '@/api/school'
import Snackbar from '@/plugins/snackbar'
import { useRoute } from 'vue-router';
import { formatName } from '@/utils/getText'
import { timesTampChange } from '@/utils/date'
import { getDict } from '@/hooks/web/useDictionaries'

const route = useRoute()

// 获取人才详情
const info = ref({})
const loading = ref(false)
const { id } = route.params

const headers = [
  { title: '实习企业', key: 'enterpriseName', sortable: false },
  { title: '投递职位', key: 'jobName', sortable: false, value: item =>  formatName(item?.job?.name) },
  { title: '实习状态', key: 'status', sortable: false },
  { title: '开始时间', key: 'startTime', sortable: false, value: item => timesTampChange(item.startTime, 'Y-M-D') },
  { title: '结束时间', key: 'endTime', sortable: false, value: item => timesTampChange(item.startTime, 'Y-M-D') },
]

// 获取学生实习情况
const practice = ref([])
const getPracticeRecord = async (userId) => {
  const data = await getInternshipById({ userId })
  practice.value = data || []
}

const getCvDetail = async () => {
  if (!id) {
    Snackbar.warning('缺少学生id')
    setTimeout(() => {
      window.close()
    }, 2000)
    return
  }
  loading.value = true
  const data = await getStudentDetailsById({ id })
  info.value = data || {}
  loading.value = false

  // 实习情况
  getPracticeRecord(data.userId)
}

const statusList = ref([])
onMounted(async () => {
  // 状态字典
  const { data } = await getDict('student_practice_status')
  statusList.value = data || []

  getCvDetail()
})

// 跳转企业详情
const handleEnterprise = (enterpriseId) => {
  if (!enterpriseId) return
  window.open(`/recruit/personal/company/details/${enterpriseId}?key=briefIntroduction`)
}
</script>

<style lang="scss" scoped>
.v-card {
  width: 940px;
}
</style>
