<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="时间:">
        <el-date-picker
          style="width: 300px"
          v-model="dateRange"
          size="small"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="完成状态:">
        <el-select
          v-model.trim="queryParams.status"
          size="small"
          style="width: 160px"
          placeholder="请选择完成状态"
        >
          <el-option value="0" label="未完成"></el-option>
          <el-option value="1" label="已完成"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="right">
        <el-button plain size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" size="mini" @click="handleQuery"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="20" v-loading="loading2" style="margin-top: 10px">
      <el-col :span="2.4">
        <el-col class="event">
          记录数量<span class="color1 colorc">{{ collectList.total }}</span>
        </el-col>
      </el-col>
      <el-col :span="2.4">
        <el-col class="event">
          进行中任务<span class="color2 colorc">{{ collectList.inWork }}</span>
        </el-col>
      </el-col>
      <el-col :span="2.4">
        <el-col class="event">
          超时任务<span class="color3 colorc">{{ collectList.timeOut }}</span>
        </el-col>
      </el-col>
      <el-col :span="2.4">
        <el-col class="event">
          完成任务
          <span class="color4 colorc">{{ collectList.success }}</span>
        </el-col>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="adminList"
      style="margin-top: 20px"
      border
      row-key="planName"
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column width="50"></el-table-column>
      <el-table-column
        label="任务名称"
        align="center"
        prop="planName"
        show-overflow-tooltip
      />
      <el-table-column
        label="维保点位"
        align="center"
        prop="pointName"
        show-overflow-tooltip
      />
      <el-table-column
        label="应完成时间"
        align="center"
        prop="endDate"
        show-overflow-tooltip
      />
      <el-table-column
        label="最近完成时间"
        align="center"
        prop="updateTime"
        show-overflow-tooltip
      />
      <el-table-column
        label="完成情况"
        align="center"
        prop="isComplete"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span :class="scope.row.isComplete == false ? 'color3' : 'color4'">{{
            scope.row.isComplete == false ? "未完成" :  scope.row.isComplete == true ? "已完成" : ""
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="相关责任人"
        align="center"
        prop="dutyName"
        show-overflow-tooltip
      />
      <el-table-column
        label="联系电话"
        align="center"
        prop="dutyPhone"
        show-overflow-tooltip
      />
      <el-table-column label="操作" width="190" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.isComplete && scope.row.updateTime"
            class="lans"
            @click="play(scope.row)"
            >视频回放</el-button
          >
          <el-button v-else class="lans" @click="play(scope.row)" disabled
            >视频回放</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @current-change="handleCurrentChange"
      :page-sizes="[10, 15, 20, 30]"
      @size-change="handleSizeChange"
      :current-page="queryParams.current"
      :page-size="queryParams.size"
      layout="sizes,prev, pager, next"
      :total="totalCount"
    >
    </el-pagination>
    <!-- 视频播放 -->
    <el-dialog :visible.sync="dialogTable2" @close="closeDialog">
      <template slot="title">
        <!-- <div
          class="titleZise"
          style="font-size: 15px; color: #484848; font-weight: 700"
        >
          {{ this.cameraTitle }}
        </div> -->
      </template>
      <video
        class="hlsVideo monitor-height"
        ref="hlsVideo"
        style="width: 100%"
        controls
        preload="true"
        muted
      ></video>
    </el-dialog>
  </div>
</template>
<script>
import Hls from "hls.js";
import { getCollect, getPlanDetail } from "@/api/MochaITOM/record";
import { getTimeFormat } from "@/assets/js/dataFormate.js";
export default {
  name: "TbAlarm",
  data() {
    return {
      piliang: false,
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      totalCount: undefined,
      // 参数表格数据
      adminList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 15,
        name: undefined,
        startTime: undefined,
        endTime: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 日期范围
      dateRange: [],
      //综合数据
      collectList: [],
      loading2: true,
      dialogTable2: false, //视频弹框
      cameraTitle: undefined, //视频标题
    };
  },
  created() {
    this.getList(); //初始化table
    // this.getCollect();//初始化table
  },
  methods: {
    //分页查询
    handleCurrentChange(val) {
      this.queryParams.pageNo = val;
      this.getList();
    },
    //分页查询
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    //强制el-input刷新
    onInput() {
      this.$forceUpdate();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams = {
        name: undefined,
        current: 1,
        size: 15,
        status: undefined,
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      if (this.dateRange.length > 0) {
        this.queryParams.startTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      }
      getPlanDetail(this.queryParams).then((response) => {
        let data = response.data.pageList;
        let tempArr = [];
        var newArr = [];
        data.forEach((i, index) => {
          if (tempArr.indexOf(i.planName) === -1) {
            newArr.push({
              planName: i.planName,
              children: [i],
            });
            // if(i.updateTime){
            //   i.isComplete = true
            // }
            tempArr.push(i.planName);
          } else {
            // if(i.updateTime){
            //   i.isComplete = true
            // }
            newArr[tempArr.indexOf(i.planName)].children.push(i);
          }
        });
        this.adminList = newArr;
        this.totalCount = response.data.totalCount;
        this.loading = false;
      });
      /** 综合列表列表 */
      getCollect(this.queryParams).then((response) => {
        this.collectList = response.data;
        this.loading2 = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      exportMaintianLog(this.queryParams).then((response) => {
        this.exportLoading = false;
        document.getElementById("xz").href = BASEPATH() + response.data;
        document.getElementById("xz").download = response.data;
        document.getElementById("xz").click();
      });
    },

    /** 视频播放 **/
    //播放按钮
    play(row) {
      let start = getTimeFormat(row.updateTime) - 20;
      let end = start + 40;
      let timeC = Number(end - start);
      let deviceId = row.channleId.split("$")[0];
      let id = row.channleId.split("$")[row.channleId.split("$").length - 1];
      this.destroyHls();
      this.cameraTitle = row.dhDeviceName;
      let url = `http://${row.nvrIp}:${row.hlsPort}/vod/device/cameraid/${deviceId}%24${id}/substream/1/recordtype/1/totallength/${timeC}/begintime/${start}/endtime/${end}.m3u8`;
      this.loadVideoFn(url);
    },
    destroyHls: function () {
      if (this.hls) {
        this.$refs.hlsVideo.pause();
        this.hls.destroy();
        this.hls = null;
      }
    },
    loadVideoFn(url) {
      this.dialogTable2 = true;
      setTimeout(() => {
        if (Hls.isSupported()) {
          this.hls = new Hls();
          this.hls.loadSource(url);
          this.hls.attachMedia(this.$refs.hlsVideo);
          this.hls.on(Hls.Events.MANIFEST_PARSED, () => {
            console.log("加载成功");
            this.$refs.hlsVideo.play();
          });

          this.hls.on(Hls.Events.ERROR, (event, data) => {
            // console.log(event, data);
            // 监听出错事件
            console.log("加载失败");
          });
        }
      }, 1000);
    },
    //关闭弹框
    closeDialog() {
      this.destroyHls();
    },
     //改变起始页
    handleCurrentChange(val) {
      this.queryParams.current = val
      this.getList()
    },
    //改变每条数
    handleSizeChange(val) {
      this.queryParams.size = val
      this.getList()
    },
  },
};
</script>
<style scoped>
.event {
  border: 1px solid #ccc;
  padding: 1.5vh 0;
  font-size: 0.9vw;
}
.colorc {
  font-size: 1.2vw;
  margin-left: 4px;
}
.color1 {
  color: #6f99fa !important;
}
.color2 {
  color: #de8c54 !important;
}
.color3 {
  color: #ff5801 !important;
}
.color4 {
  color: #37ff01 !important;
}
.el-range-editor.el-input__inner {
  margin: 0 !important;
}
</style>
