<?php
require_once 'config.php';
require_once 'auth_functions.php';
requireLogin();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>All Leaves - Leave Management System</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
    <div class="container-fluid">
        <a class="navbar-brand" href="#">
            <i class="fas fa-calendar-check"></i> Leave Management System
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav ms-auto">
                <li class="nav-item">
                    <a class="nav-link" href="user_dashboard.php">
                        <i class="fas fa-home"></i> Dashboard
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="apply_leave.php">
                        <i class="fas fa-plus-circle"></i> Apply Leave
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="guidelines.pdf" target="_blank">
                        <i class="fas fa-file-pdf"></i> Leave Guidelines
                    </a>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown">
                        <i class="fas fa-user"></i> <?php echo $_SESSION['full_name']; ?>
                    </a>
                    <ul class="dropdown-menu">
                        <li><a class="dropdown-item" href="logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>

<div class="container mt-4">
    <h2 class="mb-4">All Employees' Leave Applications</h2>

    <!-- Filter Section -->
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="mb-0"><i class="fas fa-filter"></i> Filter by Date Range</h5>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-4">
                    <label>Start Date</label>
                    <input type="date" id="filter_start" class="form-control">
                </div>
                <div class="col-md-4">
                    <label>End Date</label>
                    <input type="date" id="filter_end" class="form-control">
                </div>
                <div class="col-md-4">
                    <label>&nbsp;</label>
                    <button class="btn btn-primary w-100" onclick="loadAllLeaves()">
                        <i class="fas fa-search"></i> Apply Filter
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- All Leaves Table -->
    <div class="card">
        <div class="card-header bg-success text-white">
            <h5 class="mb-0"><i class="fas fa-table"></i> All Leave Applications</h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table id="allLeavesTable" class="table table-striped table-hover table-sm">
                    <thead>
                        <tr>
                            <th>Emp ID</th>
                            <th>Employee Name</th>
                            <th>Department</th>
                            <th>Leave Type</th>
                            <th>Start Date</th>
                            <th>End Date</th>
                            <th>Days</th>
                            <th>Reason</th>
                            <th>Apply Date</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
                <small class="text-muted">View only. No edit or delete from this page.</small>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
<script>
    let table;

    $(document).ready(function() {
        loadAllLeaves();
    });

    function loadAllLeaves() {
        const startDate = $('#filter_start').val();
        const endDate   = $('#filter_end').val();

        if (table) {
            table.destroy();
        }

        table = $('#allLeavesTable').DataTable({
            ajax: {
                url: 'ajax_get_all_leaves.php',
                data: {
                    start_date: startDate,
                    end_date: endDate
                },
                dataSrc: ''
            },
            columns: [
                { data: 'employee_id' },
                { data: 'employee_name' },
                { data: 'department' },
                { data: 'leave_type' },
                { data: 'start_date' },
                { data: 'end_date' },
                { data: 'number_of_days' },
                { data: 'reason' },
                { data: 'apply_date' },
                { 
                    data: 'status',
                    render: function(data) {
                        let badgeClass = data === 'approved'
                            ? 'success'
                            : (data === 'rejected' ? 'danger' : 'warning');
                        return `<span class="badge bg-${badgeClass}">${data.toUpperCase()}</span>`;
                    }
                }
            ],
            order: [[8, 'desc']]
        });
    }
</script>
</body>
</html>
