<template>
  <div class='content'>
    <div class="left">

      <WgtTitle :title="'设备列表'"></WgtTitle>
      <el-form :model="form" class="formSearch keep-ratio" size="mini">
        <el-form-item>
          <el-col :span="11">
            <el-input v-model="form.deviceName" size="medium" placeholder="设备名称" class="direction"></el-input>
          </el-col>
          <el-col :span="10" :offset="1">
            <el-checkbox-group v-model="form.deviceState" style="height:26px">
              <el-checkbox :label="1">在线</el-checkbox>
              <el-checkbox :label="0">异常</el-checkbox>
            </el-checkbox-group>
          </el-col>
        </el-form-item>
        <el-form-item style="display: flex; justify-content: center;">
          <el-button class="btnInfoBoard" type="add" @click.native="onSearch()">搜索</el-button>
          <el-button type="publish" @click.native="onResetSearch()">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="body">
        <vuescroll :ops="scrollOptions">
          <div :class="'device keep-ratio ' + (listIndex===index?'deviceSel':'')" v-for="(item, index) of list" @click="devClick(item,index)">
            <div>{{ item.deviceName }}</div>
            <el-tooltip :content="(item.deviceState == 0 || item.deviceState == null) ? '异常' : '在线'" placement="top">
              <img src="@/assets/jihe/images/offline.svg" class="state"
                v-if="item.deviceState == '0' || item.deviceState == null">
              <img src="@/assets/jihe/images/online.svg" class="state" v-else>
            </el-tooltip>
          </div>
          <div v-if="list.length === 0" style="color: #fff; text-align: center; line-height: 60px">
            暂无数据
          </div>
        </vuescroll>
      </div>
      <div class="footer">
        <ElPagination @current-change="bindList" @size-change="onSizeChange" width="'100%'"
          :page-sizes="[10, 20, 30, 40, 50]" :page-size="page.pageSize" :current-page.sync="page.pageNum"
          layout="total, prev, pager, next" :total="total" class="Pagination">
        </ElPagination>
      </div>
    </div>
    <div class="right">
      <div class="top">
        <div class="search">
          <el-date-picker
            size="small"
            type="date"
            class="selectDate"
            v-model="date"
            style="width: 140px"
            placeholder="请选择"
          />
          <el-select v-model="devParam" @change="devChange"
            class="devparam" placeholder="请选择" >
              <el-option v-for="item in devOptions" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
          <Button style="margin-left: 25px"  @click.native="bindData()">查询</Button>
        </div>
      </div>
      <div class="right-bottom" >
        <div id="chart1" class="chart1"></div>
        <div class="right-table">
          <Table :data="tableData" >
            <ElTableColumn label="名称" prop="name" width="300"  align="center" />
            <ElTableColumn label="采集时间" prop="timestamp"  align="center" />
            <ElTableColumn :label="devParamName" prop="result" width="300" align="center" />
          </Table>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import vuescroll from "vuescroll";
import scrollOptions from "@/common/scrollbar.js";
import { Message } from "element-ui";
import WgtTitle from "@screen/pages/perception/widgets/title";
import Button from "@screen/components/Buttons/Button.vue";
import { setLoading } from "@screen/utils/index.js";
import chart1 from "./charts";
import * as echarts from "echarts";
import Table from '@screen/components/Table.vue';
export default {
  name: 'smartAnalysis',
  components: {
    vuescroll,
    WgtTitle,
    Button,
    Table
  },
  data() {
    return {
      listIndex: -1,
      form: {
        deviceName: '',
        deviceState: [1, 0]
      },
      page: {
        pageNum: 0,
        pageSize: 20
      },
      total: 0,
      list: [],
      scrollOptions,
      date:'',
      devParam:null,
      devOptions:[],
      devParamName:'',
      tableData:[]
    }
  },
  created() {

  },
  mounted() {
    this.bindList();
    this.date = moment().format('YYYY-MM-DD')
  },
  methods: {
    bindData(){
      if(this.devParam === null){
        Message.warning('未选择参数')
        return;
      }
      const param = _.find(this.devOptions,{id:this.devParam})
      this.devParamName = param.name;
      const closeLoading = setLoading();
      request({
        url: 'business/device/properties/deviceNameData',
        method: 'post',
        data:{
          deviceId: this.list[this.listIndex].deviceId,
          propertyId: param.propertyId,
          propertyName: param.propertyName,
          deviceType: param.deviceType,
          dateTime: this.date
        }
      }).then(result => {
        if (result.code != 200) return Message.error(result?.msg);
        let name = [];
        let value = []; 
        let unit = ''
        result.data.forEach((e,index) => {
          e.name = this.list[this.listIndex].deviceName,
          name.push(e.timestamp.substr(11))
          const n = e.result.match(/\d+\.*\d*/g);
          if(n.length > 0){
            if(unit === ''){
              unit = e.result.substr(n[0].length)
            }
            value.push(parseFloat(n[0]))
          } else {
            value.push(0)
          }
        });
        this.tableData = result.data;
        chart1.xAxis.data = name;
        chart1.yAxis[0].name = unit;
        chart1.series[0].name = param.name;
        chart1.series[0].data = value;
        chart1.tooltip['valueFormatter'] = function (value) {
          return value + " "+unit;
        }
        var myChart1 = echarts.init(document.getElementById("chart1"));
        myChart1.setOption(chart1);
      }).finally(()=>{
        closeLoading()
      })
    },
    devChange(e){
      const dev = _.find(devOptions,{id: e})
      console.log(dev)
    },
    devClick(item,index){
      this.listIndex = index;
      request({
        url: 'business/device/deviceParameter',
        method: 'get',
        params:{
          deviceType:item.deviceType
        }
      }).then(result => {
        if (result.code != 200) return Message.error(result?.msg);
     
        this.devOptions = result.rows;
        if(result.rows.length > 0){
          this.devParam = result.rows[0].id
          this.bindData();
        } else {
          this.devParam = null; 
        }
        
      })
    },
    onSizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.bindList();
    },
    bindList() {
      const params = {
        ...this.form, ...this.page,
      };
      if (params.deviceState.length === 2) {
        delete params.deviceState;
      } else {
        params.deviceState = params.deviceState.join('')
      }
      if (params.deviceName === '') {
        delete params.deviceName
      }

      request({
        url: 'business/device/selectDeviceNameList',
        method: 'get',
        params
      }).then(result => {
        if (result.code != 200) return Message.error(result?.msg);
        this.list = result.rows;
        this.total = result.total;
        
      })
    },
    onResetSearch() {
      this.form = {
        deviceName: '',
        deviceState: [1, 0]
      };
      this.pageNum = 0;
      this.bindList();
    },
    onSearch() {
      this.pageNum = 0;
      this.bindList();
    },
  }
}
</script>

