// 处理 区分处理管乐迷 管乐团的数据

import {
   queryLessonCourseware_gym,
   getLessonCoursewareCourseList_gym,
   getMyCourseware_gyt,
   getMyCoursewareDetail_gyt,
   getuyAlbumInfo_klx,
   queryLessonCourseware_klx,
   getLessonCoursewareCourseList_klx
} from "@/api/cloudTextbooks.api"

import { httpAjaxErrMsg, httpAjax } from "@/plugin/httpAjax"
import { CODE_ERR_CANCELED } from "@/libs/auth"
import { ElMessage } from "element-plus"

import userStore from "@/store/modules/user"
import { ref, shallowRef, computed } from "vue"

type listType = {
   type: string
   name: string
   img: string
   id: string
   courseNum: number // 课程数量
}[][]

/**
 * 列表数据
 */
export const useDataList = () => {
   const userStoreHook = userStore()
   const listData = shallowRef<listType>([])
   let storeData: listType[number] = []
   // 专辑
   const albumId = ref("")
   const albumOpt = shallowRef<{ value: string; label: string }[]>([])
   const loading = ref(false)
   let coursewareController: AbortController

   function handleGetList() {
      //  GYM,GYT,KLX 区分   获取列表数据
      if (userStoreHook.roles === "GYM") {
         handleGetList_gym("", "")
      } else if (userStoreHook.roles === "GYT") {
         handleGetList_gyt()
      } else if (userStoreHook.roles === "KLX") {
         httpAjax(getuyAlbumInfo_klx).then(res => {
            if (res.code === 200) {
               // 专辑赋值
               albumOpt.value = (res.data || []).reduce((arr: any[], item: any) => {
                  if (item.coursewareCounts > 0) {
                     arr.push({
                        value: item.id,
                        label: item.name
                     })
                  }
                  return arr
               }, [])
               // 默认取第一个专辑
               albumOpt.value.length && (albumId.value = albumOpt.value[0].value)
               handleGetList_klx("", albumId.value, "")
            }
         })
      }
   }
   function handleListQuery(type: string, queryStr: string) {
      //  GYM,GYT,KLX 区分   查询列表数据
      if (userStoreHook.roles === "GYM") {
         handleQueryGetList_gym(type, queryStr)
      } else if (userStoreHook.roles === "GYT") {
         handleQueryGetList_gyt(type, queryStr)
      } else if (userStoreHook.roles === "KLX") {
         handleQueryGetList_klx(type, albumId.value, queryStr)
      }
   }

   // 获取管乐团数据
   function handleGetList_gyt() {
      loading.value = true
      httpAjaxErrMsg(getMyCourseware_gyt).then(res => {
         loading.value = false
         if (res.code === 200) {
            storeData = (res.data || []).map((item: any) => {
               return {
                  name: item.name,
                  type: item.courseTypeCode,
                  img: item.coverImg,
                  id: item.id,
                  courseNum: item.courseNum
               }
            })
            listData.value = chunkArray(storeData, 5)
         }
      })
   }
   // 管乐团数据查询
   function handleQueryGetList_gyt(type: string, queryStr: string) {
      const computeData = storeData.filter(item => {
         return (type ? item.type === type : true) && (queryStr ? item.name.includes(queryStr) : true)
      })
      listData.value = chunkArray(computeData, 5)
   }
   // 获取管乐迷数据
   function handleGetList_gym(type: string, queryStr: string) {
      if (coursewareController) {
         coursewareController.abort()
      }
      coursewareController = new AbortController()
      loading.value = true
      httpAjax(queryLessonCourseware_gym, type, coursewareController).then(res => {
         // 自己关闭的时候不取消加载
         if (res.code === CODE_ERR_CANCELED) {
            return
         }
         loading.value = false
         if (res.code === 200) {
            const data = (res.data?.rows || []).reduce((arr: any[], item: any) => {
               if ((type ? item.subjectId === type : true) && (queryStr ? item.name.includes(queryStr) : true)) {
                  arr.push({
                     name: item.name,
                     type: item.subjectId,
                     img: item.cover,
                     id: item.lessonCoursewareId,
                     courseNum: item.courseNum
                  })
               }
               return arr
            }, [])
            listData.value = chunkArray(data, 5)
         } else {
            if (res.code !== 511) {
               ElMessage({
                  showClose: true,
                  message: res.message,
                  type: "error"
               })
            }
         }
      })
   }
   // 管乐迷数据查询
   function handleQueryGetList_gym(type: string, queryStr: string) {
      handleGetList_gym(type, queryStr)
   }
   // 获取酷乐秀数据
   function handleGetList_klx(type: string, albumId: string, queryStr: string) {
      // 当没有专辑id时候 不查询
      if (!albumId) {
         return
      }
      if (coursewareController) {
         coursewareController.abort()
      }
      coursewareController = new AbortController()
      loading.value = true
      httpAjax(queryLessonCourseware_klx, type, albumId, coursewareController).then(res => {
         // 自己关闭的时候不取消加载
         if (res.code === CODE_ERR_CANCELED) {
            return
         }
         loading.value = false
         if (res.code === 200) {
            const data = (res.data?.rows || []).reduce((arr: any[], item: any) => {
               if (queryStr ? item.musicSheetName.includes(queryStr) : true) {
                  arr.push({
                     name: item.musicSheetName,
                     type: item.subjectId,
                     img: item.titleImg,
                     id: item.id,
                     courseNum: item.courseNum
                  })
               }
               return arr
            }, [])
            listData.value = chunkArray(data, 5)
         } else {
            if (res.code !== 511) {
               ElMessage({
                  showClose: true,
                  message: res.message,
                  type: "error"
               })
            }
         }
      })
   }
   // 管乐迷数据查询
   function handleQueryGetList_klx(type: string, albumId: string, queryStr: string) {
      handleGetList_klx(type, albumId, queryStr)
   }
   return { loading, listData, albumId, albumOpt, handleGetList, handleListQuery }
}

