<template>
  <v-card class="pa-5 card-box">
    <CtTable
      :items="items"
      :headers="headers"
      :loading="false"
      :elevation="0"
      :showPage="true"
      :total="total"
      :page-info="query"
      itemKey="id"
      @add="handleAdd"
      @edit="handleEdit"
      @del="handleDelete"
      @pageHandleChange="handleChangePage"
    >
    </CtTable>
  </v-card>
</template>

<script setup>
defineOptions({ name: 'system-post-index'})
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { timesTampChange } from '@/utils/date'
import { useI18n } from '@/hooks/web/useI18n'
import { getEnterprisePostPage, deleteEnterprisePost, checkPost } from '@/api/recruit/enterprise/system/post'
import Confirm from '@/plugins/confirm'
import Snackbar from '@/plugins/snackbar'

const { t } = useI18n()
const router = useRouter()
const total = ref(10)
const items = ref([])
const query = ref({
  pageNo: 1,
  pageSize: 10
})

const headers = [
  { title: '岗位名称', key: 'nameCn', sortable: false },
  { title: '英文名称', key: 'nameEn', sortable: false },
  { title: '岗位缩写', key: 'code', sortable: false },
  { title: '显示顺序', key: 'sort', sortable: false },
  { title: '状态', key: 'status', value: item => item.status === '1' ? '停用' : '正常', sortable: false },
  { title: '备注', key: 'remark', sortable: false },
  { title: '更新时间', key: 'updateTime', value: item => timesTampChange(item.updateTime), sortable: false },
  { title: t('common.actions'), key: 'actions', sortable: false }
]

const getList = async () => {
  const res = await getEnterprisePostPage(query.value)
  items.value = res.list
  total.value = res.total
}
getList()

const handleChangePage = (e) => {
  query.value.pageNo = e
  getList()
}

const handleAdd = () => {
  router.push('/recruit/enterprise/systemManagement/postManagement/add')
}

const handleEdit = (item) => {
  if (!item.id) return
  router.push(`/recruit/enterprise/systemManagement/postManagement/edit?id=${item.id}`)
}

const handleDelete = async ({ id }) => {
  const check = await checkPost(id)
  if (check) return Snackbar.warning('查询到该岗位有绑定用户，请先到用户管理里面修改后再试')
  Confirm(t('common.confirmTitle'), '是否确认删除此项内容？').then(async () => {
    await deleteEnterprisePost(id)
    Snackbar.success(t('common.delMsg'))
    getList()
  })
}
</script>

<style scoped lang="scss">

</style>
