<template>
  <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-header">
      <div v-if="val.active && val.status === '0' && val.job.status === '0'" class="header-btn">
        <v-btn color="primary" size="small" @click.stop="handleAgree(val)">同意</v-btn>
        <v-btn class="ml-3" color="error" size="small" @click.stop="handleRefuse(val)">拒绝</v-btn>
      </div>
      <div v-if="val.job.status === '1'" class="font-size-14 header-btn color-error">职位已关闭</div>
      <!-- <div v-if="tab === '1' || tab === '98'" class="float-right font-size-13" :style="{'padding': '12px 12px 0 0', 'color': tab === '1' ? 'var(--v-primary-base)' : 'var(--v-error-base)'}">
        您已于{{ timesTampChange(val.updateTime, 'Y-M-D h:m') }}{{ tab === '1' ? '接受' : '拒绝'}}了此面试邀请
      </div> -->
      <div class="img-box">
          <v-avatar :image="getUserAvatar(val.contact.avatar, val.contact.sex)" size="x-small"></v-avatar>
          <span class="name">
            <span class="mx-3">{{ val.contact.name }}</span>
            <span class="gray">{{ val.contact.postNameCn }}</span>
            <span v-if="val.invitePhone" class="septal-line"></span>
            <span class="gray">{{ val.invitePhone }}</span>
          </span>
      </div>
    </div>
    <div class="info-content">
        <div class="font-size-16 color-333 mr-5" style="width: 470px;">
          <div v-if="tab === '1' || tab === '98'" class="font-size-13 mb-1" :style="{'color': tab === '1' ? 'var(--v-primary-base)' : 'var(--v-error-base)'}">您已于{{ timesTampChange(val.updateTime, 'Y-M-D h:m') }}{{ tab === '1' ? '接受' : '拒绝'}}了此面试邀请</div>
          <div>面试时间：{{ timesTampChange(val.time, 'Y-M-D h:m') }}</div>
          <div class="mt-3 ellipsis" style="max-width: 470px;">面试地点：{{ val.address }}</div>
        </div>
        <div class="job-info color-666">
          <div class="job-name ellipsis" style="max-width: 410px;">
            <span class="mr-3" :class="{'cursor-pointer': val.job.status === '0', 'position-name': val.job.status === '0'}" @click.stop="handleToPositionDetails(val)">{{ formatName(val.job.name) }}</span>
            <span v-if="!val.job.payFrom && !val.job.payTo">面议</span>
            <span v-else>{{ val.job.payFrom ? val.job.payFrom + '-' : '' }}{{ val.job.payTo }}{{ val.job.payName ? '/' + val.job.payName : '' }}</span>
          </div>
          <div class="job-other d-flex align-center">
            <div style="width: 30px;height: 30px;">
              <v-img width="30" height="30" rounded contain :src="val.enterprise.logoUrl || 'https://minio.citupro.com/dev/menduner/company-avatar.png'"></v-img>
            </div>
            <div class="ellipsis" style="max-width: 400px;">
              <span class="mx-2 enterprise-name" @click.stop="jumpToEnterpriseDetail(val.enterprise.id, true)">{{ formatName(val.enterprise.anotherName || val.enterprise.name) }}</span>
              <span v-if="val.enterprise.industryName || val.enterprise.scaleName">
                [
                  <span>{{ val.enterprise.industryName }}</span>
                  <span v-if="val.enterprise.industryName && val.enterprise.scaleName">&nbsp;·&nbsp;</span>
                  <span>{{ val.enterprise.scaleName }}</span>
                ]
              </span>
            </div>
          </div>
        </div>
    </div>
  </div>
</template>

<script setup>
defineOptions({ name: 'interview-item'})
import { useI18n } from '@/hooks/web/useI18n'
import { timesTampChange } from '@/utils/date'
import { userInterviewInviteReject, userInterviewInviteConsent } from '@/api/recruit/personal/personalCenter'
import Snackbar from '@/plugins/snackbar'
import Confirm from '@/plugins/confirm'
import { getUserAvatar } from '@/utils/avatar'
import { useRouter } from 'vue-router'
import { formatName } from '@/utils/getText'
import { jumpToEnterpriseDetail } from '@/utils/position'

const { t } = useI18n()
const emits = defineEmits(['refresh'])
const props = defineProps({
  items: {
    type: Array,
    default: () => []
  },
  tab: {
    type: String,
    default: '0'
  }
})

const router = useRouter()

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

// 同意
const handleAgree = (val) => {
  if (!val.id) return
  const query = {
    id: val.id
  }
  const userInfo = localStorage.getItem('userInfo')
  if (userInfo) {
    const { phone } = JSON.parse(userInfo)
    query.phone = phone
  }

  Confirm(t('common.confirmTitle'), '是否确定接收此面试邀请？').then(async () => {
    await userInterviewInviteConsent(query)
    Snackbar.success(t('common.operationSuccessful'))
    emits('refresh')
  })
}

// 拒绝
const handleRefuse = (val) => {
  if (!val.id) return
  Confirm(t('common.confirmTitle'), '您是否确定要拒绝此面试邀请？').then(async () => {
    await userInterviewInviteReject(val.id)
    Snackbar.success(t('common.operationSuccessful'))
    emits('refresh')
  })
}
</script>

<style scoped lang="scss">
.position-item {
  height: 160px;
  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: 360px;
      max-width: 360px;
      font-weight: 500;
      font-size: 14px;
      margin-right: 12px;
      .job-name {
        height: 22px;
        line-height: 22px;
        margin-bottom: 12px;
      }
      .job-other {
        height: 22px;
        line-height: 22px;
        .enterprise-name {
          cursor: pointer;
          &:hover {
            color: var(--v-primary-base)
          }
        }
      }
    }
  }
}
.position-name:hover {
  color: var(--v-primary-base);
}
</style>
