<template>
  <div class="previewContain" v-loading="previewLoading">
    <!-- {{ previewData }} -->
    <!-- {{ previewData.name }}______{{ previewData.id }} -->
    <div class="pictureBox">
      <el-row
        v-for="i in rowNum"
        :key="'row-' + i"
        :style="{ height: 100 / rowNum + '%' }"
      >
        <el-col
          :span="24 / colNum"
          v-for="(item, index) in picList"
          :key="'col-' + index"
          v-if="index < colNum * i && index >= colNum * (i - 1)"
        >
          <div class="imgcloum" @click="handleTowerPic(item)">
            <!-- 首先判断是否是图片mediatype=0,并且path不为空  展示图片 -->
            <img
              :src="item.path + '!1366x768'"
              v-if="item.mediatype == 0 && item.path !== null"
              :id="'img' + index"
              :ref="'imgref' + index"
            />
            <!-- 判断是否是视频mediatype=1,并且path不为空  展示图片 -->
            <img
              v-else-if="item.mediatype == 1 && item.thumb !== null"
              :src="item.thumb + '!1366x768'"
            />
            <!-- 判断是否是视频mediatype=bull,并且path为空  展示暂无图片 -->
            <img
              v-else-if="item.mediatype == null"
              src="../../assets/img/nodatapic2.jpg"
            />

            <canvas
              :ref="'canvansref' + index"
              :id="'canvas_' + index"
              class="canvasAll"
            ></canvas>
            <div class="picInfo">
              <p>
                {{ item.linename }}-{{ item.towername }}-（{{
                  item.alias !== null && item.alias !== ""
                    ? item.alias
                    : item.channnelname
                }}）
              </p>
              <p class="timeinfo" v-if="item.mediatype !== null">
                {{ $moment(item.photoTime).format("YYYY-MM-DD HH:mm:ss") }}
              </p>
              <p class="timeinfo" v-else>无抓拍</p>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <div class="pageNation" v-if="picList.length !== 0">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-size="pageSize"
        layout=" prev, pager, next, jumper,total"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { getTowerAndPhotoList } from "@/utils/api/index";

export default {
  props: ["previewData"],
  data() {
    return {
      rowNum: 4,
      colNum: 4,
      page: 1, // 当前页数
      pageSize: 16, // 每页数量
      totalPage: 0, //总页数
      total: 0, //总条数
      picList: [], //图片列表数据
      id: "",
      type: "",
      previewLoading: false,
    };
  },
  mounted: function () {},
  methods: {
    //获取所有杆塔图片列表
    getPicList(id, type, page) {
      this.id = id;
      this.type = type;
      this.page = page;
      console.log(id, type);
      this.previewLoading = true;
      this.picList = [];
      getTowerAndPhotoList({
        id: id,
        type: type,
        pageindex: page,
        pagesize: this.rowNum * this.colNum,
      })
        .then((res) => {
          this.previewLoading = false;
          console.log(res);
          this.picList = res.data.list;
          this.totalPage = res.data.totalpage;
          this.total = res.data.total;
          this.$nextTick(() => {
            this.drawlineListAll(this.picList);
          });
        })
        .catch((err) => {
          console.log(err); //代码错误、请求失败捕获
        });
    },
    handleTowerPic(val) {
      console.log(val);
      this.$parent.jumpTowerPic(val);
      console.log("调用父组件方法");
    },
    //点击分页
    handleCurrentChange(val) {
      this.page = val;
      this.picList = [];
      this.getPicList(this.id, this.type, this.page);
    },
    //绘制所有的图片告警区域
    drawlineListAll(val) {
      console.log(val);
      for (let i = 0; i < val.length; i++) {
        if (val[i].isAlarm == 1) {
          console.log(i);
          console.log(document.getElementById("canvas_" + i));
          console.log(document.getElementById("img" + i));
          this.canvas = document.getElementById("canvas_" + i);
          this.imgpic = document.getElementById("img" + i);
          this.textInfo = val[i].alarmlist[0].enname;
          console.log(this.textInfo);
          console.log(this.imgpic.offsetWidth);
          console.log(this.canvas);
          this.canvas.width = this.imgpic.offsetWidth; //设置画布大小
          this.canvas.height = this.imgpic.offsetHeight; //设置画布大小
          console.log(this.canvas.width, this.canvas.height);
          this.ctx = this.canvas.getContext("2d");
          this.ctx.beginPath();
          //设置线条颜色，必须放在绘制之前
          this.ctx.strokeStyle = "#ff0000";
          // 线宽设置，必须放在绘制之前
          this.ctx.lineWidth = 1;

          // 绘制矩形
          var scaleX = this.canvas.width / val[i].alarmlist[0].photoWidth, //this.iw图片的宽
            scaleY = this.canvas.height / val[i].alarmlist[0].photoHeight; //this.ih图片的高
          console.log(scaleX, scaleY);
          var top = val[i].alarmlist[0].x * scaleX,
            left = val[i].alarmlist[0].y * scaleY,
            width = val[i].alarmlist[0].width * scaleX,
            height = val[i].alarmlist[0].height * scaleY;
          console.log(top, left, width, height);
          // this.ctx.strokeStyle = "red"; //线的颜色
          this.ctx.strokeRect(top, left, width, height); //绘制路径矩形
          //this.ctx.strokeRect(val[i].x, val[i].y, val[i].width, val[i].height);
          this.ctx.font = "16px normal";
          this.ctx.fillStyle = "#ff0000";
          //this.ctx.fillText(this.textInfo, top, left + 24);
          //距离上面有空间
          if (left > 32) {
            this.ctx.fillText(this.textInfo, top, left - 4);
          } else if (this.canvas.height - left + height > 32) {
            this.ctx.fillText(this.textInfo, top, height - 4);
          }
          this.ctx.closePath();
          this.ctx.stroke();
        } else {
          console.log("没有告警");
        }
      }
    },
  },
};
</script>

<style lang="less">
.previewContain {
  width: 100%;
  padding: 16px;
  height: auto;
  //background: #fcc;
  .pictureBox {
    height: calc(100% - 38px);
    //background-color: aquamarine;
    .el-col {
      height: 100%;
      padding-right: 0.3%;
      padding-left: 0.3%;
      padding-top: 0.3%;
      padding-bottom: 0.3%;
      border-radius: 4px;
      overflow: hidden;
    }
    .imgcloum {
      width: 100%;
      height: 100%;
      //   background: #f00;
      cursor: pointer;
      position: relative;
      border: 1px solid transparent;
      border-radius: 4px;
      overflow: hidden;
      &:hover {
        border: 1px solid #169e8c;
      }
      img {
        width: 100%;
        height: 100%;
      }
      .canvasAll {
        position: absolute;
        width: 100%;
        height: 100%;
        z-index: 2;
        left: 0;
        top: 0;
        pointer-events: none;
      }
      .picInfo {
        position: absolute;
        background: #169e8ca1;
        color: #fff;
        height: auto;
        font-size: 12px;
        left: 0;
        bottom: 0;
        width: 100%;
        text-align: center;
        z-index: 4;
        .timeinfo {
          margin-top: 4px;
          margin-bottom: 4px;
        }
      }
    }
  }
}
</style>
