<?php
session_start();
require_once '../includes/User.php';
require_once '../includes/db_config.php';

if (!User::isLoggedIn() || !User::isAdmin()) {
    header("Location: /allura/login.php");
    exit();
}

$pdo = getDBConnection();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    
    try {
        switch ($action) {
            case 'add':
                $name = trim($_POST['name']);
                $email = trim($_POST['email']);
                $phone = trim($_POST['phone']);
                $password = $_POST['password'];
                $status = $_POST['status'];
                
                if (empty($name) || empty($email) || empty($phone) || empty($password)) {
                    $_SESSION['error_message'] = "All fields are required";
                    header("Location: admin.php#laundry");
                    exit();
                }
                
                $stmt = $pdo->prepare("SELECT id FROM laundry_staff WHERE email = ?");
                $stmt->execute([$email]);
                if ($stmt->fetch()) {
                    $_SESSION['error_message'] = "Email already exists";
                    header("Location: admin.php#laundry");
                    exit();
                }
                
                $hashed_password = password_hash($password, PASSWORD_DEFAULT);
                
                $stmt = $pdo->prepare("
                    INSERT INTO laundry_staff (name, email, phone, password, status, created_at) 
                    VALUES (?, ?, ?, ?, ?, NOW())
                ");
                $stmt->execute([$name, $email, $phone, $hashed_password, $status]);
                
                $_SESSION['success_message'] = "Laundry staff added successfully";
                break;
                
            case 'edit':
                $id = $_POST['id'];
                $name = trim($_POST['name']);
                $email = trim($_POST['email']);
                $phone = trim($_POST['phone']);
                $password = $_POST['password'] ?? '';
                $status = $_POST['status'];
                
                if (empty($name) || empty($email) || empty($phone)) {
                    $_SESSION['error_message'] = "Name, email, and phone are required";
                    header("Location: admin.php#laundry");
                    exit();
                }
                
                $stmt = $pdo->prepare("SELECT id FROM laundry_staff WHERE email = ? AND id != ?");
                $stmt->execute([$email, $id]);
                if ($stmt->fetch()) {
                    $_SESSION['error_message'] = "Email already exists";
                    header("Location: admin.php#laundry");
                    exit();
                }
                
                if (!empty($password)) {
                    $hashed_password = password_hash($password, PASSWORD_DEFAULT);
                    $stmt = $pdo->prepare("
                        UPDATE laundry_staff 
                        SET name = ?, email = ?, phone = ?, password = ?, status = ? 
                        WHERE id = ?
                    ");
                    $stmt->execute([$name, $email, $phone, $hashed_password, $status, $id]);
                } else {
                    $stmt = $pdo->prepare("
                        UPDATE laundry_staff 
                        SET name = ?, email = ?, phone = ?, status = ? 
                        WHERE id = ?
                    ");
                    $stmt->execute([$name, $email, $phone, $status, $id]);
                }
                
                $_SESSION['success_message'] = "Laundry staff updated successfully";
                break;
                
            case 'delete':
                $id = $_POST['id'];
                
                $stmt = $pdo->prepare("DELETE FROM laundry_staff WHERE id = ?");
                $stmt->execute([$id]);
                
                $_SESSION['success_message'] = "Laundry staff deleted successfully";
                break;
                
            default:
                $_SESSION['error_message'] = "Invalid action";
        }
    } catch (PDOException $e) {
        $_SESSION['error_message'] = "Database error: " . $e->getMessage();
    }
    
    header("Location: admin.php#laundry");
    exit();
} else {
    header("Location: admin.php");
    exit();
}
