<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="发动机号码" prop="engineNumber">
          <el-input v-model="formData.engineNumber" placeholder="请输入发动机号码" />
        </el-form-item>
        <el-form-item label="车架号" prop="vin">
          <el-input v-model="formData.vin" placeholder="请输入车架号" />
        </el-form-item>
        <el-form-item label="车牌号" prop="licenseNumber">
          <el-input v-model="formData.licenseNumber" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车辆型号" prop="carModel">
          <el-input v-model="formData.carModel" placeholder="请输入车辆型号" />
        </el-form-item>
        <el-form-item label="保养日期" prop="maintenanceDate">
          <el-date-picker clearable v-model="formData.maintenanceDate" type="date" value-format="timestamp"
            placeholder="选择保养日期" />
        </el-form-item>
        <el-form-item label="保养里程" prop="maintenanceMileage">
          <el-input v-model="formData.maintenanceMileage" placeholder="请输入保养里程" />
        </el-form-item>
        <el-form-item label="年检日期" prop="inspectionDate">
          <el-date-picker clearable v-model="formData.inspectionDate" type="date" value-format="timestamp"
            placeholder="选择年检日期" />
        </el-form-item>
        <el-form-item label="保险日期" prop="insuranceDate">
          <el-date-picker clearable v-model="formData.insuranceDate" type="date" value-format="timestamp"
            placeholder="选择保险日期" />
        </el-form-item>
        <el-form-item label="二级维护时间" prop="checkDate">
          <el-date-picker clearable v-model="formData.checkDate" type="date" value-format="timestamp"
            placeholder="选择二级维护时间" />
        </el-form-item>
        <el-form-item label="车辆品牌" prop="carBrand">
          <el-input v-model="formData.carBrand" placeholder="请输入车辆品牌" />
        </el-form-item>
        <el-form-item label="车辆性质" prop="carNature">
          <el-input v-model="formData.carNature" placeholder="请输入车辆性质" />
        </el-form-item>
        <el-form-item label="车辆注册日期" prop="carRegisterDate">
          <el-date-picker clearable v-model="formData.carRegisterDate" type="date" value-format="timestamp"
            placeholder="选择车辆注册日期" />
        </el-form-item>
        <el-form-item label="最近办理业务" prop="recentlyHandledBusiness">
          <el-input v-model="formData.recentlyHandledBusiness" placeholder="请输入车辆性质" />
        </el-form-item>
        <el-form-item label="最近办理业务的时间" prop="recentlyHandleBusinessTime">
          <el-date-picker clearable v-model="formData.recentlyHandleBusinessTime" type="date" value-format="timestamp"
            placeholder="选择最近办理业务的时间" />
        </el-form-item>
        <el-form-item label="行驶证图片" prop="carLicenseImg">
          <el-input v-model="formData.carLicenseImg" placeholder="请输入行驶证图片" />
        </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 CarMainApi from '@/api/base/carmain';
export default {
  name: "CarMainForm",
  components: {
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        engineNumber: undefined,
        vin: undefined,
        licenseNumber: undefined,
        carModel: undefined,
        maintenanceDate: undefined,
        maintenanceMileage: undefined,
        inspectionDate: undefined,
        insuranceDate: undefined,
        checkDate: undefined,
        carBrand: undefined,
        carNature: undefined,
        carRegisterDate: undefined,
        carLicenseImg: undefined,
      },
      // 表单校验
      formRules: {
        engineNumber: [{ required: true, message: '发动机号码不能为空', trigger: 'blur' }],
        vin: [{ required: true, message: '车架号不能为空', trigger: 'blur' }],
        licenseNumber: [{ required: true, message: '车牌号不能为空', trigger: 'blur' }],
        inspectionDate: [{ required: true, message: '年检日期不能为空', trigger: 'blur' }],
        carBrand: [{ required: true, message: '车辆品牌不能为空', trigger: 'blur' }],
        carNature: [{ required: true, message: '车辆性质不能为空', trigger: 'blur' }],
        carRegisterDate: [{ required: true, message: '车辆注册日期不能为空', trigger: 'blur' }],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await CarMainApi.getCarMain(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 CarMainApi.updateCarMain(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await CarMainApi.createCarMain(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        engineNumber: undefined,
        vin: undefined,
        licenseNumber: undefined,
        carModel: undefined,
        maintenanceDate: undefined,
        maintenanceMileage: undefined,
        inspectionDate: undefined,
        insuranceDate: undefined,
        checkDate: undefined,
        carBrand: undefined,
        carNature: undefined,
        carRegisterDate: undefined,
        carLicenseImg: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
