<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="row row01">
      <el-card class="box-card row01-left" style="cursor: default">
        <div class="info">
          <span
            class="cbg"
            :style="{ backgroundImage: 'url(' + cbg + ')' }"
          ></span>
          <div class="info-top">
            <p class="p1">{{ userinfo.name }},欢迎登录本系统!</p>
            <p class="p2">上次登陆时间 : {{ userinfo.lastEnter }}</p>
          </div>
          <div class="info-bottom">
            <div class="bot-left">
              <div class="tem">
                <span>{{ userinfo.at }}</span
                >℃
              </div>
              <div class="wea">
                <p>{{ userinfo.type }}</p>
                <p>{{ userinfo.city }}</p>
              </div>
            </div>
            <div class="bot-right">
              <p>{{ userinfo.week }}</p>
              <p>{{ userinfo.time }}</p>
            </div>
          </div>
        </div>
        <div class="notice">
          <div class="tit">
            <i :style="{ backgroundImage: 'url(' + titBg01 + ')' }"></i>通知公告
          </div>
          <ul>
            <li>
              <div>春季气候偏暖 病虫害发生气象风险高</div>
              <span>2020-05-06</span>
            </li>
            <li>
              <div>我国科学家找到攻克小麦赤霉病“金钥匙”</div>
              <span>2020-05-06</span>
            </li>
            <li>
              <div>山东农大攻克小麦赤霉病 取得关键突破</div>
              <span>2020-05-06</span>
            </li>
            <li>
              <div>今年江南梅雨季启幕偏早 防范农业不利影响</div>
              <span>2020-05-06</span>
            </li>
            <li>
              <div>东部小麦产区本周需抢晴打药防治赤霉病</div>
              <span>2020-05-06</span>
            </li>
          </ul>
        </div>
      </el-card>
      <el-card class="box-card row01-right">
        <div class="tit" style="cursor: default">
          <i :style="{ backgroundImage: 'url(' + titBg02 + ')' }"></i>空气温湿度
          <el-cascader
            v-model="equip_id"
            :options="cascaderEquip"
            size="mini"
            :props="{ expandTrigger: 'hover', label: 'type_name', value: 'id' }"
            @change="handleChange"
          ></el-cascader>
        </div>

        <div id="chart01" ref="chart01Ref"></div>
      </el-card>
    </div>
    <div class="row row02" style="cursor: default">
      <el-card class="box-card row02-left">
        <div class="tit">
          <i :style="{ backgroundImage: 'url(' + titBg03 + ')' }"></i>设备统计
        </div>
        <div class="con">
          <div class="box" v-for="item in equipList" :key="item.device_id">
            <div class="box-inner">
              <span
                :class="{
                  state: true,
                  on: item.device_status == 1,
                  off: item.device_status == 0,
                }"
                >{{ item.device_status == 1 ? "在线" : "离线" }}</span
              >
              <p class="eq-id">{{ item.device_id }}</p>
              <div class="infos">
                <p>
                  <i class="iconfont icon-biaoqian"></i>
                  设备类型:{{ (item.device_type_id + "") | equipType }}
                </p>
                <p>
                  <i class="iconfont icon-biaoqian"></i>
                  设备名称:{{ item.device_name }}
                </p>
                <p>
                  <i class="iconfont icon-biaoqian"></i>
                  位置:{{ item.city }}
                </p>
              </div>
              <div class="date">
                <span>{{ (item.uptime * 1000) | formatTime }}</span>
                <a
                  href="#"
                  @click="
                    dataDetails(
                      item.device_id,
                      item.d_id,
                      item.device_type_id,
                      item.device_model
                    )
                  "
                  >详情 ></a
                >
              </div>
            </div>
          </div>
        </div>
      </el-card>
      <el-card class="box-card row02-right">
        <div class="tit">
          <i :style="{ backgroundImage: 'url(' + titBg04 + ')' }"></i>设备占比
        </div>
        <div id="chart02" ref="chart02Ref"></div>
      </el-card>
    </div>
  </div>
