import {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)
    }
  })

  Taro.useDidShow(() => {
    if ([1, 2].includes(user?.role_type || 0)) {
      setList([
        {title: '部门管理', src: dep, router: '/pages/manage/depAdmin/depAdmin'},
        {title: '现场会', src: spotMeeting, router: '/pages/manage/spotMeeting/spotMeeting'},
        {title: '课程管理', src: course, router: '/pages/manage/courseAdmin/courseAdmin'},
        {title: '个人中心', src: userInfo, router: '/pages/business/userInfo/userInfo'},
        {title: '历史记录', src: cur, router: '/pages/business/history/history'},
      ])
    } else {
      setList([
        {title: '历史记录', src: cur, router: '/pages/business/history/history'},
        {title: '个人中心', src: userInfo, router: '/pages/business/userInfo/userInfo'},
      ])
    }
  })

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

  return (
    <View className={styles.tool}>
      <View className='font-weight font-32'>工具服务</View>
      <View className={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
