<template>
  <el-dialog title="驾校信息" :close-on-click-modal="false" append-to-body :visible.sync="visible" width="600px" @close="closeDialog">
    <div>
      <el-form :model="dialogForm" :rules="dataRule" label-position="top" ref="dialogForm" @keyup.enter.native="dialogFormSubmit()">
        <el-form-item label="驾校名称" prop="schoolName">
          <el-input v-model="dialogForm.schoolName" maxlength="100" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="dialogForm.leader" maxlength="100" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="dialogForm.phone" maxlength="11" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="dialogForm.status">
            <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="dialogForm.remark" maxlength="300" placeholder="请输入" clearable></el-input>
        </el-form-item>

      </el-form>

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

export default {
  dicts: ['sys_normal_disable'],
  props: {
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  data() {
    return {
      visible: false,
      canSubmit: true,
      dialogForm: {
        schoolId: undefined,
        schoolName: undefined,
        leader: undefined,
        phone: undefined,
        remark: undefined,
        status: '0'
      },
      dataRule: {
        schoolName: [{ required: true, message: '驾校名称不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
      }
    };
  },
  created() { },
  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 = {
        schoolId: undefined,
        schoolName: undefined,
        leader: undefined,
        phone: undefined,
        remark: undefined
      }
    },
    closeDialog() {
      this.$emit('update:dialogVisible', false);
    },
    // 表单提交
    dialogFormSubmit() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          this.canSubmit = false;

          if (this.dialogForm.schoolId) {
            // 校验完成，调接口
            api
              .update(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 {
            api
              .add(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>