<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.title" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
            <a-input v-model:value="formData.title" placeholder="请输入作业名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="12">
          <a-form-item label="类型" v-bind="validateInfos.zyType">
            <j-dict-select-tag type='radio' v-model:value="formData.zyType" dictCode="zy_type" placeholder="请选择类型" :disabled="disabled"/>
          </a-form-item>
        </a-col> -->
        <a-col :span="24">
          <a-form-item label="描述" v-bind="validateInfos.content" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
            <a-textarea v-model:value="formData.content" placeholder="请输入描述" :disabled="disabled"></a-textarea>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="附件" v-bind="validateInfos.filePath" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
	          <j-upload v-model:value="formData.filePath"  :disabled="disabled" ></j-upload>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="开始时间" v-bind="validateInfos.startTime">
		        <a-date-picker placeholder="请选择开始时间" v-model:value="formData.startTime" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="结束时间" v-bind="validateInfos.endTime">
		        <a-date-picker placeholder="请选择结束时间" v-model:value="formData.endTime" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="是否网络查重" v-bind="validateInfos.wwcc">
            <j-dict-select-tag type='radio' v-model:value="formData.wwcc" dictCode="yn" placeholder="请选择是否网络查重" :disabled="disabled"/>
          </a-form-item>
          <span class="spanSm">说明：是否在外部网络资源内进行查重</span>
        </a-col>
        <a-col :span="12">
          <a-form-item label="网络通过率" v-bind="validateInfos.wwtgl">
	          <a-input-number v-model:value="formData.wwtgl" placeholder="请输入网络通过率" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
          
        </a-col>
        <a-col :span="12">
          <a-form-item label="是否课程内查重" v-bind="validateInfos.nwcc">
            <j-dict-select-tag type='radio' v-model:value="formData.nwcc" dictCode="yn" placeholder="请选择是否课程内查重" :disabled="disabled"/>
          </a-form-item>
          <span class="spanSm">说明：是否在本课程所有提交的作业资源内进行查重</span>
        </a-col>
        <a-col :span="12">
          <a-form-item label="课程内通过率" v-bind="validateInfos.nwtgl">
	          <a-input-number v-model:value="formData.nwtgl" placeholder="请输入课程内通过率" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="Aigc是否查重" v-bind="validateInfos.aigccc">
            <j-dict-select-tag type='radio' v-model:value="formData.aigccc" dictCode="yn" placeholder="请选择Aigc查重" :disabled="disabled"/>
          </a-form-item>
          <span class="spanSm">说明：是否对AI撰写相似度进行查重</span>
        </a-col>
        <a-col :span="12">
          <a-form-item label="Aigc通过率" v-bind="validateInfos.aigctgl">
	          <a-input-number v-model:value="formData.aigctgl" placeholder="请输入Aigc通过率" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted,unref } 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 '../ZyInfo.api';
  import { Form } from 'ant-design-vue';
  import { useRouter } from 'vue-router';
  import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
  
  //当前路由信息
  const { currentRoute } = useRouter();
  const { query } = unref(currentRoute);
  const { rwbh } = query;//获取传递参数
  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: '',
    title: '',
    zyType: '0',
    content: '',
    filePath: '',
    startTime: '',
    endTime: '',
    wwcc: '0',
    wwtgl: undefined,
    nwcc: '0',
    nwtgl: undefined,
    aigccc: '0',
    aigctgl: undefined,
    rwbh: '',
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 6 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const labelCol2 = ref<any>({ xs: { span: 24 }, sm: { span: 3 } });
  const wrapperCol2 = ref<any>({ xs: { span: 24 }, sm: { span: 20 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    title: [{ required: true, message: '请输入作业名称!'},],
    // zyType: [{ required: true, message: '请输入类型!'},],
    startTime: [{ required: true, message: '请输入开始时间!'},],
    endTime: [{ required: true, message: '请输入结束时间!'},],
    wwcc: [{ required: true, message: '请输入外网是否查重!'},],
    wwtgl: [{ required: false}, { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},],
    nwcc: [{ required: true, message: '请输入内网查重!'},],
    nwtgl: [{ required: false}, { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},],
    aigccc: [{ required: true, message: '请输入aigc查重!'},],
    aigctgl: [{ required: false}, { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

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

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

  /**
   * 编辑
   */
  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(',');
        }
      }
    }

    if(model.wwcc=='1'){
      if(!model.wwtgl){
        createMessage.warning('外网通过率不能为空');
        confirmLoading.value = false;
        return;
      }
    }
    if(model.nwcc=='1'){
      if(!model.nwtgl){
        createMessage.warning('内网通过率不能为空');
        confirmLoading.value = false;
        return;
      }
    }
    if(model.aigccc=='1'){
      if(!model.aigctgl){
        createMessage.warning('AIGC通过率不能为空');
        confirmLoading.value = false;
        return;
      }
    }

    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;
  }
  .spanSm{
    top: -22px;
    position: inherit;
    padding: 0 0 0 164px;
    color: #a9a9a9;
  }
</style>
