<!--
* @FileDescription: 选中弹窗
* @Author: 黄琪勇
* @Date:2024-03-22 15:21:44
-->
<template>
   <div class="chooseDialog">
      <div class="close" @click="close"></div>
      <div class="chooseHeader" v-if="isShowTabs">
         <img
            key="classlist"
            v-if="chooseType === 'classlist'"
            src="@/img/cloudTextbooks/classlist-active.png"
            @click="handleChangeType('classlist')"
         />
         <img v-else src="@/img/cloudTextbooks/classlist.png" @click="handleChangeType('classlist')" />
         <img key="search" v-if="chooseType === 'search'" src="@/img/cloudTextbooks/search-active.png" @click="handleChangeType('search')" />
         <img v-else src="@/img/cloudTextbooks/search.png" @click="handleChangeType('search')" />
      </div>
      <div class="chooseCon" v-loading="loading" v-show="chooseType === 'classlist'">
         <img class="imgMid" src="@/img/cloudTextbooks/shu.png" />
         <div class="chooseBox" v-for="(listDetails, index) in listDetailData" :key="index">
            <div
               class="operate"
               v-show="(index === 0 && pageNum !== 0) || (index === 1 && listData.length > pageNum + 1)"
               @click="handlePage(index === 0 ? 'prev' : 'next')"
            >
               <img :src="require(`@/img/cloudTextbooks/${index === 0 ? 'left' : 'right'}.png`)" />
            </div>
            <div class="chooseList">
               <div class="listItem" v-for="item in listDetails" :key="item.id">
                  <div class="img">
                     <template v-if="item.lockFlag">
                        <img src="@/img/cloudTextbooks/jy.png" />
                     </template>
                     <template v-else>
                        <img src="@/img/cloudTextbooks/bf.png" />
                        <!-- <img src="@/img/cloudTextbooks/xm.png" /> -->
                     </template>
                  </div>
                  <div class="nameCon">
                     <div class="name">{{ item.name }}</div>
                     <div class="text" v-if="item.useNum ?? false">已使用{{ item.useNum }}次</div>
                  </div>
                  <div class="play" :class="{ disabled: item.lockFlag }" @click="item.lockFlag === true || handlePaly(item.id)">查看</div>
               </div>
            </div>
         </div>
      </div>

      <div class="chooseCon" v-loading="searchLoading" v-show="chooseType === 'search'">
         <div class="chooseBox chooseBoxSearch">
            <div class="chooseList">
               <myInput
                  class="queryIpt"
                  v-model="queryStr"
                  :height="40"
                  placeholder="请输入素材关键词"
                  clearable
                  @handleQuery="handleQuery"
                  @keyup.enter="handleQuery"
               />
               <searchCollapse :search="tempSearch" :activeCollapse="activeCollapse" :courseList="listSearchData" @handleClick="handleCourseClick" />
               <el-empty
                  class="empty"
                  v-if="!listSearchData.length && !searchLoading"
                  :image="require('@/img/layout/empty.png')"
                  description="暂无搜索结果"
               />
            </div>
         </div>
      </div>
   </div>
</template>

<script setup lang="ts">
import { computed, ref } from "vue"
import { useDataDetailList } from "./useData"
import myInput from "@/components/myInput"
import searchCollapse from "./searchCollapse"
import userStore from "@/store/modules/user"
const { handleGetDetailList, loading, searchLoading, listDetailData, listSearchData, activeCollapse, listData, pageNum, handlePage } =
   useDataDetailList()
import router from "@/router"
const userStoreHook = userStore()

const chooseType = ref<"classlist" | "search">("classlist")
const queryStr = ref("")
const tempSearch = ref("")
const props = defineProps<{
   modalData: {
      id: string
   }
}>()
const emits = defineEmits<{
   (e: "onClose"): void
}>()

handleGetDetailList(props.modalData.id)

const isShowTabs = computed(() => {
   return userStoreHook.roles === "GYM" ? true : false
})

function close() {
   emits("onClose")
}

function handlePaly(id: string) {
   // const url = router.resolve({
   //    name: "coursewarePlay",
   //    params: { id }
   // }).href
   // window.open(url, "_blank")
   router.push({
      name: "coursewarePlay",
      params: {
         id
      }
   })
}

function handleCourseClick(value: any) {
   router.push({
      name: "coursewarePlay",
      params: {
         id: props.modalData.id
      },
      query: {
         source: "search",
         search: queryStr.value,
         materialId: value.id
      }
   })
}

function handleChangeType(type: "classlist" | "search") {
   chooseType.value = type
}

function handleQuery() {
   tempSearch.value = queryStr.value
   handleGetDetailList(props.modalData.id, true, queryStr.value)
}
</script>

