<template>
  <div class="collections">
    <div class="slebar">
      <div>分类：</div>
      <ul>
        <li @click="menuActive = item.id" :class="{ active: menuActive == item.id }" v-for="(item, i) in menu" :key="i">
          {{ item.name }}
        </li>
      </ul>
    </div>

    <div v-if="list && list.length > 0" class="masonry" v-infinite-scroll="getData" infinite-scroll-disabled="busy"
      :infinite-scroll-immediate-check="true" infinite-scroll-distance="30" v-masonry="containerId" fit-width="true"
      gutter="10" initLayout="true" transition-duration="0.3s" item-selector=".item">
      <div @click="onClickCollection(item)" v-masonry-tile class="item" :class="{ odd: index % 2 != 0 }" v-for="(item, index) in list" :key="index">
        <div class="itemimg">
          <div :style="`background-image:url(${item.thumb})`"></div>
        </div>
        <p class="goodBotInfo">{{ item.name }}</p>
      </div>
    </div>

    <div class="searchNone" style="padding-top: 20px" v-else>
      <img src="@/assets/images/resource/searchNone.svg" alt="" style="width: 70px;" />
      <p style="font-size: 14px;">暂时没有数据</p>
    </div>
  </div>

  <teleport to='body'>
    <div class="fnbar">
      <div class="barsub">
        <mb_select :noradius="true" class="mb_select" @handleOption="data=>currentAge=data.id" :value="currentAge" :options="ages" />
      </div>

      <div class="barsub">
        <mb_select :noradiusAll="true" class="mb_select"  @handleOption="data=>currentType=data.id" :value="currentType"
          :options="types" />
      </div>

      <div class="searchimg" v-show="!showInput">
        <img @click="showInput=true" :src="require('@/assets/images/icon/label_search.png')" alt="">
      </div>

      <transition name="fade">
        <div class="searchcon" v-if="showInput">
          <img :src="require('@/assets/images/icon/label_search.png')" alt="">
          <input v-model="searchKey" type="text" placeholder="请输入藏品名称">
          <span @click="showInput=false,searchKey=''">取消</span>
        </div>
      </transition>

    </div>
  </teleport>

  <transition name="fade">
    <teleport to='body'>
      <showCollection v-if="activeCollection" :item="activeCollection" />
    </teleport>
  </transition>

</template>

<script>
import { defineProps, getCurrentInstance, onMounted, watch, nextTick, ref } from "vue";
import mb_select from "@/components/mb_select";
import { getCollectionList, getAge, getType } from "@/config/api";

import showCollection from "@/components/showCollection/index.vue";
import emitter from "@/mitt/index";

