<template>
  <div class="stk">
    <div class="stk_head">
      <div :class="viewLibTab == 0 ? 'ac' : ''" @click="onChangeViewLibTab(0)">视图</div>
      <div :class="viewLibTab == 1 ? 'bc' : ''" @click="onChangeViewLibTab(1)">机载</div>
    </div>
    <div class="content">
      <div class="head">
        <div class="aitype">
          <el-select
            popper-class="mmc"
            v-model="aiType"
            placeholder="全部"
            @change="onChangeAiType"
            v-if="viewLibTab == 0"
          >
            <el-option
              v-for="item in aiTypeList"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>
        <div class="select">
          <!-- <div class="btns" :class="fileType == 0 ? 'bg' : ''" @click="onChangeFileType(0)">图片</div>
          <div class="btns" :class="fileType == 1 ? 'bg' : ''" @click="onChangeFileType(1)">视频</div>-->
          <el-radio-group v-model="fileType">
            <el-radio-button label="图片"></el-radio-button>
            <el-radio-button label="视频"></el-radio-button>
          </el-radio-group>
          <span class="iconfont icon-gengxin" @click="init"></span>
        </div>
      </div>
      <!--  <p v-if="viewLibTab == 1 && fileType == 1">
        如遇视频无法播放，请
        <a href="http://32.128.6.52:4500/11.11平台部署/ChromeStandaloneSetup64.exe">下载最新谷歌</a>
      </p>-->
      <div class="lists">
        <ul v-if="photoListDate" class="infinite-list" style="overflow:auto">
          <li v-for="item in photoListDate" class="infinite-list-item">
            <template v-if="item.imgList.length > 0">
              <div class="list-item-date">
                <div>{{item.date}}</div>
                <el-button type="text" size="small" @click="photoListDate = null;">返回></el-button>
              </div>
              <div class="list-item-imgs">
                <template v-for="(img, i) in item.imgList">
                  <MMCImage
                    class="list-item__img"
                    :key="i"
                    fit="cover"
                    :src="img || img"
                    :preview-src-list="item.imgList"
                    :initial-index="i"
                    @error="onImgError"
                    crossorigin
                  >
                    <div class="list-item__img-download" @click="onDownload(img)">下载</div>
                  </MMCImage>
                </template>
              </div>
            </template>
          </li>
        </ul>
        <ul
          v-else
          class="infinite-list"
          v-infinite-scroll="onLoadList"
          style="overflow:auto"
          infinite-scroll-immediate
          :infinite-scroll-disabled="noMore"
        >
          <li v-for="item in photoList" class="infinite-list-item">
            <template v-if="item.imgList.length > 0">
              <div class="list-item-date">
                <div>{{item.date}}</div>
                <el-button type="text" size="small" @click="photoListDate = [item]">更多></el-button>
              </div>
              <div class="list-item-imgs">
                <template v-for="(img, i) in item.imgList">
                  <MMCImage
                    v-if="i < 3"
                    class="list-item__img"
                    :key="i"
                    fit="cover"
                    :src="img || img"
                    :preview-src-list="item.imgList"
                    :initial-index="i"
                    @error="onImgError"
                    crossorigin
                  >
                    <div class="list-item__img-download" @click="onDownload(img)">下载</div>
                  </MMCImage>
                </template>
              </div>
            </template>
          </li>
        </ul>
      </div>
      <div class="foot" v-if="viewLibTab == 1 || fileType2 == 2">
        <div class="btns blue" v-if="selectAll">全选</div>
        <div class="btns blue" v-else>取消全选</div>
        <div class="btns" v-if="viewLibTab == 1">转存</div>
        <div class="btns red" style>删除</div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  flightTaskAPI,
  Control_API,
  ViewLibrary,
} from "../../../../../../../../api";
import { mapState } from "vuex";
import MMCImage from "../../../../../../../../components/image";
import mock from "./mock";
import download from 'downloadjs';

