<template>
  <view class="charts-box">

    <view>
      <uni-row class="demo-uni-row" >
        <uni-col :span="8">
          <uni-data-select v-model="imei" :localdata="agriList" @change="changeAgri"></uni-data-select>

        </uni-col>
        <uni-col :span="16">
          <uni-datetime-picker v-model="dateRange" type="datetimerange" @change="changeDate"/>
        </uni-col>
      </uni-row>

    </view>



    <qiun-data-charts
        type="line"
        :opts="opts"
        background="#fff"
        :chartData="chartData"
        :canvas2d="true"
        canvasId="nMPkeQGNEosMwoWKKNRBZBIEhguMoMWp"
        :ontouch="true"
        :onmovetip="true"
    />
  </view>
</template>

<script>
import { getHistoryData} from "../../api/system/data";
import {listAgri} from "../../api/system/assets/agri";
import store from "../../store";
import {parseTime} from "../../utils/agri";

export default {
  data: function () {
    return {
      chartData: {},
      //这里的 opts 是图表类型 type="line" 的全部配置参数，您可以将此配置复制到 config-ucharts.js 文件中下标为 ['line'] 的节点中来覆盖全局默认参数。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式，达到页面简洁的需求。
      opts: {
        yAxis: {
          gridType: "dash",
          dashLength: 2,
          disabled: false,
          disableGrid: false,
          splitNumber: 5,
          gridColor: "#CCCCCC",
          padding: 10,
          showTitle: true,
          data: [
            {
              type: "value",
              position: "left",
              disabled: false,
              axisLine: true,
              axisLineColor: "#CCCCCC",
              calibration: true,
              fontColor: "#666666",
              fontSize: 13,
              textAlign: "left",
              title: "温度（℃）",
              titleFontSize: 14,
              titleOffsetY: -10,
              titleOffsetX: 5,
              titleFontColor: "#333333",
              min: 0,
              max: 50,
              tofix: 1,
              unit: "",
              format: ""
            },
            {
              type: "value",
              position: "right",
              disabled: false,
              axisLine: true,
              axisLineColor: "#CCCCCC",
              calibration: true,
              fontColor: "#666666",
              fontSize: 13,
              textAlign: "right",
              title: "湿度（%RH）",
              titleFontSize: 14,
              titleOffsetY: -10,
              titleOffsetX: 0,
              titleFontColor: "#333333",
              min: 0,
              max: 100,
              tofix: 1,
              unit: "",
              format: ""
            }
          ]
        }
      },
      dateRange:[],
      end:Date.now(),
      agriList:[],
      imei:null
    };
  },
  onReady() {
    this.getAgriList();
  },
  methods: {
    getServerData() {
      const param = {
        imei: this.imei,
        startTime: this.dateRange[0],
        endTime: this.dateRange[1]
      }
      console.info("查询参数",param)
      //模拟从服务器获取数据时的延时
      getHistoryData(param).then(response => {
        if (response.code === 200 && response.data) {
          var data = response.data;
          var res = {
            categories: data.timeList,
            series: [
              {
                name: "温度1",
                index: 0,
                data: data.temp1List
              },
              {
                name: "温度2",
                index: 0,
                data: data.temp2List
              },
              {
                name: "温度3",
                index: 0,
                data: data.temp3List
              },
              {
                name: "温度4",
                index: 0,
                data: data.temp4List
              },
              {
                name: "湿度1",
                index: 1,
                data: data.humi1List
              },
              {
                name: "湿度2",
                index: 1,
                data: data.humi2List
              },
              {
                name: "湿度3",
                index: 1,
                data: data.humi3List
              },
              {
                name: "湿度4",
                index: 1,
                data: data.humi4List
              }
            ]
          };
        }
        this.chartData = JSON.parse(JSON.stringify(res));
      })
    },
    getAgriList() {
      listAgri().then(response => {
        if (response.code === 200) {
          this.agriList = response.rows.map(item => ({
            agriId: item.id,
            text: item.agriName,
            value: item.imei // 提取并改名
          }));

          if (store.getters && store.getters.name === 'admin') {
            this.agriList.push(
                {
                  text:"八方南棚",
                  value:"A"
                },
                {
                  text:"九方春棚",
                  value:"B"
                },
                {
                  text:"十二方棚",
                  value:"C"
                }
            )
          }
          this.imei = this.agriList[0].value;
          this.getServerData();
        }
      })
    },
    changeAgri(imei) {
      console.info("大棚下拉选择",imei)
    },
    changeDate(date) {
      // date[0]-date[1]
      console.info("日期选择器：",date)
      if (this.imei) {
        this.getServerData();
      }
    }
  }
};
</script>

<style scoped>
/* 请根据实际需求修改父元素尺寸，组件自动识别宽高 */
.charts-box {
  width: 100%;
  height: 300px;
}
</style>