<template>
  <Panel v-show="player.showWidgets" :isOpen="isOpen">
    <div @click="toggleOpen" class="menu color">
      <div class="logo">
        <img :src="require('@/assets/images/icon/logo.png')" alt="" />
        <p>cdf澳門上葡京店</p>
      </div>
      <div class="vline"></div>
      <ul>
        <li v-if="tours.length > 0">
          <ui-icon type="preview" @click.stop="playTour"></ui-icon>
          <div>導覽</div>
        </li>
        <li @click.stop="onClickMenu(item)" v-for="(item, i) in menulist" :key="i">
          <ui-icon :type="item.icon"></ui-icon>
          <div>{{ item.name }}</div>
        </li>

        <li v-if="showdaogou">
          <ui-icon
            type="guided_shopping"
            @click.stop="
              onClickMenu({
                icon: 'guided_shopping',
                id: 'guided_shopping',
                name: '導購',
              })
            "
          ></ui-icon>
          <div>導購</div>
        </li>

        <li>
          <ui-icon
            type="shopping"
            @click.stop="
              onClickMenu({
                icon: 'shopping',
                id: 'shopping',
                name: '購物',
              })
            "
          ></ui-icon>
          <div>購物</div>
        </li>
      </ul>
    </div>

    <div class="toolbar color">
      <div class="navigation">
        <div class="h3">專櫃導航</div>
        <div class="swiper-container" id="sw-navigation">
          <ul class="swiper-wrapper">
            <li
              class="swiper-slide"
              :class="{ liactive: item.sceneUrl === currentM && item.inPosition.indexOf(currentPose) > -1 }"
              @click.stop="onClickShop(item)"
              v-for="(item, i) in brandlist"
              :key="i"
            >
              <div v-if="item.shopLogo" class="img" :style="{ 'background-image': `url(${item.shopLogo})` }"></div>
              <div class="name" v-if="item.shopName">
                <span :class="{ active: item.shopName.length > 6 }">
                  {{ item.shopName }}
                </span>
              </div>
            </li>
          </ul>
        </div>
      </div>

      <div class="category">
        <div class="swiper-container" id="sw-category">
          <ul class="swiper-wrapper">
            <li
              class="swiper-slide"
              :class="{ categoryactive: '' == currentCategory.id }"
              @click.stop="
                onClickCategory({
                  id: '',
                  categoryName: '全部',
                })
              "
            >
              <div>全部</div>
            </li>
            <li
              @click.stop="onClickCategory(item)"
              :class="{ categoryactive: item.id == currentCategory.id }"
              class="swiper-slide"
              v-for="(item, i) in categorylist"
              :key="i"
            >
              <div>{{ item.categoryName }}</div>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </Panel>
</template>

<script setup>
import { useStore } from "vuex";
import { onMounted, watch, computed, ref, nextTick } from "vue";
import Panel from "@/views/Panel.vue";
import { useApp, getApp } from "@/app";
import * as apis from "@/apis/index.js";
import browser from "@/utils/browser";

const store = useStore();

const isOpen = ref(false);


const toggleOpen = () => {
  isOpen.value = !isOpen.value;
};

const currentCategory = ref({
  id: "",
  categoryName: "全部",
});

const currentM = computed(() => browser.getURLParam("m"));
const currentPose = computed(() => browser.getURLParam("pose"));
const showdaogou = computed(() => store.getters["rtc/showdaogou"]);

const isPlay = computed(() => {
  let status = store.getters["tour/isPlay"];
  let map = document.querySelector(".kankan-app div[xui_min_map]");
  if (map) {
    if (status) {
      map.classList.add("disabled");
    } else {
      map.classList.remove("disabled");
    }
  }
  return status;
});

const partId = computed(() => store.getters["tour/partId"]);

const playTour = async () => {
  let player = await getApp().TourManager.player;
  if (isPlay.value) {
    store.commit("tour/setData", { isPlay: true });
    player.pause();
  } else {
    store.commit("tour/setData", { isPlay: true });
    player.play(partId.value);
  }
};

const metadata = computed(() => store.getters["scene/metadata"]);

const player = computed(() => store.getters["player"]);

const tours = computed(() => store.getters["tour/tours"]);

const menulist = computed(() => {
  let fff = [
    {
      icon: "help",
      id: "help",
      name: "幫助",
    },
    {
      icon: "customer_service",
      id: "kefu",
      name: "客服",
    },
  ];

  if (!browser.isMobile()) {
    fff.shift();
  }
  return fff;
});

const categorylist = ref([]);

const brandlist = ref([]);

const brandScroll = () => {
  nextTick(() => {
    let t = setTimeout(() => {
      clearTimeout(t);
      new Swiper("#sw-navigation", {
        freeMode: true,
        slidesPerView: "auto",
        centeredSlides: false,
        spaceBetween: 10,
        grid: {
          rows: 2,
        },
        on: {
          touchMove(swiper, e) {
            e.stopPropagation();
            e.preventDefault();
          },
        },
      });

      new Swiper("#sw-category", {
        freeMode: true,
        slidesPerView: "auto",
        spaceBetween: 10,
        on: {
          touchMove(swiper, e) {
            e.stopPropagation();
            e.preventDefault();
          },
        },
      });
    }, 100);
  });
};

