<template>
  <el-dialog
    class="addDyDialog"
    :title="title"
    :visible.sync="isShow"
    :close-on-click-modal="false"
    width="470px"
  >
    <el-form
      label-position="left"
      ref="formInfo"
      label-width="100px"
      :rules="rules"
      :model="formdata"
    >
      <el-form-item label="电压名称：" prop="name">
        <el-input v-model="formdata.name" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="submitForm()">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { addDyJoggle, updateDyJoggle } from "@/utils/api/index";

export default {
  props: {
    title: String,
  },
  data() {
    return {
      isShow: false,

      formdata: {},
      rules: {
        name: [{ required: true, message: "请输入电压名称", trigger: "blur" }],
      },
    };
  },
  mounted() {},
  methods: {
    //判断
    getdataform(val) {
      if (val == null) {
        return (this.formdata = {});
      }
      //this.formdata = val;
      this.formdata = JSON.parse(JSON.stringify(val));
    },
    // 保存确定操作
    submitForm() {
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          if (this.title == "新增") {
            this.$set(this.formdata, "dyValue", "220");
            addDyJoggle(this.formdata)
              .then((res) => {
                this.isShow = false;
                this.$message({
                  duration: 1500,
                  showClose: true,
                  message: "添加成功",
                  type: "success",
                });
                this.$parent.deviceList();
              })
              .catch((err) => {
                this.$message({
                  duration: 1500,
                  showClose: true,
                  message: "添加失败",
                  type: "error",
                });
              });
          } else {
            console.log(this.formdata);
            updateDyJoggle(this.formdata)
              .then((res) => {
                this.isShow = false;
                //this.$message.success("修改成功");
                this.$message({
                  duration: 1500,
                  showClose: true,
                  message: "修改成功",
                  type: "success",
                });
                this.$parent.deviceList();
              })
              .catch((err) => {
                //this.$message.error("修改失败");
                this.$message({
                  duration: 1500,
                  showClose: true,
                  message: "修改失败",
                  type: "error",
                });
              });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    display() {
      this.isShow = true;
    },
    hide() {
      this.isShow = false;
    },
  },
};
</script>
<style lang="less">
.addDyDialog {
  .el-select {
    width: 100%;
  }
  .el-input-number {
    width: 100%;
  }
  .el-input-number .el-input__inner {
    text-align: left;
  }
}
</style>
