<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 class="masonrycon">
      <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: 40%" v-else>
        <img src="@/assets/images/resource/searchNone.svg" alt="" />
        <p>暂时没有数据</p>
      </div>
    </div>
  </div>

  <teleport to="body">
    <div class="fnbar">
      <div class="barsub">
        <mb_select
          :noradius="true"
          class="mb_select"
          @handleOption="handleTab"
          :value="activeId"
          :options="yearlist"
        />
      </div>

      <div class="barsub">
        <mb_select
          :noradiusAll="true"
          class="mb_select"
          @handleOption="handleTab"
          :value="activeId"
          :options="yearlist"
        />
      </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="inputKey" type="text" placeholder="请输入藏品名称" />
          <span @click="showInput = false">取消</span>
        </div>
      </transition>
    </div>
  </teleport>

  <teleport to="body">
    <div class="fnbar">
      <div class="barsub">
        <mb_select
          :noradius="true"
          class="mb_select"
          @handleOption="(data) => (currentMuseum = data.id)"
          :value="currentMuseum"
          :options="museumList"
        />
      </div>

      <div class="barsub">
        <mb_select
          :noradiusAll="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="inputKey" type="text" placeholder="请输入展览名称" />
          <span @click="(showInput = false), (inputKey = '')">取消</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,
  getMuseumList,
  getAge,
  getType,
  getMuseumListOrInfoAPI,
} from "@/config/api";
import showCollection from "@/components/showCollection/index.vue";
import emitter from "@/mitt/index";

export default {
  setup(props) {
    const containerId = ref("vuemasonry");

    const inputKey = ref("");
    const currentAge = ref("");
    const currentType = ref("");
    const currentMuseum = ref("");

    const busy = ref(false);
    const ages = ref([]);
    const types = ref([]);
    const museumList = ref([]);

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

    const menuActive = ref("");

    const getMuseumListOrInfoFu = async () => {
      let res = await getMuseumListOrInfoAPI("goods");
      museumList.value = res.data.map((item) => {
        return {
          ...item,
          label: item.name,
          value: item.id,
        };
      });
      if (!currentMuseum.value) {
        currentMuseum.value = museumList.value[0].value;
      }
    };

    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,
          };
        });
      });

      getMuseumListOrInfoFu();
      // getMuseumList({
      //   "cityId": '',
      //   "pageNum": 1,
      //   "pageSize": 1000,
      // }, data => {
      //   museumList.value = data.data.records.map(item => {
      //     return {
      //       ...item,
      //       label: item.name,
      //       value: item.id
      //     }
      //   })
      //   if (!currentMuseum.value) {
      //     currentMuseum.value = museumList.value[0].value
      //   }
      // })
    });

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

  data() {
    return {
      showInput: false,
      list: [],
      activeCollection: null,
      paging: {
        pageSize: 10,
        pageNum: 1,
        showSize: 4,
        current: 1,
      },
    };
  },
  watch: {
    inputKey() {
      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.inputKey,
          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 = "";
    });
  },
};
</script>
  
<style lang="scss" scoped>
.collections {
  width: 100vw;
  padding-top: 10px;
  background: #e8e3d1;

  .slebar {
    display: flex;
    color: #333;
    align-items: center;
    width: 90vw;
    margin: 0 auto;
    font-size: 14px;
    padding-bottom: 20px;

    > 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);
        }
      }
    }
  }

  .masonrycon {
    height: calc(100vh - 60px);
    overflow-y: auto;

    .masonry {
      width: 90vw;
      margin: 0 auto 16px;

      .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: 90%;
  display: flex;
  align-items: center;
  border-radius: 50px;
  font-size: 0;

  .barsub {
    width: calc((100% - 5% - 16px) / 3);
    flex-shrink: 0;
  }

  .searchimg {
    position: absolute;
    right: 5%;

    > 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>
