<template>
  <div>
    <!--1.加上rules属性即开启表单验证规则，rules动态绑定了一个对象，对象在script里面定义，对象里面包含的是要验证的表单字段-->
    <!--4.ref相当于给了el-form一个验证的时候使用的唯一名称-->
    <!--model让表单动态绑定一个数据对象-->

    <el-form
      :rules="rules"
      ref="uploadForm"
      :model="uploadForm"
      class="uploadContainer"
    >
      <h2 class="uploadTitle">题库上传</h2>

      <!--3.prop的值与rules里面要验证的字段值是对应的，在对应的标签上我们让prop等于rules里面对应的字段值-->
      <el-form-item prop="name">
        <el-input
        style="font-size:14px;"
          type="text"
          auto-complete="false"
          v-model="uploadForm.name"
          placeholder="请输入竞赛名称"
        ></el-input>
      </el-form-item>
      <el-form-item prop="value" style="padding-top:20px;margin-bottom:70px">
        <el-select
          v-model="uploadForm.value"
          filterable
          size="medium"
          placeholder="请选择竞赛类别"
        >
          <el-option
            v-for="item in uploadForm.options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!--5.@click绑定了一个点击事件-->
                  <el-button
              style="
                display: inline-block;
                width: 150px;
                height: 40px;
                margin-right: 60px;
              "
              type="primary"
              plain
              round
              size="medium"
              @click="submit"
              >上传题库</el-button
            >
            <el-button
              style="display: inline-block; width: 150px; height: 40px"
              round
              size="medium"
              @click="back"
              >返回</el-button
      >
    </el-form>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>
        <el-upload
          class="upload-demo"
          ref="upload"
          drag
          action="http://106.55.186.95:8085/upload2"
          multiple
          :auto-upload="false"
          :limit="5"
          :on-success="handleFilUploadSuccess"
          :on-remove="handleRemove"
          :headers="Myhead"
          :data="{ competitionName: uploadForm.name, type: uploadForm.value }"
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">文件大小不超过20MB</div>
        </el-upload>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpload">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "Upload",
  data() {
    return {
      dialogVisible: false,
      uploadForm: {
        name: "",
        options: [
          {
            value: "工科",
            label: "工科",
          },
          {
            value: "理科",
            label: "理科",
          },
          {
            value: "文体",
            label: "文体",
          },
          {
            value: "商科",
            label: "商科",
          },
          {
            value: "综合",
            label: "综合",
          },
        ],
        value: "",
      },
      //2.我们把要进行验证的字段都放进rules对象里
      rules: {
        name: [{ required: true, message: "请输入竞赛名称", trigger: "blur" }],
        value: [
          { required: true, message: "请选择竞赛类别", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    Myhead: function () {
      return { token: localStorage.token, userid: localStorage.userId };
    },
  },
  methods: {
    //6.点击事件
    submit() {
      //7.
      //这里refs.后面的loginForm与el-form表单中的ref属性值是对应的
      //validate：对整个表单进行校验的方法，参数为一个回调函数。这里给了一个返回值valid，为Boolean类型，表示校验是否成功

      this.$refs.uploadForm.validate((valid) => {
        if (valid) {
          this.handleUpdate();
          // this.$router.replace("/PageTable");
          //   this.$message({
          //     message: "上传成功！",
          //     type: "success",
          //   });
        }
        // else {
        //   this.$message.error("上传失败！");
        //   return false;
        // }
      });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
    // 文件上传成功时的函数
    handleFilUploadSuccess(res, file, fileList) {
      console.log(res, file, fileList);
      this.$message.success("上传成功");
      this.$router.replace("/PageTable");
    },
    handleUpdate() {
      this.dialogVisible = true;
    },
    // 处理文件上传的函数
    handleUpload() {
      // console.log(res,file)
      this.submitUpload();
      this.dialogVisible = false;
    },
    back(){
            this.$router.replace("/PageTable");
    }
  },
};
</script>
<style scoped>
.uploadContainer {
  border-radius: 15px;
  width: 600px;
  height: 300px;
  background-clip: padding-box;
  margin: 180px auto;
  padding: 15px 35px;
  background: white;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
.uploadTitle {
  margin: 2px auto 20px auto;
  text-align: center;
}
.rememberMe {
  width: 100%;
  margin: 9px auto;
}
</style>