export default {
  name: "ViewLib",
  components: {
    MMCImage,
  },
  inject: ["rootNode"],
  data() {
    return {
      checkList: [], // 多选结果
      selectAll: false, //全选
      viewLibTab: 0, // 视图库tab索引, 0: 视图, 1: 机载
      aiType: 1, // 图片类型
      aiTypeList: [
        // 普通图片或ai图片选项
        {
          id: 1,
          value: "图片",
        },
        {
          id: 2,
          value: "AI图片",
        },
        // {
        //   id: 0,
        //   value: "全部",
        // },
        // {
        //   id: 1,
        //   value: "人脸识别",
        // },
        // {
        //   id: 2,
        //   value: "车辆识别",
        // },
        // {
        //   id: 9,
        //   value: "人流识别",
        // },
        // {
        //   id: 4,
        //   value: "游泳识别",
        // },
        // {
        //   id: 5,
        //   value: "跌倒检测",
        // },
        // {
        //   id: 6,
        //   value: "异物检测",
        // },
        // {
        //   id: 7,
        //   value: "火焰烟雾",
        // },
        // {
        //   id: 8,
        //   value: "车辆朝向",
        // },
        // {
        //   id: 9,
        //   value: "车辆类型检测",
        // },
        // {
        //   id: 10,
        //   value: "车道分割线",
        // },
        // {
        //   id: 11,
        //   value: "绿化带分割",
        // },
        // {
        //   id: 10,
        //   value: "车牌识别",
        // },
      ], //视图库类型
      fileType: "图片", //视图文件类型 0：图片；1：视频
      fileType2: 0, //机载文件类型
      photoList: mock /* [] */, //资源列表
      photoListDate: null, //某日期的所有资源
      moreShow: false, //显示更多,
      page: {
        pageNo: 0,
        pageSize: 10,
      },
      noMore: false, //是否没有更多数据了
      
    };
  },
  computed: {
    ...mapState("MMCFlightControlCenter/uav", ["uav"]),
    ...mapState("MMCFlightControlCenter", ["baseUrl"]),
  },
  watch: {
    fileType() {
      this.init();
    },
  },
  mounted() {
    // this.init();
  },
  methods: {
    async init() {
      this.page.pageNo = 1;
      this.photoList = [];
      this.noMore = false;
      this.getPage();
    },
    async getPage() {
      let res = await ViewLibrary.page({
        ...this.page,
        fileType: this.fileType === "图片" ? 0 : 1,
        deviceId: this.uav.deviceId,
        type: parseInt(this.viewLibTab),
      });
      let photoList = res.data.list;
      if (photoList.length === 0) {
        this.noMore = true;
      } else {
        photoList.forEach((item) => {
          item.imgList = item.viewLibraryRespVOS
            .filter((item1) => {
              // 过滤掉对应筛选下没图片链接的数据
              return this.aiType == 1 ? item1.fileUrl : item1.aiImageUrl;
            })
            .map((item1) => {
              return this.aiType == 1
                ? `${item1.fileUrl}`
                : `${item1.aiImageUrl}`;
            });
        });
        this.photoList = this.photoList.concat(photoList);
      }
    },
    /**
     * 视图库tab更改
     */
    async onChangeViewLibTab(index) {
      this.viewLibTab = index;
    },
    /**
     * 图片ai类型切换
     */
    onChangeAiType() {
      this.init();
    },
    /**
     * 更改视图中文件类型
     */
    onChangeFileType(i) {
      this.fileType = i;
      this.init();
    },
    /**
     * 更改机载中文件类型
     */
    onChangeFileType2(i) {
      this.fileType2 = i;
      this.init();
    },
    onDownload(url) {
      download(url);
    },
    /**
     * 加载下一页数据
     */
    onLoadList() {
      this.page.pageNo++;
      this.getPage();
    },
    onImgError(e) {
      /* if(e?.target){
        e.target.crossOrigin  = '';
      } */
    },
  },
};
</script>