<style lang='scss' scoped>
.content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: row;
  background-color: #101d23;
  padding: 20px;

  .left {
    width: 380px;
    background-color: #112533;
    height: 100%;
    display: flex;
    flex-direction: column;

    .body {
      flex: 1;
      height: 0;
      padding: 0 10px 10px;
    }

    .footer {
      display: flex;
      justify-content: center;
      align-items: center;
      margin: 10px;
    }
  }

  .right {
    flex: 1;
    margin-left: 30px;
    .top{
      .search{
        display: flex;
        justify-content: flex-end;
      }
    }
    .right-bottom{
      width: 100%;
      height: 100%;
      flex:1;
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      align-items: flex-start;
    }
    .chart1{
      width: 100%;
      flex:1;
    }
    .right-table{
      width: 100%;
      height: 550px;
      overflow-y: auto;
      margin-bottom: 30px;
    }
  }
}

::v-deep .formSearch {
  padding: 20px 20px 0;

  .el-form-item__label {
    color: #fff;
  }
}

.direction {
  ::v-deep .el-input {
    .el-input__inner {
      font-size: 14px !important;
      padding: 8px 5px;
    }
  }
}

.device {
  padding: 7.5px 15px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
}
.deviceSel{
  background-color:#1798a9;
}
.device:hover {
  background-color: #1d647f;
}

.state {
  width: 18px;
  height: 18px;
}

</style>

<style lang="scss" scoped>
.Pagination {
 
  ::v-deep {

    >button,
    >ul li {
      background: #064258;
      border-radius: 2px 2px 2px 2px;
      opacity: 1;
      color: #fff;
    }

    >button {
      padding: 0 3px;
      border: 1px solid #00b3cc;
    }

    >ul li {
      background: linear-gradient(180deg, #004960 0%, #004b62 100%);
      margin: 0 1.5px;

      &.active {
        background: linear-gradient(180deg, #005c79 0%, #009bcc 100%);
      }
    }

    .el-pagination__total {
      color: #fff;
    }

    .el-pagination__sizes {
      .el-select {
        .el-input {
          width: 81px;

          .el-input__inner {
            font-size: 12px;
            height: 23px;
            background-color: #064258 !important;
          }
        }

        .el-input__suffix {
          .el-input__icon {
            line-height: 23px;
          }
        }
      }
    }

    .el-pagination__jump {
      margin-left: 10px;
      color: #fff;
      font-size: 12px;

      .el-input {
        width: 27px;
        margin: 0 3px;

        .el-input__inner {
          font-size: 12px;
          background-color: #064258 !important;
          border-radius: 2px 2px 2px 2px;
        }
      }
    }

    .btn-next {
      margin-left: 6px;
    }

    .btn-prev {
      margin-right: 6px;
    }
  }

  ::v-deep .el-pagination__total {
    margin-left: 10px;
  }
}
.devparam{
  width: 200px;
  margin-left: 20px;
  
}
.selectDate {
    width: 89px;
    border: 1px solid #00b3cc;
    
    ::v-deep {
      .el-input__prefix{
        top:-4px;
      }
      .el-input__inner {
        
        background-color: #064258 !important;
        border-width: 0px !important;
      }
    }
  }
</style>
