<template>
  <el-dialog title="驾校信息" :close-on-click-modal="false" append-to-body :visible.sync="visible" width="600px" @close="closeDialog">
    <div>
      <el-form ref="dialogForm" :model="dialogForm" :rules="dataRule" label-position="top" @keyup.enter.native="dialogFormSubmit()">
        <el-form-item label="制度名" prop="ruleName">
          <el-input v-model="dialogForm.ruleName" placeholder="请输入制度名" />
        </el-form-item>
        <el-form-item label="实施时间" prop="implementTime">
          <el-date-picker clearable v-model="dialogForm.implementTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择实施时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到期时间" prop="dueTime">
          <el-date-picker clearable v-model="dialogForm.dueTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="制度内容" prop="ruleContent">
          <editor v-model="dialogForm.ruleContent" :min-height="192" />
        </el-form-item>
      </el-form>

    </div>
    <span slot="footer" class="dialog-footer">
      <el-button plain @click="(visible=false)">取消</el-button>
      <el-button v-jclick type="primary" :disabled="!canSubmit" @click="dialogFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
  
  <script>
import { addRules, updateRules } from "@/api/sch/rules";

export default {
  data() {
    return {
      visible: false,
      canSubmit: true,
      dialogForm: {
        ruleId: undefined,
        ruleName: undefined,
        implementTime: undefined,
        dueTime: undefined,
        ruleContent: undefined
      },
      dataRule: {
        ruleName: [{ required: true, message: '制度名不能为空', trigger: 'blur' }],
        implementTime: [{ required: true, message: '实施时间不能为空', trigger: 'blur' }],
        ruleContent: [{ required: true, message: '制度内容不能为空', trigger: 'blur' }]
      }
    };
  },
  methods: {
    init(info = undefined) {
      // debugger
      this.visible = true;
      this.$nextTick(() => {
        this.resetDialogForm();
        this.$refs['dialogForm'].resetFields();
        if (info) {
          this.dialogForm = this.deepClone(info);
        }
      });
    },
    resetDialogForm() {
      this.dialogForm = {
        ruleId: undefined,
        ruleName: undefined,
        implementTime: undefined,
        dueTime: undefined,
        ruleContent: undefined
      };
    },
    closeDialog() {
      this.$emit('update:dialogVisible', false);
    },
    // 表单提交
    dialogFormSubmit() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          this.canSubmit = false;

          if (this.dialogForm.ruleId) {
            // 校验完成，调接口
            updateRules(this.dialogForm)
              .then((resp) => {
                this.canSubmit = true;
                if (resp.code == 200) {
                  this.$message.success('保存成功');
                  this.$emit('refreshDataList');
                  this.visible = false;
                }
              })
              .catch(() => {
                this.canSubmit = true;
              });
          } else {
            addRules(this.dialogForm)
              .then((resp) => {
                this.canSubmit = true;
                if (resp.code == 200) {
                  this.$message.success('保存成功');
                  this.$emit('refreshDataList');
                  this.visible = false;
                }
              })
              .catch(() => {
                this.canSubmit = true;
              });
          }
        }
      });
    }
  }
};
  </script>
  