<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="学院" v-bind="validateInfos.xy">
            <a-input v-model:value="formData.xy" placeholder="请输入学院" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="讲师姓名" v-bind="validateInfos.hxr">
            <a-input v-model:value="formData.hxr" placeholder="请输入讲师姓名" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="职称" v-bind="validateInfos.zc">
            <a-input v-model:value="formData.zc" placeholder="请输入职称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="课程名称" v-bind="validateInfos.kcmc">
            <a-input v-model:value="formData.kcmc" placeholder="请输入课程名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="课堂主题" v-bind="validateInfos.ktzt">
            <a-input v-model:value="formData.ktzt" placeholder="请输入课堂主题" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="上课日期" v-bind="validateInfos.rq">
            <a-input v-model:value="formData.rq" placeholder="请输入日期" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="上课开始时间" v-bind="validateInfos.kssj">
            <a-input v-model:value="formData.kssj" placeholder="请输入开始时间" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="上课结束时间" v-bind="validateInfos.jssj">
            <a-input v-model:value="formData.jssj" placeholder="请输入结束时间" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="课程链接">
            <a-input v-model:value="formData.kclj" placeholder="请输入课程链接" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="教师介绍链接" v-bind="validateInfos.jslj">
            <a-input v-model:value="formData.jslj" placeholder="请输入教师介绍链接" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="12">
          <a-form-item label="当前状态" v-bind="validateInfos.dqzt">
            <a-input v-model:value="formData.dqzt" placeholder="请输入当前状态" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col> -->
        <a-col :span="12">
          <a-form-item label="当前状态" v-bind="validateInfos.dqztpx">
            <j-dict-select-tag type='radio' v-model:value="formData.dqztpx" dictCode="dqztpx" placeholder="请选择当前状态排序" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="是否显示" v-bind="validateInfos.sfxs">
            <j-dict-select-tag type='radio' v-model:value="formData.sfxs" dictCode="sfxs" placeholder="请选择是否显示" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="候选人类别" v-bind="validateInfos.hxrlb">
            <a-input v-model:value="formData.hxrlb" placeholder="请输入候选人类别" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        
        <a-col :span="12">
          <a-form-item label="教师图片" v-bind="validateInfos.tpmc">
            <j-image-upload  v-model:value="formData.tpmc" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
        
        <!-- <a-col :span="12">
          <a-form-item label="教师图片2" v-bind="validateInfos.tpmc2">
            <j-image-upload  v-model:value="formData.tpmc2" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
        
        <a-col :span="12">
          <a-form-item label="教师图片3" v-bind="validateInfos.tpmc3">
            <j-image-upload  v-model:value="formData.tpmc3" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col> -->
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../KcGongkaike.api';
  import { Form } from 'ant-design-vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
import { filterDictTextByCache } from '/@/utils/dict/JDictSelectUtil.js';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    xy: '',
    hxr: '',
    zc: '',
    rq: '',
    kssj: '',
    jssj: '',
    kcmc: '',
    ktzt: '',
    kclj: '',
    tpmc: '',
    tpmc2: '',
    tpmc3: '',
    jslj: '',
    dqzt: '',
    dqztpx: '',
    sfxs: '',
    hxrlb: '',
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    xy: [{ required: true, message: '请输入学院!'},],
    hxr: [{ required: true, message: '请输入候选人!'},],
    zc: [{ required: true, message: '请输入职称!'},],
    rq: [{ required: true, message: '请输入日期!'},],
    kssj: [{ required: true, message: '请输入开始时间!'},],
    jssj: [{ required: true, message: '请输入结束时间!'},],
    kcmc: [{ required: true, message: '请输入课程名称!'},],
    ktzt: [{ required: true, message: '请输入课堂主题!'},],
    kclj: [{ required: true, message: '请输入课程链接!'},],
    dqztpx: [{ required: true, message: '请输入当前状态排序 1 正在上课 2公开课预告 3 已下课!'},],
    sfxs: [{ required: true, message: '请输入是否显示 1 显示 0 不显示!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    let dqztpx = model.dqztpx
    model.dqzt = filterDictTextByCache("dqztpx",dqztpx)
    console.log(`🚀 ~ file: KcGongkaikeForm.vue:215 ~ submitForm ~ model:`, model)
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