/**
 * 列表详情数据
 */
type listDetail = {
   name: string
   id: string
   useNum?: number
   lockFlag?: boolean
}[]
type listDetailType = [listDetail, listDetail]
export const useDataDetailList = () => {
   const userStoreHook = userStore()
   const listData = shallowRef<listDetail[][]>([])
   const pageNum = ref<number>(0)
   const loading = ref(false)
   const listDetailData = computed<listDetailType>(() => {
      const data = listData.value[pageNum.value] || []
      return [data[0] || [], data[1] || []]
   })
   function handleGetDetailList(id: string) {
      //  GYM,GYT,KLX 区分   查询详情列表
      if (userStoreHook.roles === "GYM") {
         handleGetDetaList_gym(id)
      } else if (userStoreHook.roles === "GYT") {
         handleGetDetailList_gyt(id)
      } else if (userStoreHook.roles === "KLX") {
         handleGetDetailList_klx(id)
      }
   }
   function handlePage(type: "next" | "prev") {
      type === "next" ? pageNum.value++ : pageNum.value--
   }
   // 获取管乐迷
   function handleGetDetaList_gym(id: string) {
      loading.value = true
      httpAjaxErrMsg(getLessonCoursewareCourseList_gym, id).then(res => {
         loading.value = false
         if (res.code === 200) {
            const data = (res.data || []).map((item: any) => {
               return {
                  name: item.coursewareDetailName,
                  id: item.coursewareDetailId
               }
            })
            listData.value = chunkArray(chunkArray(data, 7), 2)
         }
      })
   }
   // 获取管乐团
   function handleGetDetailList_gyt(id: string) {
      loading.value = true
      httpAjaxErrMsg(getMyCoursewareDetail_gyt, id).then(res => {
         loading.value = false
         if (res.code === 200) {
            const data = (res.data || []).map((item: any) => {
               return {
                  name: item.coursewareDetailName,
                  id: item.lessonCoursewareDetailId,
                  useNum: item.useNum,
                  lockFlag: false // 云课堂默认不锁
               }
            })
            listData.value = chunkArray(chunkArray(data, 7), 2)
         }
      })
   }
   // 获取酷乐秀
   function handleGetDetailList_klx(id: string) {
      loading.value = true
      httpAjaxErrMsg(getLessonCoursewareCourseList_klx, id).then(res => {
         loading.value = false
         if (res.code === 200) {
            const data = (res.data || []).map((item: any) => {
               return {
                  name: item.coursewareDetailName,
                  id: item.coursewareDetailId
               }
            })
            listData.value = chunkArray(chunkArray(data, 7), 2)
         }
      })
   }
   return {
      handleGetDetailList,
      loading,
      listDetailData,
      listData,
      pageNum,
      handlePage
   }
}

function chunkArray(array: any[], size: number) {
   const result = []
   for (let i = 0; i < array.length; i += size) {
      result.push(array.slice(i, i + size))
   }
   return result
}
