<template>
  <a-spin :spinning="confirmLoading">
    <div style="padding: 20px;">
      <a-steps :current="current" size="small">
        <a-step title="基本信息" :key="1" />
        <a-step title="导入数据" :key="2" />
        <a-step title="生成报告" :key="3" />
      </a-steps>
      <div v-if="current == 0" style="padding: 20px;">
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-row>
            <!-- <a-col :span="24" class="shuoming">
              <span>请注意：点击后不可返回修改信息，请仔细填写！</span>
            </a-col> -->
            <a-col :span="24">
              <a-form-item label="学校名称" v-bind="validateInfos.schoolName">
                <a-input v-model:value="formData.schoolName" placeholder="请输入学校名称" ></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="评估单位" v-bind="validateInfos.pgdw">
                <a-input v-model:value="formData.pgdw" placeholder="请输入评估单位" ></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="评估日期" v-bind="validateInfos.pgrq">
                <a-input v-model:value="formData.pgrq" placeholder="请输入评估日期" ></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="学年" v-bind="validateInfos.xuenian">
                <a-input v-model:value="formData.xuenian" placeholder="请输入学年" ></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="调查时点" v-bind="validateInfos.dysd">
                <a-input v-model:value="formData.dysd" placeholder="请输入调查时点" ></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="学生总人数" v-bind="validateInfos.teacherSumno">
                <a-input-number
                  v-model:value="formData.teacherSumno"
                  placeholder="请输入学生总人数"
                  style="width: 100%"
                ></a-input-number>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="最小有效样本数" v-bind="validateInfos.minNum">
                <a-input-number
                  v-model:value="formData.minNum"
                  placeholder="请输入最小有效样本数"
                  style="width: 100%"
                ></a-input-number>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div v-if="current == 1" style="padding: 20px;">

        <a-col :span="24" class="shuoming">
          <span>温馨提示：点击导入后选择数据模板进行导入！</span>
        </a-col>
        <span style="text-align: center;">
          <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls($event, { id: formData.id})">导入</j-upload-button>
        </span>
      </div>
      <div v-if="current == 2" style="padding: 20px;text-align: center;line-height: 100px;">
        <div>
          <a-spin tip="报告生成中，请等待，生成完成后自动关闭页面...">
          </a-spin>
        </div>

        <span>生成时间预计1分钟</span>
      </div>
      <div class="steps-action" style="text-align: center;">
        <a-button v-if="current == 0" type="primary" @click="submitForm">下一页</a-button>
        <!-- <a-button v-if="current > 0" style="margin-left: 8px" @click="prev">返回</a-button> -->
        <!-- <a-button v-if="current == 2" type="primary" style="margin-left: 8px" @click="handleWancheng"> 完成 </a-button> -->
      </div>
    </div>
  </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,getImportUrl } from '../BlStuInfo.api';
import { Form } from 'ant-design-vue';

import { useListPage } from '/@/hooks/system/useListPage';
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: '',
  schoolName: '',
  pgdw: '',
  pgrq: '',
  xuenian: '',
  dysd: '',
  teacherSumno: '',
  minNum: '',
});


  //注册table数据
  const {  onImportXls } = useListPage({
    importConfig: {
      url: getImportUrl,
      success: handleSuccess
    },
    tableProps: {
      title: '学生调研报告',
    },
  });


const current = ref<number>(0);
const next = () => {
  current.value++;
};
const prev = () => {
  current.value--;
};
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 = {
  schoolName: [{ required: true, message: '请输入学校名称!' }],
  pgdw: [{ required: true, message: '请输入评估单位!' }],
  pgrq: [{ required: true, message: '请输入评估日期!' }],
  xuenian: [{ required: true, message: '请输入学年!' }],
  dysd: [{ required: true, message: '请输入调查时点!' }],
  teacherSumno: [{ required: true, message: '请输入学生总人数!' }],
  minNum: [{ required: true, message: '请输入最小有效样本数!' }],
};
const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });
//上传完成回调
function handleSuccess(){
  current.value++;
  console.log("参数--------》",{id:formData.id})
  defHttp.get({url:'/blStuAnswer/blStuAnswer/createWord',params:{id:formData.id}}).then(res=>{
    console.log('---生成报告--->',res);
    if(res.code==200||res=='生成成功'){
      handleWancheng();
    }
  })
}
//完成回调
function handleWancheng() {
    emit('ok');
}
// 表单禁用
const disabled = computed(() => {
  if (props.formBpm === true) {
    if (props.formData.disabled === false) {
      return false;
    } else {
      return true;
    }
  }
  return props.formDisabled;
});

/**
 * 新增
 */
function add() {
  current.value = 0;
  edit({});
}

/**
 * 编辑
 */
function edit(record) {
  nextTick(() => {
    resetFields();
    const tmpData = {};
    Object.keys(formData).forEach((key) => {
      if (record.hasOwnProperty(key)) {
        tmpData[key] = record[key];
      }
    });
    //赋值
    Object.assign(formData, tmpData);
  });
}

/**
 * 提交数据
 */
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) => {
      console.log('------------>',res);
      if (res.success) {
        createMessage.success("数据保存成功");
        current.value++;
        formData.value = res.result;
    //赋值
    Object.assign(formData, res.result);
        console.log('-------formData----->',formData);
        console.log('-------value----->',formData.value);
        // emit('ok');
      } else {
        createMessage.warning("操作失败");
      }
    })
    .finally(() => {
      confirmLoading.value = false;
    });
}

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

<style lang="less" scoped>
.antd-modal-form {
  height: 490px !important;
  overflow-y: auto;
  padding: 14px;
}
.shuoming{
  text-align: center;
  padding: 20px;
  color: red;
  font-weight: bold;
  font-size: 16px;
}
</style>
