<?php
// complementary_study_pdf.php — PDF for complementary study order
include __DIR__ . '/core/autoload.php';
require_once 'tcpdf/tcpdf.php';

$id = intval($_GET['id'] ?? 0);
if (!$id) { die('Estudio ID requerido'); }

$con = Database::getCon();
$study = $con->query("SELECT cs.*, p.name AS patient_name, p.lastname AS patient_lastname, p.dni AS patient_dni, u.name AS creator_name, u.lastname AS creator_lastname, m.name AS medic_name, m.lastname AS medic_lastname FROM complementary_study cs LEFT JOIN pacient p ON p.id=cs.pacient_id LEFT JOIN user u ON u.id=cs.created_by LEFT JOIN user m ON m.id=cs.referring_medic_id WHERE cs.id=$id")->fetch_object();
if (!$study) { die('Estudio no encontrado'); }

$services = $con->query("SELECT css.*, ss.name AS service_name FROM complementary_study_service css JOIN study_service ss ON ss.id=css.service_id WHERE css.study_id=$id ORDER BY css.item_number")->fetch_all(MYSQLI_ASSOC);
$total = 0; foreach ($services as $s) { $total += floatval($s['cost']); }

$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(15, 15, 15);
$pdf->SetAutoPageBreak(true, 15);
$pdf->AddPage();
$pdf->SetFont('helvetica', 'B', 16);
$pdf->Cell(0, 8, 'SAN JORGE - Clinica Especializada', 0, 1, 'C');
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(0, 5, 'Orden de Estudio Complementario #' . $study->id, 0, 1, 'C');
$pdf->Ln(5);

$pdf->SetFont('helvetica', 'B', 11);
$pdf->Cell(35, 6, 'Fecha:', 0); $pdf->SetFont('helvetica', '', 11); $pdf->Cell(0, 6, date('d/m/Y', strtotime($study->fecha)), 0, 1);
$pdf->SetFont('helvetica', 'B', 11); $pdf->Cell(35, 6, 'Estado:', 0);
$pdf->SetFont('helvetica', '', 11); $pdf->Cell(0, 6, ucfirst($study->status), 0, 1);
$pdf->SetFont('helvetica', 'B', 11); $pdf->Cell(35, 6, 'Creado por:', 0);
$pdf->SetFont('helvetica', '', 11); $pdf->Cell(0, 6, trim(($study->creator_name ?? '') . ' ' . ($study->creator_lastname ?? '')), 0, 1);
if ($study->medic_name) {
    $pdf->SetFont('helvetica', 'B', 11); $pdf->Cell(35, 6, 'Medico referente:', 0);
    $pdf->SetFont('helvetica', '', 11); $pdf->Cell(0, 6, trim($study->medic_name . ' ' . $study->medic_lastname), 0, 1);
}
$pdf->Ln(3);

$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(0, 6, 'Paciente', 0, 1);
$pdf->SetFont('helvetica', '', 11);
$pdf->Cell(35, 6, 'Nombre:', 0); $pdf->Cell(0, 6, trim($study->patient_name . ' ' . $study->patient_lastname), 0, 1);
$pdf->Cell(35, 6, 'CI:', 0); $pdf->Cell(0, 6, $study->patient_dni ?: '-', 0, 1);
$pdf->Ln(3);

$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(0, 6, 'Servicios', 0, 1);
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Cell(120, 7, 'Servicio', 1, 0, 'L');
$pdf->Cell(35, 7, 'Costo (Bs)', 1, 0, 'R');
$pdf->Cell(25, 7, 'Comision %', 1, 1, 'R');
$pdf->SetFont('helvetica', '', 10);
foreach ($services as $s) {
    $pdf->Cell(120, 6, $s['service_name'], 1, 0, 'L');
    $pdf->Cell(35, 6, number_format($s['cost'], 2), 1, 0, 'R');
    $pdf->Cell(25, 6, number_format($s['commission_pct'], 2) . '%', 1, 1, 'R');
}
$pdf->SetFont('helvetica', 'B', 11);
$pdf->Cell(120, 7, 'TOTAL', 1, 0, 'R');
$pdf->Cell(60, 7, 'Bs ' . number_format($total, 2), 1, 1, 'R');

if (!empty($study->notes)) {
    $pdf->Ln(5);
    $pdf->SetFont('helvetica', 'B', 11);
    $pdf->Cell(0, 6, 'Notas', 0, 1);
    $pdf->SetFont('helvetica', '', 10);
    $pdf->MultiCell(0, 5, $study->notes, 0, 'L');
}

$pdf->Output('estudio_' . $study->id . '.pdf', 'I');
