<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="officeName">
          <el-input v-model="dialogForm.officeName" placeholder="请输入报名点名称" />
        </el-form-item>
        <el-form-item label="推送驾校" prop="schoolList">
          <el-select v-model="dialogForm.schoolList" filterable placeholder="请选择" value-key="schoolId" clearable size="small" multiple>
            <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="leader">
          <el-input v-model="dialogForm.leader" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="dialogForm.phone" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="dialogForm.remark" placeholder="请输入备注" />
        </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 { addOffice, updateOffice } from '@/api/zs/office';
import schoolAPi from '@/api/sch/school'

export default {
  data() {
    return {
      visible: false,
      canSubmit: true,
      dialogForm: {
        officeId: undefined,
        deptId: undefined,
        officeName: undefined,
        leader: undefined,
        phone: undefined,
        schoolList: [],
        remark: undefined
      },
      dataRule: {
        officeName: [{ required: true, message: '报名点名称不能为空', trigger: 'blur' }],
        schoolList: [{ required: true, message: '推送驾校不能为空', trigger: 'blur' }]
      },
      schoolOptions: []
    };
  },
  methods: {
    init(info = undefined) {
      // debugger
      this.visible = true;
      this.getSchools()
      this.$nextTick(() => {
        this.resetDialogForm();
        this.$refs['dialogForm'].resetFields();
        if (info) {
          this.dialogForm = this.deepClone(info);
        }
      });
    },
    resetDialogForm() {
      this.dialogForm = {
        officeId: undefined,
        deptId: undefined,
        officeName: undefined,
        leader: undefined,
        phone: undefined,
        schoolList: [],
        remark: undefined
      };
    },
    closeDialog() {
      this.$emit('update:dialogVisible', false);
    },
    // 表单提交
    dialogFormSubmit() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          this.canSubmit = false;

          if (this.dialogForm.officeId) {
            // 校验完成，调接口
            updateOffice(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 {
            addOffice(this.dialogForm)
              .then((resp) => {
                this.canSubmit = true;
                if (resp.code == 200) {
                  this.$message.success('保存成功');
                  this.$emit('refreshDataList');
                  this.visible = false;
                }
              })
              .catch(() => {
                this.canSubmit = true;
              });
          }
        }
      });
    },
    getSchools() {
      schoolAPi.pageList().then((resp) => {
        this.schoolOptions = resp.rows
      })
    },
  }
};
  </script>
  