<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-row :gutter="2">
          <el-col :span="10">
            <el-form-item label="品牌名称" prop="brandName">
              <el-input v-model="formData.brandName" placeholder="请输入品牌名称" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="英文名称" prop="englishName">
              <el-input v-model="formData.englishName" placeholder="请输入品牌英文名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="2">
          <el-col :span="10">
            <el-form-item label="品牌缩写" prop="abbName">
              <el-input v-model="formData.abbName" placeholder="请输入品牌缩写" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="品牌类型 " prop="brandType">
              <el-select v-model="formData.brandType" placeholder="请选择品牌类型 ">
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DICT_BRAND_TYPE)" :key="dict.value" :label="dict.label"
                           :value="dict.value" />
              </el-select>
            </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="20">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="车标图片">
          <ImageUpload v-model="formData.logoImg" :limit="1"/>
        </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 CarBrandApi from '@/api/base/carbrand';
import ImageUpload from '@/components/ImageUpload';
export default {
  name: "CarBrandForm",
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        brandName: undefined,
        status: undefined,
        remark: undefined,
        logoImg: undefined,
        englishName: undefined,
        ruleMileage: undefined,
        ruleTime: undefined,
        abbName: undefined,
        brandType: undefined,
      },
      // 表单校验
      formRules: {
        brandName: [{ required: true, message: '品牌名称不能为空', trigger: 'blur' }],
        // status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
        brandType: [{ required: true, message: '品牌类型不能为空', trigger: 'change' }],
        ruleMileage: [{ required: true, message: '保养里程不能为空', trigger: 'change' }],
        ruleTime: [{ required: true, message: '保养时间不能为空', trigger: 'change' }],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await CarBrandApi.getCarBrand(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 CarBrandApi.updateCarBrand(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await CarBrandApi.createCarBrand(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        brandName: undefined,
        status: undefined,
        remark: undefined,
        logoImg: undefined,
        englishName: undefined,
        abbName: undefined,
        brandType: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
