<?php
require('fpdf186/fpdf.php');
// require_once 'jpgraph/src/jpgraph.php';
// require_once 'jpgraph/src/jpgraph_line.php';
include "core/autoload.php";
include "core/app/model/HistoryData.php";
include "core/app/model/PatientData.php";
include "core/app/model/PlaceData.php";
include "core/app/model/DiseaseData.php";
include "core/app/model/MaritalData.php";
include "core/app/model/CountryData.php";
include "core/app/model/MedicineData.php";
include "core/app/model/MedicinePacientData.php";
include "core/app/model/HistoryPacientData.php";

class PDF extends FPDF
{
    // Encabezado de la página
    function Header()
    {
        // Logo
        $this->Image('assets/images/icons/oxygenesis.png', 10, 3, 50);
        //$this->Image('forza.png', 160, 11, 40); // Cambia 'logo.png' por la ruta de tu logo
        // Título
        $this->SetFont('Arial', 'B', 12);
        $this->Ln(10);
        $this->Cell(160, 0, utf8_decode('FECHA:'), 0, 1, 'R');
        $this->Cell(170, 15, utf8_decode('HISTORIA CLINICA'), 0, 1, 'C');

        $this->Ln(1); // Salto de línea
    }

    // Pie de página
    function Footer()
    {
        $this->SetY(-15); // Posición a 1.5 cm del final
        $this->SetFont('Arial', 'I', 8);
        $this->Cell(0, 10, 'FIRMA DEL PACIENTE', 0, 0, 'C');
    }

    // Título de sección
    function SectionTitle($title)
    {
        $this->SetFont('Arial', 'B', 12);
        $this->Cell(0, 10, $title, 0, 1, 'L');
        $this->Ln(5);
    }

    // Tabla genérica
    function BasicTable($header, $data)
    {
        $this->SetFont('Arial', 'B', 10);
        foreach ($header as $col) {
            $this->Cell(40, 7, $col, 1, 0, 'C');
        }
        $this->Ln();

        $this->SetFont('Arial', '', 10);
        foreach ($data as $row) {
            foreach ($row as $col) {
                $this->Cell(40, 6, $col, 1);
            }
            $this->Ln();
        }
    }
    function DrawCheckbox($pdf, $checked, $x, $y)
    {
        $pdf->SetLineWidth(0.2);
        $pdf->Rect($x, $y, 3, 3); // Empty checkbox
        if ($checked) {
            $pdf->Line($x, $y, $x + 3, $y + 3);
            $pdf->Line($x + 3, $y, $x, $y + 3);
        }
    }
}
$pdf = new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
$u = PatientData::getById($_GET['id']);
$c = HistoryData::getAllBy('pacient_id', $u->id);
$hist = HistoryPacientData::getBy('pacient_id', $_GET["id"]);

$fechaNacimiento = new DateTime($u->day_of_birth);
$hoy = new DateTime();
$edad = $fechaNacimiento->diff($hoy)->y;
$pdf->Ln(5);
$pdf->SetFont('Arial', 'B', 9);
$pdf->MultiCell(0, 5, 'FILIACION', 0, 1);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(60, 5, 'AP. PATERNO:' . $u->lastname, 0, 0);
$pdf->Cell(60, 5, 'AP. MATERNO:' . $u->lastname2, 0, 0);
$pdf->Cell(70, 5, 'NOMBRE:' . $u->name, 0, 1);
$pdf->Cell(30, 5, 'EDAD:' . $edad, 0, 0);
$pdf->Cell(60, 5, 'SEXO:' . $u->gender, 0, 0);
$pdf->Cell(50, 5, 'FECHA NAC.:' . $u->day_of_birth, 0, 0);
$pdf->Cell(50, 5, 'LUGAR NAC.:' . $u->getPlaceB()->name, 0, 1);
$pdf->Cell(100, 5, 'CIUDAD DONDE VIVE.:' . $u->getPlaceL()->name, 0, 0);
$pdf->Cell(50, 5, 'NACIONALIDAD.:' . $u->getCountry()->name, 0, 1);
$pdf->Cell(100, 5, 'OCUPACION:' . $u->occupation, 0, 0);
$pdf->Cell(60, 5, 'CENTRO DE TRABAJO:' . $u->work, 0, 1);
$pdf->Cell(100, 5, 'ESTADO CIVIL:' . $u->getMarital()->name, 0, 0);
$pdf->Cell(70, 5, 'C.I.:' . $u->ci, 0, 1);
$pdf->Cell(100, 5, 'TELEFONO FIJO:' . $u->phone_h, 0, 0);
$pdf->Cell(90, 5, 'CELULAR:' . $u->phone, 0, 1);
$pdf->Cell(100, 5, 'DOMICILIO:' . $u->address, 0, 0);
$pdf->Cell(90, 5, 'ZONA:' . $u->district, 0, 1);
$pdf->Cell(90, 5, 'CORREO ELECTRONICO:' . $u->email, 0, 1);
$pdf->Ln(5);
$pdf->SetFont('Arial', 'B', 9);
$pdf->MultiCell(0, 5, 'ANTECEDENTES', 0, 1);
$pdf->Ln(5);
$pdf->MultiCell(0, 5, 'ENFERMEDADES ACTUALES:', 0, 1);
$pdf->Ln(5);
// Add checkbox drawing function
function DrawCheckbox($pdf, $checked, $x, $y)
{
    $pdf->SetLineWidth(0.2);
    $pdf->Rect($x, $y, 3, 3); // Empty checkbox
    if ($checked) {
        $pdf->Line($x, $y, $x + 3, $y + 3);
        $pdf->Line($x + 3, $y, $x, $y + 3);
    }
}
// Draw checkboxes with labels
$y = $pdf->GetY();

