<?php
require_once 'C:\xampp\htdocs\multi_admin\AdminLt\TCPDF-main\TCPDF-main/tcpdf.php'; // Actual path to tcpdf.php

// require_once 'path/to/TCPDF-main/TCPDF-main/tcpdf.php'; // Include the TCPDF library
require_once 'connection.php'; 
use PhpOffice\PhpWord\TemplateProcessor;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  
    $name = $_POST['name'];
    $fullName = $_POST['full_name'];
    $year = $_POST['year'];
    $designation = $_POST['designation'];
    $department = $_POST['department'];

    $sql = "INSERT INTO joining (name, full_name, year, desgination, department) VALUES (?, ?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);
    
    if (!$stmt) {
        die("Database query error: " . $conn->error);
    }

    $stmt->bind_param("sssss", $name, $fullName, $year, $designation, $department);
    if (!$stmt->execute()) {
        die("Database query error: " . $stmt->error); 
    }

    // Create a new TCPDF object
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Your Name');
    $pdf->SetTitle('Offer Letter');
    $pdf->SetSubject('Offer Letter');
    $pdf->SetKeywords('Offer Letter, PDF');

    $pdf->SetMargins(10, 10, 10);
    $pdf->SetAutoPageBreak(true, 10);

    $pdf->AddPage();

    // Generate offer letter content
    $pdf->SetFont('helvetica', '', 12);
    $content = "Dear $name,\n\n";
    $content .= "We are pleased to offer you the position of $designation in the $department department for the year $year.\n\n";
    $content .= "Sincerely,\n\n";
    $content .= "Your Company Name";
    
    // Add content to the PDF
    $pdf->writeHTML($content, true, false, true, false, '');
    
    // Output the PDF as a download
    $pdf->Output('offer_letter.pdf', 'D');

    // Exit to prevent further execution
    exit();
}
?>