import React from 'react'
import FormNovaConsulta from '../components/AgendarConsulta/FormNovaConsulta'
import API_KEY from '../components/utils/apiKeys'
import { useAuth } from '../components/utils/AuthProvider'
import { useEffect,useState } from 'react'
import dayjs from 'dayjs'
import { UserInfos } from '../components/utils/Functions-Endpoints/General'
const AgendamentoCadastroManager = ({setPageConsulta}) => {

    const {getAuthorizationHeader} = useAuth()
    const [agendamento, setAgendamento] = useState({status:'confirmed'})
    const [idUsuario, setIDusuario] = useState('0')

  let authHeader = getAuthorizationHeader() 


  useEffect(() => {

    const ColherInfoUsuario =async () => {
      const result = await UserInfos(authHeader)
    
      setIDusuario(result?.profile?.id)
    
    }
    ColherInfoUsuario()
    

  }, [])



      const handleSave = (Dict) => {
        let DataAtual = dayjs()
        var myHeaders = new Headers();
        myHeaders.append("apikey", API_KEY);
        myHeaders.append("Authorization", authHeader);
        myHeaders.append("Content-Type", "application/json");
    
    var raw = JSON.stringify({
       "patient_id": Dict.patient_id,
       "doctor_id": Dict.doctor_id,
       "scheduled_at": `${Dict.dataAtendimento}T${Dict.horarioInicio}:00.000Z`,
       "duration_minutes": 30,
       "appointment_type": Dict.tipo_consulta,
       
       "patient_notes": "Prefiro horário pela manhã",
       "insurance_provider": Dict.convenio,
       "status": Dict.status,
       "created_by": idUsuario
    });
    
    var requestOptions = {
       method: 'POST',
       headers: myHeaders,
       body: raw,
       redirect: 'follow'
    };
    
    fetch("https://yuanqfswhberkoevtmfr.supabase.co/rest/v1/appointments", requestOptions)
       .then(response => response.text())
       .then(result => console.log(result))
       .catch(error => console.log('error', error));
    
      }

  return (
    <div>

    <FormNovaConsulta onSave={handleSave} agendamento={agendamento} setAgendamento={setAgendamento} onCancel={() => setPageConsulta(false)}/>

    </div>
  )
}

export default AgendamentoCadastroManager