<?php
require("dbconnect.php");   // ✅ DB connection
require("fpdf.php");        // ✅ FPDF library include

// ---- Resume Form Data ----
$full_name       = $_POST['full_name'] ?? '';
$job_title       = $_POST['job_title'] ?? '';
$email           = $_POST['email'] ?? '';
$phone           = $_POST['phone'] ?? '';
$website         = $_POST['website'] ?? '';
$address         = $_POST['address'] ?? '';
$profile         = $_POST['profile'] ?? '';
$expertise       = $_POST['expertise'] ?? '';
$languages       = $_POST['languages'] ?? '';
$reference_info  = $_POST['reference_info'] ?? '';

// ✅ Education (multiple rows → single string)
$education = "";
if(isset($_POST['degree'])){
    $eduArr = [];
    foreach($_POST['degree'] as $i=>$deg){
        $inst = $_POST['institute'][$i] ?? '';
        $yr   = $_POST['edu_year'][$i] ?? '';
        $eduArr[] = "$deg - $inst ($yr)";
    }
    $education = implode("\n", $eduArr);
}

// ✅ Experience (multiple rows → single string)
$work_experience = "";
if(isset($_POST['company'])){
    $expArr = [];
    foreach($_POST['company'] as $i=>$comp){
        $pos   = $_POST['position'][$i] ?? '';
        $sy    = $_POST['start_year'][$i] ?? '';
        $ey    = $_POST['end_year'][$i] ?? '';
        $desc  = $_POST['exp_desc'][$i] ?? '';
        $expArr[] = "$comp - $pos ($sy - $ey)\n$desc";
    }
    $work_experience = implode("\n\n", $expArr);
}

// ---- Photo upload ----
$photo = "";
if(isset($_FILES['photo']['name']) && $_FILES['photo']['name'] != ""){
    $photo = "uploads/".time()."_".$_FILES['photo']['name'];
    if (!is_dir("uploads")) { mkdir("uploads"); }
    move_uploaded_file($_FILES['photo']['tmp_name'], $photo);
}

// ✅ Save to Database
$query = "INSERT INTO resumes 
(full_name, job_title, email, phone, website, address, profile, education, expertise, languages, work_experience, reference_info, photo) 
VALUES 
('$full_name','$job_title','$email','$phone','$website','$address','$profile','$education','$expertise','$languages','$work_experience','$reference_info','$photo')";

if(mysqli_query($con, $query)){   // ✅ $con use किया

    // ✅ Generate Resume PDF
    $pdf = new FPDF();
    $pdf->AddPage();

    // ---- Photo
    if($photo != "" && file_exists($photo)){
        $pdf->Image($photo, 150, 10, 40, 40);
    }

    // ---- Name & Title
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(0,10,$full_name,0,1,'C');
    $pdf->SetFont('Arial','I',12);
    $pdf->Cell(0,10,$job_title,0,1,'C');

    // ---- Contact Info
    $pdf->Ln(10);
    $pdf->SetFont('Arial','',12);
    $pdf->MultiCell(0,8,"Email: $email\nPhone: $phone\nWebsite: $website\nAddress: $address");

    // ---- Profile
    if($profile != ""){
        $pdf->Ln(5);
        $pdf->SetFont('Arial','B',14);
        $pdf->Cell(0,10,'Profile Summary',0,1);
        $pdf->SetFont('Arial','',12);
        $pdf->MultiCell(0,8,$profile);
    }

    // ---- Education
    if($education != ""){
        $pdf->Ln(5);
        $pdf->SetFont('Arial','B',14);
        $pdf->Cell(0,10,'Education',0,1);
        $pdf->SetFont('Arial','',12);
        $pdf->MultiCell(0,8,$education);
    }

    // ---- Work Experience
    if($work_experience != ""){
        $pdf->Ln(5);
        $pdf->SetFont('Arial','B',14);
        $pdf->Cell(0,10,'Work Experience',0,1);
        $pdf->SetFont('Arial','',12);
        $pdf->MultiCell(0,8,$work_experience);
    }

    // ---- Expertise
    if($expertise != ""){
        $pdf->Ln(5);
        $pdf->SetFont('Arial','B',14);
        $pdf->Cell(0,10,'Expertise',0,1);
        $pdf->SetFont('Arial','',12);
        $pdf->MultiCell(0,8,$expertise);
    }

    // ---- Languages
    if($languages != ""){
        $pdf->Ln(5);
        $pdf->SetFont('Arial','B',14);
        $pdf->Cell(0,10,'Languages',0,1);
        $pdf->SetFont('Arial','',12);
        $pdf->MultiCell(0,8,$languages);
    }

    // ---- References
    if($reference_info != ""){
        $pdf->Ln(5);
        $pdf->SetFont('Arial','B',14);
        $pdf->Cell(0,10,'References',0,1);
        $pdf->SetFont('Arial','',12);
        $pdf->MultiCell(0,8,$reference_info);
    }

    // ✅ Download PDF
    $pdf->Output('D','resume.pdf');

} else {
    echo "❌ Error: " . mysqli_error($con);  // ✅ $con use किया
}
?>
