<template>
  <el-dialog
    ref="dialog"
    :title="(isEdit ? '编辑' : '新增') + '场次'"
    width="450px"
    :visible.sync="visible"
    @close="closeDialog"
  >
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-position="right"
      label-width="120px"
    >
      <el-row>
        <el-form-item label="考试时间" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="交卷时间" prop="finishTime">
          <el-date-picker
            v-model="form.finishTime"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="考试时长" prop="prepareSeconds">
          <MinuteInput v-model="form.maxDurationSeconds" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="候考时间" prop="prepareSeconds">
          <MinuteInput v-model="form.prepareSeconds" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="迟到时长" prop="prepareSeconds">
          <MinuteInput v-model="form.openingSeconds" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="状态" prop="enable">
          <el-radio-group class="pull_right_sm" v-model="form.enable">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row class="d-flex justify-content-center">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import MinuteInput from "@/components/MinuteInput";
import { saveActivity } from "@/api/examwork-activity";
export default {
  name: "ActivityManagementDialog",
  components: { MinuteInput },
  props: {
    examId: String,
    activity: Object,
  },
  computed: {
    isEdit() {
      return this.activity.id;
    },
  },
  data() {
    return {
      visible: false,
      form: {
        id: "",
        startTime: null,
        finishTime: null,
        prepareSeconds: 0,
        openingSeconds: 0,
        maxDurationSeconds: 0,
        enable: 0,
      },
      rules: {},
    };
  },
  watch: {
    activity(val) {
      let tmp = { ...val };
      if (!tmp.id) {
        tmp = {
          id: "",
          startTime: null,
          finishTime: null,
          prepareSeconds: 0,
          openingSeconds: 0,
          maxDurationSeconds: 0,
          enable: 0,
        };
      }
      this.form = tmp;
    },
  },
  methods: {
    openDialog() {
      this.visible = true;
    },
    closeDialog() {
      this.visible = false;
    },
    async submitForm() {
      let data = this.form;
      data = { ...data, examId: this.examId };
      if (this.isEdit) {
        data = { ...data, id: this.activity.id };
      }
      await saveActivity(data);
      this.$emit("reload");
      this.closeDialog();
    },
  },
};
</script>

<style></style>
