<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"
            size="small"
          >
            <el-option
              v-for="item in aiTypeList"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            ></el-option>
          </el-select>
          <el-checkbox
            v-if="viewLibTab === 1"
            :indeterminate="isIndeterminate"
            v-model="checkAll"
            @change="handleCheckAllChange"
          >全选</el-checkbox>
        </div>
        <div class="select">
          <el-radio-group v-model="fileType" size="small">
            <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">
        <!-- 上传的资源 -->
        <template v-if="viewLibTab === 0">
          <ul v-if="!photoListDate" class="infinite-list" style="overflow:auto">
            <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">
                    <template v-if="i < 3">
                      <MMCImage
                        v-if="fileType === '图片'"
                        class="list-item__img"
                        :key="i"
                        fit="cover"
                        :src="img || img"
                        :preview-src-list="item.imgList"
                        :initial-index="i"
                        crossorigin
                      >
                        <div class="list-item__img-download" @click.stop="onDownload(img)">下载</div>
                      </MMCImage>
                      <LivePlayer
                        v-else
                        class="list-item__img"
                        aspect="fulllscreen"
                        :controls="false"
                        :videoUrl="img"
                        :autoplay="false"
                      >
                        <div class="list-item__img-download" @click.stop="onDownload(img)">下载</div>
                      </LivePlayer>
                    </template>
                  </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 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
                      v-if="fileType === '图片'"
                      class="list-item__img"
                      :key="i"
                      fit="cover"
                      :src="img || img"
                      :preview-src-list="item.imgList"
                      :initial-index="i"
                      crossorigin
                    >
                      <div class="list-item__img-download" @click.stop="onDownload(img)">下载</div>
                    </MMCImage>
                    <LivePlayer
                      v-else
                      class="list-item__img"
                      aspect="fulllscreen"
                      :controls="false"
                      :videoUrl="img"
                      :autoplay="false"
                    >
                      <div class="list-item__img-download" @click.stop="onDownload(img)">下载</div>
                    </LivePlayer>
                  </template>
                </div>
              </template>
            </li>
          </ul>
        </template>
        <!-- 机载的资源 -->
        <el-checkbox-group
          v-else
          v-model="checkList"
          @change="handleCheckedCitiesChange"
          style="height: 100%;"
        >
          <ul v-if="!photoListDate" class="infinite-list" style="overflow:auto">
            <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">
                    <el-checkbox v-if="i < 2" :label="img" :key="img">
                      <MMCImage
                        v-if="fileType === '图片'"
                        class="list-item__img"
                        :key="i"
                        fit="cover"
                        :src="img || img"
                        :preview-src-list="item.imgList"
                        :initial-index="i"
                        crossorigin
                      >
                        <div class="list-item__img-download" @click.stop="onDownload(img)">下载</div>
                      </MMCImage>
                      <LivePlayer
                        v-else
                        class="list-item__img"
                        aspect="fulllscreen"
                        :controls="false"
                        :videoUrl="img"
                        :autoplay="false"
                      >
                        <div class="list-item__img-download" @click.stop="onDownload(img)">下载</div>
                      </LivePlayer>
                    </el-checkbox>
                  </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 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">
                    <!-- 机载带多选 -->
                    <el-checkbox :label="img" :key="img">
                      <MMCImage
                        v-if="fileType === '图片'"
                        class="list-item__img"
                        :key="i"
                        fit="cover"
                        :src="img || img"
                        :preview-src-list="item.imgList"
                        :initial-index="i"
                        crossorigin
                      >
                        <div class="list-item__img-download" @click.stop="onDownload(img)">下载</div>
                      </MMCImage>
                      <LivePlayer
                        v-else
                        class="list-item__img"
                        aspect="fulllscreen"
                        :controls="false"
                        :videoUrl="img"
                        :autoplay="false"
                      >
                        <div class="list-item__img-download" @click.stop="onDownload(img)">下载</div>
                      </LivePlayer>
                    </el-checkbox>
                  </template>
                </div>
              </template>
            </li>
          </ul>
        </el-checkbox-group>
      </div>
      <div class="foot" v-if="viewLibTab == 1">
        <el-button
          type="primary"
          size="small"
          v-if="viewLibTab == 1"
          @click="onTransfer"
          :disabled="checkList.length === 0"
          :loading="transferLoading"
        >转存</el-button>
        <el-button
          type="danger"
          size="small"
          @click="onDel"
          :disabled="checkList.length === 0"
          :loading="delLoading"
        >删除</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { ViewLibrary } from "../../../../../../../../api";
