<template>
  <div :class="{'disable': info && info?.entitlement && !info?.entitlement?.hireJob}">
    <v-card class="card-box pa-5">
      <div class="d-flex justify-center mt-3">
        <TextUI :item="textItem" @enter="handleEnter" @appendInnerClick="handleEnter"></TextUI>
      </div>
      <div class="text-end">
        <v-btn prepend-icon="mdi-plus" color="primary" @click="handleAdd">{{ $t('position.newPositionsAdded') }}</v-btn>
        <v-btn :loading="exportLoading" prepend-icon="mdi-export-variant" color="primary" variant="tonal" class="ml-3" @click="handleExport">职位列表下载</v-btn>
      </div>
      
      <div class="mt-3">
        <v-tabs v-model="tab" align-tabs="start" color="primary" bg-color="#f7f8fa" @update:model-value="handleChangeTab">
          <v-tab v-for="val in tabList" :key="val.value" :value="val.value"> {{ val.label }}</v-tab>
        </v-tabs>
        <v-window v-model="tab" class="mt-3">
          <v-window-item v-for="val in tabList" :key="val.value" :value="val.value">
            <PositionItem v-if="items.length" :tab="tab" :items="items" @refresh="handleRefresh"></PositionItem>
          </v-window-item>
        </v-window>
        <Empty v-if="!items.length" :message="loading ? '加载中...' : tipsText" :elevation="false"></Empty>
        <CtPagination
          v-else
          :total="total"
          :page="query.pageNo"
          :limit="query.pageSize"
          @handleChange="handleChangePage"
        ></CtPagination>
      </div>
    </v-card>
  </div>
</template>

<script setup>
defineOptions({ name: 'enterprise-hire-position-list'})
import { ref } from 'vue'
import TextUI from '@/components/FormUI/TextInput'
import PositionItem from './components/item.vue'
import { useRouter } from 'vue-router'; const router = useRouter()
import { getJobAdvertisedList, getJobAdvertisedExport } from '@/api/position'
import { dealDictArrayData } from '@/utils/position'
import { useI18n } from '@/hooks/web/useI18n'
import { useUserStore } from '@/store/user'
import download from '@/utils/download'
import { getEnterprisePubJobTypePermission } from '@/api/recruit/enterprise/position'
import Snackbar from '@/plugins/snackbar'

const store = useUserStore()

const { t } = useI18n()
const total = ref(0)
const tipsText = ref(t('common.noData'))
const query = ref({
  pageSize: 10,
  pageNo: 1,
  status: 0, // 0招聘中 1已关闭
  hasExpiredData: false, // true 到期职位
  hire: true
})
const exportLoading = ref(false)

const tab = ref(1)

const tabList = [
  { label: '待发布', value: 0, status: 99 },
  { label: t('position.recruitmentInProgress'), value: 1, status: 0 },
  { label: t('position.closed'), value: 2, status: 1 },
  { label: t('position.expiredPosition'), value: 3 }
]

const items = ref([])
const textItem = ref({
  type: 'text',
  width: 600,
  value: '',
  label: '请输入职位名称',
  clearable: true,
  appendInnerIcon: 'mdi-magnify'
})

// 获取企业权益信息
const info = ref(localStorage.getItem('entBaseInfo') ? JSON.parse(localStorage.getItem('entBaseInfo')) : {})
store.$subscribe((mutation, state) => {
  if (Object.keys(state.entBaseInfo).length) info.value = state.entBaseInfo
})

const handleAdd = async () => {
  const data = await getEnterprisePubJobTypePermission()
  if (!data || !data.length) return Snackbar.warning('没有该操作权限，请联系平台管理员升级后再试')
  router.push('/recruit/enterprise/hirePosition/add')
  await store.getEnterpriseUserAccountInfo()
}

const handleExport = async () => {
  exportLoading.value = true
  try {
    const data = await getJobAdvertisedExport(query.value)
    download.excel(data, '众聘职位列表.xlsx')
  } finally {
    exportLoading.value = false
  }
}


const loading = ref(false)
// 获取职位列表
const getPositionList = async () => {
  items.value = []; total.value = 0
  loading.value = true
  if (tab.value !== 3) {
    query.value.status = tabList[tab.value].status
    query.value.hasExpiredData = false
  } else {
    query.value.hasExpiredData = true
    delete query.value.status
  }
  const { list, total: number } = await getJobAdvertisedList(query.value)
  if (!list.length) {
    if (query.value.name) tipsText.value = '暂无数据，请更换关键词后再试'
  }
  total.value = number
  items.value = list.length ? dealDictArrayData([], list) : []
  loading.value = false
}
getPositionList()

const handleRefresh = (index) => {
  query.value.pageNo = 1
  if (index) tab.value = index
  getPositionList()
}

const handleChangeTab = () => {
  query.value.pageNo = 1
  getPositionList()
}

const handleChangePage = (index) => {
  query.value.pageNo = index
  getPositionList()
}

// 职位名称检索
const handleEnter = (e) => {
  query.value.name = e
  query.value.pageNo = 1
  getPositionList()
}
</script>

<style scoped lang="scss">
.disable {
  position: relative;
  overflow: hidden;
  &::after {
    content: '很抱歉，您当前没有权限使用全员猎寻模块';
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5em;
    font-weight: bold;
    color: #fff;
    top: 0;
    border-radius: 12px;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.35);
  }
}
</style>
