<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (!isset($_SESSION['admin_id'])) { header("Location: login.php"); exit(); }
include '../include/config.php';

$msg = '';

// Create staff table if not exists
$conn->query("CREATE TABLE IF NOT EXISTS staff (
    staff_id INT AUTO_INCREMENT PRIMARY KEY,
    staff_number VARCHAR(50) UNIQUE,
    first_name VARCHAR(100) NOT NULL,
    last_name VARCHAR(100) NOT NULL,
    email VARCHAR(100),
    phone VARCHAR(20),
    department VARCHAR(50),
    position VARCHAR(100),
    profile_photo VARCHAR(255),
    status ENUM('active','inactive','on_leave') DEFAULT 'active',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['add'])) {
        $staff_number = $_POST['staff_number'];
        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $department = $_POST['department'];
        $position = $_POST['position'];
        
        $stmt = $conn->prepare("INSERT INTO staff (staff_number, first_name, last_name, email, phone, department, position, status) VALUES (?, ?, ?, ?, ?, ?, ?, 'active')");
        $stmt->bind_param("sssssss", $staff_number, $first_name, $last_name, $email, $phone, $department, $position);
        
        if ($stmt->execute()) {
            $msg = "Staff added successfully!";
        } else {
            $msg = "Error: " . $conn->error;
        }
        $stmt->close();
    } elseif (isset($_POST['delete'])) {
        $id = (int)$_POST['staff_id'];
        $conn->query("DELETE FROM staff WHERE staff_id = $id");
        $msg = "Staff deleted.";
    }
}

$staff = $conn->query("SELECT * FROM staff ORDER BY created_at DESC");
?>
<!DOCTYPE html>
<html>
<head>
    <title>Manage Staff</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
</head>
<body>
<div class="container mt-4">
    <h2><i class="fas fa-user-tie"></i> Manage Staff</h2>
    <?php if($msg): ?>
        <div class="alert alert-success alert-dismissible fade show">
            <?= htmlspecialchars($msg) ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>
    
    <button class="btn btn-primary mb-3" data-bs-toggle="modal" data-bs-target="#addModal">
        <i class="fas fa-plus"></i> Add Staff
    </button>
    
    <div class="table-responsive">
        <table class="table table-bordered table-hover">
            <thead class="table-dark">
                <tr><th>ID</th><th>Staff Number</th><th>Name</th><th>Email</th><th>Department</th><th>Position</th><th>Status</th><th>Action</th></tr>
            </thead>
            <tbody>
                <?php if($staff && $staff->num_rows > 0): ?>
                    <?php while($s = $staff->fetch_assoc()): ?>
                    <tr>
                        <td><?= $s['staff_id'] ?></td>
                        <td><?= htmlspecialchars($s['staff_number']) ?></td>
                        <td><?= htmlspecialchars($s['first_name'] . ' ' . $s['last_name']) ?></td>
                        <td><?= htmlspecialchars($s['email']) ?></td>
                        <td><?= ucfirst($s['department']) ?></td>
                        <td><?= htmlspecialchars($s['position']) ?></td>
                        <td><span class="badge bg-<?= $s['status'] == 'active' ? 'success' : 'secondary' ?>"><?= $s['status'] ?></span></td>
                        <td>
                            <form method="POST" onsubmit="return confirm('Delete this staff member?');" style="display:inline;">
                                <input type="hidden" name="staff_id" value="<?= $s['staff_id'] ?>">
                                <button type="submit" name="delete" class="btn btn-danger btn-sm"><i class="fas fa-trash"></i> Delete</button>
                            </form>
                        </td>
                    </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr><td colspan="8" class="text-center">No staff members found.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
    <a href="dashboard.php" class="btn btn-secondary"><i class="fas fa-arrow-left"></i> Back to Dashboard</a>
</div>

<!-- Add Staff Modal -->
<div class="modal fade" id="addModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-success text-white">
                <h5><i class="fas fa-user-plus"></i> Add New Staff</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6 mb-2"><input type="text" name="staff_number" class="form-control" placeholder="Staff Number (e.g., STF001)" required></div>
                        <div class="col-md-6 mb-2"><input type="text" name="first_name" class="form-control" placeholder="First Name" required></div>
                        <div class="col-md-6 mb-2"><input type="text" name="last_name" class="form-control" placeholder="Last Name" required></div>
                        <div class="col-md-6 mb-2"><input type="email" name="email" class="form-control" placeholder="Email"></div>
                        <div class="col-md-6 mb-2"><input type="text" name="phone" class="form-control" placeholder="Phone"></div>
                        <div class="col-md-6 mb-2">
                            <select name="department" class="form-control" required>
                                <option value="">Select Department</option>
                                <option value="administration">Administration</option>
                                <option value="reception">Reception</option>
                                <option value="billing">Billing</option>
                                <option value="hr">Human Resources</option>
                                <option value="it">Information Technology</option>
                                <option value="pharmacy">Pharmacy</option>
                                <option value="lab">Laboratory</option>
                                <option value="other">Other</option>
                            </select>
                        </div>
                        <div class="col-md-6 mb-2"><input type="text" name="position" class="form-control" placeholder="Position (e.g., Senior Receptionist)" required></div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" name="add" class="btn btn-primary">Add Staff</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>