<template>
  <div>
    <d2-container>
      <!-- <template slot="header">首页/硬件参数</template> -->
      <div class="main">
        <div class="left">
          <!-- <el-input placeholder="输入关键字进行过滤" v-model="filterText" >
          </el-input> -->

          <el-tree
            class="filter-tree"
            :filter-node-method="filterNode"
            v-loading="loading"
            :data="leftList"
            node-key="id"
            :props="defaultProps"
            :expand-on-click-node="false"
            @node-click="handleNodeClick"
            default-expand-all
            highlight-current
            :default-expanded-keys="this.defaultExpanded"
            ref="tree"
          >
            <span class="custom-tree-node" slot-scope="{ node, data }">
              <span v-if="node.level == 1">
                <i :class="node.icon"></i>{{ node.label }}
              </span>
              <span v-if="node.level == 2">
                <i :class="node.icon"></i>{{ node.label }}
              </span>
              <span v-if="node.level == 3">
                <i :class="node.icon"></i>{{ node.label }}
              </span>
              <span v-if="node.level == 4"> <i></i>{{ node.label }} </span>
            </span>
          </el-tree>
        </div>
        <div class="right">
          <div class="nav">当前所在位置:首页>硬件参数</div>
          <div class="right_main">
            <div class="right_title">
              <i
                class="fa fa-cog"
                aria-hidden="true"
                style="padding-right: 5px"
              ></i
              >硬件参数
            </div>
            <div class="right_table">
              <el-form
                :inline="true"
                :model="formInline"
                class="demo-form-inline"
                id="formLabel"
              >
                <el-form-item label="设备名称:">
                  <el-input
                    v-model="formInline.deviceName"
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button
                    type="primary"
                    icon="el-icon-search"
                    @click="search"
                    >查询</el-button
                  >
                </el-form-item>

                <el-form-item>
                  <el-button
                    type="primary"
                    icon="el-icon-refresh-left"
                    @click="reset"
                    >重置</el-button
                  >
                </el-form-item>
              </el-form>
              <div class="addBtn">
                <el-button
                  type="primary"
                  icon="el-icon-plus"
                  @click="addTantou(1)"
                  >新增激光探头</el-button
                >
              </div>
              <el-table
                :data="hardwareList"
                border
                style="width: 100%"
                :header-cell-style="{ background: '#0064b9', color: '#c9f5fa' }"
              >
                <el-table-column
                  align="center"
                  label="序号"
                  type="index"
                  width="100px"
                  :index="indexMethod"
                  show-overflow-tooltip
                ></el-table-column>

                <el-table-column
                  prop="deviceName"
                  label="设备名称"
                  align="center"
                >
                </el-table-column>
                <el-table-column prop="deviceCode" label="设备编号" align="center">
                </el-table-column>
                       <el-table-column prop="port" label="端口" align="center">
                </el-table-column>
                <el-table-column prop="ipAdress" label="设备IP" align="center">
                </el-table-column>
                <el-table-column align="center" label="操作" width="180">
                  <template slot-scope="scope">
                    <el-button
                      type="text"
                      size="small"
                      icon="el-icon-edit"
                      @click="editTantou(scope.row,2)"
                      >编辑</el-button
                    >
                    <el-button type="text" size="small" icon="el-icon-delete"
                      @click="deleted(scope.row)">删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pagination.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pagination.total"
              >
              </el-pagination>
            </div>
          </div>
        </div>
      </div>
    </d2-container>

    <!-- //弹框 -->
    <el-dialog :title="title" :visible.sync="dialogTantou" id="dialogStyle">
      <el-form :model="dialog" ref="formData">
        <el-form-item label="设备名称:" :label-width="formLabelWidth">
          <el-input v-model="dialog.deviceName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="设备IP:" :label-width="formLabelWidth">
          <el-input v-model="dialog.ipAdress" autocomplete="off"></el-input>
        </el-form-item>
         <el-form-item label="端口:" :label-width="formLabelWidth">
          <el-input v-model="dialog.port" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="设备编码:" :label-width="formLabelWidth">
          <el-input v-model="dialog.deviceCode" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTantou = false">取 消</el-button>
        <el-button type="primary" @click="determine('formData')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import api from "@/api";
