<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 class="margin-top" :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>
    </div>
    <div slot="footer" class="dialog-footer">
      <!-- <el-button @click="isShow = false">取 消</el-button> -->
      <el-button type="primary" @click="isShow = false">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getDeviceList, getNewDeviceList } from "@/utils/api/index";
export default {
  props: {
    title: String,
  },
  data() {
    return {
      loading: true,
      isShow: false,
      infornr: {}, //装置信息内容
      timer: null,
      i: 0,
      queryTime: "", //查询ID
      cmdid: "",
    };
  },
  mounted() {},
  methods: {
    //获取第一次数据
    getListData(val, cmdid) {
      this.loading = true;
      this.cmdid = cmdid;
      getDeviceList({
        termId: val,
      })
        .then((res) => {
          this.queryTime = res.data.queryTime;
          this.getNewListData(val);
          this.timer = window.setInterval(() => {
            this.getNewListData(val);
            this.i++;
          }, 10000);
        })
        .catch((err) => {});
    },
    //最新数据
    getNewListData(val) {
      getNewDeviceList({
        queryTime: this.queryTime,
        termId: val,
      })
        .then((res) => {
          this.loading = false;
          if (res.code == 200) {
            this.infornr = res.data;
            if (res.data.isNew == true) {
              this.i = 0;
              this.$message.success("装置信息已更新");
              clearInterval(this.timer);
            } else if (this.i > 9) {
              this.i = 0;
              this.$message.warning("暂无响应，请稍后再试！");
              clearInterval(this.timer);
            }
          } else {
            this.$message.error(res.code.msg);
          }
        })
        .catch((err) => {});
    },
    display() {
      this.isShow = true;
    },
    hide() {
      this.isShow = false;
    },
    handleclose() {
      this.i = 0;
      clearInterval(this.timer);
      this.timer = null;
    },
  },
};
</script>
<style lang="less">
.infoDialog {
}
</style>
