<template>
  <div class="equipmentStatusBox" ref="statisRef">
    <div class="searchBox" ref="searchref" v-if="drawerSearch">
      <el-form :inline="true" :model="formdata" class="demo-form-inline">
        <el-form-item label="电压等级" class="dyclass">
          <el-select v-model="formdata.dyid" @change="getSearchxl">
            <el-option
              v-for="item in dyOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
              {{ item.name }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="线路名称" class="xlclass">
          <el-select v-model="formdata.lineid" @change="getSearchgt" filterable>
            <el-option
              v-for="item in xlOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="杆塔名称" class="gtclass">
          <el-select v-model="formdata.towerid" filterable>
            <el-option
              v-for="item in gtOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否在线" class="isonLineClass">
          <el-select v-model="formdata.isonline" filterable>
            <el-option
              v-for="item in onlineOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规约" class="isonLineClass">
          <el-select v-model="formdata.protocol" filterable>
            <el-option
              v-for="item in protocolOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="活动"
          class="activitybox"
          v-if="roleName === 'superadmin' || roleName === 'Matthew'"
        >
          <el-select
            @keyup.enter.native="onSubmit()"
            v-model="formdata.activityId"
            filterable
            @change="changeActive"
          >
            <el-option
              v-for="item in activityOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始日期" class="dateclass">
          <el-date-picker
            v-model="formdata.starttime"
            type="datetime"
            placeholder="开始日期"
            value-format="timestamp"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" class="dateclass">
          <el-date-picker
            v-model="formdata.endtime"
            type="datetime"
            placeholder="结束日期"
            value-format="timestamp"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="装置查询" class="schclass">
          <el-input
            v-model="formdata.search"
            placeholder="请输入设备名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="deviceTable">
      <el-table
        v-loading="termLoading"
        ref="multipleTable"
        :data="termAllList"
        tooltip-effect="dark"
        stripe
        style="width: 100%"
        :height="tableheight"
      >
        <template slot="empty">
          <el-empty :image-size="160" description="暂无数据"></el-empty>
        </template>
        <el-table-column label="序号" width="50px" fixed>
          <template slot-scope="scope">
            {{ (page - 1) * pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>

        <el-table-column
          prop="dyName"
          label="电压等级"
          fixed
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="lineName" label="线路名称" min-width="180" fixed>
        </el-table-column>
        <el-table-column
          prop="towerName"
          label="杆塔名称"
          min-width="180"
          show-overflow-tooltip
          fixed
        >
        </el-table-column>
        <!-- <el-table-column prop="displayName" label="设备名称" min-width="180" fixed> </el-table-column> -->
        <el-table-column prop="cmdid" label="装置id" min-width="180" fixed>
          <template slot-scope="scope">
            {{ scope.row.cmdid }}
          </template>
        </el-table-column>
        <el-table-column prop="sim" label="SIM卡号" min-width="180">
        </el-table-column>
        <el-table-column prop="netType" label="网络类型">
          <template slot-scope="scope">
            <span v-if="scope.row.netType == 1">移动</span>
            <span v-else-if="scope.row.netType == 2">联通</span>
            <span v-else-if="scope.row.netType == 3">电信</span>
          </template>
        </el-table-column>
        <el-table-column prop="onlinestatus" label="状态">
          <template slot-scope="scope">
            <span style="color: #169e8c" v-if="scope.row.onlinestatus == 1"
              >在线</span
            >
            <span style="color: #f56c6c" v-else-if="scope.row.onlinestatus == 0"
              >离线</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="model" label="装置型号" min-width="120">
        </el-table-column>
        <el-table-column prop="bsManufacturer" label="装置厂家">
        </el-table-column>
        <el-table-column prop="protocolName" label="规约版本" min-width="180">
        </el-table-column>
        <el-table-column prop="workingDate" label="投运日期" min-width="140">
        </el-table-column>

        <el-table-column prop="onlinestatus" label="最新运行状态">
        </el-table-column>
        <el-table-column prop="photoInfo.totalDays" label="在线天数">
        </el-table-column>
        <el-table-column prop="photoInfo.photoCount" label="图片数量">
        </el-table-column>
        <el-table-column
          prop="photoInfo.lastRecvTime"
          label="最后数据上送时间"
          min-width="140"
        >
        </el-table-column>
        <el-table-column
          prop="photoInfo.lastPhotoTime"
          label="最后图片上送时间"
          min-width="140"
        >
        </el-table-column>

        <el-table-column
          prop="lastInfo.updateTimeStr"
          label="最新基本信息采集时间"
          min-width="140"
        >
        </el-table-column>
        <el-table-column
          prop="lastInfo.equipName"
          label="装置名称"
          min-width="150"
        >
        </el-table-column>
        <el-table-column prop="lastInfo.model" label="装置型号" min-width="120">
        </el-table-column>
        <el-table-column prop="lastInfo.version" label="装置版本号">
        </el-table-column>
        <el-table-column
          prop="lastInfo.bsManu"
          label="生产厂家"
          min-width="120"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="lastInfo.productionDate"
          label="生产日期"
          min-width="140"
        >
        </el-table-column>
        <el-table-column prop="lastInfo.bsId" label="出厂编号" min-width="150">
        </el-table-column>

        <el-table-column
          prop="workingStatus.wsUpdateTime"
          label="最新工作状态采集时间"
          min-width="140"
        >
          <template slot-scope="scope">
            {{
              scope.row.workingStatus.wsUpdateTime == 0 ||
              scope.row.workingStatus.wsUpdateTime == null
                ? ""
                : $moment(scope.row.workingStatus.wsUpdateTime * 1000).format(
                    "YYYY-MM-DD HH:mm:ss"
                  )
            }}</template
          >
        </el-table-column>
        <el-table-column prop="workingStatus.batteryVoltage" label="电源电压">
        </el-table-column>
        <el-table-column prop="workingStatus.opTemperature" label="工作温度">
        </el-table-column>
        <el-table-column prop="workingStatus.batteryCapacity" label="电池电量">
        </el-table-column>
        <el-table-column prop="workingStatus.floatingCharge" label="浮充状态">
        </el-table-column>
        <el-table-column
          prop="workingStatus.totalWorkingTime"
          label="工作总时间"
        >
        </el-table-column>
        <el-table-column prop="workingStatus.workingTime" label="连续工作时间">
        </el-table-column>
        <el-table-column
          prop="workingStatus.connectionState"
          label="网络连接状态"
        >
        </el-table-column>

        <el-table-column
          prop="workingStatus.wsUpdateTime"
          label="最新运行状态采集时间"
          min-width="140"
        >
          <template slot-scope="scope">
            {{
              scope.row.workingStatus.wsUpdateTime == 0 ||
              scope.row.workingStatus.wsUpdateTime == null
                ? ""
                : $moment(scope.row.workingStatus.wsUpdateTime * 1000).format(
                    "YYYY-MM-DD HH:mm:ss"
                  )
            }}</template
          >
        </el-table-column>
        <el-table-column
          prop="workingStatus.signalStrength4g"
          label="4G信号强度"
        >
        </el-table-column>
        <el-table-column
          prop="workingStatus.signalStrength2g"
          label="2G信号强度"
        >
        </el-table-column>
        <el-table-column prop="workingStatus.remainingRam" label="剩余运行内存">
        </el-table-column>
        <el-table-column prop="workingStatus.remainingRom" label="剩余存储内">
        </el-table-column>

        <el-table-column
          prop="positions.updateTime"
          label="最新GPS位置采集时间"
          min-width="140"
        >
        </el-table-column>
        <el-table-column prop="positions.radius" label="半径">
        </el-table-column>
        <el-table-column prop="positions.latitude" label="维度">
        </el-table-column>
        <el-table-column prop="positions.longitude" label="经度">
        </el-table-column>
      </el-table>
      <div class="pageNation">
        <el-pagination
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :current-page="page"
          :page-size="pageSize"
          layout="sizes, prev, pager, next, jumper,total"
          :total="total"
          background
        >
        </el-pagination>
      </div>
    </div>
    <!-- <ipDialog ref="ipDialogref"></ipDialog>
    <photoDialog ref="photoDialogref"></photoDialog> -->
  </div>
</template>
<script>
import {
  gettermAllList,
  getSearchInfo,
  getProtocolList,
  getActivityApi,
} from "@/utils/api/reportApi";
// import ipDialog from "./components/ipDialog";
// import photoDialog from "./components/termAllList";
export default {
  name: "photoStatisBox",
  components: {
    // ipDialog,
    // photoDialog,
  },
  props: ["statusFlag"],
  data() {
    return {
      roleName: "",
      pickerOptions: {
        disabledDate(date) {
          return date.getTime() > Date.now(); // 禁用大于今天的日期
        },
      },
      termAllList: [],
      dyOptions: [{ id: -1, name: "全部" }], //电压数据
      xlOptions: [{ id: -1, name: "全部" }], //线路数据
      gtOptions: [{ id: -1, name: "全部" }], //杆塔数据
      activityOptions: [{ id: -1, name: "全部" }], //活动
      onlineOptions: [
        { id: -1, name: "全部" },
        { id: 1, name: "在线" },
        { id: 0, name: "离线" },
      ],
      protocolOptions: [{ id: -1, name: "全部" }], //规约数据
      formdata: {
        dyid: -1,
        lineid: -1,
        towerid: -1,
        isonline: -1,
        protocol: -1,
        activityId: -1,
        starttime: new Date(new Date().setHours(0, 0, 0, 0)).getTime(), // 设置开始时间为当天凌晨00:00:00的时间戳
        endtime: new Date(new Date().setHours(23, 59, 59, 0)).getTime(), // 设置结束时间为当天夜晚23:59:59的时间戳
        search: "",
      },
      page: 1, // 当前页数
      pageSize: 20, // 每页数量
      total: 0, //总条数
      termLoading: false,
      drawerSearch: false,
      tableheight: "100%",
    };
  },
  created() {
    this.roleName = localStorage.getItem("userName");
  },
  mounted() {
    // if (this.statusFlag) {
    //   this.getSearchdy();
    //   this.getProtocol();
    //   this.getactiveList();
    // }
  },
  watch: {},
  methods: {
    initfn() {
      this.getSearchdy();
      this.getProtocol();
      this.getactiveList();
    },
    //获取电压信息
    getSearchdy() {
      getSearchInfo({ type: 1 })
        .then((res) => {
          this.dyOptions = this.dyOptions.concat(res.data.list);
          console.log(this.dyOptions);
          this.getSearchxl();
        })
        .catch((err) => {});
    },
    //获取线路数据
    getSearchxl() {
      getSearchInfo({ type: 2, id: this.formdata.dyid })
        .then((res) => {
          this.xlOptions = this.xlOptions.concat(res.data.list);
          this.getSearchgt();
        })
        .catch((err) => {});
    },
    //获取杆塔数据
    getSearchgt() {
      getSearchInfo({ type: 3, id: this.formdata.lineid })
        .then((res) => {
          this.gtOptions = this.gtOptions.concat(res.data.list);
          this.onSubmit();
        })
        .catch((err) => {});
    },
    getProtocol() {
      //获取规约版本
      getProtocolList()
        .then((res) => {
          console.log(res);
          this.protocolOptions = this.protocolOptions.concat(res.data.list);
          this.formdata.protocol = this.protocolOptions[0].id;
        })
        .catch((err) => {});
    },
    //获取活动列表
    getactiveList() {
      getActivityApi()
        .then((res) => {
          console.log(res);
          // this.activityOptions = this.activityOptions.concat(res.data.list);
          let activeArr = res.data.map((item) => ({
            id: item.id,
            name: item.title,
          }));
          this.activityOptions = this.activityOptions.concat(activeArr);
          this.formdata.activityId = this.activityOptions[0].id;
          // this.formdata.activityId =
          // JSON.parse(localStorage.getItem("activeId")) !== null
          //   ? JSON.parse(localStorage.getItem("activeId"))
          //   : this.activityOptions[0].id;
        })
        .catch((err) => {});
    },
    changeActive(val) {
      //localStorage.setItem("activeId", JSON.stringify(val));
    },
    onSubmit() {
      if (this.formdata.starttime > this.formdata.endtime) {
        return this.$message({
          duration: 1500,
          showClose: true,
          message: "开始日期不能大于结束日期",
          type: "warning",
        });
      }
      this.page = 1;
      this.getAllFun();
    },
    //获取带有图片信息的内容
    getAllFun() {
      console.log(this.formdata);
      this.termLoading = true;
      let params = {
        dataFlag: "PSOCB", //全要
        start: this.formdata.starttime / 1000,
        end: this.formdata.endtime / 1000,
        pageNum: this.page,
        pageSize: this.pageSize,
      };

      // 根据条件添加参数
      if (this.formdata.dyid !== -1) {
        params.dyId = this.formdata.dyid;
      }
      if (this.formdata.lineid !== -1) {
        params.lineId = this.formdata.lineid;
      }
      if (this.formdata.towerid !== -1) {
        params.towerId = this.formdata.towerid;
      }
      if (this.formdata.isonline !== -1) {
        params.isonline = this.formdata.isonline;
      }
      if (this.formdata.protocol !== -1) {
        params.protocol = this.formdata.protocol;
      }
      if (this.formdata.activityId !== -1) {
        params.activityId = this.formdata.activityId;
      }
      if (this.formdata.search !== "") {
        params.cmdid = this.formdata.search;
      }
      setTimeout(() => {
        gettermAllList(params)
          .then((res) => {
            console.log(res);
            this.termAllList = res.data.list;
            this.total = res.data.total;
            this.termLoading = false;
          })
          .catch((err) => {
            this.termLoading = false;
          });
      }, 100);
    },

    //点击分页
    handleCurrentChange(val) {
      this.page = val;
      this.getAllFun();
    },

    //每页条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.getAllFun();
    },
    // //点击装置查看历史图片
    // handleShowPhoto(row) {
    //   let params = this.formdata;
    //   this.$refs.photoDialogref.display(row, params);
    // },
    // //点击最后心跳时间获取历史 ip和端口
    // handleShowIp(row) {
    //   this.$refs.ipDialogref.display(row);
    // },
    showSearch() {
      this.drawerSearch = !this.drawerSearch;
      console.log(this.drawerSearch);

      this.$nextTick(() => {
        if (this.drawerSearch) {
          // 如果抽屉现在是可见的，获取其高度
          console.log(this.$refs.statisRef.offsetHeight);
          const searchBoxHeight = this.$refs.searchref.offsetHeight;
          console.log("搜索框的高度:", searchBoxHeight);
          this.tableheight =
            this.$refs.statisRef.offsetHeight - searchBoxHeight - 36;
          console.log(this.tableheight);
        } else {
          this.tableheight = "100%";
        }
      });
    },
  },
};
</script>
<style lang="less">
.equipmentStatusBox {
  height: 100%;
  .searchBox {
    .dyclass {
      .el-select {
        width: 120px;
      }
    }
    .xlclass {
      .el-select {
        width: 150px;
      }
    }
    .gtclass {
      .el-select {
        width: 180px;
      }
    }
    .tdclass {
      .el-select {
        width: 80px;
      }
    }
    .dateclass {
      .el-date-editor.el-input,
      .el-date-editor.el-input__inner {
        width: 190px;
      }
    }
    .isonLineClass {
      .el-form-item__content {
        width: 120px;
      }
    }
  }
  .deviceTable {
    height: calc(100% - 32px);
    //background: #fcc;
    .el-table {
      .cell {
        text-align: center;
        vertical-align: middle; /* 如果需要垂直居中 */
      }
    }
    .el-table {
      color: #000;
      thead {
        color: #000;
        font-weight: bold;
      }
      th.el-table__cell {
        background: #dcdcdc !important;
      }
      td.el-table__cell,
      th.el-table__cell.is-leaf {
        border-bottom: 1px solid #dcdcdc;
      }
      .el-table__cell {
        border-right: 1px solid #dcdcdc;
      }
    }
  }
}
</style>