const onClickMenu = (item) => {
  if (item.id == "kefu") {
    let mglink =
      "https://webpage.qidian.qq.com/2/chat/h5/index.html?linkType=1&env=ol&kfuin=3009110132&fid=3655&key=9b4334768c39150ead3f23e11e5dc2e4&cate=7&source=0&isLBS=0&isCustomEntry=0&type=10&ftype=1&_type=wpa&qidian=true&_pid=kvrmvu.74cg11.l43qvbcu&translateSwitch=0&isSsc=0&roleValue=4&roleData=922223821";
    window.open(mglink, "_blank");
  } else if (item.id == "shopping") {
    browser.openLink(
      "/subPackage/pages/shoppingcart/shoppingcart",
      "https://m.cdfmembers.com/shop/600667208/shoppingcart",
      "/subPackage/pages/shoppingcart/shoppingcart"
    );
  } else if (item.id == "help") {
    store.commit("showUserGuide", true);
  } else if (item.id == "guided_shopping") {
    store.commit("showShoppingguide", true);
  }
};

const getCategorylist = async () => {
  let res = await apis.get_category_list({});
  categorylist.value = res.data;
  brandScroll();
};

const onClickCategory = (item) => {
  currentCategory.value.id = item.id;
  currentCategory.value.categoryName = item.categoryName;
};

const onClickShop = (item) => {
  let url = window.location.href;

  if (!browser.hasURLParam("pose")) {
    url += `&${item.inPosition}`;
  } else {
    url = browser.replaceQueryString(url, "pose", item.inPosition.replace("pose=", ""));
  }

  url = browser.replaceQueryString(url, "m", item.sceneUrl);
  window.location.href = url;
};

watch(
  () => currentCategory,
  (val, old) => {
    getShoplist();
  },
  {
    deep: true,
  }
);

const getShoplist = async () => {
  let res = await apis.get_shop_list({
    categoryId: currentCategory.value.id,
  });
  brandlist.value = res.data;
  brandScroll();
};

onMounted(() => {
  useApp().then((app) => {
    getCategorylist();
    getShoplist();
  });
});
</script>

<style lang="scss" scoped>
.menu {
  width: 100%;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(0, 0, 0, 0.2);
  display: flex;
  justify-content: space-between;
  box-sizing: border-box;
  position: relative;
  align-items: center;
  height: 48px;
  padding: 0 12px;
  text-align: center;
  .logo {
    width: 90px;
    transform: translateY(-24%);
    > img {
      width: 100%;
      border-radius: 4px;
    }
    > p {
      margin: 2px 0;
      font-size: 10px;
    }
  }
  .vline {
    width: 1px;
    height: 14px;
    background: #fff;
  }
  > ul {
    display: flex;
    align-items: center;
    font-size: 0;
    justify-content: flex-end;
    > li {
      margin-left: 0.5rem;
      &:first-of-type {
        margin-left: 0;
      }
      > div {
        margin-top: 4px;
        font-size: 10px;
      }
    }
  }
}

.toolbar {
  width: 100%;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  margin-top: 8px;
  margin-bottom: 30px;
  background: rgba(0, 0, 0, 0.2);
  .navigation {
    padding: 8px 0;

    .h3 {
      font-size: 14px;
      padding: 0 14px;
    }
    .swiper-container {
      width: 100%;
      height: 130px;
      overflow: hidden;
      margin-top: 8px;
      padding-right: 2px;
      padding-left: 14px;
      position: relative;

      &::after {
        position: absolute;
        right: 0;
        bottom: 0;
        content: "";
        display: inline-block;
        height: 100%;
        z-index: 99;
        width: 17px;
        background: linear-gradient(90deg, rgba(0, 0, 0, 0) 0%, #000000 100%);
        opacity: 0.4;
        pointer-events: none;
      }
      > ul {
        margin: 0 !important;
        > li {
          width: 60px;
          height: 60px;
          border-radius: 4px;
          position: relative;
          font-size: 0;
          overflow: hidden;
          border: 1px transparent solid;
          box-sizing: border-box;
          &.liactive {
            color: var(--editor-main-color);
            border: 1px var(--editor-main-color) solid;
          }
          .img {
            width: 100%;
            height: 100%;
            background-size: contain;
          }
          .name {
            width: 100%;
            position: absolute;
            bottom: -1px;
            font-size: 12px;
            left: 0;
            background: rgba(0, 0, 0, 0.5);
            // text-overflow: ellipsis;
            // white-space: nowrap;
            // overflow: hidden;
            text-align: center;
            padding: 2px 4px;
            box-sizing: border-box;
            word-break: break-all;
            > span {
              display: inline-block;
              white-space: nowrap;
            }
            .active {
              animation: 5s wordsLoop linear infinite normal;
            }
          }
        }
      }
    }
  }
  .category {
    padding: 8px 14px;
    border-top: 1px solid rgba(255, 255, 255, 0.2);

    .swiper-container {
      width: 100%;
      overflow: hidden;
      > ul {
        > li {
          width: auto;
          color: rgba(255, 255, 255, 0.5);
          > div {
            width: 100%;
            font-size: 14px;
            padding: 2px 4px;
          }
        }
        .categoryactive {
          color: #fff;
        }
      }
    }
  }
}

@keyframes wordsLoop {
  0% {
    transform: translateX(100%);
    -webkit-transform: translateX(100%);
  }

  100% {
    transform: translateX(-100%);
    -webkit-transform: translateX(-100%);
  }
}
</style>
