<?php
require("fpdf/fpdf.php");

function sanitize($data) {
    return htmlspecialchars(strip_tags(trim($data)));
}

// Get POST data
$post_applied_for = sanitize($_POST['post_applied_for'] ?? '');
$name             = sanitize($_POST['name'] ?? '');

// --- Experience (India)
$india_experience = [];
if (isset($_POST['india_company'])) {
    foreach ($_POST['india_company'] as $i => $comp) {
        $india_experience[] = [
            'company'  => strtoupper(sanitize($comp)),
            'site'     => strtoupper(sanitize($_POST['india_site'][$i] ?? '')),
            'position' => strtoupper(sanitize($_POST['india_position'][$i] ?? '')),
            'from'     => sanitize($_POST['india_from'][$i] ?? ''),
            'to'       => sanitize($_POST['india_to'][$i] ?? ''),
        ];
    }
}

// --- Experience (Abroad)
$abroad_experience = [];
if (isset($_POST['abroad_company'])) {
    foreach ($_POST['abroad_company'] as $i => $comp) {
        $abroad_experience[] = [
            'company'  => strtoupper(sanitize($comp)),
            'site'     => strtoupper(sanitize($_POST['abroad_site'][$i] ?? '')),
            'position' => strtoupper(sanitize($_POST['abroad_position'][$i] ?? '')),
            'from'     => sanitize($_POST['abroad_from'][$i] ?? ''),
            'to'       => sanitize($_POST['abroad_to'][$i] ?? ''),
        ];
    }
}

// --- Generate PDF
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Courier', 'B', 16); // Monospaced font for alignment
$pdf->Cell(0, 10, 'BIO DATA', 0, 1, 'C');
$pdf->Ln(5);

$pdf->SetFont('Courier', '', 12);

// --- Header Info
$pdf->Cell(0, 6, 'POST APPLIED FOR: ' . strtoupper($post_applied_for), 0, 1);
$pdf->Cell(0, 6, 'NAME            : ' . strtoupper($name), 0, 1);
$pdf->Ln(6);

// ----------------------------
// Experience in India
if (!empty($india_experience)) {
    $pdf->SetFont('Courier', 'B', 12);
    $pdf->Cell(0, 6, 'EXPERIENCE DETAILS (IN INDIA)', 0, 1);
    $pdf->SetFont('Courier', '', 11);

    $pdf->Ln(2);
    $pdf->MultiCell(0, 6, str_repeat('-', 75));
    $pdf->MultiCell(0, 6, "| NAME OF COMPANY |     SITE       |  POSITION  |   FROM   |   TO     |");
    $pdf->MultiCell(0, 6, str_repeat('-', 75));

    foreach ($india_experience as $row) {
        $line = sprintf("| %-15s | %-13s | %-10s | %-8s | %-8s |",
            substr($row['company'], 0, 15),
            substr($row['site'], 0, 13),
            substr($row['position'], 0, 10),
            $row['from'],
            $row['to']
        );
        $pdf->MultiCell(0, 6, $line);
    }

    $pdf->MultiCell(0, 6, str_repeat('-', 75));
    $pdf->Ln(5);
}

// ----------------------------
// Experience Abroad
if (!empty($abroad_experience)) {
    $pdf->SetFont('Courier', 'B', 12);
    $pdf->Cell(0, 6, 'EXPERIENCE DETAILS (IN ABROAD)', 0, 1);
    $pdf->SetFont('Courier', '', 11);

    $pdf->Ln(2);
    $pdf->MultiCell(0, 6, str_repeat('-', 75));
    $pdf->MultiCell(0, 6, "| NAME OF COMPANY |     SITE       |  POSITION  |   FROM   |   TO     |");
    $pdf->MultiCell(0, 6, str_repeat('-', 75));

    foreach ($abroad_experience as $row) {
        $line = sprintf("| %-15s | %-13s | %-10s | %-8s | %-8s |",
            substr($row['company'], 0, 15),
            substr($row['site'], 0, 13),
            substr($row['position'], 0, 10),
            $row['from'],
            $row['to']
        );
        $pdf->MultiCell(0, 6, $line);
    }

    $pdf->MultiCell(0, 6, str_repeat('-', 75));
    $pdf->Ln(5);
}

// Output PDF
$pdf->Output('D', 'biodata_text_style.pdf'); // Force download
exit;
?>