</template>
<script>
import echarts from "echarts";
import { home1Line, home1Pie } from "./chart.js";
export default {
  data() {
    return {
      titBg01: require("@/assets/images/home/home01/tit01.png"),
      titBg02: require("@/assets/images/home/home01/tit02.png"),
      titBg03: require("@/assets/images/home/home01/tit03.png"),
      titBg04: require("@/assets/images/home/home01/tit04.png"),
      cbg: require("@/assets/images/home/home01/cbg.png"),
      myChart01: "",
      myChart02: null,
      equipList: [],
      equip_id: "",
      userinfo: {
        name: "",
        lastEnter: "",
        at: "",
        city: "",
        type: "",
        week: "",
        time: "",
      },
      cascaderEquip: [],
    };
  },
  mounted() {
    this.userinfo.lastEnter = localStorage.getItem("lastEnter");
    this.userinfo.name = localStorage.getItem("cUsername");
    this.getcity();
    this.chartsInit();
    this.getPieData();
    this.getEquipList();
    this.getCascaderEquip();
  },
  destroyed() {
    window.onresize = null;
  },
  computed: {
    username() {
      return localStorage.getItem("cUsername");
    },
  },
  methods: {
    chartsInit() {
      // 折线图初始化
      this.myChart01 = echarts.init(this.$refs.chart01Ref);
      //饼状图初始化
      this.myChart02 = echarts.init(this.$refs.chart02Ref);

      window.addEventListener("resize", () => {
        this.myChart01.resize();
        this.myChart02.resize();
      });
    },
    getEquipList() {
      this.$axios({
        method: "POST",
        url: "/api/api_gateway?method=home.homes.equip_list",
        data: this.qs.stringify({
          "user ": this.username,
        }),
      }).then((res) => {
        this.equipList = res.data.data;
        // this.equip_id = this.equipList[0].device_id
        // this.getLineOption()
      });
    },
    getPieData() {
      this.$axios({
        method: "POST",
        url: "/api/api_gateway?method=home.homes.equip_total",
      }).then((res) => {
        let data = res.data.data;
        if (data) {
          var scd = data.scd_num || 0;
          var qxz = data.qxz_num || 0;
          var cbd = data.cbd_num || 0;
          var bzy = data.bzy_num || 0;
          let pieOption = home1Pie(bzy, cbd, qxz, scd);
          this.myChart02.setOption(pieOption);
        }
      });
    },
    getCascaderEquip() {
      this.$axios({
        method: "POST",
        url: "/api/api_gateway?method=home.homes.equip_broken_line",
      }).then((res) => {
        if (res.data.message == "") {
          this.cascaderEquip = res.data.data.data;
          this.equip_id =
            this.cascaderEquip.length&&this.cascaderEquip[0].children.length &&
            this.cascaderEquip[0].children[0].id;
          this.getLineOption();
        }
      });
    },
    getLineOption() {
      this.$axios({
        method: "POST",
        url: "/api/api_gateway?method=home.homes.equip_tp_line",
        data: this.qs.stringify({
          equip_id: this.equip_id,
        }),
      }).then((res) => {
        let data = res.data.data;
        let option = {
          time: [],
          ah: [],
          at: [],
        };
        data.forEach((item) => {
          option.time.push(this.formatTime(item.addtime * 1000));
          option.ah.push(item.ah);
          option.at.push(item.at);
        });
        this.myChart01.setOption(home1Line(option));
      });
    },
    handleChange() {
      this.equip_id = this.equip_id[1];
      this.getLineOption();
    },
    // 数据详情
    dataDetails(e_id, d_id, type, device_model) {
      if (type == 2) {
        //杀虫灯
        this.$router.push(`/index/scdDetail/${e_id}/${d_id}`);
      } else if (type == 3) {
        //测报
        this.$router.push(`/index/cbdDataDetails/${e_id}/${d_id}`);
      } else if (type == 4) {
        //性诱
        this.$router.push({
          path: `/index/xycbDataDetail/${e_id}/${d_id}`,
          query: { device_model: device_model },
        });
      } else if (type == 5) {
        //气象站
        this.$router.push(`/index/historyData/${e_id}`);
      } else if (type == 6) {
        //监控
        this.$router.push({
          path: `/index/monitor`,
          query: { id: `${e_id}`, jktype: 0 },
        });
      } else if (type == 7) {
        //孢子
        this.$router.push(`/index/dataDetail/${e_id}/${d_id}`);
      } else if (type == 9) {
        //糖醋
        this.$router.push(`/index/tcDataDetail/${e_id}/${d_id}`);
      }
    },
    getcity() {
      this.$axios({
        method: "POST",
        url: "/api/api_gateway?method=home.homes.city_addr",
      }).then((res) => {
        if (res.data.message == "") {
          var data = res.data.data.data;
          this.userinfo.at = data.forecast[0].high.substring(2);
          this.userinfo.type = data.forecast[0].type;
          this.userinfo.week = data.forecast[0].date.substring(
            data.forecast[0].date.length - 3
          );
          var myDate = new Date();
          var year = myDate.getFullYear(); //获取当前年
          var mon = myDate.getMonth() + 1; //获取当前月
          var date = myDate.getDate(); //获取当前日
          this.userinfo.time = year + "年" + mon + "月" + date + "日";
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.row {
  display: flex;
  .tit {
    font-size: 18px;
    font-weight: 800;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    i {
      display: inline-block;
      width: 16px;
      height: 20px;
      margin-right: 10px;
      background-size: 100%;
      background-position: center;
      background-repeat: no-repeat;
    }
  }
}
.row01-right,
.row02-left {
  flex: 3;
  margin: 5px;
}
.row01-left,
.row02-right {
  flex: 1.3;
  margin: 5px;
}
.row01-left {
  .info {
    .cbg {
      width: 80px;
      height: 60px;
      position: absolute;
      background-size: 100% 100%;
      right: 10px;
      top: 10px;
    }
    position: relative;
    width: 100%;
    background-color: #34d496;
    background-image: linear-gradient(#2ed399, #a5de39);
    border-radius: 6px;
    padding: 20px 20px;
    box-sizing: border-box;
    color: #fff;
    margin-bottom: 20px;
    .info-top {
      .p1 {
        font-size: 18px;
      }
      .p2 {
        font-size: 14px;
        line-height: 28px;
      }
      margin-bottom: 50px;
    }
    .info-bottom {
      display: flex;
      justify-content: space-between;
      .bot-left {
        display: flex;
        .tem {
          font-size: 18px;
          span {
            font-size: 40px;
            margin-right: 5px;
          }
          margin-right: 15px;
        }
        .wea {
          font-size: 14px;
        }
      }
      .bot-right {
        font-size: 14px;
        text-align: right;
      }
    }
  }
  .notice {
    li {
      display: flex;
      justify-content: space-between;
      line-height: 28px;
      div {
        font-size: 14px;
        width: 70%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      span {
        color: #888;
        flex: 1;
        text-align: right;
      }
    }
  }
}
.row01-right {
  position: relative;
  #chart01 {
    width: 100%;
    height: 350px;
  }
  .el-cascader {
    margin-left: 20px;
    width: 220px;
  }
}
.row02-left {
  .con {
    // background:#f4f4f4;
    min-height: 320px;
    display: flex;
    flex-wrap: wrap;
    .box {
      width: 25%;
      padding: 5px;
      box-sizing: border-box;
      .box-inner {
        box-shadow: 0 0 5px 3px rgba(0, 0, 0, 0.08);
        background: #fff;
        border-radius: 4px;
        padding: 10px;
        position: relative;
        .eq-id {
          font-size: 16px;
          color: #11ce94;
          font-weight: 600;
          margin-bottom: 10px;
        }
        .infos {
          color: #777;
          line-height: 26px;
          font-size: 13px;
          i {
            color: #ccc;
            margin-right: 15px;
          }
        }
        .date {
          margin-top: 10px;
          span {
            font-size: 12px;
            color: #999;
          }
          a {
            font-size: 12px;
            color: #999;
            float: right;
          }
          a:hover {
            color: #11ce94;
          }
        }
        .state {
          position: absolute;
          display: inline-block;
          right: 0;
          top: 0;
          color: #fff;
          width: 46px;
          height: 26px;
          line-height: 26px;
          text-align: center;
          font-size: 12px;
          border-top-right-radius: 4px;
          &:before {
            content: "";
            position: absolute;
            right: 26px;
            width: 0;
            height: 0;
            border-width: 26px 20px;
            border-style: solid;
            border-radius: 4px;
          }
        }
        .on {
          background: #0ed094;
          &:before {
            border-color: #0ed094 transparent transparent transparent;
          }
        }
        .off {
          background: #fc595a;
          &:before {
            border-color: #fc595a transparent transparent transparent;
          }
        }
      }
    }
  }
}
.row02-right {
  position: relative;
  min-height: 360px;
  /deep/.el-card__body {
    height: 100%;
    #chart02 {
      // position: absolute;
      // top: 70px;
      // bottom: 10px;
      width: 100%;
      height: calc(100% - 30px);
    }
  }
}
</style>
