<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-form-item label="照片">
          <ImageUpload v-model="formData.image"/>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="formData.age" placeholder="请输入年龄"/>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="formData.sex">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_USER_SEX)"
                      :key="dict.value" :label="dict.value">{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="formData.phone" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="formData.idNumber" placeholder="请输入身份证号"/>
        </el-form-item>
        <el-form-item label="车牌" prop="carId">
          <el-input v-model="formData.carId" placeholder="请输入车牌"/>
        </el-form-item>
        <el-form-item label="户籍地址" prop="regAddress">
          <el-input v-model="formData.regAddress" placeholder="请输入户籍地址"/>
        </el-form-item>
        <el-form-item label="家庭住址" prop="address">
          <el-input v-model="formData.address" placeholder="请输入家庭住址"/>
        </el-form-item>
        <el-form-item label="教龄" prop="seniority">
          <el-input v-model="formData.seniority" placeholder="请输入教龄"/>
        </el-form-item>
        <el-form-item label="个人简介" prop="instructorDesc">
          <el-input v-model="formData.instructorDesc" placeholder="请输入个人简介"/>
        </el-form-item>
        <el-form-item label="工作人员类型" prop="type">
          <el-select v-model="formData.type" placeholder="请选择工作人员类型">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DRIVE_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="身份证" prop="idPhoto">
          <ImageUpload v-model="formData.idPhoto" />
        </el-form-item>
        <el-form-item label="生活照" prop="lifePhoto">
          <ImageUpload v-model="formData.lifePhoto" />
        </el-form-item>
        <el-form-item label="驾驶证" prop="drivePhoto">
          <ImageUpload v-model="formData.drivePhoto" />
        </el-form-item>
        <el-form-item label="其他证件" prop="otherPhoto">
          <ImageUpload v-model="formData.otherPhoto" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as DlDriveSchoolCoachApi from '@/views/drivingSchool/schoolCoach/api';
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "DlDriveSchoolCoachForm",
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        image: undefined,
        name: undefined,
        age: undefined,
        sex: undefined,
        phone: undefined,
        idNumber: undefined,
        carId: undefined,
        regAddress: undefined,
        address: undefined,
        seniority: undefined,
        instructorDesc: undefined,
        type: undefined,
        userId: undefined,
        idPhoto: undefined,
        lifePhoto: undefined,
        drivePhoto: undefined,
        otherPhoto: undefined,
      },
      // 表单校验
      formRules: {
        name: [
          { required: true, message: '请输入教练/员工名称', trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '请输入教练/员工联系方式，联系方式将作为登录账号', trigger: 'blur' },
        ],
        type: [
          { required: true, message: '请选择工作人员类型', trigger: 'blur' },
        ],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await DlDriveSchoolCoachApi.getDlDriveSchoolCoach(id);
          this.formData = res.data;
          this.title = "修改驾校教练";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增驾校教练";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await DlDriveSchoolCoachApi.saveCoach(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await DlDriveSchoolCoachApi.saveCoach(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        image: undefined,
        name: undefined,
        age: undefined,
        sex: undefined,
        phone: undefined,
        idNumber: undefined,
        carId: undefined,
        regAddress: undefined,
        address: undefined,
        seniority: undefined,
        instructorDesc: undefined,
        type: undefined,
        userId: undefined,
        idPhoto: undefined,
        lifePhoto: undefined,
        drivePhoto: undefined,
        otherPhoto: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
