<template>
  <div>
    <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>
      </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-1">
          <v-window-item v-for="val in tabList" :key="val.value" :value="val.value">
            <PositionItem v-if="items.length" :tab="val.value" :items="items" @refresh="getPositionList"></PositionItem>
          </v-window-item>
        </v-window>
        <Empty v-if="!items.length" :message="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-position-list'})
import { ref } from 'vue'
import TextUI from '@/components/FormUI/TextInput'
import PositionItem from './components/item.vue'
import { useRoute } from 'vue-router'; const route = useRoute()
import { useRouter } from 'vue-router'; const router = useRouter()
import { getJobAdvertisedList } from '@/api/position'
import { dealDictArrayData } from '@/utils/position'
import { useI18n } from '@/hooks/web/useI18n'
import { useUserStore } from '@/store/user'

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: false // true 众聘岗位
})

const showHire = (route.query?.hire - 0) || 0
const tab = ref(showHire ? 4: 1)
if (showHire) history.replaceState({ ...route.query, hire: 0 }, '', route.path) // 更新浏览器历史记录，不触发页面重新加载 ( 目的：去除目标参数 )

const tabList = [
  { label: t('position.recruitmentInProgress'), value: 1 },
  { label: t('position.closed'), value: 2 },
  { label: t('position.expiredPosition'), value: 3 },
  { label: t('position.publicRecruitment'), value: 4 }
]

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

const handleAdd = async () => {
  router.push('/recruit/enterprise/position/add')
  await store.getEnterpriseUserAccountInfo()
}


// 获取职位列表
const getPositionList = async () => {
  query.value.hasExpiredData = tab.value === 3 ? true : false
  query.value.hire = tab.value === 4 ? true : false

  if (tab.value !== 3) {
    query.value.status = tab.value === 1 ? 0 : (tab.value === 2 ? 1 : null)
  } else 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) : []
}
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">
</style>
