<template>
  <div>
    <CtForm ref="formPageRef" :items="items" style="width: 600px;">
      <template #explain>
        <span style="color: var(--v-error-base); cursor: pointer;" @click="handleViewRule">
          <v-icon size="25" color="error">mdi-help-circle-outline</v-icon>
          众聘岗位规则说明
        </span>
      </template>
      <template #ratio>
        <div class="font-size-13 mb-3" style="color: red;">
          众聘岗位分配比例：平台占比{{ ratio.headhuntRate }}%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 推荐人占比{{ ratio.recommendRate }}%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 投递人占比{{ ratio.cvRate }}%
        </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>

    <CtDialog :visible="show" :widthType="1" titleClass="text-h6" title="众聘岗位规则说明" :footer="false" @close="show = false">
      <RulePage />
    </CtDialog>
  </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'
import RulePage from './rule.vue'
import { getPublicRatio } from '@/api/recruit/enterprise/position'

const props = defineProps({
  itemData: Object
})

const show = ref(false)
const ratio = ref({})

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

// 众聘奖励类型
const handleChangeHireType = () => {
  const hireType = getValue('hireType')
  let hirePrice = getValue('hirePrice')
  let hirePoint = getValue('hirePoint')
  if (hireType.value) {
    hirePrice.hide = false
    hirePoint.hide = true
    hirePoint.value = null
  } else {
    hirePoint.hide = false
    hirePrice.hide = true
    hirePrice.value = null
  }
}

// 是否众聘岗位
const handleChangePublic = (val) => {
  let hireType
  items.value.options.forEach(e => {
    if (!val && Object.prototype.hasOwnProperty.call(e, 'hide')) {
      e.hide = true
      e.value = null
      return
    }
    if (e.key === 'hireType') {
      e.hide = !val
      e.value = props.itemData && props.itemData.hirePoint ? false : true
      hireType = e
    }
    if (Object.prototype.hasOwnProperty.call(e, 'show')) {
      if (hireType.value === e.show) e.hide = false
      return
    }
    if (e.slotName === 'explain') e.hide = val ? false : true
  })
}

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

const items = ref({
  options: [
    {
      type: 'ifRadio',
      key: 'hire',
      value: false,
      label: '众聘岗位 *',
      width: 90,
      disabled: false,
      hideDetails: true,
      items: [
        { label: '是', value: true },
        { label: '否', value: false }
      ],
      change: val => handleChangePublic(val)
    },
    {
      slotName: 'explain',
      noParam: true,
      value: null,
      disabled: false,
      hide: true,
      flexStyle: 'mb-3'
    },
    {
      type: 'ifRadio',
      key: 'hireType',
      value: null,
      label: '众聘奖励类型 *',
      width: 120,
      hide: true,
      disabled: false,
      noParam: true,
      hideDetails: true,
      items: [
        { label: '赏金', value: true },
        { label: '积分', value: false }
      ],
      change: handleChangeHireType
    },
    {
      slotName: 'ratio',
      noParam: true,
      show: true,
      hide: true
    },
    {
      type: 'number',
      key: 'hirePrice',
      value: null,
      label: '众聘赏金 *',
      suffix: '元',
      hide: true,
      show: true,
      rules: [
        value => {
          if (value) return true
          return '请填写众聘赏金'
        },
        value => {
          if (value >= 1) return true
          return '赏金金额不得小于1'
        }
      ]
    },
    {
      type: 'number',
      key: 'hirePoint',
      value: null,
      label: '众聘奖励积分数 *',
      suffix: '点',
      hide: true,
      disabled: false,
      show: false,
      rules: [
        value => {
          if (value) return true
          return '请填写众聘众聘奖励积分数'
        },
        value => {
          if (value >= 1) return true
          return '积分数不得小于1'
        }
      ]
    },
    
    {
      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 getRatio = async () => {
  const data = await getPublicRatio()
  ratio.value = data
}
getRatio()

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

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

// 众聘规则查看
const handleViewRule = () => {
  show.value = true
}

const getQuery = async () => {
  const { valid } = await formPageRef.value.formRef.validate()
  if (!valid) return
  const obj = {}
  items.value.options.forEach(e => {
    if (e.noParam) return
    else obj[e.key] = e.value
  })
  // 不是众聘岗位的默认给0
  if (!obj.hire) {
    obj.hirePoint = 0
    obj.hirePrice = 0
  }
  query = Object.assign(query, obj)
  return query
}

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

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