import request from '@/helpers/request'
import { state } from '@/state'
import { Image, Loading } from 'vant'
import { defineComponent, reactive, onMounted, watch } from 'vue'
import { RouterView, useRoute, useRouter } from 'vue-router'
import { getImage } from './images'
import styles from './index.module.less'
import MusicList from './music-list'

export default defineComponent({
  name: 'accompany',
  setup(props, ctx) {
    const route = useRoute()
    const router = useRouter()
    const data = reactive({
      childPath: '',
      svgs: [] as any,
      musicTreeActive: ''
    })
    const getTree = async () => {
      try {
        const res: any = await request.get(
          state.platformApi + '/musicSheetCategories/queryTree?enable=true'
        )
        if (Array.isArray(res?.data)) {
          data.svgs = res.data.map((n: any, index: number) => {
            return {
              ...n,
              id: n.id,
              icon: n.coverImg,
              btnIcon: getImage('b2.svg'),
              color: '#9881FF',
              title: n.name,
              subtitle: '',
              btnText: 'GO >'
            }
          })
          console.log('🚀 ~ data.svgs', data.svgs)
        }
      } catch (error) {
        console.log(error)
      }
      handleOpen()
    }
    const handleOpen = () => {
      const src: string = route?.path || 'accompany'
      data.childPath = src.replace(/\//g, '') || 'accompany'
      console.log('🚀 ~ route.path', data.childPath)
    }
    watch(
      () => route.path,
      () => {
        handleOpen()
      }
    )
    onMounted(() => {
      getTree()
    })
    return () => (
      <div class={styles.accompany}>
        <div class={styles.accompanyCategory}>
          {data.svgs.map((item: any) => {
            return (
              <div
                class={styles.container}
                onClick={() => {
                  router.push({
                    path: '/musicList',
                    query: {
                      categorieid: item.id
                    }
                  })
                }}
              >
                <Image src={item.icon} width="100%" height="100%">
                  {{
                    loading: () => <Loading type="spinner" size="20" />
                  }}
                </Image>
              </div>
            )
          })}
        </div>
      </div>
    )
  }
})
