<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-header">
        <div v-if="val.active && val.job.status === '0'" class="header-btn">
          <v-btn v-if="props.showCancelBtn" class="half-button ml-3" color="primary" size="small" @click="handleCancel(val)">取消收藏</v-btn>
          <v-btn class="half-button ml-3" color="primary" size="small" @click="toDetails(val)">立即沟通</v-btn>
        </div>
        <div v-if="val.job.status === '1'" class="font-size-14 header-btn color-error">职位已关闭</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>
        </div>
      </div>
      <div class="info-content" >
        <div class="job-info">
          <div class="job-name" :class="{'cursor-pointer': val.job.status === '0'}">
            <span class="mr-3" :class="{'info-name': val.job.status === '0'}" @click="handleToPositionDetails(val)">{{ val.job.name }}</span>
            <span v-if="val?.job?.areaName">[{{ val.job.areaName }}]</span>
          </div>
          <div class="job-other">
            <span v-if="!val.job.payFrom && !val.job.payTo" class="salary">面议</span>
            <span v-else class="salary">{{ 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 width="50" height="50" :src="val.enterprise.logoUrl || 'https://minio.citupro.com/dev/menduner/7.png'"></v-img>
          </div>
          <div class="ml-3">
            <div class="cursor-pointer info-name" @click="handleToEnterprise(val)">{{ 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>

    <!-- 快速登录 -->
    <login-page v-if="showLogin" @loginSuccess="loginSuccess" @close="loginClose"></login-page>
  </div>
</template>

<script setup>
defineOptions({ name: 'longStrip'})
import { getPersonJobUnfavorite } from '@/api/position'
import { useI18n } from '@/hooks/web/useI18n'
import Snackbar from '@/plugins/snackbar'
import { getUserAvatar } from '@/utils/avatar'
import { useRouter } from 'vue-router'
import { ref } from 'vue'
import { prologue, defaultText } from '@/hooks/web/useIM'
import loginPage from '@/views/common/loginDialog.vue'
import { getToken } from '@/utils/auth'

const emits = defineEmits(['refresh'])
const { t } = useI18n()
const props = defineProps({
  items: {
    type: Array,
    default: () => []
  },
  showCancelBtn: {
    type: Boolean,
    default: false
  }
})

const router = useRouter()

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

const handleCancel = async (item) => {
  if (!item.job.id) return Snackbar.warning(t('sys.api.operationFailed'))
  await getPersonJobUnfavorite(item.job.id)
  emits('refresh')
  Snackbar.success(t('common.operationSuccessful'))
}

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

// 企业详情
const handleToEnterprise = (item) => {
  router.push(`/recruit/personal/company/details/${item.enterprise.id}?key=briefIntroduction`)
}

let toDetailsInfo = {}
// 立即沟通
const toDetails = async (info) => {
  if (info) toDetailsInfo = info // 快速登录弹窗回调使用
  else info = toDetailsInfo
  if (!getToken()) {
    showLogin.value = true // 打开快速登录弹窗
    Snackbar.warning('您还未登录，请先登录后再试')
    //
    loginCloseWarningWord = '您已取消登录，无法对职位进行沟通' // 取消登录提示语
    nextFunc.value = toDetails // 登录成功后要执行的操作 (toDetails执行不成功，原因未找到)
    return
  }
  const userId = info.contact.userId
  const enterpriseId = info.contact.enterpriseId
  const textObj = {
    text: defaultText,
    positionInfo: { ...info.job, enterprise: info.enterprise, contact: info.contact },
  }
  await prologue({userId, enterpriseId, text: JSON.stringify(textObj)})
  let url = `/recruit/personal/message?id=${info.job.id}`
  if (info.contact.enterpriseId) {
    url += `&enterprise=${info.contact.enterpriseId}`
  }

  router.push(url)
}

const showLogin = ref(false)
const nextFunc = ref(null)
let loginCloseWarningWord = ''
// 快速登录
const loginSuccess = () => {
  showLogin.value = false
  Snackbar.success('登录成功')
  if (nextFunc.value) nextFunc.value()
}

const loginClose = () => {
  showLogin.value = false
  Snackbar.warning(loginCloseWarningWord)
}
</script>

<style scoped lang="scss">
.position-item {
  height: 144px;
  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 {
        height: 22px;
        line-height: 22px;
        color: var(--color-222);
        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>
