<?php
require('fpdf/fpdf.php');

function sanitize($data) {
    return htmlspecialchars(strip_tags(trim($data)));
}

// === Basic Info
$post_applied_for       = sanitize($_POST['post_applied_for'] ?? '');
$name                   = sanitize($_POST['name'] ?? '');
$present_address        = sanitize($_POST['present_address'] ?? '');
$permanent_address      = sanitize($_POST['permanent_address'] ?? '');
$mobile                 = sanitize($_POST['mobile'] ?? '');
$languages_known        = sanitize($_POST['languages_known'] ?? '');
$qualification          = sanitize($_POST['qualification'] ?? '');
$technical_qualification = sanitize($_POST['technical_qualification'] ?? '');

// === Passport & Personal Info
$passport_number        = sanitize($_POST['passport_number'] ?? '');
$passport_issue_date    = sanitize($_POST['passport_issue_date'] ?? '');
$passport_expiry_date   = sanitize($_POST['passport_expiry_date'] ?? '');
$place_of_issue         = sanitize($_POST['place_of_issue'] ?? '');
$father_name            = sanitize($_POST['father_name'] ?? '');
$mother_name            = sanitize($_POST['mother_name'] ?? '');
$dob                    = sanitize($_POST['dob'] ?? '');
$place_of_birth         = sanitize($_POST['place_of_birth'] ?? '');
$signature_text         = sanitize($_POST['signature_text'] ?? '');

// === Experience - India
$india_experience = [];
if (isset($_POST['india_company'])) {
    foreach ($_POST['india_company'] as $i => $comp) {
        if (!empty($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) {
        if (!empty($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] ?? ''),
            ];
        }
    }
}

// === Handle Photo Upload (Optional)
$photo_path = '';
if (isset($_FILES['photo']) && $_FILES['photo']['error'] === UPLOAD_ERR_OK) {
    $ext = strtolower(pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION));
    $allowed = ['jpg', 'jpeg', 'png'];
    if (in_array($ext, $allowed)) {
        $tmp_name = $_FILES['photo']['tmp_name'];
        $photo_path = 'uploads/photo_' . time() . '.' . $ext;
        move_uploaded_file($tmp_name, $photo_path);
    }
}

// === Generate PDF
$pdf = new FPDF();
$pdf->AddPage();

// Set colors and fonts
$pdf->SetFont('Arial', 'B', 16);
$pdf->SetTextColor(0, 0, 80);
$pdf->Cell(0, 10, 'BIO DATA', 0, 1, 'C');
$pdf->Ln(5);

// === Add Photo (top-right)
if ($photo_path && file_exists($photo_path)) {
    $pdf->Image($photo_path, 160, 20, 30, 40); // x, y, width, height
}

$pdf->SetFont('Arial', 'B', 13);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(0, 8, 'POST APPLIED FOR: ' . strtoupper($post_applied_for), 0, 1);

$pdf->SetFont('Arial', 'B', 14);
$pdf->SetTextColor(0, 51, 102);
$pdf->Cell(0, 10, strtoupper($name), 0, 1);

$pdf->SetFont('Arial', '', 12);
$pdf->SetTextColor(0);
$pdf->MultiCell(0, 7, "PRESENT ADDRESS : $present_address");
$pdf->MultiCell(0, 7, "PERMANENT ADDRESS: $permanent_address");
$pdf->Cell(0, 7, "MOBILE           : $mobile", 0, 1);
$pdf->Cell(0, 7, "LANGUAGES KNOWN  : $languages_known", 0, 1);
$pdf->Cell(0, 7, "QUALIFICATION    : $qualification", 0, 1);
$pdf->Cell(0, 7, "TECHNICAL QUAL.  : $technical_qualification", 0, 1);
$pdf->Ln(5);

// === Passport Details & Personal Info as TABLE
$pdf->SetFont('Arial', 'B', 13);
$pdf->SetFillColor(230, 230, 255); // Light blue
$pdf->Cell(0, 10, 'PASSPORT DETAILS & PERSONAL INFO', 0, 1, 'L', true);

// Create table rows: Label | Value
$pdf->SetFont('Arial', '', 12);
$info = [
    'Passport Number'   => $passport_number,
    'Issue Date'        => $passport_issue_date,
    'Expiry Date'       => $passport_expiry_date,
    'Place of Issue'    => $place_of_issue,
    "Father's Name"     => $father_name,
    "Mother's Name"     => $mother_name,
    'Date of Birth'     => $dob,
    'Place of Birth'    => $place_of_birth,
];

// Column widths
$label_width = 60;
$value_width = 130;

foreach ($info as $label => $value) {
    $pdf->SetFillColor(245, 245, 245); // light grey for rows
    $pdf->Cell($label_width, 8, $label, 1, 0, 'L', true);
    $pdf->Cell($value_width, 8, $value, 1, 1, 'L');
}

$pdf->Ln(5);

// === Experience Table Generator Function
function addExperienceTable($pdf, $title, $data) {
    if (empty($data)) return;

    $pdf->SetFillColor(200, 220, 255);
    $pdf->SetFont('Arial', 'B', 13);
    $pdf->Cell(0, 10, $title, 0, 1, 'L', true);

    $pdf->SetFont('Arial', 'B', 11);
    $pdf->SetFillColor(220, 220, 220);
    $pdf->Cell(50, 8, 'Company', 1, 0, 'C', true);
    $pdf->Cell(40, 8, 'Site', 1, 0, 'C', true);
    $pdf->Cell(40, 8, 'Position', 1, 0, 'C', true);
    $pdf->Cell(30, 8, 'From', 1, 0, 'C', true);
    $pdf->Cell(30, 8, 'To', 1, 1, 'C', true);

    $pdf->SetFont('Arial', '', 11);
    foreach ($data as $row) {
        $pdf->Cell(50, 8, $row['company'], 1);
        $pdf->Cell(40, 8, $row['site'], 1);
        $pdf->Cell(40, 8, $row['position'], 1);
        $pdf->Cell(30, 8, $row['from'], 1);
        $pdf->Cell(30, 8, $row['to'], 1);
        $pdf->Ln();
    }

    $pdf->Ln(5);
}

// === Experience Tables
addExperienceTable($pdf, 'EXPERIENCE DETAILS (INDIA)', $india_experience);
addExperienceTable($pdf, 'EXPERIENCE DETAILS (ABROAD)', $abroad_experience);

// === Signature
$pdf->Ln(8);
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(0, 8, 'Signature: ' . $signature_text, 0, 1, 'R');

// Output PDF
$pdf->Output('I', 'Resume_Biodata.pdf');
exit;
?>