export default {
  name: "hardwareParame",
  data() {
    return {
      rowParams:{},
      filterText: "",
      //初始化弹框标题
      title: "",
      //仓房id
      houseId: "",
      //设备名称
      deviceName: "",
      // label宽度
      formLabelWidth: "120px",
      // 弹框默认关闭
      dialogTantou: false,
      filterText: "",
      leftList: [],
      defaultProps: {
        children: "children",
        label: "orgName",
      },

      loading: false,
      //默认展开的节点
      defaultExpanded: [],
      cfbh: "",

      formInline: {
        deviceNames: "",
      },
      //表格字段
      hardwareList: [],
      //弹框字段
      dialog: {
        deviceName: "",
        port: "",
        deviceCode:"",
        ipAdress: "",
      },
      //1新增   2编辑
      btnType:null,

      pagination: {
        //当前页数
        curPage: 1,
        //每页显示的条数
        pageSize: 10,
        //总条数
        total: 0,
      },
      // 过滤字段初始化
      orgName: "",
    };
  },

  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },

  methods: {
    //获取左侧树
    getLeftTree() {
      this.loading = true;
      let params = {
        orgName: this.orgName,
      };

      api
        .leftTree(params)
        .then((res) => {
          if (res.code == 200) {
            console.log(res);
            this.leftList = [];
            if (res.data) {
              this.leftList = res.data;

              // if (res.data[0].id) {
              //   this.defaultExpanded.push(res.data[0].id);
              //   if (res.data[0].children) {
              //     this.defaultExpanded.push(
              //       res.data[0].children[0].children[0].id
              //     );

              //     this.cfbh =
              //       res.data[0].children[0].children[0].children[0].id;

              //     setTimeout(() => {
              //       //  document.getElementsByClassName("el-tree-node__content")[0].style.display="none"
              //       // document.getElementsByClassName("el-tree-node__content")[1].style.display="none"
              //       this.$refs.tree.setCurrentKey(this.cfbh);
              //     }, 100);
              //     console.log(this.leftList);
              //     this.getList();
              //   }
              // }

               var lastLevel=res.data;
              if(lastLevel.length>0){
                lastLevel=res.data[0]
                while(lastLevel.children&&lastLevel.children.length>0){
                  lastLevel=lastLevel.children[0];
                }
           
                this.defaultExpanded.push(lastLevel.id);
                this.cfbh=lastLevel.id
                setTimeout(()=>{
                    this.$refs.tree.setCurrentKey(this.cfbh);
                
                },200)
                this.getList();
              }
            }
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
          this.loading = false;
        })
        .catch((error) => {
          this.loading = false;
          console.log(error);
        });
    },
    //获取列表数据
    getList() {
      console.log("....");
      var condition = {
        houseId: this.cfbh,
        deviceName: this.formInline.deviceName,
      };
      condition = JSON.stringify(condition);
      var data = {
        pageIndex: this.pagination.curPage,
        pageSize: this.pagination.pageSize,
        condition: condition,
      };
      api
        .hardParam(data)
        .then((res) => {
          if (res.code == 200) {
            this.hardwareList = res.data.records;
            this.pagination.pageSize = res.data.size;
            this.pagination.total = res.data.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //点击左侧树节点
    handleNodeClick(data, node) {
      // if (node.level === 4) {
      //   this.cfbh = data.id;
      //   console.log(this.cfbh, ";;;");
      // }
      // this.getList();
            if(node.childNodes.length===0){
            this.cfbh = data.id;
        console.log(this.cfbh, ";;;");
        this.getList();
      }

    },
    //新增
    addList() {
      var data = {
        houseId: this.cfbh,
        deviceName: this.dialog.deviceName,
        ipAdress: this.dialog.ipAdress,
        port: this.dialog.port,
        deviceCode: this.dialog.deviceCode,
      };
      api
        .addHardParam(data)
        .then((res) => {
          if (res.code == 200) {
            this.$message.success("新增成功");
            this.getList();
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },

    //编辑接口
    editList() {
      var data = {
        id: this.dialog.id,
        houseId: this.dialog.houseId,
        deviceName: this.dialog.deviceName,
        ipAdress: this.dialog.ipAdress,
        port: this.dialog.port,
        deviceCode:this.dialog.deviceCode
      };
      api
        .editHardParam(data)
        .then((res) => {
          if (res.code == 200) {
            this.$message.success("修改成功");
            this.getList();
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },

    filterNode(value, data) {
      if (!value) return true;
      return data.orgName.indexOf(value) !== -1;
    },
    search() {
      this.getList();
    },
    reset() {
      this.formInline.deviceName = "";
      this.getList();
    },
    //新增
    addTantou(type) {
      this.btnType=type
      this.dialog={};
      this.title = "新增硬件参数";
      this.dialogTantou = true;
    },

    //编辑
    editTantou(row,type) {
         this.btnType=type
      this.title = "修改硬件参数";
      this.dialogTantou = true;
      this.dialog = JSON.parse(JSON.stringify(row));
      console.log(this.dialog, "....");
      
    },
    //删除
      deleted (row) {
        // this.rowParams=row;
      console.log(row, "////");
      this.$confirm("删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let id = row.id

          api
            .deleteHardParam(id)
            .then((res) => {
              if (res.code && res.code == 200) {
                this.$message.success("删除成功");
                this.getList();
              }
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //表格序号
    indexMethod(index) {
      return (
        (this.pagination.curPage - 1) * this.pagination.pageSize + index + 1
      );
    },
    //当前页改变时
    handleCurrentChange(val) {
      this.pagination.curPage = val;
      this.getList();
    },
    //每页显示的页数改变时
    handleSizeChange(val) {
      this.pagination.pageSize = val;
      this.getList();
    },
    //弹框确定按钮
    determine(formData) {
      this.$refs[formData].validate((valid) => {
        if (valid) {
          if(this.btnType==1){
                this.addList();
          }else{
            this.editList()
          }
     
          this.dialogTantou = false;
        }
      });
    },
  },
  mounted() {
    console.log("aaaa");
    // this.getList();
    this.getLeftTree();
  },
};
</script>
<style lang="scss" scoped>
.main {
  height: 100%;
  display: flex;
  
  .right {
    margin-left: 20px;
    width: calc(100% - 280px);
    .nav {
      padding: 10px 0 10px 10px;
      color: #fff;

      font-size: 14px;

      background: #004a93;
    }
  }
  .addBtn {
    margin-bottom: 20px;
  }
}
</style>