<template>
  <div class="jcsbAddBox">
    <el-dialog
      class="jcsbAddDialogBox"
      :title="title"
      :visible.sync="jcsbDialogshow"
      width="520px"
      :close-on-click-modal="false"
    >
      <el-form
        label-position="left"
        ref="formInfo"
        label-width="104px"
        :rules="rules"
        :model="formInfo"
      >
        <el-form-item label="主设备名称：" prop="zsbName">
          <el-input v-model="formInfo.zsbName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="名称：" prop="name">
          <el-input v-model="formInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="相位：" prop="phase">
          <el-input v-model="formInfo.phase"></el-input>
        </el-form-item>
        <el-form-item label="devId：" prop="devId">
          <el-input v-model="formInfo.devId"></el-input>
        </el-form-item>
        <el-form-item label="选择类型：">
          <el-select v-model="formInfo.typeId" @change="changetypeId">
            <el-option
              v-for="item in tableOptions"
              :key="item.id"
              :label="item.mc"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="传感器代码：" prop="sensorCode">
          <el-input v-model="formInfo.sensorCode"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="hide">取 消</el-button>
        <el-button type="primary" @click="submitForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  modevAddApi,
  modevUpdateApi,
  modevtypeListAllApi,
} from "@/utils/api/index";
export default {
  props: ["title"],
  data() {
    return {
      jcsbDialogshow: false,
      formInfo: {
        zsbName: "",
        name: "",
        phase: "",
        typeId: "",
        devId: "",
        sensorCode: "",
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        devId: [{ required: true, message: "请输入devId", trigger: "blur" }],
      },
      tableOptions: [],
      zsbInfo: "",
    };
  },
  created() {},
  mounted() {
    this.$nextTick(() => {
      this.zsbInfo = JSON.parse(localStorage.getItem("zsbInfo"));
      console.log(this.zsbInfo);
      this.formInfo.zsbName = this.zsbInfo.mc;
    });
  },
  watch: {},
  methods: {
    //判断
    getdataform(val) {
      console.log(val);
      this.formInfo = JSON.parse(JSON.stringify(val));
    },
    submitForm() {
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          if (this.title == "添加监测设备") {
            console.log(this.formInfo);
            this.$set(this.formInfo, "zsbId", this.formInfo.zsbId);
            modevAddApi(this.formInfo)
              .then((res) => {
                if (res.success) {
                  this.$message({
                    duration: 1500,
                    showClose: true,
                    message: "添加成功",
                    type: "success",
                  });
                  this.hide();
                  this.$parent.getmonitoringAllList(); //刷新
                }
              })
              .catch((err) => {});
          } else {
            this.$set(this.formInfo, "zsbId", this.formInfo.zsbId);
            console.log(this.formInfo);
            modevUpdateApi(this.formInfo)
              .then((res) => {
                if (res.success) {
                  this.$message({
                    duration: 1500,
                    showClose: true,
                    message: "修改成功",
                    type: "success",
                  });
                  this.hide();
                  this.$parent.getmonitoringAllList(); //刷新
                }
              })
              .catch((err) => {});
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    changetypeId(val) {
      console.log(val);
      this.formInfo.typeId = val;
    },
    //获取所有的设备类型管理
    getTableListAll() {
      modevtypeListAllApi()
        .then((res) => {
          console.log(res);
          this.tableOptions = res.data;
          console.log(this.title);
          if (this.title == "添加监测设备") {
            console.log(this.formInfo);
            this.formInfo.typeId = this.tableOptions[0].id;
          }
        })
        .catch((err) => {
          console.log(err); //代码错误、请求失败捕获
        });
    },

    display() {
      this.jcsbDialogshow = true;
      this.getTableListAll();
    },
    hide() {
      this.jcsbDialogshow = false;
      this.$refs.formInfo.resetFields(); // 重置表单字段值
    },
  },
};
</script>
<style lang="less">
.jcsbAddBox {
  .jcsbAddDialogBox {
    .el-select {
      width: 376px;
    }
  }
}
</style>
