<script setup lang="ts">
import homeIcon from "@/assets/img/home-icon.png";
import dataList from "@/data/data";
import { useStore } from "@/store/index";

const route = useRoute();
const router = useRouter();
const store = useStore();

const goBack = (id: string) => {
  router.push({
    path: "/",
  });
};

const goDetail = (item: string, index: number) => {
  store.currentPart = currentScene.value.parts[index];
  store.currentParagraph = item;
  router.push({
    path: "/audioDetail",
  });
};
const currentPart = ref(-1);
const open = (partIndex: number) => {
  if (currentPart.value == partIndex) {
    currentPart.value = 0;
  } else {
    currentPart.value = partIndex;
  }
};

const getAssetURL = (image: string) => {
  return new URL(`../../assets/img/${image}`, import.meta.url).href;
};

const currentScene = ref();

onMounted(() => {
  if (route.query.id) {
    currentScene.value = dataList.find((item) => {
      return item.id == Number(route.query.id);
    });
    store.currentScene = currentScene.value;
  } else {
    currentScene.value = store.currentScene;
  }
  currentPart.value = 1;
});
</script>

<template>
  <div class="all" v-if="currentScene">
    <div class="content">
      <div class="top-img">
        <img :src="getAssetURL(`home/${currentScene.coverImg}`)" alt="" />
      </div>
      <div class="content-box">
        <div
          class="content-box-item"
          v-for="(item, index) in currentScene.parts"
          :key="item.partName"
        >
          <div class="item-title" @click="open(index)">
            {{ item.partName }}
            <img
              v-show="index != 0"
              :src="
                currentPart == index
                  ? getAssetURL('up.png')
                  : getAssetURL('down.png')
              "
              alt=""
            />
          </div>
          <div class="item-list" v-if="currentPart == index || index == 0">
            <div
              class="item-list-item"
              v-for="(p, i) in item.paragraphs"
              @click="goDetail(p, index)"
            >
              {{ p.paragraphName }}
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 回到主页按钮 -->
    <img :src="homeIcon" alt="" class="home-icon" @click="goBack" />
  </div>
</template>

<style lang="less" scoped>
.all {
  width: 100%;
  height: 100%;
  padding: 5%;
  overflow: hidden;

  .content {
    width: 100%;
    height: 110%;
    background: url(/src\assets\img\contentBg.png);
    background-size: 100% 100%;
    border-radius: 8px 8px 0 0;
    .top-img {
      width: 100%;
      // height: 30vh;
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        box-shadow: 0px 4px 4px 0px rgba(0, 0, 0, 0.25);
      }
    }
    .content-box {
      width: 100%;
      height: 70vh;
      overflow: auto;
      padding: 5%;

      &-item {
        width: 100%;
        margin-bottom: 15px;

        .item-title {
          width: 100%;
          height: 8vh;
          background: rgba(44, 44, 44, 0.8);
          border-radius: 12px 12px 0px 0px;
          color: white;
          font-size: 16px;
          font-weight: bold;
          text-align: center;
          line-height: 8vh;
          position: relative;
          img {
            width: 6vw;
            height: 6vw;
            opacity: 0.6;
            position: absolute;
            top: 50%;
            right: 5%;
            transform: translateY(-50%);
          }
        }

        .item-list {
          transition: height 2s;
          &-item {
            width: 100%;
            height: 8vh;
            // height: 10vh;
            color: white;
            background: rgba(60, 126, 112, 0.6);
            box-shadow: 0px 2px 2px 0px rgba(0, 0, 0, 0.2);
            margin-top: 2px;
            font-size: 15px;
            text-align: center;
            line-height: 8vh;
            font-weight: bold;
            // white-space: nowrap;
            // overflow: hidden;
            // text-overflow: ellipsis;
          }
        }
      }

      // background: url(/src\assets\img\contentBg.png);
      // background-size: 100% 100%;
    }
    .content-box::-webkit-scrollbar-track {
      /*滚动条里面轨道*/
      -webkit-box-shadow: inset 0 0 5px transparent;
      border-radius: 10px;
      background: transparent;
    }
  }
  .home-icon {
    width: 15vw;
    height: 15vw;
    position: fixed;
    right: 20px;
    bottom: 30px;
    opacity: 0.7;
  }
}
</style>
