<template>
  <div>
    <div v-if="props.items.length" class="d-flex align-center mb-1">
      <v-checkbox v-model="selectAll" :label="!selectAll ? '全选' : `已选中${selectList.length}条`" hide-details color="primary" @update:model-value="handleChangeSelectAll"></v-checkbox>
      <div v-if="tab === 1" class="ml-8">
        <v-btn :disabled="!selectAll" color="primary" variant="tonal" size="small" @click="handleAction(2, 'batch', {})">刷新</v-btn>
        <v-btn class="mx-3" :disabled="!selectAll" color="primary" variant="tonal" size="small" @click="handleAction(3, 'top', {})">置顶</v-btn>
        <v-btn :disabled="!selectAll" color="primary" variant="tonal" size="small" @click="handleAction(0, 'close', {})">关闭</v-btn>
      </div>
      <v-btn v-if="tab === 2" class="ml-8" :disabled="!selectAll" color="primary" variant="tonal" size="small" @click="handleAction(1, 'activation', {})">激活</v-btn>
    </div>
    <div v-for="val in items" :key="val.id" class="itemBox mb-3">
      <div class="d-flex justify-space-between pa-5">
        <div class="position">
          <div class="item-select">
            <v-checkbox v-model="val.select" hide-details color="primary" @update:model-value="handleChangeSelect"></v-checkbox>
          </div>
          <div class="ml-10">
            <span v-if="val.name.indexOf('style')" v-html="val.name" class="position-name" @click="handleDetails(val)"></span>
            <span v-else class="position-name" @click="handleDetails(val)">{{ val.name }}</span>
          </div>
          <div class="mt-3 other-info ellipsis ml-10">
            <span>{{ val.areaName }}</span>
            <span class="lines"></span>
            <span>{{ val.eduName }}</span>
            <span class="lines"></span>
            <span>{{ val.expName }}</span>
            <span class="lines"></span>
            <span>{{ val.payFrom }}-{{ val.payTo }}/{{ val.payName }}</span>
            <span class="lines"></span>
            <span>{{ val.positionName }}</span>
          </div>
        </div>
        <div class="d-flex align-center">
          <div v-if="tab === 1">
            <v-btn color="primary" variant="tonal">人才搜索</v-btn>
            <v-btn class="ml-3" color="primary" @click="handleAction(2, '', val)">刷新职位</v-btn>
          </div>
          <div v-if="tab === 2">
            <v-btn color="primary" @click="handleAction(1, '', val)">激活职位</v-btn>
          </div>
        </div>
      </div>
      <div class="bottom pa-5 d-flex justify-space-between align-center">
        <div>刷新时间：{{ timesTampChange(val.updateTime).slice(0, 10) }} {{ val.expireDay && Number(val.expireDay) >= 1 ? `（${ val.expireDay }天后到期）` : '' }}</div>
        <div class="d-flex">
          <div class="ml-10 d-flex">
            <div v-if="tab === 1">
              <span class="cursor-pointer" @click="handleAction(3, '', val)">置顶</span>
              <span class="lines"></span>
              <span class="cursor-pointer" @click="handleAction(0, '', val)">{{ $t('common.close') }}</span>
              <span class="lines"></span>
            </div>
            <span class="cursor-pointer" @click="handleToStatistics">{{ $t('position.recruitmentStatistics') }}</span>
            <div v-if="tab !== 3">
              <span class="lines"></span>
              <span class="cursor-pointer" @click="handleEdit(val)">{{ $t('common.edit') }}</span>
            </div>
            <div v-if="tab === 4">
              <span class="lines"></span>
              <span class="cursor-pointer">{{ $t('common.close') }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
defineOptions({ name: 'enterprise-position-item'})
import { defineEmits, ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { timesTampChange } from '@/utils/date'
import { closeJobAdvertised, enableJobAdvertised, refreshJobAdvertised, topJobAdvertised } from '@/api/position'
import Snackbar from '@/plugins/snackbar'

const emit = defineEmits(['refresh'])
const props = defineProps({
  tab: {
    type: Number,
    default: 1
  },
  items: Array
})

const selectAll = ref(false) // 全选
const selectList = ref([]) // 选中列表
const dealSelect = () => {
  selectList.value = props.items.filter(e => e.select).map(k => k.id)
}

// 全选
const handleChangeSelectAll = () => {
  props.items.map(k => {
    k.select = selectAll.value
    return k
  })
  dealSelect()
}
// 单选
const handleChangeSelect = () => {
  const length = props.items.filter(k => k.select).length
  selectAll.value = length > 0 ? true : false
  dealSelect()
}

// tab改变时清空选中的项
watch(
  () => props.tab,
  () => {
    props.items.map(e => e.select = false)
    selectList.value = []
    selectAll.value = false
  },
  { immediate: true }
)

// 分页选中回显
watch(
  () => props.items,
  (newVal) => {
    selectList.value.forEach(e => {
      const obj = newVal.find(k => k.id === e)
      if (obj) obj.select = true
    })
  },
  { immediate: true },
  { deep: true }
)

const apiList = [
  { api: closeJobAdvertised, desc: '关闭成功' },
  { api: enableJobAdvertised, desc: '激活成功' },
  { api: refreshJobAdvertised, desc: '刷新成功' },
  { api: topJobAdvertised, desc: '置顶成功' }
]

// 职位关闭、激活、刷新、置顶
const handleAction = async (index, type, { id }) => {
  const ids = type ? props.items.filter(e => e.select).map(k => k.id) : [id]
  if (!ids.length && !index) return
  await apiList[index].api(ids)
  Snackbar.success(apiList[index].desc)
  // 清空选项
  selectList.value = []
  selectAll.value = false
  emit('refresh')
}

const router = useRouter()
// 职位编辑
const handleEdit = (val) => {
  router.push(`/enterprise/position/edit?id=${val.id}`)
}

// 职位详情
const handleDetails = (val) => {
  window.open(`/enterprise/position/details/${val.id}`)
}

// 跳转招聘统计
const handleToStatistics = () => {
  router.push('/enterprise/statistics/overallAnalysis')
}
</script>

<style scoped lang="scss">
.itemBox {
  border: 1px solid #e5e6eb;
  height: 145px;
}
.position-name {
  color: #333;
  font-size: 19px;
  cursor: pointer;
  &:hover {
    color: var(--v-primary-base);
  }
}
.position {
  max-width: 46%;
  position: relative;
  .item-select {
    position: absolute;
    left: -8px;
    top: -13px;
  }
}
.lines {
  display: inline-block;
  width: 1px;
  height: 17px;
  vertical-align: middle;
  background-color: #e0e0e0;
  margin: 0 10px;
}
.other-info {
  font-size: 15px;
  color: #666;
}
.bottom {
  height: 40px;
  background-color: #f7f8fa;
  font-size: 14px;
  color: #888;
}
.resume {
  display: flex;
  font-size: 13px;
  flex-direction: column;
  align-items: center;
  color: #888;
  margin-right: 100px;
  cursor: pointer;
}
.resume-number {
  font-size: 20px;
  font-weight: 700;
  color: #999;
}
</style>
