<template>
  <el-dialog
    class="addchannelDialog"
    :title="title"
    :visible.sync="isShow"
    :close-on-click-modal="false"
    width="470px"
    @close="handleclose"
  >
    <el-form
      label-position="left"
      ref="formInfo"
      label-width="100px"
      :rules="rules"
      :model="formdata"
    >
      <el-form-item label="通道名称：" prop="channelname">
        <el-input v-model="formdata.channelname" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="装置ID：" prop="id" :error="errorMsg">
        <el-input-number v-model="formdata.id"></el-input-number>
      </el-form-item>
      <!-- <el-form-item label="分辨率高：" prop="maxResolutionHeight">
        <el-input-number
          v-model="formdata.maxResolutionHeight"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="分辨率宽：" prop="maxResolutionWidth">
        <el-input-number
          v-model="formdata.maxResolutionWidth"
        ></el-input-number>
      </el-form-item> -->
      <el-form-item label="状态：" prop="status" v-if="title == '修改'">
        <el-switch
          v-model="formdata.status"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </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 { addChannelapi, updateChannelapi } from "@/utils/api/index";
export default {
  props: {
    title: String,
  },
  data() {
    return {
      isShow: false,
      errorMsg: "",
      formdata: {},
      rules: {
        channelname: [
          { required: true, message: "请输入名称", trigger: "blur" },
        ],
        id: [{ required: true, message: "请输入id", trigger: "blur" }],
        // maxResolutionHeight: [
        //   { required: true, message: "请输入分辨率高", trigger: "blur" },
        // ],
        // maxResolutionWidth: [
        //   { required: true, message: "请输入分辨率宽", trigger: "blur" },
        // ],
      },
    };
  },
  methods: {
    //判断
    getdataform(val) {
      console.log(val);
      if (val == null) {
        return (this.formdata = {});
      }
      this.formdata = val;
    },
    // 保存确定操作
    submitForm() {
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          if (this.title == "新增") {
            let formArr = [];
            formArr.push(this.formdata);
            addChannelapi({ list: formArr })
              .then((res) => {
                //this.errorMsg = "";
                if (res.code == 200) {
                  this.$message({
                    showClose: true,
                    message: "添加成功",
                    type: "success",
                  });
                  this.$parent.deviceList();
                  this.isShow = false;
                } else {
                  //this.errorMsg = res.msg;
                  this.$message.error(res.msg);
                }
              })
              .catch((err) => {
                this.$message({
                  showClose: true,
                  message: "添加失败",
                  type: "error",
                });
              });
          } else {
            updateChannelapi(this.formdata)
              .then((res) => {
                if (res.code == 200) {
                  this.$message.success("修改成功");
                  this.$parent.deviceList();
                  this.isShow = false;
                } else {
                  this.$message.error(res.msg);
                }
              })
              .catch((err) => {
                this.$message.error("修改失败");
              });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    display() {
      this.isShow = true;
    },
    hide() {
      this.isShow = false;
    },
    handleclose() {
      this.$parent.deviceList();
    },
  },
  mounted() {},
};
</script>
<style lang="less">
.addchannelDialog {
  .el-form-item {
    .el-input,
    .el-input-number {
      width: 100%;
    }
  }
}
</style>
