<template>
  <el-dialog
    class="infoDialog"
    title="装置信息"
    :visible.sync="isShow"
    :close-on-click-modal="false"
    width="50%"
    @close="handleclose"
  >
    <div class="infoTable" v-loading="loading">
      <el-descriptions :column="1" border>
        <el-descriptions-item>
          <template slot="label"> 装置编号 </template>{{ cmdid }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 电池电量 </template
          >{{ infornr.batteryCapacity }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 电池电压 </template
          >{{ infornr.batteryVoltage }}V
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> MEID </template>{{ infornr.cmld }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 网络连接状态 </template
          >{{ infornr.connectionState == 0 ? "正常" : "断开" }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 浮充状态 </template
          >{{ infornr.floatingCharge == 0 ? "充电" : "放电" }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 工作总时间 </template
          >{{ infornr.totalWorkingTime }}小时
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 本次连续工作时间 </template
          >{{ infornr.workingTime }}小时
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 工作状态更新时间 </template
          >{{
            $moment(infornr.wsUpdateTime * 1000).format("yy-MM-DD HH:mm:ss")
          }}
        </el-descriptions-item>
      </el-descriptions>
      <el-button type="text" class="newBtn" @click="getListData()"
        >获取最新装置信息</el-button
      >
    </div>
    <div slot="footer" class="dialog-footer">
      <!-- <el-button @click="isShow = false">取 消</el-button> -->
      <el-button type="primary" @click="handleclose" :disabled="loading"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>
<script>
import {
  getDeviceList,
  getNewDeviceList,
  getinfoStatus,
} from "@/utils/api/index";
export default {
  props: {
    title: String,
  },
  data() {
    return {
      loading: false,
      isShow: false,
      infornr: {}, //装置信息内容
      timer: null,
      i: 0,
      queryTime: "", //查询ID
      cmdid: "",
      zztermid: "",
    };
  },
  mounted() {},
  methods: {
    //数据库获取最新数据
    getinfo() {
      this.loading = true;
      getinfoStatus({
        termId: this.zztermid,
      })
        .then((res) => {
          this.infornr = res.data;
          this.loading = false;
        })
        .catch((err) => {});
    },
    // getNewData() {
    //   let params = [
    //     {
    //       name: "act",
    //       value: "runningstatus",
    //     },
    //   ];
    //   this.setTermFn(params);
    // },
    // setTermFn(dataParams) {
    //   console.log("点击了统一的接口", "runningstatus");
    //   setTermCamera({
    //     termId: this.zztermid,
    //     list: dataParams,
    //   })
    //     .then((res) => {
    //       console.log(res);
    //       this.requestid = res.data.requestId;
    //       //this.getinfo1();

    //       clearInterval(this.timer);
    //       this.timer = window.setInterval(() => {
    //         this.getinfo();
    //         this.i++;
    //       }, 1000);
    //       // this.timer = setInterval(() => {
    //       //   this.getinfo();
    //       //   this.i++;
    //       // }, 1000);
    //     })
    //     .catch((err) => {});
    // },
    // getinfo() {
    //   getTermCameraRequest({ requestid: this.requestid })
    //     .then((res) => {
    //       if (res.data.success == 1) {
    //         window.clearInterval(this.timer);
    //         this.timer = null;
    //         this.i = 0;

    //         console.log("终止轮询");
    //         // && res.data.result == 255
    //         console.log(JSON.parse(res.data.data));

    //         this.$message({
    //           duration: 1500,
    //           showClose: true,
    //           message: "信息已更新",
    //           type: "success",
    //         });
    //       } else if (this.i > 9) {
    //         window.clearInterval(this.timer);
    //         this.timer = null;
    //         this.i = 0;
    //         this.$message({
    //           duration: 1500,
    //           showClose: true,
    //           message: "暂未获取到信息，请稍后再试！！",
    //           type: "warning",
    //         });
    //       }
    //     })
    //     .catch((err) => {});
    // },
    //获取第一次数据
    getListData() {
      this.loading = true;
      getDeviceList({
        termId: this.zztermid,
      })
        .then((res) => {
          this.queryTime = res.data.queryTime;
          //this.getNewListData(this.zztermid);
          this.timer = window.setInterval(() => {
            this.getNewListData(this.zztermid);
            this.i++;
          }, 2000);
        })
        .catch((err) => {});
    },
    //最新数据
    getNewListData(val) {
      getNewDeviceList({
        queryTime: this.queryTime,
        termId: val,
      })
        .then((res) => {
          if (res.code == 200) {
            this.infornr = res.data;

            if (res.data.isNew == true) {
              this.i = 0;
              this.$message.success("装置信息已更新");
              clearInterval(this.timer);
              this.loading = false;
            } else if (this.i > 4) {
              this.i = 0;
              this.$message.warning("暂无响应，请稍后再试！");
              clearInterval(this.timer);
              this.loading = false;
            }
          } else {
            this.$message.error(res.code.msg);
          }
        })
        .catch((err) => {});
    },
    // display() {
    //   this.isShow = true;

    // },
    display(newTermId, cmdid) {
      console.log(newTermId, cmdid);
      this.isShow = true;
      this.zztermid = newTermId;
      this.cmdid = cmdid;
    },
    hide() {
      this.isShow = false;
    },
    handleclose() {
      this.isShow = false;
      this.loading = false;
      this.i = 0;
      clearInterval(this.timer);
      this.timer = null;
    },
  },
  destroyed() {
    this.isShow = false;
    this.loading = false;
    this.i = 0;
    clearInterval(this.timer);
    this.timer = null;
  },
};
</script>
<style lang="less">
.infoDialog {
  .newBtn {
    margin-top: 14px;
  }
}
</style>