<style lang="scss" scoped>
.chooseDialog {
   width: 100%;
   height: 100%;
   padding: 40px;
   .close {
      position: absolute;
      top: -14px;
      right: -16px;
      width: 42px;
      height: 44px;
      cursor: pointer;
      background: url("@/img/useDialogConfirm/close.png") no-repeat;
      background-size: cover;
      &:hover {
         background: url("@/img/useDialogConfirm/closeHover.png") no-repeat;
         background-size: cover;
      }
   }

   .chooseHeader {
      position: absolute;
      top: -7px;
      left: 54px;
      img {
         width: 131px;
         height: 49px;
         margin-right: 12px;
      }
   }

   .chooseCon {
      width: 100%;
      height: 100%;
      display: flex;
      position: relative;
      & > :deep(.el-loading-mask) {
         border-radius: 17px;
      }
      .imgMid {
         width: 68px;
         height: 518px;
         position: absolute;
         left: 50%;
         top: 50%;
         transform: translate(-50%, -50%);
         z-index: 1;
      }
      .chooseBox {
         width: 50%;
         height: 100%;
         background: #f6d7c1;
         box-shadow: 0px 2px 3px 0px #a05400;
         border-radius: 17px;
         padding: 5px;
         position: relative;
         &:nth-child(2) {
            .operate {
               left: -30px;
               border-radius: 6px 0px 0px 6px;
            }
            &:hover .operate {
               opacity: 1;
            }
         }
         &:nth-child(3) {
            margin-left: 10px;
            .operate {
               right: -30px;
               border-radius: 0px 6px 6px 0px;
            }
            &:hover .operate {
               opacity: 1;
            }
         }
         .operate {
            width: 30px;
            height: 68px;
            background: #fffefb;
            position: absolute;
            top: 50%;
            transform: translate(0, -50%);
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: opacity 0.28s;
            opacity: 0;
            &:hover {
               background-color: #edeff0;
            }
            > img {
               width: 13px;
               height: 21px;
            }
         }
         .chooseList {
            width: 100%;
            height: 100%;
            background: #fdf7f0;
            border-radius: 17px;
            padding: 0 40px 0 30px;
            overflow: hidden;
            .listItem {
               margin-top: 12px;
               padding: 10px 0;
               display: flex;
               justify-content: space-between;
               align-items: center;
               border-bottom: 1px solid #eaeaea;
               &:first-child {
                  margin-top: 32px;
               }
               &:last-child {
                  border-bottom: none;
               }
               .img {
                  flex-shrink: 0;
                  width: 35px;
                  height: 40%;
                  position: relative;
                  & > img:nth-child(1) {
                     width: 100%;
                  }
                  & > img:nth-child(2) {
                     position: absolute;
                     left: 0;
                     top: 0;
                     z-index: 1;
                  }
               }
               .nameCon {
                  margin-left: 7px;
                  flex-grow: 1;
                  .name {
                     font-weight: 600;
                     font-size: 16px;
                     color: #333333;
                     padding: 3px 0;
                  }
                  .text {
                     font-weight: 400;
                     font-size: 12px;
                     color: #777777;
                     padding: 2px 0;
                  }
               }
               .play {
                  flex-shrink: 0;
                  background: linear-gradient(180deg, #ffab71 0%, #ff6e45 100%);
                  border-radius: 12px;
                  text-align: center;
                  font-weight: 500;
                  font-size: 12px;
                  padding: 6px 18px;
                  color: #ffffff;
                  letter-spacing: 1px;
                  cursor: pointer;
                  &:hover {
                     opacity: $opacity-hover;
                  }
                  &.disabled {
                     background: linear-gradient(180deg, #d3d3d3 0%, #b5b5b5 100%);
                     cursor: not-allowed;
                     &:hover {
                        opacity: 1;
                     }
                  }
               }
            }
         }
      }
      .chooseBoxSearch {
         width: 100%;

         .chooseList {
            padding: 20px 80px;
            display: flex;
            flex-direction: column;

            .courseCollapse {
               margin-top: 16px;
               flex: 1;
               overflow-y: auto;
               overflow-x: hidden;
               &::-webkit-scrollbar {
                  display: none;
               }
            }

            &:deep(.empty) {
               position: absolute;
               top: 50%;
               left: 50%;
               transform: translate(-50%, -50%);
               .el-empty__image {
                  width: 360px;
               }
            }
         }
      }
   }
}
</style>
<style lang="scss">
.h-modalFrame.chooseDialog {
   /* prettier-ignore */
   --modalFrameTitHeight: 0PX;
   .modalFrameBox {
      background: url("@/img/cloudTextbooks/bg.png") no-repeat;
      background-size: cover;
      box-shadow: none;
      .modalFrameTitle {
         display: none;
      }
   }
}
</style>
