<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <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="24">
          <a-form-item label="轮次编号" v-bind="validateInfos.lcbh">
            <a-input v-model:value="formData.lcbh" placeholder="请输入轮次编号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="轮次出镜人" v-bind="validateInfos.lccjr">
            <a-input v-model:value="formData.lccjr" placeholder="请输入轮次出镜人" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="听课人工号" v-bind="validateInfos.tkrgh">
            <a-input v-model:value="formData.tkrgh" placeholder="请输入听课人工号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="听课人姓名" v-bind="validateInfos.tkrxm">
            <a-input v-model:value="formData.tkrxm" placeholder="请输入听课人姓名" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="听课人单位名称" v-bind="validateInfos.tkrdwmc">
            <a-input v-model:value="formData.tkrdwmc" placeholder="请输入听课人单位名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="听课时间学期" v-bind="validateInfos.tksjxq">
            <a-input v-model:value="formData.tksjxq" placeholder="请输入听课时间学期" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="课程节次" v-bind="validateInfos.kcjc">
            <a-input v-model:value="formData.kcjc" placeholder="请输入课程节次" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="课程单位" v-bind="validateInfos.kcdm">
            <a-input v-model:value="formData.kcdm" placeholder="请输入课程单位" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="听课教师" v-bind="validateInfos.tkjs">
            <a-input v-model:value="formData.tkjs" placeholder="请输入听课教师" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="听课课程名称" v-bind="validateInfos.tkkcmc">
            <a-input v-model:value="formData.tkkcmc" placeholder="请输入听课课程名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="任课教师" v-bind="validateInfos.rkjs">
            <a-input v-model:value="formData.rkjs" placeholder="请输入任课教师" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="总体评价" v-bind="validateInfos.ztpj">
            <a-input v-model:value="formData.ztpj" placeholder="请输入总体评价" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <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="24">
          <a-form-item label="全体意见" v-bind="validateInfos.qtyj">
	          <a-textarea v-model:value="formData.qtyj" rows="4" placeholder="请输入全体意见" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="教师评估1" v-bind="validateInfos.jspg1">
            <a-input v-model:value="formData.jspg1" placeholder="请输入教师评估1" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="教师评估2" v-bind="validateInfos.jspg2">
            <a-input v-model:value="formData.jspg2" placeholder="请输入教师评估2" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="教师评估3" v-bind="validateInfos.jspg3">
            <a-input v-model:value="formData.jspg3" placeholder="请输入教师评估3" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="教师评估4" v-bind="validateInfos.jspg4">
            <a-input v-model:value="formData.jspg4" placeholder="请输入教师评估4" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="教师评估5" v-bind="validateInfos.jspg5">
            <a-input v-model:value="formData.jspg5" placeholder="请输入教师评估5" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="学生评估1" v-bind="validateInfos.xspg1">
            <a-input v-model:value="formData.xspg1" placeholder="请输入学生评估1" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="学生评估2" v-bind="validateInfos.xspg2">
            <a-input v-model:value="formData.xspg2" placeholder="请输入学生评估2" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="学生评估3" v-bind="validateInfos.xspg3">
            <a-input v-model:value="formData.xspg3" placeholder="请输入学生评估3" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="课程编号" v-bind="validateInfos.kcbh">
            <a-input v-model:value="formData.kcbh" placeholder="请输入课程编号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="时间戳" v-bind="validateInfos.timestamps">
		        <a-date-picker placeholder="请选择时间戳" v-model:value="formData.timestamps" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="课表编号" v-bind="validateInfos.kbbh">
            <a-input v-model:value="formData.kbbh" placeholder="请输入课表编号" :disabled="disabled"></a-input>
          </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 '../XxhbbkjxtkpjBadLog.api';
  import { Form } from 'ant-design-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: '',
    bh: '',
    lcbh: '',
    lccjr: '',
    tkrgh: '',
    tkrxm: '',
    tkrdwmc: '',
    tksjxq: '',
    kcjc: '',
    kcdm: '',
    tkjs: '',
    tkkcmc: '',
    rkjs: '',
    ztpj: '',
    jsgh: '',
    qtyj: '',
    jspg1: '',
    jspg2: '',
    jspg3: '',
    jspg4: '',
    jspg5: '',
    xspg1: '',
    xspg2: '',
    xspg3: '',
    kcbh: '',
    timestamps: '',
    kbbh: '',
  });
  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 = {
    bh: [{ required: true, message: '请输入编号!'},],
  };
  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>
