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

if (!isset($_SESSION['user_id'])) {
    header("Location: index.php");
    exit();
}

/* ---------- SEARCH & FILTER ---------- */
$search = trim($_GET['search'] ?? '');
$where  = $search ? "WHERE CONCAT(first_name,' ',last_name) LIKE ?" : '';
$stmt   = $conn->prepare("SELECT id, first_name, last_name, basic_salary FROM users $where ORDER BY first_name");
if ($search) $stmt->bind_param('s', $like);
$like = "%$search%";
$stmt->execute();
$res = $stmt->get_result();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Select Employee for Payroll</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">
    <style>
        body { background:#f4f4f4; }
        .card { max-width:900px; margin:2rem auto; }
    </style>
</head>
<body>
<div class="container">
    <div class="card shadow">
        <div class="card-header bg-primary text-white">
            <h5 class="mb-0"><i class="fas fa-users"></i> Select Employee to Process Salary</h5>
        </div>
        <div class="card-body">
            <!-- Search -->
            <form class="mb-3">
                <div class="input-group">
                    <input type="text" name="search" class="form-control"
                           placeholder="Search employee name…" value="<?=htmlspecialchars($search)?>">
                    <button class="btn btn-outline-primary-primary" type="submit"><i class="fas fa-search"></i></button>
                    <?php if($search):?>
                        <a href="select_employee.php" class="btn btn-outline-secondary">Clear</a>
                    <?php endif;?>
                </div>
            </form>

            <!-- Employee Table -->
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="table-dark">
                        <tr>
                            <th>#</th>
                            <th>Name</th>
                            <th>Basic Salary</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $i=1; while($row = $res->fetch_assoc()): ?>
                        <tr>
                            <td><?= $i++ ?></td>
                            <td><?= htmlspecialchars($row['first_name'].' '.$row['last_name']) ?></td>
                            <td>₹ <?= number_format($row['basic_salary'],2) ?></td>
                            <td>
                                <?php
                                // Build URL for the original payroll page
                                $url = "process_payroll.php?emp_id={$row['id']}";
                                // Keep current month/year if you want (optional)
                                $url .= "&month=" . (intval($_GET['month'] ?? date('m')));
                                $url .= "&year="  . (intval($_GET['year']  ?? date('Y')));
                                ?>
                                <a href="<?= $url ?>" class="btn btn-success btn-sm">
                                    <i class="fas fa-calculator"></i> Process Salary
                                </a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                        <?php if($i===1): ?>
                        <tr><td colspan="4" class="text-center text-muted">No employees found.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>

            <a href="payroll_dashboard.php" class="btn btn-secondary mt-3">
                <i class="fas fa-arrow-left"></i> Back to Dashboard
            </a>
        </div>
    </div>
</div>
</body>
</html>
<?php $stmt->close(); ?>