<style lang="scss" scoped>
.stk::v-deep {
  position: absolute;
  right: 70px;
  top: 190px;
  width: 423px;
  height: 318px;
  background: #222222;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  display: flex;
  flex-direction: column;

  .stk_head {
    display: flex;
    cursor: pointer;
    background: #3c3c3c;
    border-radius: 10px 10px 0px 0px;
    flex-shrink: 0;

    div {
      padding: 0 8px;
      // width: 243px;
      width: 100%;
      text-align: center;
      line-height: 36px;
      font-size: 16px;
      font-family: YouSheBiaoTiHei;
      color: #ffffff;
      text-shadow: 0px 1px 1px rgba(2, 32, 56, 0.2);
      height: 36px;
    }

    .ac {
      background: #3388ff;
      border-radius: 10px 0px 0px 0px;
    }

    .bc {
      background: #3388ff;
      border-radius: 0px 10px 0px 0px;
    }
  }

  .content {
    margin-top: 10px;
    padding: 0 15px;
    display: flex;
    flex-direction: column;
    flex: auto;
    overflow: hidden;

    > p {
      font-size: 14px;
      color: #fff;
      cursor: default;
      a {
        cursor: pointer;
        color: #00ffff;
      }
    }
    .foot {
      flex-shrink: 0;
      margin-top: 12px;
      margin-bottom: 12px;
      display: flex;
      // justify-content: space-between;
      align-items: center;
      gap: 12px;
      .btns {
        width: 64px;
        height: 32px;
        text-align: center;
        line-height: 32px;
        font-size: 14px;
        font-family: MicrosoftYaHei;
        color: #ffffff;
        background: url("../../assets/images/zc.png") no-repeat;
        background-size: 100% 100%;
        cursor: pointer;
      }
      .blue {
        background: url("../../assets/images/qx.png") no-repeat;
        background-size: 100% 100%;
      }
      .red {
        background: url("../../assets/images/red.png") no-repeat;
        background-size: 100% 100%;
      }
    }
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex-shrink: 0;

      .btns {
        width: 64px;
        height: 32px;
        text-align: center;
        line-height: 32px;
        font-size: 14px;
        font-family: MicrosoftYaHei;
        color: #ffffff;
        background: url("../../assets/images/zc.png") no-repeat;
        background-size: 100% 100%;
        cursor: pointer;
      }
      .bg {
        background: url("../../assets/images/qx.png") no-repeat;
        background-size: 100% 100%;
      }

      .blue {
        background: url("../../assets/images/qx.png") no-repeat;
        background-size: 100% 100%;
      }

      .select {
        display: flex;
        align-items: center;
        gap: 10px;

        .iconfont {
          color: #fff;
        }

        .btns {
          margin-right: 16px;
          width: 64px;
          height: 32px;
          text-align: center;
          line-height: 32px;
          font-size: 14px;
          font-family: MicrosoftYaHei;
          color: #ffffff;
          background: url("../../assets/images/zc.png") no-repeat;
          background-size: 100% 100%;

          // &:hover {
          //   background: url("../../assets/images/qx.png") no-repeat;
          //   background-size: 100% 100%;
          // }
        }
        .bg {
          background: url("../../assets/images/qx.png") no-repeat;
          background-size: 100% 100%;
        }

        .el-select {
          width: 80px;
          height: 32px;

          .el-input--suffix {
            width: 100%;
            height: 100%;

            .el-input__inner {
              width: 100%;
              height: 100%;
            }
          }

          .el-input__suffix {
            display: flex;
            align-items: center;
          }
        }
      }
    }

    .lists {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      overflow: hidden;
      margin-top: 10px;
      margin-bottom: 10px;

      .infinite-list {
        height: 100%;
      }

      .list-item-date {
        font-weight: 400;
        font-size: 14px;
        color: #878787;
        line-height: 22px;
        margin-bottom: 6px;
        display: flex;
        justify-content: space-between;
      }

      .list-item-imgs {
        display: flex;
        gap: 11px;
        flex-wrap: wrap;
      }

      .list-item__img {
        width: 122px;
        height: 67px;
        position: relative;

        &:hover {
          .list-item__img-download {
            position: absolute;
            right: 0;
            bottom: 0;
            width: 38px;
            height: 24px;
            background: #000000;
            border-radius: 2px 0px 2px 0px;
            opacity: 0.5;
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 14px;
            color: #ffffff;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
          }
        }
      }
    }
    .boxs {
      display: flex;
      flex-wrap: wrap;
      align-content: flex-start;
      height: 100%;
    }
    .boxst {
      width: 100%;
      display: flex;
      flex-direction: column;
      height: 100%;
      .name {
        margin: 0 8px;
        display: flex;
        justify-content: space-between;
        color: #fff;
      }

      .images {
        display: flex;
        flex-wrap: wrap;
        align-content: flex-start;

        .box {
          position: relative;
          width: 135px;
          height: 88px;
          background: #ffffff;
          border-radius: 2px;
          margin-left: 8px;
          margin-bottom: 14px;

          .el-image {
            width: 138px;
            height: 88px;
            background: #ffffff;
            border-radius: 2px;
          }
          img {
            width: 135px;
            height: 88px;
          }
          .down {
            position: absolute;
            bottom: 0;
            right: -3px;
            width: 30px;
            height: 20px;
            text-align: center;
            line-height: 20px;
            background: rgba($color: #000000, $alpha: 0.6);
            color: #fff;
            font-size: 12px;
            display: none;
          }
          &:hover {
            .down {
              display: block;
            }
          }
        }

        .boxv2 {
          width: 430px;
          height: 120px;
          margin-left: 0;

          video {
            width: 430px;
            height: 120px;
          }
        }
      }
    }
  }

  .down {
    cursor: pointer;
  }

  .icon-gengxin {
    cursor: pointer;
  }
}
</style>