import { mapState } from "vuex";
import MMCImage from "../../../../../../../../components/image";
import mock from "./mock";
import download from "./download";
import LivePlayer from "@liveqing/liveplayer";

export default {
  name: "ViewLib",
  components: {
    MMCImage,
    LivePlayer,
  },
  inject: ["rootNode"],
  data() {
    return {
      checkList: [], // 多选结果
      checkAll: false,
      isIndeterminate: false,
      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：视频
      photoList: [], //资源列表对应的对象
      photoListDate: null, //某日期的所有资源
      moreShow: false, //显示更多,
      page: {
        pageNo: 0,
        pageSize: 10,
      },
      noMore: false, //是否没有更多数据了
      transferLoading: false, //转存中
      delLoading: false, //删除中
    };
  },
  computed: {
    ...mapState("MMCFlightControlCenter/uav", ["uav"]),
    ...mapState("MMCFlightControlCenter", ["baseUrl"]),
    // 全部图片
    imgListAll() {
      let list = [];
      this.photoList.forEach((item) => {
        item.imgList.forEach((img) => {
          list.push(img);
        });
      });
      return list;
    },
  },
  watch: {
    fileType() {
      this.init();
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    handleCheckAllChange(val) {
      this.checkList = val ? this.imgListAll : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.imgListAll.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.imgListAll.length;
    },
    async init() {
      this.page.pageNo = 1;
      this.photoList = [];
      this.noMore = false;
      this.photoListDate = null;
      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) => {
              // 过滤掉对应筛选下没图片链接的数据
              if (this.viewLibTab == 0) {
                return this.aiType == 1 ? item1.fileUrl : item1.aiImageUrl;
              } else {
                return item1.fileUrl;
              }
            })
            .map((item1) => {
              if (this.viewLibTab == 0) {
                return this.aiType == 1 ? item1.fileUrl : item1.aiImageUrl;
              } else {
                return item1.fileUrl;
              }
            });
        });
        this.photoList = this.photoList.concat(photoList);
      }
      // this.photoList = mock;
    },
    /**
     * 视图库tab更改
     */
    async onChangeViewLibTab(index) {
      this.viewLibTab = index;
      this.init();
    },
    /**
     * 图片ai类型切换
     */
    onChangeAiType() {
      this.init();
    },
    /**
     * 更改视图中文件类型
     */
    onChangeFileType(i) {
      this.fileType = i;
      this.init();
    },
    onDownload(url) {
      download(url);
    },
    /**
     * 加载下一页数据
     */
    onLoadList() {
      this.page.pageNo++;
      this.getPage();
    },
    /**
     * 转存
     */
    async onTransfer() {
      this.transferLoading = true;

      try {
        let req = [];
        this.checkList.forEach((img) => {
          // 绝对路径转相对路径
          let arr = img.split('/');
          let src = '/' + arr.slice(4).join('/');
          debugger
          req.push(
            ViewLibrary.create({
              fileUrl: src,
              fileType: this.fileType === '图片' ? 0 : 1,
              deviceId: this.uav.deviceId,
            })
          );
        });
        await Promise.all(req);
        this.$message.success("转存完成");
      } catch (e) {
        this.$message.error("转存失败");
      }

      this.transferLoading = false;
    },
    /**
     * 删除
     */
    async onDel() {
      this.delLoading = true;

      try {
        let req = [];
        this.checkList.forEach((url) => {
          // 找出该url对应的资源id
          let find = null;
          this.photoList.find((item) => {
            find = item.viewLibraryRespVOS.find(
              (item1) => url === item1.fileUrl
            );
            return find;
          });
          req.push(
            ViewLibrary.delete({
              id: find.id,
            })
          );
        });
        await Promise.all(req);

        this.$message.success("删除完成");
      } catch (e) {
        this.$message.error("删除失败");
      }

      this.delLoading = false;
    },
  },
};
</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;
      // justify-content: space-between;
      align-items: center;
      .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>