<template>
  <div class="listeningPracticeList">
    <div class="headCon">
      <div class="headLeft">
        <img class="tipImg" src="@/views/Editor/CanvasTool/imgs/tylx.png" alt="" />
        <div class="title">听音练习</div>
      </div>
      <div class="headright">
        <img @click="emits('close')" class="closeBtn" src="../../cloudCoachElement/cloudCoachList/imgs/close.png" alt="" />
      </div>
    </div>
    <div class="content">
      <div class="tabTools">
        <div class="tabCon">
          <div class="tab" @click="handleTabChange(item)" :class="{ active: item.id === tabActive?.id }" v-for="item in tabData" :key="item.id">
            {{ item.name }}
          </div>
        </div>
      </div>
      <div class="instrumentsContent">
        <div class="instrument" v-for="item in tabActive?.instruments || []" :key="item.code" @click="handleAddListeningPractice(item)">
          <div class="imgBox">
            <img :src="item.img" />
          </div>
          <div class="title">{{ item.name }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getSubjectListApi } from "@/api/pptOperate"
import { httpAjax } from "@/plugins/httpAjax"
import { ref } from "vue"

const emits = defineEmits<{
  (event: "update", item: Record<string, any>): void
  (event: "close"): void
}>()

function handleAddListeningPractice(item: Record<string, any>) {
  emits("update", item)
}

type tabType = {
  id: number
  name: string
  instruments: {
    code: string
    name: string
    img: string
  }[]
}

const tabData = ref<tabType[]>([])
const tabActive = ref<tabType>()

getTabList()
function getTabList() {
  httpAjax(getSubjectListApi).then(res => {
    if (res.code === 200) {
      const instruments = res.data.reduce((subTab: any[], item: any) => {
        subTab.push(...item.instruments)
        return subTab
      }, [])
      tabData.value = [
        {
          id: -199,
          name: "全部声部",
          instruments
        },
        ...res.data
      ]
      handleTabChange(tabData.value[0])
    }
  })
}

function handleTabChange(activeTab: tabType) {
  tabActive.value = activeTab
}
</script>

<style lang="scss" scoped>
.listeningPracticeList {
  width: 100%;
  height: 100%;
  .headCon {
    width: 100%;
    height: 64px;
    border-bottom: 1px solid #eaeaea;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .headLeft {
      margin-left: 30px;
      display: flex;
      align-items: center;
      .tipImg {
        width: 24px;
        height: 24px;
      }
      .title {
        font-weight: 600;
        font-size: 18px;
        color: #131415;
        margin-left: 8px;
      }
    }
    .headright {
      margin-right: 30px;
      display: flex;
      align-items: center;
      .closeBtn {
        width: 24px;
        height: 24px;
        cursor: pointer;
        &:hover {
          opacity: 0.8;
        }
      }
    }
  }
  .content {
    width: 100%;
    height: calc(100% - 64px);
    .tabTools {
      flex-shrink: 0;
      width: 100%;
      padding: 24px 30px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .tabCon {
        display: flex;
        .tab {
          margin-right: 32px;
          font-weight: 400;
          font-size: 16px;
          color: #8b8d98;
          line-height: 22px;
          cursor: pointer;
          &:hover {
            opacity: 0.8;
          }
          &:last-child {
            margin-right: 0;
          }
          &.active {
            font-weight: 600;
            color: #131415;
            position: relative;
            &::after {
              content: "";
              position: absolute;
              width: 100%;
              height: 10px;
              background: linear-gradient(90deg, #77bbff 0%, rgba(163, 231, 255, 0.22) 100%);
              bottom: 0;
              left: 0;
              z-index: -1;
            }
          }
        }
      }
    }
    .instrumentsContent {
      display: flex;
      flex-wrap: wrap;
      padding: 0 30px;
      .instrument {
        margin-right: 40px;
        margin-bottom: 40px;
        cursor: pointer;
        &:nth-child(5n) {
          margin-right: 0;
        }
        &:hover {
          .imgBox {
            border-color: #198cfe;
            transform: scale(1.02);
            transition: all 0.2s ease;
          }
        }
        .imgBox {
          width: 104px;
          height: 104px;
          border-radius: 12px;
          border: 2px solid transparent;
          position: relative;
          overflow: hidden;
          &::after {
            content: "";
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background: url("./imgs/subject-bg.png") no-repeat center;
            z-index: -1;
          }
          img {
            width: 100%;
            height: 100%;
            object-fit: contain;
          }
        }
        .title {
          margin-top: 10px;
          font-weight: 400;
          font-size: 14px;
          color: #131415;
          line-height: 20px;
          text-align: center;
        }
      }
    }
  }
}
</style>
