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

$formname = 'Offer Letter';
$maindb = 'offerletter';
$file = 'offerletter.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 FROM $zroot.`company` WHERE code = '$comp'") or die(mysqli_error($conn));
$ftcomp = mysqli_fetch_object($gtcomp);

$company = $ftcomp->compname;
$contactinfo = $ftcomp->htel;


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);

$gtcompdt = mysqli_query($conn, "SELECT `code`,compname FROM $zroot.`company` WHERE `code` = '$getdata[company]'") or die(mysqli_error($conn));
$ftcompdt = mysqli_fetch_object($gtcompdt);

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

// Sample data from POST (replace with actual $getdata values)
$date = convertDate($getdata["vouchdt"]);
$fullname = $getdata['employee'];
$firstname = explode(" ", $fullname)[0];
$add1 = trim($getdata['add1']);
$add2 = trim($getdata['add2']);
$add3 = trim($getdata['add3']);

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

$jobtitle = $getdata['jobtitle'];

$startdate = convertDate($getdata['startdate']);
$reportingto = $getdata['reportingto'] . " / " . $showdept['name'];
$location = $getdata['worklocation'];
$amount = number_format($getdata['monsalary']);
$amountwords = rsword($amount);

$paydate = $getdata['salaryon'];

$starttime_12hr = $getdata['starttime'];
$endtime_12hr = $getdata['endtime'];

$starttime = date("g:i A", strtotime($starttime_12hr)); // → 2:30 PM
$endtime   = date("g:i A", strtotime($endtime_12hr));   // → 6:00 PM

$weeklyoff = $getdata['weeklyoff'];
$noticeperiod = $getdata['terminationdays'];

// 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, 'OFFER LETTER', 0, 1, 'C');
$pdf->Ln(5);

// Body Content
$pdf->SetFont('Arial', '', 12);
$pdf->MultiCell(0, 8, "Date: $date\n\nTo,\n$fullname\n$address\n\nDear $firstname,\n", 0);

$pdf->MultiCell(0,8,"We are pleased to offer you the position of $jobtitle at $company, effective from $startdate. We are excited to welcome you to our team and look forward to a mutually beneficial association.\n",0);

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(0, 8, "1. Position and Reporting", 0, 1);
$pdf->SetFont('Arial', '', 12);
$pdf->MultiCell(0, 8, "You will be designated as $jobtitle, reporting to $reportingto. Your primary work location will be $location.\n", 0);

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(0, 8, "2. Monthly Salary", 0, 1);
$pdf->SetFont('Arial', '', 12);
$pdf->MultiCell(0, 8, "Your total monthly salary will be Rs. $amount/- (Rupees $amountwords only), which will be paid on or before the $paydate of each month.\n", 0);

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(0, 8, "3. Working Hours", 0, 1);
$pdf->SetFont('Arial', '', 12);
$pdf->MultiCell(0, 8, "Your working hours will be from $starttime to $endtime, for all days of the week with a weekly off on $weeklyoff.\n", 0);

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(0, 8, "4. Leave Policy", 0, 1);
$pdf->SetFont('Arial', '', 12);
$pdf->MultiCell(0, 8, "You are entitled to leave as per the company's leave policy, which will be shared with you upon joining.\n", 0);

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(0, 8, "5. Confidentiality and Conduct", 0, 1);
$pdf->SetFont('Arial', '', 12);
$pdf->MultiCell(0, 8, "You are expected to maintain the highest standards of professionalism, integrity, and confidentiality. All company policies, including code of conduct, will apply to you during your employment.\n", 0);

$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(0, 8, "6. Termination", 0, 1);
$pdf->SetFont('Arial', '', 12);
$pdf->MultiCell(0, 8, "Either party may terminate this agreement with $noticeperiod days prior written notice or salary in lieu thereof, subject to company policies.\n", 0);

$pdf->MultiCell(0, 8, "We request you to sign and return a copy of this letter as a token of your acceptance.\n\nWe welcome you to $company and wish you success in your new role.\n\nWarm regards,\n$company\nContact No.: $contactinfo\n\n", 0);

// Acknowledgement
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(0, 8, "Acknowledgement and Acceptance", 0, 1);
$pdf->SetFont('Arial', '', 12);
$pdf->MultiCell(0, 8, "I, $fullname, accept the terms and conditions mentioned above and confirm my joining on $startdate.\n\nSignature: ______________________            Date: __________________________", 0);

// Output the PDF
$pdf->Output("I", "Offer_Letter_$fullname.pdf");
