<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-row :gutter="2">
          <el-col :span="10">
            <el-form-item label="型号名称" prop="modelName">
              <el-input v-model="formData.modelName" placeholder="请输入型号名称" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="型号缩写" prop="abbName">
              <el-input v-model="formData.abbName" placeholder="请输入型号缩写" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="2">
          <el-col :span="10">
            <el-form-item label="保养里程" prop="ruleMileage">
              <el-input v-model="formData.ruleMileage" placeholder="请输入保养里程">
                <template slot="suffix">km</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="保养时间" prop="ruleTime">
              <el-input v-model="formData.ruleTime" placeholder="请输入保养时间">
                <template slot="suffix">月</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="2">
          <el-col :span="10">
            <el-form-item label="排序" prop="sort">
              <el-input-number v-model="formData.sort" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formData.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>







      </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 CarModelApi from '@/api/base/carmodel';
export default {
  name: "CarModelForm",
  components: {
  },
  props: {
    brandId: {
      type: String,
      required: true
    },
  },
  data() {

    return {

      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        sort: 0,
        modelName: undefined,
        ruleMileage: undefined,
        ruleTime: undefined,
        remark: undefined,
        abbName: undefined,
      },
      // 表单校验
      formRules: {
        sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
        modelName: [{ required: true, message: '型号名称不能为空', trigger: 'blur' }],
        ruleMileage: [{ required: true, message: '保养里程不能为空', trigger: 'blur' }],
        ruleTime: [{ required: true, message: '保养时间不能为空', trigger: 'blur' }],
      },
    };
  },
  created() {

  },
  mounted() {
    console.log(this.brandId, "brandIdpops");
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await CarModelApi.getCarModel(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;
        data.brandId = this.brandId;
        // 修改的提交
        if (data.id) {
          await CarModelApi.updateCarModel(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await CarModelApi.createCarModel(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        sort: 0,
        modelName: undefined,
        remark: undefined,
        abbName: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
