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

import {
   queryLessonCourseware_gym,
   getLessonCoursewareCourseList_gym,
   getMyCourseware_gyt,
   getMyCoursewareDetail_gyt
} 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 loading = ref(false)
   let coursewareController: AbortController

   function handleGetList() {
      userStoreHook.roles === "GYM" ? handleGetList_gym("", "") : handleGetList_gyt()
   }
   function handleListQuery(type: string, queryStr: string) {
      userStoreHook.roles === "GYM" ? handleQueryGetList_gym(type, queryStr) : handleQueryGetList_gyt(type, 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 {
            ElMessage({
               showClose: true,
               message: res.message,
               type: "error"
            })
         }
      })
   }
   // 管乐迷数据查询
   function handleQueryGetList_gym(type: string, queryStr: string) {
      handleGetList_gym(type, queryStr)
   }

   return { loading, listData, 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) {
      userStoreHook.roles === "GYM" ? handleGetDetaList_gym(id) : handleGetDetailList_gyt(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)
         }
      })
   }

   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
}
