<template>
  <el-card class="content-div">
    <div class="all-content">
      <fieldset class="left  box2">
        <legend class="box-ht">驱动列表</legend>
        <div v-for="(item, i) in qdDatas" :class="i === indexi ? 'active' : ''" :key="i" @click="changeQd(i, item)">
          <img src="../assets/qd.png" alt="">
          {{ item.driver_name }}
        </div>
      </fieldset>
      <div class="right">
        <div class="right-set">
          <el-tree ref="taskTree" :data="data" node-key="device_name" :props="defaultProps" :highlight-current="true" accordion
            @node-click="handleNodeClick">
          </el-tree>
        </div>
        <div class="right-table">
          <el-table :data="tableData" border :row-class-name="tableRowClassName"
            :header-cell-style="{ background: '#F6F7FC' }" size="large">
            <el-table-column type="index" label="序号" width="80" align="center" />
            <el-table-column prop="point_name" label="名称" width="150" align="center" show-overflow-tooltip />
            <el-table-column prop="point_description" label="描述" align="center" show-overflow-tooltip />
            <el-table-column prop="point_quality" label="质量" width="100" align="center" show-overflow-tooltip />
            <el-table-column prop="point_value" label="值" width="100" align="center" show-overflow-tooltip />
            <el-table-column prop="point_Unit" label="单位" width="100" align="center" show-overflow-tooltip />
            <el-table-column prop="point_type" label="类型" width="100" align="center" show-overflow-tooltip />
            <el-table-column prop="point_timestamp" label="时间戳" width="200" align="center" show-overflow-tooltip />
            <el-table-column label="操作" align="center" show-overflow-tooltip>
              <template #default="scope">
                <el-button type="success" size="large">
                  历史
                </el-button>
                <el-button type="warning" size="large" v-if="scope.row.point_permissions ==2">
                  写值
                </el-button>

              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script>
import { onMounted, reactive, ref, toRefs, nextTick } from "vue";
import infoApi from "@/api/infoApi.js";
import { ElMessage } from "element-plus";
export default {
  name: "data",
  setup() {
    const taskTree = ref(null);
    const state = reactive({
      indexi: 0,
      qdDatas: [],
      data: [],
      tableData: [],
      defaultProps: {
        label: 'device_name',
        children: 'children'
      }
    });

    onMounted(() => {
      getDriverData();
    });

    const getDriverData = async () => {
      const res = await infoApi.getDriverData();
      if (res.code == 0) {
        state.qdDatas = res.data || [];
        if (state.qdDatas.length > 0) {
          changeQd(0, state.qdDatas[0]);
        }
      }


    }
    const changeQd = (i, item) => {
      state.indexi = i;
      getDeviceData(item.driver_name);
    }

    const getDeviceData = async (driver_name) => {
      const param = {
        driver_name: driver_name
      }
      const res = await infoApi.getDeviceData(param);
      if (res.code == 0) {
        state.data = res.data || [];
        if (state.data.length > 0) {
          handleNodeClick(state.data[0]);
          nextTick(() => {
            taskTree.value.setCurrentKey(state.data[0].device_name);
          })
        }
      }

    }

    const handleNodeClick = (item) => {
      getPointData(item);
    }

    const getPointData = async (item) => {
      const param = {
        driver_name: item.driver_name,
        device_name: item.device_name
      }

      const res = await infoApi.getPointData(param);
      if(res.code == 0) {
        state.tableData = res.data;
      }
    }



    return {
      ...toRefs(state),
      changeQd,
      handleNodeClick,
      taskTree
    };
  },
};
</script>
<style lang="scss" scoped>
.all-content {
  display: flex;
  justify-content: space-between;
  font-size: 14px;

  .left {
    width: 15%;
    border: 1px solid #006b3b;
    font-size: 16px;

    img {
      width: 24px;
    }

    >div {
      background: #e2f4e5;
      margin-top: 10px;
      text-align: center;
      line-height: 40px;
      cursor: pointer;
    }

    .active {
      background: #00AAA3;
      color: #fff;
    }
  }

  .right {
    width: 84%;
    border: 1px solid #006b3b;
    display: flex;
    justify-content: space-around;

    .right-set {
      width: 10%;
      padding: 20px;
    }

    .right-table {
      width: 88%;
      //  .el-tree-node {
      //     border: 1px solid #ddd !important;
      //     margin-top: 10px !important;
      // }
    }
  }
}
</style>
