<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = 'Joining Letter';
$maindb = 'offerletter';
$file = 'joiningletter.php';

$keyfld = "vouchno";
$usertype = $_SESSION["usertype"];
$username = $_SESSION["username_" . $dbname];
$comp = $_SESSION["comp_" . $dbname];
$zroot = $_SESSION["zdir"];

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);

$gtcomp = mysqli_query($conn, "SELECT code,compname,htel,hoadd1,hoadd2,hoadd3 FROM $zroot.`company` WHERE code = '$comp'") or die(mysqli_error($conn));
$ftcomp = mysqli_fetch_object($gtcomp);

$company = $ftcomp->compname;
$contactinfo = $ftcomp->htel;
$compadd1 = trim($ftcomp->hoadd1);
$compadd2 = trim($ftcomp->hoadd2);
$compadd3 = trim($ftcomp->hoadd3);
$compaddress = $compadd1 . " " . $compadd2 . " " . $compadd3;

mysqli_select_db($conn, $zdb);

$vouchno = $_GET["vouchno"];

$result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$vouchno'") or die(mysqli_error($conn));
$getdata = mysqli_fetch_array($result);

$getdept = mysqli_query($conn, "SELECT * FROM `dept` WHERE `code` = '$getdata[department]'") or die(mysqli_error($conn));
$showdept = mysqli_fetch_array($getdept);

require('library/fpdf/fpdf.php');

// Sample data from POST (replace with actual $getdata values)
$date = convertDate($getdata["vouchdt"]);
$fullname = $getdata['employee'];

$add1 = trim($getdata['add1']);
$add2 = trim($getdata['add2']);
$add3 = trim($getdata['add3']);
$address = $add1 . " " . $add2 . " " . $add3;

$jobtitle = $getdata['jobtitle'];
$joindate = convertDate($getdata['joiningdt']);

// Initialize PDF
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 12);
$pdf->SetAutoPageBreak(true, 10);

$pdf->Ln(30);

// Title
$pdf->SetFont('Arial', 'B', 14);
$pdf->Cell(0, 10, 'JOINING LETTER', 0, 1, 'C');
$pdf->Ln(5);

// Date
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(0, 10, "Date: $date", 0, 1);
$pdf->Ln(2);

// To section
$pdf->MultiCell(0, 8, "To,\nThe HR Manager\n$company\n$compaddress", 0);
$pdf->Ln(5);

// Subject
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(0, 10, "Subject: Joining Letter", 0, 1);
$pdf->SetFont('Arial', '', 12);
$pdf->Ln(1);

// Body
$body = "Dear Sir/Madam,

I am pleased to inform you that I am joining $company as $jobtitle on $joindate, as per the offer letter issued to me. I thank you for this opportunity and look forward to contributing positively to the organization.

I assure you of my sincere efforts, dedication, and commitment to the responsibilities assigned to me.

Please find enclosed the required documents for your reference and record.

Thank you once again.";

$pdf->MultiCell(0, 8, $body, 0);
$pdf->Ln(10);

// Closing
$pdf->MultiCell(0, 8, "Sincerely,\n$fullname\n$address", 0);

// Output
$pdf->Output("I", "JoiningLetter_$fullname.pdf");

