<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <span class="spana">作业名称：{{formData.title}}</span>
        </a-col>
        <a-col :span="24">
          <span class="spana">作业分值：{{formData.score?formData.score:'未填写'}}</span>
        </a-col>
        <a-col :span="24">
          <span class="spana">描述：
            <span v-if="formData.content">
              {{formData.content}}
            </span>
            <span v-else>暂无内容</span>
          </span>
        </a-col>
        <a-col :span="12">
          <span class="spana">附件：
            <span v-if="formData.filePath">
              <a-button  :ghost="true" type="primary" preIcon="ant-design:download" size="small" @click="downloadFile(formData.filePath)"> 下载 </a-button>
            </span>
            <span v-else>暂无内容</span>
          </span>
        </a-col>
        <a-col :span="12">
          <span class="spana">上传时间：{{formData.startTime}} ~ {{formData.endTime}}</span>
        </a-col>
        <a-col :span="12">
          <span class="spana">是否网络查重：{{formData.wwcc=='0'?'否':'是'}}</span> <span class="spanb">网络通过率：{{formData.wwtgl?formData.wwtgl+"%":'未配置'}}</span>
        </a-col>
        <a-col :span="12">
          <span class="spana">是否课程内查重：{{formData.nwcc=='0'?'否':'是'}}</span> <span class="spanb">网络通过率：{{formData.nwtgl?formData.nwtgl+"%":'未配置'}}</span>
        </a-col>
        <a-col :span="12">
          <span class="spana">Aigc是否查重：{{formData.aigccc=='0'?'否':'是'}}</span> <span class="spanb">网络通过率：{{formData.aigctgl?formData.aigctgl+"%":'未配置'}}</span>
        </a-col>
        <a-col :span="12">
          <span class="spana">是否校内查重：{{formData.xncc=='0'?'否':'是'}}</span> <span class="spanb">网络通过率：{{formData.xntgl?formData.xntgl+"%":'未配置'}}</span>
        </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 dayjs, { Dayjs } from 'dayjs';
  import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
  
  import { downloadFile } from '/@/utils/common/renderUtils';
  //当前路由信息
  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: '',
    xncc: '0',
    xntgl: undefined,
  });
  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: '请输入数字!'},],
    xncc: [{ required: true, message: '请输入aigc查重!'},],
    xntgl: [{ required: false}, { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });
  const disabledDate = (current: Dayjs) => {
      return current && current < dayjs().subtract(1,'days').endOf('day');
    };
  // 表单禁用
  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;
      }
    }
    if(model.xncc=='1'){
      if(!model.xntgl){
        createMessage.warning('校内通过率不能为空');
        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: 300px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
  .spanSm{
    top: -22px;
    position: inherit;
    padding: 0 0 0 164px;
    color: #a9a9a9;
  }
  .spana{
    margin-left: 30px;
    line-height: 30px;
  }
  .spanb{
    margin-left: 30px;
    line-height: 30px;
  }
</style>
