<template>
  <div class="default-width banner px-6">
    <div v-if="Object.keys(info).length">
      <div class="banner-title" v-if="Object.keys(info).length">
        <div class="float-left d-flex align-center">
          <v-img width="60" height="60" :src="info.enterprise.logoUrl || 'https://minio.citupro.com/dev/menduner/company-avatar.png'"></v-img>
          <div class="ml-4">
            <div class="contact-name">
              {{ info.enterprise.name }}
              <v-icon :color="statusInfo.color" size="20">{{ statusInfo.mdi }}</v-icon>
              <span :style="{'color': statusInfo.color,'font-size': '14px'}">{{ statusInfo.label }}</span>
            </div>
            <div class="contact-info">
              {{ info.business.type }}
              <span v-if="info.business.type && info.scaleName">·</span> 
              {{ info.scaleName }}
              <span v-if="info.industryName && info.scaleName">·</span> 
              {{ info.industryName }}
            </div>
          </div>
        </div>
        <div class="float-right d-flex">
          <div class="tools-box text-center" v-if="info.jobAdvertisedCount">
            <div class="tools-box-number">{{ info.jobAdvertisedCount }}</div>
            <div class="tools-box-text">职位在招</div>
          </div>
          <!-- 是否关注该企业 -->
          <v-tooltip location="bottom">
            <template v-slot:activator="{ props }">
              <v-icon v-bind="props" class="ml-5 mr-2" size="25" :color="isCollection ? 'error' : ''" @click="handleFollow">{{ isCollection ? 'mdi-heart' : 'mdi-heart-outline' }}</v-icon>
            </template>
            <span>关注该企业</span>
          </v-tooltip>
        </div>
      </div>
      <v-divider></v-divider>
      <div class="mt-3">
        <v-tabs v-model="tab" align-tabs="start" color="primary" bg-color="#f3f3f3" @update:model-value="handleTabClick">
          <v-tab :value="1">公司简介</v-tab>
          <v-tab :value="2">在招职位{{ info.jobAdvertisedCount ? `(${info.jobAdvertisedCount})` : '' }}</v-tab>
        </v-tabs>
        <div class="d-flex" v-if="Object.keys(info).length">
          <div class="content-left">
            <EnterpriseIntroduction v-if="tab === 1" :info="info" />
            <recruitmentPositions v-else :info="info"/>
          </div>
          <div class="content-right">
            <div v-if="info.enterprise?.workTime && info?.enterprise?.welfareList.length" class="welfare mb-3">
              <h4>工作时间及福利</h4>
              <div v-if="info.enterprise?.workTime" class="my-3" style="color: var(--color-666);font-size: 14px;">
                <v-icon size="17" color="#ccc" class="mr-2">mdi-clock</v-icon>{{ info.enterprise.workTime }}
              </div>
              <div class="welfare-tags">
                <v-chip size="small" label v-for="(k, i) in info?.enterprise?.welfareList?.slice(0, 6)" :key="i" class="mb-2 welfare-tags-item ellipsis" color="primary">{{ k }}</v-chip>
              </div>
            </div>
            <div class="welfare">
              <h4>工商信息</h4>
              <div :class="['mt-2', 'business-item']" v-for="val in businessList" :key="val.value">
                <div>{{ val.label }}</div>
                <div class="business-value ellipsis">{{ info.business[val.value] || '暂无' }}</div>
                <div :class="['my-3']"></div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
defineOptions({ name: 'enterprise-details'})
import { ref, computed } from 'vue'
import EnterpriseIntroduction from './components/introduction.vue'
import recruitmentPositions from './components/positions.vue'
import { getEnterpriseAuthDetails } from '@/api/position'
import { getEnterpriseDetails, getEnterpriseSubscribeCheck, getEnterpriseSubscribe, getEnterpriseUnsubscribe, enterpriseClick } from '@/api/enterprise'
import { timesTampChange } from '@/utils/date'
import { dealDictObjData } from '@/utils/position'
import { useRoute, useRouter } from 'vue-router'
import { getToken } from '@/utils/auth'

const props = defineProps({
  id: {
    type: String,
    default: ''
  }
})

const route = useRoute()
const router = useRouter()
const tab = ref(1)
const savedTab = new URLSearchParams(window.location.search).get('key')
tab.value = savedTab ? (savedTab === 'briefIntroduction' ? 1 : 2) : 1
// router.push(`${route.path}?key=${savedTab ? savedTab : 'briefIntroduction'}`)

