<?php
session_start();
include 'config.php';

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'employee') {
    header("Location: login.php");
    exit();
}

$emp_id = $_GET['emp_id'] ?? '';
$month = (int)($_GET['month'] ?? 0);
$year = (int)($_GET['year'] ?? 0);

if (empty($emp_id) || $month < 1 || $month > 12 || $year < 2000) {
    die("Invalid parameters");
}

// Fetch payroll data
$sql = "SELECT u.first_name, u.last_name, u.designation, u.division, p.* 
        FROM payroll_records p 
        JOIN users u ON p.employee_id = u.id 
        WHERE p.employee_id = ? AND p.month = ? AND p.year = ? AND p.status = 'sent'";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sii", $emp_id, $month, $year);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();

if (!$row) {
    die("Payslip not found or not released yet.");
}

// Generate PDF using TCPDF or similar library
require_once('tcpdf/tcpdf.php'); // Download TCPDF from https://tcpdf.org

$pdf = new TCPDF();
$pdf->SetCreator('MyPeopleConnect');
$pdf->SetAuthor('HR division');
$pdf->SetTitle('Payslip');
$pdf->SetSubject('Monthly Payslip');

$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);

// Header
$pdf->Cell(0, 10, 'PAYSLIP - ' . date('F Y', mktime(0,0,0,$month,1,$year)), 0, 1, 'C');
$pdf->Ln(5);

// Employee Details
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Cell(50, 8, 'Employee:', 0, 0);
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(0, 8, $row['first_name'] . ' ' . $row['last_name'], 0, 1);
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Cell(50, 8, 'Designation:', 0, 0);
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(0, 8, $row['designation'], 0, 1);
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Cell(50, 8, 'division:', 0, 0);
$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(0, 8, $row['division'], 0, 1);
$pdf->Ln(5);

// Earnings Table
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Cell(90, 8, 'EARNINGS', 1, 0, 'C');
$pdf->Cell(90, 8, 'DEDUCTIONS', 1, 1, 'C');

$pdf->SetFont('helvetica', '', 9);
$pdf->Cell(60, 8, 'Basic Salary', 1, 0);
$pdf->Cell(30, 8, number_format($row['basic_salary'], 2), 1, 0, 'R');
$pdf->Cell(60, 8, 'Income Tax', 1, 0);
$pdf->Cell(30, 8, number_format($row['it'], 2), 1, 1, 'R');

$pdf->Cell(60, 8, 'HRA', 1, 0);
$pdf->Cell(30, 8, number_format($row['hra'], 2), 1, 0, 'R');
$pdf->Cell(60, 8, 'Professional Tax', 1, 0);
$pdf->Cell(30, 8, number_format($row['pt'], 2), 1, 1, 'R');

$pdf->Cell(60, 8, 'Other Allowance', 1, 0);
$pdf->Cell(30, 8, number_format($row['other_allowance'], 2), 1, 0, 'R');
$pdf->Cell(60, 8, 'PF', 1, 0);
$pdf->Cell(30, 8, number_format($row['pf'], 2), 1, 1, 'R');

$pdf->Cell(60, 8, '', 1, 0);
$pdf->Cell(30, 8, '', 1, 0, 'R');
$pdf->Cell(60, 8, 'Other Deductions', 1, 0);
$pdf->Cell(30, 8, number_format($row['other_deductions'], 2), 1, 1, 'R');

$pdf->SetFont('helvetica', 'B', 10);
$pdf->Cell(60, 8, 'Gross Salary', 1, 0);
$pdf->Cell(30, 8, number_format($row['total_gross_salary'], 2), 1, 0, 'R');
$pdf->Cell(60, 8, 'Total Deductions', 1, 0);
$pdf->Cell(30, 8, number_format($row['total_deductions'], 2), 1, 1, 'R');
$pdf->Ln(5);

$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(150, 10, 'NET PAYABLE: ₹' . number_format($row['net_earnings'], 2), 0, 1, 'R');
$pdf->Ln(10);

// Footer
$pdf->SetFont('helvetica', '', 8);
$pdf->Cell(0, 10, 'Generated on: ' . date('d M Y H:i'), 0, 1, 'C');
$pdf->Cell(0, 10, 'Confidential - For Employee Use Only', 0, 1, 'C');

$filename = "Payslip_{$row['first_name']}_{$row['last_name']}_{$month}_{$year}.pdf";
$pdf->Output($filename, 'D');
?>