import {useEffect, useState} from "react";
import {Image, View} from "@tarojs/components";
import Taro from "@tarojs/taro";
import {Profile} from '@/store/profile'
import styles from '../../my.module.scss'
import dep from '@/static/img/dep.png'
import cur from '@/static/img/cur.png'
import userInfo from '@/static/img/userInfo.png'
import spotMeeting from '@/static/img/spotMeeting.png'
import course from '@/static/img/course.png'
import {userApi} from "@/api";

interface List {
  title: string;
  src: string;
  router: string;
}

const Service = () => {
  const [list, setList] = useState<List[]>([
    {title: '历史记录', src: cur, router: '/pages/business/history/history'},
    {title: '个人中心', src: userInfo, router: '/pages/business/userInfo/userInfo'},
  ])

  const {token,user, setUser} = Profile.useContainer()

  Taro.useDidShow(async () => {
    if (user?.id) {
      const res = await userApi.info(String(user.id))
      setUser(res)
    }
  })

  useEffect(() => {
    const oldList: List[] = JSON.parse(JSON.stringify(list))
    if(token){
      if ([1, 2].includes(user?.role_type || 0)) {
        oldList.unshift(...[
          {title: '部门管理', src: dep, router: '/pages/manage/depAdmin/depAdmin'},
          // {title: '课程市场', src: buy, router: '/pages/manage/curriculum/curriculum'},
          {title: '现场会', src: spotMeeting, router: '/pages/manage/spotMeeting/spotMeeting'},
          {title: '课程管理', src: course, router: '/pages/manage/courseAdmin/courseAdmin'},
        ])
        setList(oldList)
      }
    }
  }, [])

  function jump(url: string) {
    if(!token){
      Taro.navigateTo({url:'/pages/login/login'})
      return
    }
    Taro.navigateTo({url})
  }

  return (
    <View className={'mt-3 ' + styles.tool}>
      <View className='font-weight font-32'>工具服务</View>
      <View className={'mt-4 ' + styles.service}>
        {
          list.map(d => {
            return (
              <View onClick={() => jump(d.router)} key={d.title}>
                <Image src={d.src} mode='aspectFit' className={styles.serviceImage}/>
                <View>{d.title}</View>
              </View>
            )
          })
        }
      </View>
    </View>
  )
}

export default Service
