<!-- 乐理知识 弹窗列表 -->
<template>
  <div class="musicTheoryList">
    <div class="headCon">
      <div class="headLeft">
        <img class="tipImg" src="@/views/Editor/CanvasTool/imgs/ylzs.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="leftCon">
        <courseCollapse
          :courseList="listKnowledgeData"
          :activeCollapse="listKnowledgeData[0]?.id || ''"
          :activeCollapseId="activeCollapseItem?.id || ''"
          @handleCollapseItem="handleCollapseItem"
        />
      </div>
      <div class="rightCon">
        <div v-if="loading" class="loading-overlay">
          <div class="loadingBox">
            <div class="loadingItem"></div>
            <div class="loadingItem"></div>
            <div class="loadingItem"></div>
            <div class="loadingItem"></div>
          </div>
        </div>
        <iframe
          @load="handleIframeLoad"
          v-if="activeCollapseItem.id"
          :key="activeCollapseItem.id"
          class="musicIframe"
          frameborder="0"
          :src="url"
        ></iframe>
      </div>
    </div>
    <div class="btnCon">
      <div class="cancelBtn" @click="emits('close')">取消</div>
      <div class="addBtn" @click="handleAdd">添加</div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getListKnowledge } from "@/api/musicResources"
import { httpAjax } from "@/plugins/httpAjax"
import { ref, computed } from "vue"
import courseCollapse from "./courseCollapse.vue"
import { getMusicResourcesUrl } from "../index"

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

const listKnowledgeData = ref<any[]>([])
const activeCollapseItem = ref<Record<string, any>>({})
initListKnowledge()
function initListKnowledge() {
  httpAjax(getListKnowledge).then(res => {
    if (res.code === 200) {
      listKnowledgeData.value = res.data || []
      activeCollapseItem.value = listKnowledgeData.value[0]?.lessonCoursewareDetailKnowledgeDetailList[0] || {}
    }
  })
}
function handleCollapseItem(item: Record<string, any>) {
  loading.value = true
  activeCollapseItem.value = item
}
const url = computed(() => {
  return getMusicResourcesUrl("THEORY", "modal", activeCollapseItem.value?.id)
})

const loading = ref(true)
function handleIframeLoad() {
  loading.value = false
}
function handleAdd() {
  emits("update", activeCollapseItem.value, "THEORY")
}
</script>

<style lang="scss" scoped>
.musicTheoryList {
  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% - 130px);
    background: #f5f6fa;
    display: flex;
    padding: 24px 0 24px 30px;
    .leftCon {
      overflow-y: auto;
      width: 360px;
      height: 100%;
      flex-shrink: 0;
      background: #ffffff;
      border-radius: 16px;
      padding: 12px;
    }
    .rightCon {
      margin-left: 20px;
      height: 100%;
      flex-grow: 1;
      position: relative;
      .musicIframe {
        width: 100%;
        height: 100%;
      }
      .loading-overlay {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        z-index: 10;
        color: #fff;
        .loadingBox {
          width: 30px;
          height: 30px;
          display: flex;
          justify-content: space-between;
          flex-wrap: wrap;
          align-content: space-between;
          animation: rotate 1.5s linear infinite;
          .loadingItem {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background: #20bdff;
            opacity: 0.5;
            &:nth-child(2) {
              opacity: 1;
            }
          }
        }
        @keyframes rotate {
          from {
            transform: rotate(0deg);
          }
          to {
            transform: rotate(360deg);
          }
        }
      }
    }
  }
  .btnCon {
    width: 100%;
    height: 66px;
    padding: 0 30px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    border-top: 1px solid #eaeaea;
    .addBtn {
      width: 76px;
      height: 34px;
      background: linear-gradient(312deg, #1b7af8 0%, #3cbbff 100%);
      border-radius: 6px;
      line-height: 34px;
      text-align: center;
      font-weight: 600;
      font-size: 14px;
      color: #ffffff;
      cursor: pointer;
      &:hover {
        opacity: 0.8;
      }
      &.disabled {
        opacity: 0.7;
        cursor: default;
      }
    }
    .cancelBtn {
      margin-right: 12px;
      width: 76px;
      height: 34px;
      background: #f1f2f6;
      border-radius: 6px;
      line-height: 34px;
      text-align: center;
      font-weight: 600;
      font-size: 14px;
      color: #1e2022;
      cursor: pointer;
      &:hover {
        opacity: 0.8;
      }
    }
  }
}
</style>