const handleTabClick = () => {
  router.push(`${route.path}?key=${tab.value === 1 ? 'briefIntroduction' : 'recruitmentPositions'}`)
}

// 企业埋点
const handleEnterpriseClick = async () => {
  if (!props.id) return
  await enterpriseClick({ id: props.id })
}
handleEnterpriseClick()

const statusList = [
  { label: '未认证', color: '#fb8c00', value: null, mdi: 'mdi-shield-remove' },
  { label: '审核中', color: '#fb8c00', value: '0', mdi: 'mdi-shield-half-full' },
  { label: '已认证', color: 'var(--v-primary-base)', value: '1', mdi: 'mdi-shield-check' },
  { label: '已驳回', color: '#fe574a', value: '2', mdi: 'mdi-shield-off' }
]

// 企业详情
const info = ref({})
const authInfo = ref({})
const getDetails = async () => {
  if (!props.id) return
  const data = await getEnterpriseDetails({ id: props.id })
  // 成立日期
  data.business.establishmentTime = timesTampChange(data.business.establishmentTime, 'Y-M-D')

  info.value = { ...data, ...dealDictObjData({}, data.enterprise) }
  getCollectionStatus(props.id)
  // 企业实名认证信息
  authInfo.value = await getEnterpriseAuthDetails(props.id)
}
getDetails()

const statusInfo = computed(() => {
  const obj = (authInfo.value && Object.keys(authInfo.value).length) ? statusList.find(e => e.value === authInfo.value.status) : statusList[0]
  return obj
})

// 效验求职者是否关注该企业
const isCollection = ref(false)
const getCollectionStatus = async (id) => {
  if (!getToken()) return isCollection.value = false
  const data = await getEnterpriseSubscribeCheck({ enterpriseId: id })
  isCollection.value = data
}

// 关注&取消关注企业
const handleFollow = async () => {
  const api = isCollection.value ? getEnterpriseUnsubscribe : getEnterpriseSubscribe
  await api(isCollection.value ? props.id : { enterpriseId: props.id })
  getCollectionStatus(props.id)
}

// 工商信息
const businessList = [
  { label: '企业类型：', value: 'type' },
  { label: '统一社会信用代码：', value: 'code' },
  { label: '成立日期：', value: 'establishmentTime' },
  { label: '注册资本：', value: 'registeredCapital' }
]
</script>

<style scoped lang="scss">
.banner {
  background-color: #fff;
  padding: 0 0 20px;
}
.banner-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 125px;
}
.content-left {
  width: 844px;
  padding: 20px;
}
.content-right {
  flex: 1;
  padding: 20px 20px 0 0;
}
.contact {
  height: 60px;
  line-height: 60px;
}
.contact-name {
  font-size: 28px;
  font-weight: 700;
  color: #37576c;
  line-height: 28px;
}
.contact-info {
  font-size: 15px;
  font-weight: 500;
  color: var(--color-222);
  line-height: 21px;
  margin-top: 8px;
}
.tools-box {
  height: 80px;
  width: 80px;
  background-color: var(--color-d5e6e8);
  border-radius: 5px;
}
.tools-box-number {
  font-size: 30px;
  font-weight: 700;
  color: var(--v-primary-base);
}
.tools-box-text {
  color: var(--v-primary-base);
  font-size: 14px;
}
.welfare {
  background-color: var(--color-f3);
  border-radius: 8px;
  padding: 12px;
}
.welfare-tags {
  display: flex;
  width: 242px;
  flex-wrap: wrap;
  height: 100px;
  overflow: hidden;
  text-align: center;
}
.welfare-tags-item {
  display: block;
  width: 117px;
  max-width: 117px;
  text-align: center;
  line-height: 26px;
  margin-right: 8px;
  &:nth-child(2n) {
    margin-right: 0;
  }
}
.business-item {
  font-size: 14px;
  color: var(--color-666);
  font-weight: 600;
}
.business-value {
  width: 228px;
  color: #000;
  font-weight: 500;
}
.business-source {
  font-size: 14px;
  color: var(--color-666);
}
.desc {
  width: 180px;
  color: var(--color-666);
  font-weight: 500;
  font-size: 12px;
}
.position-name {
  width: 180px;
  font-size: 14px;
  font-weight: 600;
}
</style>
