import {FC, useState} from "react";
import {View} from "@tarojs/components";
import {ManageApi} from "@/api/manage";
import {Profile} from '@/store'
import PopPut from "@/components/popPut/popPut";
import Taro from "@tarojs/taro";


interface Props {
  cur_id: number
  name?: string
}

const Dep: FC<Props> = ({cur_id}: Props) => {
  const [bindDep, setBindDep] = useState<Department[]>([])

  async function getBind() {
    try {
      const res = await ManageApi.bingDep(cur_id)
      setBindDep(res.department)
    } catch (e) {
    }
  }

  function jump(item: Department) {
    Taro.showModal({
      title: "是否查看" + item.name,
      success({confirm}) {
        confirm && Taro.navigateTo({url: `/pages/manage/depCur/depCur?id=${item.id}`})
      }
    })
  }

  Taro.useDidShow(() => {
    getBind()
  })

  return (<>
    {bindDep && <View className='header'>
      <View className='font-weight font-26'>课程已分配部门</View>
      {bindDep && bindDep.map(d => (
        <PopPut title={d.name} height={40} content={'查看'} onClick={() => jump(d)}/>
      ))}
    </View>
    }
  </>)
}

const StudentRecord: FC<Props> = ({cur_id, name}: Props) => {
  function jump() {
    Taro.navigateTo({url: `/pages/manage/studentRecord/studentRecord?cur_id=${cur_id}&name=${name}`})
  }

  return (
    <View className='header'>
      <PopPut title={'学员学习记录'} height={40} content={'查看'} onClick={jump}/>
    </View>
  )
}

const ContainDeps: FC<Props> = ({cur_id,name}: Props) => {
  const {user} = Profile.useContainer()

  return (
    <>
      {user?.role_type !== 0 ?
        <View>
          <Dep cur_id={cur_id}/>
          <StudentRecord cur_id={cur_id} name={name}/>
        </View>
        : null}
    </>
  )
}
export default ContainDeps