$xOffset = 40; // Space between checkboxes horizontally
$yOffset = 6;  // Space between rows
$itemsPerRow = 3; // Number of items per row
$currentX = 15; // Initial X position
$currentItem = 0;

foreach ($c as $item) {
    if ($item->val == 1) {
        $xPos = $currentX + ($currentItem % $itemsPerRow) * $xOffset;
        $yPos = $y + floor($currentItem / $itemsPerRow) * $yOffset;

        DrawCheckbox($pdf, $item->val == 1, $xPos, $yPos);
        $pdf->SetXY($xPos + 5, $yPos);
        $pdf->Cell(30, 3, utf8_decode($item->getDisease()->description), 0, 0);

        $currentItem++;

        // Add line break after each row
        if ($currentItem % $itemsPerRow == 0) {
            $pdf->Ln();
        }
    }
}
// Add spacing between sections
$pdf->Ln(10);
$pdf->MultiCell(0, 5, 'MEDICAMENTOS QUE USA ACTUALMENTE:', 0, 1);
$pdf->Ln(5);

// Reset Y position for medicines section
$y = $pdf->GetY();

// Get medicines data
$medicines = MedicinePacientData::getAllBy('pacient_id', $u->id);

$currentItem = 0; // Reset counter for new section
// Increase cell width and horizontal spacing
$xOffset = 65; // Increased from 40 to 65 for more horizontal space
$yOffset = 6;  // Keep the same vertical spacing
$itemsPerRow = 3; // Keep the same number of items per row
$currentX = 15; // Keep the same initial X position

foreach ($medicines as $item) {
    if ($item->val == 1) {
        $xPos = $currentX + ($currentItem % $itemsPerRow) * $xOffset;
        $yPos = $y + floor($currentItem / $itemsPerRow) * $yOffset;

        DrawCheckbox($pdf, $item->val == 1, $xPos, $yPos);
        $pdf->SetXY($xPos + 5, $yPos);
        $pdf->Cell(60, 3, utf8_decode($item->getMedicine()->description), 0, 0); // Increased from 50 to 60

        $currentItem++;

        if ($currentItem % $itemsPerRow == 0) {
            $pdf->Ln();
        }
    }
}
$pdf->Ln(10);
$pdf->SetFont('Arial', 'B', 9);
$pdf->MultiCell(0, 5, 'RECIBIO ESTE TRATAMIENTO ANTES?:', 0, 1);
$pdf->SetFont('Arial', '', 9);
$pdf->MultiCell(0, 5, $hist->befor, 0, 1);
$pdf->Ln(10);
$pdf->SetFont('Arial', 'B', 9);
$pdf->MultiCell(0, 5, 'TRATAMIENTOS POSTERIORES:', 0, 1);
$pdf->SetFont('Arial', '', 9);
$pdf->MultiCell(0, 5, $hist->previous, 0, 1);
$pdf->Ln(10);
$pdf->SetFont('Arial', 'B', 9);
$pdf->MultiCell(0, 5, 'CIRUGIAS ANTERIORES:', 0, 1);
$pdf->SetFont('Arial', '', 9);
$pdf->MultiCell(0, 5, $hist->surgery, 0, 1);
$pdf->Ln(10);
$pdf->SetFont('Arial', 'B', 9);
$pdf->MultiCell(0, 5, 'ALERGIA A MEDICAMENTOS:', 0, 1);
$pdf->SetFont('Arial', '', 9);
$pdf->MultiCell(0, 5, $hist->alergy, 0, 1);
$pdf->Ln(10);
$pdf->SetFont('Arial', 'B', 9);
$pdf->MultiCell(0, 5, 'HABITOS NOCIVOS:', 0, 1);
$pdf->Ln(5);
$currentItem = 0; // Reset counter for new section
// Increase cell width and horizontal spacing
$xOffset = 65; // Increased from 40 to 65 for more horizontal space
$yOffset = 6;  // Keep the same vertical spacing
$itemsPerRow = 3; // Keep the same number of items per row
$currentX = 15; // Keep the same initial X position
// Reset Y position for habits section
$y = $pdf->GetY();
$xOffset = 65;
$currentX = 15;

// Smoke habits
$xPos = $currentX;
$yPos = $y;
DrawCheckbox($pdf, !empty($hist->smoke), $xPos, $yPos);
$pdf->SetXY($xPos + 5, $yPos);
$pdf->Cell(60, 3, 'Tabaco ' , 0, 0);

// Alcohol habits
$xPos = $currentX + $xOffset;
DrawCheckbox($pdf, !empty($hist->alcohol), $xPos, $yPos);
$pdf->SetXY($xPos + 5, $yPos);
$pdf->Cell(60, 3, 'Alcohol:' , 0, 0);

// Drugs habits
$xPos = $currentX + ($xOffset * 2);
DrawCheckbox($pdf, !empty($hist->drugs), $xPos, $yPos);
$pdf->SetXY($xPos + 5, $yPos);
$pdf->Cell(60, 3, 'Drogas ' , 0, 1); // Increased from 50 to 60
$pdf->Ln(5);
$pdf->SetFont('Arial', 'B', 9);
$pdf->MultiCell(0, 5, 'FRECUENCIA:', 0, 1);
$pdf->SetFont('Arial', '', 9);
$pdf->MultiCell(0, 5, $hist->frequency, 0, 1);
$pdf->Output();
