<?php
// --- Auth gate: PDF standalone, sin sesión no se sirve ---
if (session_status() !== PHP_SESSION_ACTIVE) { @session_start(); }
if (empty($_SESSION['user_id'])) { header('Location: index.php?view=login'); exit; }
// --- end auth gate ---
/**
 * Standalone PDF generator for Contracts module
 */
define('ROOT', dirname(__FILE__));

session_start();
if (!isset($_SESSION['user_id'])) { die('Acceso denegado'); }

require_once('core/autoload.php');
Core::$root = '';
require_once('core/app/autoload.php');

$c = ContractData::getById(intval($_GET['id'] ?? 0));
if (!$c) { header('Location: ./?view=contracts'); exit; }
$config = ContractConfigData::get();
if (!$config) { $config = new ContractConfigData(); }

// Etiqueta según tipo de contrato
$typeLabels = array('servicio'=>'PRESTACION DE SERVICIOS','consultoria'=>'CONSULTORIA EXTERNA','alquiler'=>'ALQUILER DE ESPACIO');
$docLabel = $typeLabels[$c->contract_type] ?? strtoupper($c->contract_type);

require_once('fpdf/fpdf.php');

$pdf = new FPDF('P','mm','Letter');
$pdf->SetAutoPageBreak(true, 25);
$pdf->SetMargins(25, 15, 25);
$pdf->AddPage();

// === HEADER: Logo + CITE ===
$logoPath = $_SERVER['DOCUMENT_ROOT'] . '/ser/ser_logo.jpg';
if (file_exists($logoPath)) {
    $pdf->Image($logoPath, 25, 12, 24);
}
$pdf->SetY(12);
$pdf->SetFont('Helvetica', 'B', 10);
$pdf->Cell(0, 6, 'Contrato Nro: ' . str_pad($c->id, 3, '0', STR_PAD_LEFT) . '/2026', 0, 1, 'R');
// Tipo de contrato centrado
$pdf->SetFont('Helvetica', 'B', 14);
$pdf->Cell(0, 7, utf8_decode($docLabel), 0, 1, 'C');

// Linea separadora
$pdf->Line(25, 40, 190, 40);
$pdf->SetY(47);

// === TITULO ===
$pdf->SetFont('Helvetica', 'B', 12);
$pdf->Cell(0, 7, utf8_decode('CONTRATO DE ' . $docLabel), 0, 1, 'C');
$pdf->Ln(3);

$pdf->SetFont('Helvetica', '', 11);
$pdf->Cell(0, 6, utf8_decode('Que celebran ' . $c->part_a_name . ' (PRIMERA PARTE) y ' . $c->part_b_name . ' (SEGUNDA PARTE)'), 0, 1, 'C');
$pdf->Ln(10);

// === PARTES ===
$pdf->SetFont('Helvetica', 'B', 11);
$pdf->Cell(0, 7, utf8_decode('PRIMERA PARTE:'), 0, 1);
$pdf->SetFont('Helvetica', '', 11);
$pdf->Cell(0, 6, utf8_decode($c->part_a_name), 0, 1);
$pdf->SetFont('Helvetica', 'I', 10);
$pdf->Cell(0, 5, utf8_decode('C.I.: ' . $c->part_a_ci . ' | Cargo: ' . $c->part_a_cargo), 0, 1);
$pdf->Ln(5);

$pdf->SetFont('Helvetica', 'B', 11);
$pdf->Cell(0, 7, utf8_decode('SEGUNDA PARTE:'), 0, 1);
$pdf->SetFont('Helvetica', '', 11);
$pdf->Cell(0, 6, utf8_decode($c->part_b_name), 0, 1);
$pdf->SetFont('Helvetica', 'I', 10);
$pdf->Cell(0, 5, utf8_decode('C.I.: ' . $c->part_b_ci . ' | Cargo: ' . $c->part_b_cargo), 0, 1);
if (!empty($c->part_b_address)) {
    $pdf->Cell(0, 5, utf8_decode('Dirección: ' . $c->part_b_address), 0, 1);
}
$pdf->Ln(10);

// === VIGENCIA ===
$pdf->SetDrawColor(0);
$pdf->SetFont('Helvetica', 'B', 11);
$pdf->Cell(0, 7, utf8_decode('VIGENCIA: ' . date("d/m/Y", strtotime($c->start_date)) . ' al ' . date("d/m/Y", strtotime($c->end_date))), 0, 1, 'C');
$pdf->Rect(60, $pdf->GetY()-4, 96, 14);
$pdf->Ln(15);

// === MONTO ===
$pdf->SetFont('Helvetica', 'B', 13);
$pdf->Cell(0, 7, utf8_decode('MONTO TOTAL: Bs. ' . number_format($c->amount, 2, ',', '.')), 0, 1, 'C');
if (!empty($c->payment_terms)) {
    $pdf->SetFont('Helvetica', 'I', 10);
    $pdf->Cell(0, 5, utf8_decode('(' . $c->payment_terms . ')'), 0, 1, 'C');
}
$pdf->Ln(15);

// === CLÁUSULAS ===
$pdf->SetFont('Helvetica', '', 10);
$pdf->MultiCell(0, 5.5, utf8_decode($c->clauses));
$pdf->Ln(15);

// === FIRMAS ===
$pdf->SetAutoPageBreak(false);
$pdf->SetY(-45);
$line_width = 65;
$pdf->SetFont('Helvetica', 'B', 10);

// Primera firma (izquierda)
$pdf->Line(25, $pdf->GetY(), 25 + $line_width, $pdf->GetY());
$pdf->Ln(3);
$pdf->Cell($line_width, 6, utf8_decode($c->signature_a_name), 0, 0, 'L');
$pdf->Cell(10); // espacio
$pdf->Cell($line_width, 6, utf8_decode($c->signature_b_name), 0, 1, 'R');

$pdf->SetFont('Helvetica', '', 9);
$pdf->Cell($line_width, 5, utf8_decode($c->signature_a_cargo), 0, 0, 'L');
$pdf->Cell(10);
$pdf->Cell($line_width, 5, utf8_decode($c->signature_b_cargo), 0, 1, 'R');

$pdf->Cell($line_width, 5, utf8_decode($config->org_name), 0, 0, 'L');
$pdf->Ln(15);

// === PIE DE PAGINA ===
$pdf->Line(25, $pdf->GetY(), 190, $pdf->GetY());
$pdf->Ln(2);
$pdf->SetFont('Helvetica', '', 7);
$pdf->Cell(0, 4, utf8_decode($config->org_name . ' - ' . $config->org_address), 0, 1, 'C');
$pdf->Cell(0, 4, utf8_decode('Telf.: ' . $config->org_phone . ' - ' . $config->org_email . ' - ' . $config->org_website), 0, 1, 'C');

$pdf->Output('Contrato_' . str_pad($c->id, 3, '0', STR_PAD_LEFT) . '_2026' . '.pdf', 'I');