<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.xm">
            <a-input v-model:value="formData.xm" placeholder="请输入姓名" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="创建人" v-bind="validateInfos.cjr">
            <a-input v-model:value="formData.cjr" placeholder="请输入创建人" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="手机号" v-bind="validateInfos.sjh">
            <a-input v-model:value="formData.sjh" placeholder="请输入手机号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="调课事由" v-bind="validateInfos.tksy">
            <a-input v-model:value="formData.tksy" placeholder="请输入调课事由" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="12">
          <a-form-item label="xyjwmsshyj" v-bind="validateInfos.xyjwmsshyj">
            <a-input v-model:value="formData.xyjwmsshyj" placeholder="请输入xyjwmsshyj" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col> -->
        <a-col :span="12">
          <a-form-item label="yjxq" v-bind="validateInfos.yjxq">
            <a-input v-model:value="formData.yjxq" placeholder="请输入yjxq" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="申请人单位" v-bind="validateInfos.sqrdw">
            <a-input v-model:value="formData.sqrdw" placeholder="请输入申请人单位" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="12">
          <a-form-item label="bizbh" v-bind="validateInfos.bizbh">
            <a-input v-model:value="formData.bizbh" placeholder="请输入bizbh" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="jwcsh" v-bind="validateInfos.jwcsh">
            <a-input v-model:value="formData.jwcsh" placeholder="请输入jwcsh" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="szxyldshyj" v-bind="validateInfos.szxyldshyj">
            <a-input v-model:value="formData.szxyldshyj" placeholder="请输入szxyldshyj" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="shxq" v-bind="validateInfos.shxq">
            <a-input v-model:value="formData.shxq" placeholder="请输入shxq" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="lcjgbh" v-bind="validateInfos.lcjgbh">
            <a-input v-model:value="formData.lcjgbh" placeholder="请输入lcjgbh" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col> -->
        <a-col :span="12">
          <a-form-item label="补课计划" v-bind="validateInfos.bkjh">
            <a-input v-model:value="formData.bkjh" placeholder="请输入补课计划" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="时间戳" v-bind="validateInfos.timestamps">
            <a-input v-model:value="formData.timestamps" 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="pbh" v-bind="validateInfos.pbh">
            <a-input v-model:value="formData.pbh" placeholder="请输入pbh" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col> -->
        <a-col :span="12">
          <a-form-item label="教材" v-bind="validateInfos.jc">
            <a-input v-model:value="formData.jc" placeholder="请输入教材" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="上课日期" v-bind="validateInfos.skrq">
            <a-input v-model:value="formData.skrq" placeholder="请输入上课日期" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="课程信息编号" v-bind="validateInfos.kcxxbh">
            <a-input v-model:value="formData.kcxxbh" placeholder="请输入课程信息编号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="学期" v-bind="validateInfos.xq">
            <a-input v-model:value="formData.xq" placeholder="请输入学期" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="教师工号" v-bind="validateInfos.jsgh">
            <a-input v-model:value="formData.jsgh" placeholder="请输入教师工号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="编号" v-bind="validateInfos.bh">
            <a-input v-model:value="formData.bh" placeholder="请输入编号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="授课教室" v-bind="validateInfos.skjs">
            <a-input v-model:value="formData.skjs" placeholder="请输入授课教室" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="12">
          <a-form-item label="lcbh" v-bind="validateInfos.lcbh">
            <a-input v-model:value="formData.lcbh" placeholder="请输入lcbh" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col> -->
        <a-col :span="12">
          <a-form-item label="调整事由" v-bind="validateInfos.tklx">
            <!-- <a-input v-model:value="formData.tklx" placeholder="请输入1-调整时间，2-调整地点，3-更换教师，4-其他" :disabled="disabled"></a-input> -->
            <j-dict-select-tag
                  placeholder="请选择调整事由" v-model:value="formData.tklx" :disabled="disabled"
                  :options="[{ value: 1, label: '调整时间'},{ value: 2, label: '调整地点'},{ value: 3, label: '更换教师'},{ value: 4, label: '其他'}]"
                />
          </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 { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../Xxhbtkxx.api';
  import { Form } from 'ant-design-vue';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';

  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: '',
    xm: '',
    cjr: '',
    sjh: '',
    tksy: '',
    xyjwmsshyj: '',
    yjxq: '',
    sqrdw: '',
    bizbh: '',
    jwcsh: '',
    szxyldshyj: '',
    shxq: '',
    lcjgbh: '',
    bkjh: '',
    timestamps: '',
    kcmc: '',
    pbh: '',
    jc: '',
    skrq: '',
    kcxxbh: '',
    xq: '',
    jsgh: '',
    bh: '',
    skjs: '',
    lcbh: '',
    tklx: '',
  });
  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 = {
  };
  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(',');
        }
      }
    }
    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>
