<template>
  <div>
    <LinkTitlesCustom
      :currentPaths="['考试管理', '考试信息', '离线考试设置']"
    />
    <section class="content">
      <div class="box box-info">
        <!-- 正文信息 -->
        <div class="box-body">
          <el-form
            :inline="true"
            :rules="rules"
            ref="form"
            :model="form"
            label-position="right"
          >
            <div style="margin-bottom: 10px">
              <el-button type="primary" size="small" @click="saveExam"
                >保 存</el-button
              >
              <el-button type="primary" size="small" @click="back"
                >返 回</el-button
              >
            </div>
            <el-tabs type="border-card" v-model="activeName">
              <!-- 基础信息 -->
              <el-tab-pane label="基础信息" name="tab1">
                <el-row>
                  <el-form-item
                    label="考试名称"
                    label-width="80px"
                    placeholder="请输入考试名称"
                    prop="name"
                  >
                    <el-input v-model="form.name"></el-input>
                  </el-form-item>
                </el-row>
                <el-row>
                  <el-form-item label="考试类型" label-width="80px">
                    <el-select
                      style="width:205px"
                      :disabled="true"
                      v-model="form.examType"
                      placeholder="请选择"
                    >
                      <el-option
                        v-for="item in examTypeList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-row>
                <el-row>
                  <el-form-item label="是否开启" label-width="80px">
                    <el-radio-group
                      v-model="form.enable"
                      style="margin-left: 15px"
                    >
                      <el-radio label="true">开启</el-radio>
                      <el-radio label="false">关闭</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-row>
                <el-row>
                  <el-form-item
                    label="考试时间"
                    prop="examDatetimeRange"
                    label-width="70px"
                  >
                    <el-date-picker
                      style="margin-left: 10px"
                      v-model="examDatetimeRange"
                      type="datetimerange"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      :clearable="false"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-row>
                <el-row>
                  <el-form-item label="上传附件">
                    <el-radio-group
                      v-model="form.properties.CAN_UPLOAD_ATTACHMENT"
                    >
                      <el-radio label="true">允许</el-radio>
                      <el-radio label="false">不允许</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-row>
              </el-tab-pane>
            </el-tabs>
          </el-form>
        </div>
      </div>
    </section>
  </div>
</template>

<script>
import { EXAM_TYPE, EXAM_WORK_API } from "@/constants/constants.js";
import moment from "moment";
import LinkTitlesCustom from "@/components/LinkTitlesCustom.vue";

let _this = null;

let validateName = (rule, value, callback) => {
  let name = _this.form.name;
  if (name == "") {
    callback(new Error("请输入考试名称"));
    _this.activeName = "tab1";
  } else {
    callback();
  }
};

export default {
  components: { LinkTitlesCustom },
  data() {
    return {
      activeName: "tab1",
      examDatetimeRange: [],
      show_ckeditor: false,
      form: {
        started: false,
        name: "",
        examType: "OFFLINE",
        examTimes: 1,
        beginTime: null,
        endTime: null,
        duration: 120,
        enable: "true",
        properties: { CAN_UPLOAD_ATTACHMENT: "true" }
      },
      examTypeList: EXAM_TYPE,
      examId: "",
      rules: {
        name: [{ required: true, validator: validateName, trigger: "blur" }]
      }
    };
  },

  methods: {
    init() {
      if (this.examId != "add") {
        let url = EXAM_WORK_API + "/exam/" + this.examId;
        this.$http.get(url).then(response => {
          let body = response.data;
          body.properties = this.form.properties;
          this.form = Object.assign(this.form, response.data);
          this.form.enable = this.form.enable ? "true" : "false";
          this.examDatetimeRange = [this.form.beginTime, this.form.endTime];
          console.log("getOnlineExam(); form: ", this.form);

          let url = EXAM_WORK_API + "/exam/allProperties/" + this.examId;
          this.$http.get(url).then(response => {
            this.form.properties = Object.assign(
              this.form.properties,
              response.data
            );
            this.show_ckeditor = true;
          });
        });
      } else {
        let now = moment().format("YYYY-MM-DD HH:mm:ss");
        this.examDatetimeRange = [now, now];
        this.show_ckeditor = true;
      }
    },
    saveExam: function() {
      this.form.beginTime = this.examDatetimeRange[0];
      this.form.endTime = this.examDatetimeRange[1];
      console.log(this.form);
      let url = EXAM_WORK_API + "/exam";
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.examId != "add") {
            this.$http.put(url, this.form).then(response => {
              if (200 != response.status) {
                this.$notify({
                  type: "error",
                  message: response.body.desc
                });
                return;
              }
              this.$notify({
                type: "success",
                message: "保存成功"
              });
            });
          } else {
            this.$http.post(url, this.form).then(response => {
              console.log(response);
              this.$notify({
                type: "success",
                message: "新增成功"
              });
              this.back();
            });
          }
        } else {
          return false;
        }
      });
    },
    back() {
      this.$router.push({ path: "/examwork/examInfo" });
    }
  },
  created() {
    _this = this;
    this.examId = this.$route.params.id;
    this.init();
  }
};
</script>
<style scoped></style>
