<template>
  <div>
    <CtForm ref="formPageRef" :items="items" style="width: 600px;">
      <template #enterpriseName="{ item }">
        <div class="mb-4">
          <span style="color: var(--color-666);">公司：</span>
          <span style="color: #555">{{ item.value }}</span>
        </div>
      </template>
      <template #positionId="{ item }">
        <v-menu :close-delay="1" :open-delay="0" v-bind="$attrs">
          <template v-slot:activator="{  props }">
            <textUI
              :modelValue="item.value"
              :item="item"
              v-bind="props"
              style="position: relative;"
            ></textUI>
          </template>
          <jobTypeCard class="jobTypeCardBox" :select="[query.positionId].filter(Boolean)" :isSingle="true" @handleJobClick="handleJobClickItem"></jobTypeCard>
        </v-menu>
      </template>
    </CtForm>
  </div>
</template>

<script setup>
defineOptions({ name: 'position-add-baseInfo'})
import CtForm from '@/components/CtForm'
import { reactive, ref, defineExpose, watch } from 'vue'
import textUI from '@/components/FormUI/TextInput'
import jobTypeCard from '@/components/jobTypeCard'

const props = defineProps({
  itemData: Object
})

const handleChangePublic = () => {
  const value = items.value.options.find(e => e.key === 'isPublicRecruitment').value
  const obj = items.value.options.find(e => e.key === 'moneyReward')
  obj.hide = value === '1' ? false : true
}

const formPageRef = ref()
let query = reactive({})
const items = ref({
  options: [
    {
      type: 'ifRadio',
      key: 'isPublicRecruitment',
      value: '0',
      label: '众聘岗位 *',
      width: 90,
      noParam: true,
      items: [
        { label: '是', value: '1' },
        { label: '否', value: '0' }
      ],
      change: handleChangePublic
    },
    {
      type: 'number',
      key: 'moneyReward',
      value: null,
      label: '众聘赏金 *',
      hide: true,
      noParam: true,
      rules: [v => !!v || '请填写众聘赏金']
    },
    {
      type: 'text',
      key: 'name',
      value: '',
      label: '职位名称 *',
      rules: [v => !!v || '请填写职位名称']
    },
    {
      slotName: 'positionId',
      key: 'positionId',
      value: '',
      labelKey: 'positionName',
      label: '职位类型 *',
      noParam: true,
      rules: [v => !!v || '请选择职位类型']
    },
    {
      type: 'datePicker',
      key: 'expireTime',
      value: null,
      class: 'mb-3',
      options: {
        format: 'timestamp',
        placeholder: '到期时间 *',
      },
      rules: [v => !!v || '请选择到期时间']
    },
    {
      type: 'textarea',
      key: 'content',
      rows: 10,
      value: '',
      label: '岗位职责 *',
      counter: 5000,
      clearable: true,
      rules: [
        value => {
          if (value) return true
          return '请输入岗位职责'
        },
        value => {
          if (value?.length <= 5000) return true
          return '请输入2-5000个字符'
        }
      ]
    },
    {
      type: 'textarea',
      key: 'requirement',
      rows: 10,
      value: '',
      label: '岗位要求 *',
      counter: 5000,
      clearable: true,
      rules: [
        value => {
          if (value) return true
          return '请输入岗位要求'
        },
        value => {
          if (value?.length <= 5000) return true
          return '请输入2-5000个字符'
        }
      ]
    }
  ]
})

const setValue = (key, value) => {
  items.value.options.find(e => e.key === key).value = value
}

// 编辑回显
watch(
  () => props.itemData,
  (val) => {
    if (!Object.keys(val).length) return
    items.value.options.forEach(e => {
      if (e.labelKey) {
        query[e.key] = val[e.key]
        e.value = val[e.labelKey]
        return
      }
      if (e.noParam) return
      e.value = val[e.key]
    })
  },
  { immediate: true },
  { deep: true }
)

// 职位类型
const handleJobClickItem = (list, name) => {
  if (!list.length) {
    delete query.positionId
    setValue('positionId', '')
    return
  }
  query.positionId = list[0]
  setValue('positionId', name)
}

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

defineExpose({
  formPageRef,
  getQuery
})
</script>

<style scoped lang="scss">
.enterpriseName {
  color: var(--color-666);
}
.jobTypeCardBox {
  position: absolute;
  top: -22px;
  left: 0;
}
</style>
