<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="80">
        <el-form-item label="名称" prop="name">
          <el-input type="text" v-model="formData.name" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input type="number" v-model="formData.price" 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 {updateOther} from "@/api/repair/other";

export default {
  name: "OtherForm",
  data() {
    return {
      formData: {
        name: null,
        price: null
      },
      dialogTitle: null,
      dialogVisible: false,
      formRules: {
        name: [{required: true, message: '名称不能为空', trigger: 'blur'}],
        price: [{required: true, message: '价格不能为空', trigger: 'blur'}]
      },
      formLoading: false
    }
  },
  methods: {
    async submitForm() {
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        await updateOther(this.formData)
        this.$modal.msgSuccess(this.formData.id ? "修改成功" : "新增成功")
        this.$emit("success")
      } finally {
        this.dialogVisible = false
        this.formLoading = false
      }
    },
    async open(row) {
      this.reset()
      this.dialogTitle = row?.id ? "修改" : "新增"
      if (row){
        this.formData = row
      }
      this.dialogVisible = true
    },
    reset() {
      this.formData = {
        name: null,
        price: null
      }
      this.resetForm("formRef");
    }
  }
}
</script>

<style scoped lang="scss">

</style>
