import React from 'react'
import PatientForm from '../components/patients/PatientForm'
import {useEffect, useState} from 'react'
import { GetPatientByID } from '../components/utils/Functions-Endpoints/Patient'
import API_KEY from '../components/utils/apiKeys'
import {useNavigate, useParams } from 'react-router-dom'
import { useAuth } from '../components/utils/AuthProvider'

const EditPage = ({DictInfo}) => {
  const navigate = useNavigate()
  const [PatientToPUT, setPatientPUT] = useState({})

   const { getAuthorizationHeader, isAuthenticated } = useAuth();

useEffect(() => {
	setPatientPUT(DictInfo)
}, [DictInfo])

const HandlePutPatient = async () => { 
  const authHeader = getAuthorizationHeader()
  

  var myHeaders = new Headers();
  myHeaders.append('apikey', API_KEY)
  myHeaders.append("Authorization", authHeader);
  myHeaders.append("Content-Type", "application/json");

  var raw = JSON.stringify({...PatientToPUT, bmi:Number(PatientToPUT) || null});

  console.log("Enviando  atualização:", PatientToPUT);

  var requestOptions = {
    method: 'PATCH',
    headers: myHeaders,
    body: raw,
    redirect: 'follow'
  };

    fetch(`https://yuanqfswhberkoevtmfr.supabase.co/rest/v1/patients?id=eq.${PatientToPUT.id}`,requestOptions) 
      .then(response => response.json)   
      .then(result => console.log(result))
      .catch(console.log("erro"))
   
};

  return (
    <div>

    <PatientForm
    onSave={HandlePutPatient}
    
    formData={PatientToPUT}
    setFormData={setPatientPUT}
    />
    
    </div>
  )
}

export default EditPage
