<?php
require_once 'config.php';
require_once 'auth_functions.php';
requireAdmin();

require_once 'tcpdf/tcpdf.php';

$user_id = $_GET['user_id'];
$start_date = $_GET['start_date'] ?? '';
$end_date = $_GET['end_date'] ?? '';

// Get employee details
$sql_emp = "SELECT * FROM users WHERE id = $user_id";
$employee = $conn->query($sql_emp)->fetch_assoc();

$where = "WHERE la.user_id = $user_id";
if ($start_date && $end_date) {
    $where .= " AND la.start_date >= '$start_date' AND la.end_date <= '$end_date'";
}

$sql = "SELECT la.* FROM leave_applications la $where ORDER BY la.apply_date DESC";
$result = $conn->query($sql);

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

$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Leave Management System');
$pdf->SetTitle('Employee Leave Report');

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

$pdf->AddPage();

$pdf->SetFont('helvetica', 'B', 16);
$pdf->Cell(0, 10, 'Employee Leave Report', 0, 1, 'C');

$pdf->SetFont('helvetica', '', 10);
$pdf->Cell(0, 5, 'Employee: ' . $employee['full_name'], 0, 1);
$pdf->Cell(0, 5, 'Employee ID: ' . $employee['employee_id'], 0, 1);
$pdf->Cell(0, 5, 'Department: ' . $employee['department'], 0, 1);

if ($start_date && $end_date) {
    $pdf->Cell(0, 5, "Period: $start_date to $end_date", 0, 1);
}

$pdf->Ln(5);

// Get leave summary
$current_year = date('Y');
$sql_summary = "SELECT * FROM leave_balance WHERE user_id = $user_id AND year = $current_year";
$summary_result = $conn->query($sql_summary);

if ($summary_result->num_rows > 0) {
    $summary = $summary_result->fetch_assoc();
    
    $pdf->SetFont('helvetica', 'B', 11);
    $pdf->Cell(0, 6, 'Leave Summary for ' . $current_year, 0, 1);
    
    $summary_html = '<table border="1" cellpadding="5">
        <tr style="background-color: #f0f0f0;">
            <th><b>Leave Type</b></th>
            <th><b>Used</b></th>
            <th><b>Total Allowed</b></th>
            <th><b>Remaining</b></th>
        </tr>
        <tr>
            <td>Casual Leave</td>
            <td>' . $summary['casual_leaves_used'] . '</td>
            <td>20</td>
            <td>' . (20 - $summary['casual_leaves_used']) . '</td>
        </tr>
        <tr>
            <td>Medical Leave</td>
            <td>' . $summary['medical_leaves_used'] . '</td>
            <td>21</td>
            <td>' . (21 - $summary['medical_leaves_used']) . '</td>
        </tr>
        <tr>
            <td>Duty Leave</td>
            <td>' . $summary['duty_leaves_used'] . '</td>
            <td>Unlimited</td>
            <td>-</td>
        </tr>
    </table>';
    
    $pdf->writeHTML($summary_html, true, false, true, false, '');
    $pdf->Ln(5);
}

// Table of leave applications
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Cell(0, 6, 'Leave Applications History', 0, 1);

$html = '<table border="1" cellpadding="4">
<thead>
    <tr style="background-color: #427eea; color: white; font-weight: bold;">
        <th width="15%">Leave Type</th>
        <th width="12%">Start Date</th>
        <th width="12%">End Date</th>
        <th width="8%">Days</th>
        <th width="33%">Reason</th>
        <th width="10%">Status</th>
        <th width="10%">Applied</th>
    </tr>
</thead>
<tbody>';

$pdf->SetFont('helvetica', '', 8);

while ($row = $result->fetch_assoc()) {
    $status_color = $row['status'] === 'approved' ? '#28a745' : ($row['status'] === 'rejected' ? '#dc3545' : '#ffc107');
    
    $html .= '<tr>
        <td>' . ucfirst($row['leave_type']) . '</td>
        <td>' . date('Y-m-d', strtotime($row['start_date'])) . '</td>
        <td>' . date('Y-m-d', strtotime($row['end_date'])) . '</td>
        <td>' . $row['number_of_days'] . '</td>
        <td>' . substr($row['reason'], 0, 60) . '</td>
        <td style="color: ' . $status_color . '"><b>' . ucfirst($row['status']) . '</b></td>
        <td>' . date('M d', strtotime($row['apply_date'])) . '</td>
    </tr>';
}

$html .= '</tbody></table>';

$pdf->writeHTML($html, true, false, true, false, '');

$filename = 'employee_' . $employee['employee_id'] . '_leave_report_' . date('Y-m-d') . '.pdf';
$pdf->Output($filename, 'D');
?>