export default {
  props: ["currentMuseumItem"],
  setup(props) {
    const containerId = ref("vuemasonry");
    const currentMuseum = ref("");

    const searchKey = ref("");
    const currentAge = ref("");
    const currentType = ref("");
    const busy = ref(false)
    const ages = ref([]);
    const types = ref([]);
    const menu = ref([
      {
        id: "",
        name: "全部",
      },
      {
        id: "model",
        name: "三维模型",
      },
      {
        id: "img",
        name: "图片",
      },
      {
        id: "video",
        name: "视频",
      },
      {
        id: "audio",
        name: "音频",
      },
    ]);

    const menuActive = ref("");

    onMounted(() => {
      getAge(data => {
        ages.value = data.data.data.map(item => {
          return {
            ...item,
            label: item.name,
            value: item.id
          }
        })
      })

      getType(data => {
        types.value = data.data.data.map(item => {
          return {
            ...item,
            label: item.name,
            value: item.id
          }
        })
      })
      currentMuseum.value = props.currentMuseumItem.id

    })

    return { ages, types, menu, currentAge, menuActive, currentType, searchKey, busy, currentMuseum }
  },
  components: { showCollection, mb_select },

  data() {
    return {
      showInput: false,
      list: [],
      activeCollection: null,
      paging: {
        pageSize: 10,
        pageNum: 1,
        showSize: 4,
        current: 1,
      }
    }
  },
  watch: {
    searchKey() {
      this.getData(true)
    },
    currentAge() {
      this.getData(true)
    },
    currentMuseum() {
      this.getData(true)
      // this.$emit('updateMuseum', this.museumList.find((item => item.id == this.currentMuseum)))
    },
    currentType() {
      this.getData(true)
    },
    menuActive() {
      this.getData(true)
    }
  },
  methods: {
    handleTab() {

    },
    getData(reset) {
      if (reset) {
        this.list = []
      }
      this.busy = true
      getCollectionList({
        "ageId": this.currentAge,
        "museumId": this.currentMuseum,
        "type": this.menuActive,
        "pageNum": this.paging.pageNum,
        "pageSize": this.paging.pageSize,
        "searchKey": this.searchKey,
        "textureId": this.currentType,
      }, data => {
        this.busy = false
        if (data.data.total <= this.list.length) {
          this.busy = true
          return
        }
        this.list = this.list.concat(data.data.records)
        this.paging.pageNum += 1

        this.$nextTick(() => {
          this.$redrawVueMasonry(this.containerId);
        });
      })

      // console.log(this);
      // this.list = this.list.concat([]);
      // // list.value = list.value.concat(list.value);
      // this.$nextTick(() => {
      //   this.$redrawVueMasonry(this.containerId);
      // });
    },
    onClickCollection(data) {
      this.activeCollection = data
    }
  },
  mounted() {
    emitter.on('closeCollection', () => {
      this.activeCollection = ''
    })
    this.getData()
  },
};
</script>

<style lang="scss" scoped>
.collections {
  width: 90vw;
  margin: 10px auto;

  .slebar {
    display: flex;
    color: #333;
    align-items: center;
    font-size: 14px;

    >ul {
      display: flex;
      align-items: center;

      >li {
        text-align: center;
        margin: 0 6px;
        cursor: pointer;
        white-space: nowrap;

        &.active {
          height: 30px;
          color: var(--main-color);
          line-height: 30px;
          border-radius: 60px;
          padding: 0 8px;
          border: 1px solid var(--main-color);
        }
      }
    }
  }

  .masonry {
    margin-top: 16px;
    width: 90vw;

    .item {
      width: calc(45vw - 5px);
      height: 200px;
      border-radius: 10px;
      margin-bottom: 10px;
      overflow: hidden;
      cursor: pointer;

      .itemimg {
        background: linear-gradient(180deg, #d9d9d9 0%, #b9b9b9 100%);
        font-size: 0;
        height: calc(100% - 40px);
        width: 100%;

        >div {
          height: 100%;
          width: 100%;
          background-repeat: no-repeat;
          background-size: cover;
          background-position: center;
        }
      }

      p {
        background: #fffef6;
        color: #999;
        height: 40px;
        line-height: 40px;
        padding: 0 10px;

        &.active {
          color: var(--main-color);
        }
      }
    }

    .odd {
      height: 310px;
    }
  }
}


.fnbar {
  position: absolute;
  bottom: 20px;
  right: 5%;
  background: var(--main-color);
  z-index: 99;
  width: 56%;
  display: flex;
  align-items: center;
  border-radius: 50px;
  font-size: 0;

  .barsub {
    width: 40%;
    flex-shrink: 0;
  }

  .searchimg {
    position: absolute;
    right: 20px;

    >img {
      width: 16px;
      height: 16px;
    }
  }

  .searchcon {
    width: 100%;
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    z-index: 999;
    background: var(--main-color);
    border-radius: 50px;
    display: flex;
    align-items: center;
    font-size: 14px;
    padding: 0 10px;
    justify-content: space-around;

    >img {
      width: 20px;
      height: 20px;
    }

    >input {
      width: 100%;
      height: 100%;
      padding-left: 16px;
      color: #fff;

      &::placeholder {
        color: #D2D2D2;
      }
    }

    >span {
      display: inline-block;
      color: var(--font-active);
      width: 30px;
      white-space: nowrap;
    }
  }
}
</style>
