<template>
  <div>
    <CtForm ref="formPageRef" :items="items" style="width: 650px;">
      <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>
        <v-btn v-if="showTemplateBtn" class="ml-3 half-button" color="primary" style="margin-top: 2px;" @click="useJobTemplate(item)">岗位模版</v-btn>
      </template>
    </CtForm>
  </div>
</template>

<script setup>
defineOptions({ name: 'position-add-baseInfo'})
import CtForm from '@/components/CtForm'
import { reactive, ref, watch } from 'vue'
import textUI from '@/components/FormUI/TextInput'
import jobTypeCard from '@/components/jobTypeCard'
import { getRecruitPositionDetails } from '@/api/recruit/enterprise/position'
import Confirm from '@/plugins/confirm'
import Snackbar from '@/plugins/snackbar'
import { useI18n } from '@/hooks/web/useI18n';

const { t } = useI18n()
const props = defineProps({
  itemData: Object
})

const getValue = (key) => {
  return items.value.options.find(e => e.key === key)
}

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

const items = ref({
  options: [
    {
      type: 'text',
      key: 'name',
      value: '',
      label: '职位名称 *',
      rules: [v => !!v || '请填写职位名称']
    },
    {
      type: 'datePicker',
      key: 'expireTime',
      value: null,
      format: 'YYYY-MM-DD',
      label: '到期时间 *',
      labelWidth: 120
    },
    {
      slotName: 'positionId',
      key: 'positionId',
      value: '',
      flexStyle: 'mt-5',
      labelKey: 'positionName',
      label: '职位类型 *',
      noParam: true,
      readonly: true,
      rules: [v => !!v || '请选择职位类型']
    },
    {
      type: 'wangEditor',
      key: 'content',
      value: '',
      label: '岗位职责 *',
      maxLength: 5000,
      rules: '请填写岗位职责'
    },
    {
      type: 'wangEditor',
      key: 'requirement',
      value: '',
      label: '岗位要求 *',
      maxLength: 5000,
      rules: '请填写岗位要求'
    }
  ]
})

// 编辑回显
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) => {
  const positionId = getValue('positionId')
  if (!list.length) {
    delete query.positionId
    positionId.value = ''
    return
  }
  showTemplateBtn.value = true
  query.positionId = list[0]
  positionId.value = name
}

const useJobTemplate = async () => {
  if (!query.positionId) return Snackbar.warning('请先选择职位类型')
  // 获取职位模板内容-赋值
  const res = await getRecruitPositionDetails(query.positionId)
  if (!res || !res.content || !res.requirement) {
    Snackbar.warning('此职位类型没有可使用的模板！')
    showTemplateBtn.value = false
    return
  }
  const content =  items.value.options.find(e => e.key === 'content')
  const requirement =  items.value.options.find(e => e.key === 'requirement')
  if ((content && content.value) || (requirement && requirement.value)) {
    // 弹窗提示
    Confirm(t('common.confirmTitle'), '您确定要放弃目前岗位描述的内容吗？').then(() => {
      content.value = res.content
      requirement.value = res.requirement
      Snackbar.success('模板填充完成！')
    })
  } else {
    // 无内容点击默认填充
    if (content) content.value = res.content
    if (requirement) requirement.value = res.requirement
    Snackbar.success('模板填充完成！')
  }
}

const getQuery = async () => {
  const { valid } = await formPageRef.value.formRef.validate()
  if (!valid) return
  const obj = {
    hirePrice: 0,
    hire: false
  }
  items.value.options.forEach(e => {
    if (e.noParam) return
    else obj[e.key] = e.value
  })
  if (!obj.content) {
    Snackbar.warning('请填写岗位职责')
    return 'failed'
  }
  if (!obj.requirement) {
    Snackbar.warning('请填写岗位要求')
    return 'failed'
  }
  
  query = Object.assign(query, obj)
  return query
}

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

<style scoped lang="scss">
.jobTypeCardBox {
  position: absolute;
  top: -22px;
  left: 0;
}
.calculation {
  display: block;
  width: 120px;
}
</style>
