<template>
  <div style="width: 100%;">
    <CtForm ref="formPageRef" :items="items"></CtForm>
  </div>
</template>

<script setup>
import { getDict } from '@/hooks/web/useDictionaries'
defineOptions({name: 'shareJob-form-baseInfo'})
import { reactive, ref } from 'vue'

const formPageRef = ref()
let query = reactive({})

const items = ref({
  options: [
    {
      type: 'text',
      key: 'name',
      value: '',
      default: null,
      label: '姓名 *',
      outlined: true,
      rules: [v => !!v || '请输入姓名']
    },
    {
      type: 'phoneNumber',
      key: 'phone',
      value: '',
      clearable: true,
      label: '联系手机号 *',
      rules: [v => !!v || '请填写联系手机号']
    },
    {
      type: 'autocomplete',
      key: 'jobStatus',
      value: '',
      default: null,
      label: '求职状态 *',
      outlined: true,
      itemText: 'label',
      itemValue: 'value',
      dictTypeName: 'menduner_job_status',
      rules: [v => !!v || '请选择求职状态'],
      items: []
    },
    {
      type: 'autocomplete',
      key: 'expType',
      value: '',
      default: null,
      label: '工作经验 *',
      outlined: true,
      itemText: 'label',
      itemValue: 'value',
      dictTypeName: 'menduner_exp_type',
      rules: [v => !!v || '请选择工作经验'],
      items: []
    },
    {
      type: 'autocomplete',
      key: 'eduType',
      value: '',
      default: null,
      label: '最高学历 *',
      outlined: true,
      itemText: 'label',
      itemValue: 'value',
      dictTypeName: 'menduner_education_type',
      rules: [v => !!v || '请选择最高学历'],
      items: []
    },
  ]
})

// 获取字典内容
const getDictData = async (dictTypeName) => {
  const item = items.value.options.find(e => e.dictTypeName === dictTypeName)
  if (item) {
    const { data } = await getDict(dictTypeName)
    item.items = data
  }
}

items.value.options.forEach((e) => {
  if (e.dictTypeName) getDictData(e.dictTypeName) // 查字典set options
  // formItems回显
  // const infoExist = baseInfo.value && Object.keys(baseInfo.value).length
  // if (infoExist && baseInfo.value[e.key]) e.value = baseInfo.value[e.key]
  // // 日期相关
  // if (e.type === 'datepicker') e.value = timesTampChange(e.value).slice(0, 10)
  // // 所在城市回显
  // if (infoExist && e.nameKey) e[e.nameKey] = baseInfo.value[e.nameKey]
})

const getQuery = async () => {
  const { valid } = await formPageRef.value.formRef.validate()
  if (!valid) return false
  const obj = {}
  items.value.options.forEach(e => {
    if (Object.prototype.hasOwnProperty.call(e, 'data')) return obj[e.key] = e.data
    obj[e.key] = e.value
  })
  query = Object.assign(query, obj)
  return query
}

defineExpose({
  getQuery
})
</script>