<template>
  <div>
    <Panel v-show="player.showWidgets" :class="{ disabled: !showdaogou }" :isOpen="isOpen">
      <div @click="toggleOpen" class="menu color">
        <div class="logo">
          <img :src="require('@/assets/images/icon/logo.png')" alt="" />
          <p v-if="player.lang != 'en'">{{$t('common.title') }}</p>
        </div>
        <div class="vline"></div>
        <ul :class="player.lang" @touchmove.stop>
          <li v-if="tours.length > 0">
            <ui-icon type="preview" @click.stop="playTour"></ui-icon>
            <div>{{$t('common.tour')}}</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: $t('common.mode'),
                })
              "
            ></ui-icon>
            <div>{{ $t('common.mode') }}</div>
          </li>

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

      <div class="toolbar color">
        <div class="navigation">
          <div class="h3">
            <span>{{ $t('common.guide') }}</span>
            <div @click="showShopList = true">
              <img :src="require(`@/assets/images/icon/search.svg`)" alt="" />
              {{ $t('common.searchguide') }}
            </div>
          </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: $t('common.all'),
                  })
                "
              >
                <div>{{ $t('common.all') }}</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>
    <teleport :to="`#app`">
      <div v-show="showShopList" class="shoplist">
        <div class="l-title">
          {{ $t('common.guidelist') }} ({{ searchList.length }})
          <ui-icon type="close" @click="showShopList = false" />
        </div>

        <div class="search">
          <img :src="require(`@/assets/images/icon/search.svg`)" alt="" />
          <input v-model="searchKey" type="text" :placeholder="$t('common.guideSearch')" />
          <ui-icon class="close" v-if="searchKey.length > 0" @click.stop="searchKey = ''" type="state_f"></ui-icon>
        </div>

        <ul v-if="searchList.length > 0">
          <li @click.stop="onClickShop(item)" v-for="(item, idx) in searchList" :key="idx">
            <div class="img" :style="{ 'background-image': `url(${item.shopLogo})` }"></div>
            <div class="name" v-if="item.shopName">
              <span>
                {{ item.shopName }}
              </span>
            </div>
            <ui-icon class="right" type="right"></ui-icon>
          </li>
        </ul>

        <div class="noresult" v-else>{{ $t('common.noResult') }}</div>
      </div>
    </teleport>
  </div>
</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';
import { useI18n, getLocale } from '@/i18n';
const { t } = useI18n({ useScope: 'global' });

const store = useStore();

const isOpen = ref(false);
const searchKey = ref('');

const showShopList = 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', { isHome: true });
    player.pause();
  } else {
    store.commit('tour/setData', { isHome: false });
    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: t('common.help'),
    },
    {
      icon: 'customer_service',
      id: 'kefu',
      name: t('common.kefu'),
    },
  ];

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

const categorylist = ref([]);

const brandlist = ref([]);

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

watch(
  () => searchKey.value,
  (val, old) => {
    console.log(searchKey.value);
    getSearchList();
  }
);

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

const getSearchList = async () => {
  let res = await apis.get_shop_list({
    shopName: searchKey.value,
  });
  searchList.value = res.data;
};

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%);
    min-width: 90px;
    flex-shrink: 0;
    > 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;
    overflow-x: auto;
    > li {
      margin-left: 0.4rem;
      &:first-of-type {
        margin-left: 0;
      }
      > div {
        margin-top: 4px;
        font-size: 10px;
      }
    }
  }

  .en{
    justify-content: flex-start;
    width: calc(100% - 100px);
  }
}

.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;
      display: flex;
      justify-content: space-between;
      align-items: center;
      > div {
        width: 96px;
        height: 24px;
        background: rgba(0, 0, 0, 0.5);
        border-radius: 24px;
        display: flex;
        font-size: 12px;
        justify-content: center;
        align-items: center;
      }
      img {
        width: 12px;
        margin-right: 6px;
      }
    }
    .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;
        }
      }
    }
  }
}

.shoplist {
  position: fixed;
  z-index: 999;
  width: 100%;
  height: 30px;
  left: 0;
  right: 0;
  bottom: 0;
  height: 80vh;
  background: rgba(0, 0, 0, 0.8);
  border-radius: 10px 10px 0px 0px;
  padding: 12px 15px;
  .l-title {
    font-size: 16px;
    font-weight: bold;
    color: #ffffff;
    text-align: center;
    position: relative;
    width: 100%;

    i {
      position: absolute;
      right: 0;
      top: 2px;
    }
  }

  .search {
    width: 100%;
    height: 34px;
    background: rgba(46, 46, 46, 0.9);
    border-radius: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 8px;
    margin-top: 20px;
    > img {
      width: 16px;
      opacity: 0.8;
    }
    > input {
      text-align: left;
      font-size: 16px;
      width: 100%;
      color: #fff;
      margin-left: 10px;
    }
    .close {
      color: #7d7e80;
    }
  }

  > ul {
    width: 100%;
    height: calc(100% - 80px);
    overflow-y: auto;
    > li {
      width: 100%;
      height: 60px;
      position: relative;
      font-size: 0;
      overflow: hidden;
      box-sizing: border-box;
      display: flex;
      justify-content: center;
      align-items: center;
      margin: 18px 0;
      .img {
        width: 60px;
        height: 60px;
        background-size: contain;
        flex-shrink: 0;
        border-radius: 4px;
        margin-right: 10px;
      }
      .name {
        width: 100%;
        font-size: 12px;
        left: 0;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
        text-align: left;
        padding: 2px 4px;
        box-sizing: border-box;
        word-break: break-all;
        > span {
          display: inline-block;
          white-space: nowrap;
        }
      }
      .right {
        font-size: 14px;
      }
    }
  }
}

.noresult {
  text-align: center;
  margin-top: 20vh;
}

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

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