<template>
  <div class="stk">
    <div class="stk_head">
      <div :class="viewLibTab == 0 ? 'ac' : ''" @click="onChangeViewLibTab(0)">{{ $t('controlMenu.视图') }}</div>
      <div :class="viewLibTab == 1 ? 'bc' : ''" @click="onChangeViewLibTab(1)">{{ $t('controlMenu.机载') }}</div>
    </div>
    <div class="content">
      <div class="head">
        <div class="aitype">
          <el-select
            popper-class="mmc"
            v-model="aiType"
            :placeholder="$t('controlMenu.全部')"
            @change="onChangeAiType"
            v-if="viewLibTab == 0 && fileType == $t('controlMenu.图片')"
            size="small"
          >
            <el-option
              v-for="item in aiTypeList"
              :key="item.typeId"
              :label="item.title"
              :value="item.typeId"
            ></el-option>
          </el-select>
          <el-checkbox
            v-if="viewLibTab === 1"
            :indeterminate="isIndeterminate"
            v-model="checkAll"
            @change="handleCheckAllChange"
          >{{ $t('controlMenu.全选') }}</el-checkbox>
        </div>
        <div class="select">
          <el-radio-group v-model="fileType" @change="onChangeFileType" size="small">
            <el-radio-button :label="$t('controlMenu.图片')"></el-radio-button>
            <el-radio-button :label="$t('controlMenu.视频')"></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]">{{ $t('controlMenu.更多') }}></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 === $t('controlMenu.图片')"
                        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)">{{ $t('controlMenu.下载') }}</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)">{{ $t('controlMenu.下载') }}</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;">{{ $t('controlMenu.返回') }}></el-button>
                </div>
                <div class="list-item-imgs">
                  <template v-for="(img, i) in item.imgList">
                    <MMCImage
                      v-if="fileType === $t('controlMenu.图片')"
                      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)">{{ $t('下载') }}</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)">{{ $t('下载') }}</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 === $t('controlMenu.图片')"
                        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)">{{ $t('下载') }}</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)">{{ $t('下载') }}</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 === $t('controlMenu.图片')"
                        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"
        >{{ $t('controlMenu.转存') }}</el-button>
        <el-button
          type="danger"
          size="small"
          @click="onDel"
          :disabled="checkList.length === 0"
          :loading="delLoading"
        >{{ $t('common.删除') }}</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, // 图片类型
      //视图库类型
      fileType: this.$t('controlMenu.图片'), //视图文件类型 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"]),
    // AI类型列表
    aiTypeList() {
      return [
        {
          typeId: 1,
          title: this.$t('controlMenu.人车识别'),
        },
        {
          typeId: 2,
          title: this.$t('controlMenu.烟火识别'),
        },
        {
          typeId: 4,
          title: this.$t('controlMenu.行人识别'),
        },
        {
          typeId: 5,
          title: this.$t('controlMenu.机动车识别'),
        },
        {
          typeId: 6,
          title: this.$t('controlMenu.两轮车识别'),
        },
        {
          typeId: 7,
          title: this.$t('controlMenu.三轮车识别'),
        },
        {
          typeId: 8,
          title: this.$t('controlMenu.挖掘机识别'),
        },
        {
          typeId: 9,
          title: this.$t('controlMenu.车牌识别'),
        },
        {
          typeId: 10,
          title: this.$t('controlMenu.人脸识别'),
        },
        {
          typeId: 11,
          title: this.$t('controlMenu.口罩识别'),
        },
        {
          typeId: 14,
          title: this.$t('controlMenu.烟雾识别'),
        },
        {
          typeId: 15,
          title: this.$t('controlMenu.火焰识别'),
        },
        {
          typeId: 17,
          title: this.$t('controlMenu.占道经营'),
        },
        {
          typeId: 22,
          title: this.$t('controlMenu.船舶识别'),
        },
        {
          typeId: 23,
          title: this.$t('controlMenu.漏油识别'),
        },
        {
          typeId: 24,
          title: this.$t('controlMenu.头盔识别'),
        },
        {
          typeId: 25,
          title: this.$t('controlMenu.管线识别'),
        },
        {
          typeId: 26,
          title: this.$t('controlMenu.路面裂缝'),
        },
        {
          typeId: 27,
          title: this.$t('controlMenu.顶棚违建'),
        },
        {
          typeId: 28,
          title: this.$t('controlMenu.门牌广告'),
        },
        {
          typeId: 29,
          title: this.$t('controlMenu.井盖识别'),
        },
        {
          typeId: 30,
          title: this.$t('controlMenu.搅拌车识别'),
        },
        {
          typeId: 31,
          title: this.$t('controlMenu.油罐车识别'),
        },
        {
          typeId: 32,
          title: this.$t('controlMenu.渣土车识别'),
        },
        {
          typeId: 33,
          title: this.$t('controlMenu.排气口识别'),
        },
        {
          typeId: 34,
          title: this.$t('controlMenu.人流识别'),
        },
        {
          title: this.$t('controlMenu.裸土识别'),
          typeId: 35,
        },
      ];
    },
    // 全部图片
    imgListAll() {
      let list = [];
      this.photoList.forEach((item) => {
        item.imgList.forEach((img) => {
          list.push(img);
        });
      });
      return list;
    },
  },
  watch: {
  },
  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 === this.$t('controlMenu.图片') ? 0 : 1,
        deviceId: this.uav.deviceId,
        type: parseInt(this.viewLibTab),
        detectorType: this.aiType || undefined,
      });
      let photoList = res.data.list;
      if (photoList.length === 0) {
        this.noMore = true;
      } else {
        photoList.forEach((item) => {
          item.imgList = item.viewLibraryRespVOS.map((item1) => {
            return item1.aiImageUrl || item1.fileUrl;
          });
        });
        this.photoList = this.photoList.concat(photoList);
      }
      // this.photoList = mock;
    },
    /**
     * 视图库tab更改
     */
    async onChangeViewLibTab(index) {
      this.viewLibTab = index;
      this.aiType = 0;
      this.init();
    },
    /**
     * 图片ai类型切换
     */
    onChangeAiType() {
      this.init();
    },
    /**
     * 更改视图中文件类型
     */
    onChangeFileType(i) {
      this.fileType = i;
      this.aiType = 0;
      this.checkList = [];
      this.isIndeterminate = false;
      this.checkAll = false;
      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("/");
          req.push(
            ViewLibrary.create({
              fileUrl: src,
              fileType: this.fileType === this.$t('controlMenu.图片') ? 0 : 1,
              deviceId: this.uav.deviceId,
            })
          );
        });
        await Promise.all(req);
        this.$message.success(this.$t('controlMenu.转存完成'));
      } catch (e) {
        this.$message.error(this.$t('controlMenu.转存失败'));
      }

      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(this.$t('controlMenu.删除完成'));
      } catch (e) {
        this.$message.error(this.$t('controlMenu.删除失败'));
      }

      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 {
            display: initial;
            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;
          }
        }

        .list-item__img-download {
          display: none;
        }
      }
    }
    .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>