<template>
  <add :after-add="afterAdd" :valid="validate" :isFair="true">
    <template #timeline>
      <v-timeline-item
          dot-color="light-blue darken-1"
          icon="mdi-numeric-3"
        >
        <div>
          <h2 class="mt-n1 headline font-weight-regular">{{ t('common.other') }}</h2>
          <CtForm ref="formPageRef" class="mt-3" :items="items" style="width: 650px;"></CtForm>
        </div>
      </v-timeline-item>
    </template>
  </add>
</template>

<script setup>
defineOptions({ name: 'editJob' })
import { ref } from 'vue'
import {  useRoute, useRouter } from 'vue-router'
import CtForm from '@/components/CtForm'
import Add from '@/views/recruit/enterprise/positionManagement/components/add.vue'
import { useI18n } from '@/hooks/web/useI18n'
import { schoolMajorByName, schoolMajorById } from '@/api/recruit/personal/resume'
import {
  saveJobAdvertisedExtend,
  joinJobFairPosition,
  getJobAdvertisedExtend
} from '@/api/recruit/enterprise/jobFair'
import Snackbar from '@/plugins/snackbar'

const { t } = useI18n()
const route = useRoute()
const router = useRouter()

const formPageRef = ref(null)

const items = ref({
  options: [
    {
      type: 'text',
      key: 'dept',
      value: null,
      label: '招聘部门 ',
      // rules: [v => !!v || '请选择招聘部门']
    },
    {
      type: 'autocomplete',
      key: 'majorId',
      search: getMajorList,
      value: null,
      label: '专业要求 ',
      itemText: 'nameCn',
      itemValue: 'id',
      // rules: [v => !!v || '专业要求'],
      noDataText: '请输入检索专业',
      items: []
    },
    {
      type: 'autocomplete',
      key: 'frequency-dateType',
      value: null,
      label: '工作频率 ',
      col: 6,
      // rules: [v => !!v || '请选择工作频率'],
      items: [
        { label: '每周', value: 'week' },
        { label: '每月', value: 'month' },
        { label: '每年', value: 'year' },
      ]
    },
    {
      type: 'number',
      key: 'frequency-day',
      value: null,
      flexStyle: 'ml-3',
      col: 6,
      label: '出勤天数 ',
      // rules: [v => v > 0 || '请填写正确的出勤天数']
    },
    {
      type: 'ifRadio',
      key: 'hot',
      value: 1,
      label: '热门职位 ',
      items: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
      // rules: [v => !!v || '请选择是否热门']
    },
  ]
})

// console.log(route)
if (route.query.id) {
  // 编辑
  initPosition(route.query.id)
}

async function initPosition (jobId) {
  const res = await getJobAdvertisedExtend(jobId)
  if (!res) return
  items.value.options.forEach(e => {
    if (e.key.includes('frequency') && res.frequency) {
      const keys = e.key.split('-')
      e.value = res[keys[0]][keys[1]]
      return
    }
    if (e.key === 'majorId') {
      getMajorById(res.majorId)
    }
    e.value = res[e.key]
  })
}

async function getMajorList (name) {
  if (!name) {
    return
  }
  const res = await schoolMajorByName({ name })
  items.value.options.find(e => e.key === 'majorId').items = res
}

async function getMajorById (id) {
  if (!id) return
  const res = await schoolMajorById({ id })
  items.value.options.find(e => e.key === 'majorId').items = [res]
}

const validate = async () => {
  const res = await formPageRef.value.formRef.validate()
  return res
}
const afterAdd = async (jobId) => {
  try {
    const query = items.value.options.reduce((r, v) => {
      if (v.key.includes('frequency')) {
        const keys = v.key.split('-')
        if (!r[keys[0]]) {
          r[keys[0]] = {}
        }
        r[keys[0]][keys[1]] = v.type === 'number' ? +v.value : v.value
        return r
      }
      if (v.key === 'majorId') {
        r.major = v.items.find(e => e.id === v.value)?.nameCn || ''
      }
      r[v.key] = v.type === 'number' ? +v.value : v.value
      return r
    }, { jobId })
    await saveJobAdvertisedExtend(query)
    console.log('招聘会职位扩展信息保存成功')
    await joinJobFairPosition({
      jobFairId: route.params.id,
      jobId
    })
    Snackbar.success(t('common.publishSuccessMsg'))
    router.push(`/recruit/enterprise/jobFair/details/${route.params.id}`)
  } catch (error) {
    console.error(error)
    Snackbar.error(error)
  }
}
</script>

<style lang="scss" scoped>

</style>