import { knowledgeTypeData, lessonType } from '@/views/knowledge-manage/knowledgeTypeData'
import {
  FormInst,
  FormItemRule,
  FormRules,
  NButton,
  NForm,
  NFormItem,
  NInput,
  NSpace,
  NSelect,
  NSpin,
  useMessage,
  NRadio,
  NRadioGroup
} from 'naive-ui'
import { SelectMixedOption } from 'naive-ui/es/select/src/interface'
import { defineComponent, onMounted, reactive, ref } from 'vue'
import { lessonTrainingSave, lessonTrainingUpdate } from '../../api'

export default defineComponent({
  name: 'AddTraining',
  emits: ['handleSuccess', 'close'],
  props: ['item'],
  setup(props, { emit }) {
    console.log('🚀 ~ props', props)
    const message = useMessage()
    const loading = ref(false)
    const formRef = ref<FormInst | null>(null)
    const saveModel = reactive({
      name: props?.item?.name || '',
      courseTypeCode: props?.item?.courseTypeCode?.split(',') || [] //课程类型
    })
    function validateCourseTypeCode(rule: FormItemRule, value: []) {
      if (Array.isArray(value) && !value.length) {
        return new Error('请选择课程类型')
      }
      return true
    }
    const rules: FormRules = {
      name: [{ required: true, message: '请输入训练名称', trigger: ['blur', 'change'] }],
      courseTypeCode: [
        {
          validator: validateCourseTypeCode,
          message: '请选择课程类型',
          trigger: ['blur', 'change']
        }
      ]
    }
    const hanldeAdd = () => {
      formRef.value?.validate(async (err) => {
        console.log('🚀 ~ err', err)
        if (!err) {
          let res: any = null
          if (props?.item?.id) {
            const params: any = {
              ...props.item,
              ...saveModel,
              courseTypeCode: saveModel.courseTypeCode?.join(',') || '',
              details: props.item?.details || []
            }
            res = await lessonTrainingUpdate(params)
          } else {
            const params: any = {
              ...saveModel,
              courseTypeCode: saveModel.courseTypeCode?.join(',') || '',
              trainingNum: 0,
              details: []
            }
            res = await lessonTrainingSave(params)
          }
          if (res?.code == 200) {
            message.success('保存成功')
            emit('handleSuccess')
          } else {
            message.warning('保存失败')
          }
        }
      })
    }
    return () => (
      <div>
        <NSpin show={loading.value}>
          <NForm
            ref={formRef}
            labelPlacement="top"
            model={saveModel}
            rules={rules}
            require-mark-placement="left"
          >
            <NFormItem label="训练名称" path="name">
              <NInput v-model:value={saveModel.name} />
            </NFormItem>
            <NFormItem label="课程类型" path="courseTypeCode">
              <NSelect
                multiple
                disabled={props?.item ? true : false}
                v-model:value={saveModel.courseTypeCode}
                options={lessonType}
                clearable
              />
            </NFormItem>
          </NForm>
          <NSpace justify="end">
            <NButton onClick={() => emit('close')}>取消</NButton>
            <NButton type="primary" onClick={hanldeAdd}>
              保存
            </NButton>
          </NSpace>
        </NSpin>
      </div>
    )
  }
})
