import { api } from "@nice/client";
import { createContext, ReactNode, useContext, useEffect } from "react";

interface DailyContextProviderProps{
    staffsMsg:{
        id:string,
        showname:string,
        username:string
    }[],
    staffsLoading:boolean
}

interface DailyContextProps{
    children:ReactNode
}

const DailyContextProvider = createContext<DailyContextProviderProps>(null)

export default function DailyContext({children}:DailyContextProps){
    // 获取当前单位下的所有staff的记录
    const {data:staffs,isLoading:staffsLoading} = api.staff.findByDept.useQuery({
        deptId:"cm84jt1gv000do5gqsxx68iko" 
    })
    const staffsMsg = staffs?.map((staff)=>{
        return {
            id:staff.id,
            showname:staff.showname,
            username:staff.username
        }
    })
    useEffect(()=>{
        console.log(staffs)
    },[staffs])
    return <DailyContextProvider.Provider value={{
        staffsMsg,
        staffsLoading
    }}>
        {children}
    </DailyContextProvider.Provider>
}

export function useDaily(){
    const daily = useContext(DailyContextProvider)
    if(!daily) {throw new Error("useParams must be used within a ParamsProvider");}
    return daily
}