
<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="schoolId">
          <el-select v-model="dialogForm.schoolId" filterable placeholder="请选择" value-key="schoolId" clearable size="small" style="width:100%">
            <el-option v-for="(dict, index) in schoolOptions" :key="index" :label="dict.schoolName" :value="dict.schoolId" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属场地" prop="placeId">
          <el-select v-model="dialogForm.placeId" filterable placeholder="请选择" clearable value-key="placeId" size="small" style="width:100%">
            <el-option v-for="(dict, index) in placeOptions.filter(item => item.schoolId === dialogForm.schoolId)" :key="index" :label="dict.name" :value="dict.placeId" />
          </el-select>
        </el-form-item>
        <el-form-item label="教练名" prop="coachName">
          <el-input v-model="dialogForm.coachName" placeholder="请输入教练名" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="dialogForm.phone" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="微信openid" prop="openId">
          <el-input v-model="dialogForm.openId" placeholder="请输入微信openid" />
        </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 { addCoach, updateCoach } from '@/api/sch/coach';

export default {
  props: {
    schoolOptions: {
      type: Array,
      default: []
    },
    placeOptions: {
      type: Array,
      default: []
    }
  },
  data () {
    return {
      visible: false,
      canSubmit: true,
      dialogForm: {
        coachId: null,
        deptId: null,
        schoolId: null,
        placeId: null,
        coachName: null,
        phone: null,
        openId: null
      },
      dataRule: {
        schoolId: [{ required: true, message: '所属驾校不能为空', trigger: 'blur' }],
        coachName: [{ required: true, message: '教练名不能为空', trigger: 'blur' }],
        phone: [{ 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 = {
        coachId: null,
        deptId: null,
        schoolId: null,
        placeId: null,
        coachName: null,
        phone: null,
        openId: null
      };
    },
    closeDialog () {
      this.$emit('update:dialogVisible', false);
    },
    // 表单提交
    dialogFormSubmit () {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          this.canSubmit = false;

          if (this.dialogForm.coachId) {
            // 校验完成，调接口
            updateCoach(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 {
            addCoach(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>
