<?php
/**
 * ADMIN CUSTOMER SUSPENSION PAGE
 * File: admin/customer-suspend.php
 * Suspend or activate customer accounts
 */

require_once '../app/Core/init.php';

// Check admin authentication
Auth::requireAdmin();

$page_title = "Customer Management";
$active_menu = "customers";

// Get customer ID
$customer_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if (!$customer_id) {
    flash('error', 'Invalid customer ID', 'danger');
    redirect('customers.php');
}

// Initialize models
$customer_model = new Customer();

// Get customer details
$customer = $customer_model->find($customer_id);

if (!$customer) {
    flash('error', 'Customer not found', 'danger');
    redirect('customers.php');
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    $reason = trim($_POST['reason'] ?? '');
    $admin_id = Auth::id();
    
    try {
        if ($action === 'suspend') {
            if (empty($reason)) {
                flash('error', 'Please provide a reason for suspension', 'danger');
            } else {
                $result = $customer_model->suspend($customer_id, $admin_id, $reason);
                if ($result) {
                    // Send notification
                    $notification = new NotificationService();
                    $notification->send(
                        $customer['user_id'],
                        'Account Suspended',
                        "Your customer account has been suspended. Please contact support for more information.",
                        'danger',
                        'customer'
                    );
                    
                    flash('success', 'Customer suspended successfully', 'success');
                    redirect('customers.php');
                } else {
                    flash('error', 'Failed to suspend customer', 'danger');
                }
            }
        } elseif ($action === 'activate') {
            $result = $customer_model->activate($customer_id, $admin_id);
            if ($result) {
                // Send notification
                $notification = new NotificationService();
                $notification->send(
                    $customer['user_id'],
                    'Account Activated',
                    "Your customer account has been reactivated. You can now continue using our services.",
                    'success',
                    'customer'
                );
                
                flash('success', 'Customer activated successfully', 'success');
                redirect('customers.php');
            } else {
                flash('error', 'Failed to activate customer', 'danger');
            }
        }
    } catch (Exception $e) {
        flash('error', 'An error occurred: ' . $e->getMessage(), 'danger');
    }
}

// Get customer stats
$order_count = $customer_model->getOrderCount($customer_id);
$total_spent = $customer_model->getTotalSpent($customer_id);

// Handle flash messages
$flash_message = flash('message');

include '../templates/layouts/admin/head.php';
?>

<div class="main-content">
    <?php include '../templates/layouts/admin/header.php'; ?>
    
    <!-- Page Header -->
    <div style="padding: 30px; background: white; border-bottom: 1px solid #eee;">
        <h1 style="font-size: 28px; font-weight: 700; color: var(--dark); margin-bottom: 5px;">
            <i class="fas fa-users"></i> Customer Management
        </h1>
        <div style="display: flex; gap: 8px; font-size: 14px; color: #666;">
            <a href="index.php" style="color: var(--primary); text-decoration: none;">Home</a>
            <span>/</span>
            <a href="customers.php" style="color: var(--primary); text-decoration: none;">Customers</a>
            <span>/</span>
            <span><?= Security::escapeOutput($customer['first_name'] . ' ' . $customer['last_name']) ?></span>
        </div>
    </div>
    
    <!-- Content -->
    <div style="padding: 30px;">
        
        <!-- Flash Messages -->
        <?php if ($flash_message): ?>
            <div class="alert alert-<?= $flash_message['type'] ?>">
                <?= Security::escapeOutput($flash_message['message']) ?>
            </div>
        <?php endif; ?>
        
        <div class="row">
            <div class="col-lg-8">
                <!-- Customer Information -->
                <div style="background: white; border-radius: 12px; padding: 25px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); margin-bottom: 30px;">
                    <h3 style="font-size: 18px; font-weight: 600; margin-bottom: 20px; color: var(--dark);">
                        <i class="fas fa-info-circle"></i> Customer Information
                    </h3>
                    
                    <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px;">
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;">Full Name</label>
                            <p style="font-size: 16px; font-weight: 600; color: var(--dark); margin: 0;">
                                <?= Security::escapeOutput($customer['first_name'] . ' ' . $customer['last_name']) ?>
                            </p>
                        </div>
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;">Email</label>
                            <p style="font-size: 16px; color: var(--dark); margin: 0;">
                                <?= Security::escapeOutput($customer['email'] ?? $customer['user_email']) ?>
                            </p>
                        </div>
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;">Phone</label>
                            <p style="font-size: 16px; color: var(--dark); margin: 0;">
                                <?= Security::escapeOutput($customer['phone'] ?? $customer['user_phone']) ?>
                            </p>
                        </div>
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;">Status</label>
                            <span class="status-badge <?= $customer['status'] === 'active' ? 'status-success' : 'status-rejected' ?>">
                                <?= ucfirst($customer['status'] ?? 'unknown') ?>
                            </span>
                        </div>
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;">Total Orders</label>
                            <p style="font-size: 16px; font-weight: 600; color: var(--dark); margin: 0;">
                                <?= number_format($order_count) ?>
                            </p>
                        </div>
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;">Total Spent</label>
                            <p style="font-size: 16px; font-weight: 600; color: var(--success); margin: 0;">
                                <?= format_money($total_spent) ?>
                            </p>
                        </div>
                    </div>
                    
                    <?php if (!empty($customer['address'])): ?>
                        <div style="margin-top: 20px;">
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;">Address</label>
                            <p style="color: var(--dark); line-height: 1.6;">
                                <?= Security::escapeOutput($customer['address']) ?>
                            </p>
                        </div>
                    <?php endif; ?>
                    
                    <?php if (!empty($customer['suspension_reason'])): ?>
                        <div style="margin-top: 20px; padding: 15px; background: #f8d7da; border-radius: 8px; border-left: 4px solid var(--danger);">
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: var(--danger);">Suspension Reason</label>
                            <p style="color: var(--dark); margin: 0;">
                                <?= Security::escapeOutput($customer['suspension_reason']) ?>
                            </p>
                        </div>
                    <?php endif; ?>
                </div>
                
                <!-- Quick Actions -->
                <div style="background: white; border-radius: 12px; padding: 25px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); margin-bottom: 30px;">
                    <h3 style="font-size: 18px; font-weight: 600; margin-bottom: 20px; color: var(--dark);">
                        <i class="fas fa-bolt"></i> Quick Actions
                    </h3>
                    
                    <div style="display: flex; gap: 15px; flex-wrap: wrap;">
                        <a href="customer-details.php?id=<?= $customer_id ?>" class="btn btn-primary">
                            <i class="fas fa-eye"></i> View Full Details
                        </a>
                        <a href="orders.php?customer=<?= $customer_id ?>" class="btn btn-info">
                            <i class="fas fa-shopping-cart"></i> View Orders
                        </a>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-4">
                <!-- Action Panel -->
                <div style="background: white; border-radius: 12px; padding: 25px; box-shadow: 0 2px 10px rgba(0,0,0,0.05);">
                    <h3 style="font-size: 18px; font-weight: 600; margin-bottom: 20px; color: var(--dark);">
                        <i class="fas fa-cog"></i> Account Actions
                    </h3>
                    
                    <?php if ($customer['status'] === 'active'): ?>
                        <!-- Suspend Form -->
                        <div style="border: 2px solid var(--warning); border-radius: 8px; padding: 20px;">
                            <h4 style="color: var(--warning); margin-bottom: 15px;">
                                <i class="fas fa-exclamation-triangle"></i> Suspend Account
                            </h4>
                            <p style="color: #666; margin-bottom: 20px;">
                                Suspending this account will prevent the customer from placing new orders.
                            </p>
                            
                            <form method="POST">
                                <input type="hidden" name="action" value="suspend">
                                
                                <div style="margin-bottom: 15px;">
                                    <label style="display: block; margin-bottom: 8px; font-weight: 600; color: var(--dark);">Reason for Suspension <span style="color: var(--danger);">*</span></label>
                                    <textarea name="reason" class="form-control" rows="4" required
                                              placeholder="Provide a detailed reason for suspension..."></textarea>
                                </div>
                                
                                <button type="submit" class="btn btn-warning" style="width: 100%;"
                                        onclick="return confirm('Are you sure you want to suspend this customer account?')">
                                    <i class="fas fa-pause"></i> Suspend Account
                                </button>
                            </form>
                        </div>
                    <?php else: ?>
                        <!-- Activate Form -->
                        <div style="border: 2px solid var(--success); border-radius: 8px; padding: 20px;">
                            <h4 style="color: var(--success); margin-bottom: 15px;">
                                <i class="fas fa-check-circle"></i> Activate Account
                            </h4>
                            <p style="color: #666; margin-bottom: 20px;">
                                Activating this account will restore full access for the customer.
                            </p>
                            
                            <form method="POST">
                                <input type="hidden" name="action" value="activate">
                                
                                <button type="submit" class="btn btn-success" style="width: 100%;"
                                        onclick="return confirm('Are you sure you want to activate this customer account?')">
                                    <i class="fas fa-play"></i> Activate Account
                                </button>
                            </form>
                        </div>
                    <?php endif; ?>
                </div>
                
                <!-- Account History -->
                <div style="background: white; border-radius: 12px; padding: 25px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); margin-top: 20px;">
                    <h3 style="font-size: 18px; font-weight: 600; margin-bottom: 20px; color: var(--dark);">
                        <i class="fas fa-history"></i> Account History
                    </h3>
                    
                    <div style="font-size: 14px; color: #666;">
                        <div style="margin-bottom: 10px;">
                            <strong>Joined:</strong><br>
                            <?= format_date($customer['created_at'], 'M d, Y h:i A') ?>
                        </div>
                        <?php if ($customer['updated_at']): ?>
                            <div>
                                <strong>Last Updated:</strong><br>
                                <?= format_date($customer['updated_at'], 'M d, Y h:i A') ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
.form-control {
    border: 1px solid #ddd;
    border-radius: 6px;
    padding: 10px 12px;
    font-size: 14px;
    transition: border-color 0.3s;
    width: 100%;
}

.form-control:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
}

.btn {
    border: none;
    border-radius: 6px;
    padding: 10px 20px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    text-decoration: none;
    display: inline-block;
    font-size: 14px;
}

.btn-primary {
    background: var(--primary);
    color: white;
}

.btn-primary:hover {
    background: #0056b3;
    transform: translateY(-2px);
}

.btn-success {
    background: var(--success);
    color: white;
}

.btn-success:hover {
    background: #1e7e34;
}

.btn-warning {
    background: var(--warning);
    color: white;
}

.btn-warning:hover {
    background: #e0a800;
}

.btn-info {
    background: var(--info);
    color: white;
}

.btn-info:hover {
    background: #138496;
}

.alert {
    padding: 15px 20px;
    border-radius: 8px;
    margin-bottom: 20px;
}

.alert-success {
    background: #d4edda;
    color: #155724;
    border: 1px solid #c3e6cb;
}

.alert-danger {
    background: #f8d7da;
    color: #721c24;
    border: 1px solid #f5c6cb;
}

.status-badge {
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    color: white;
}

.status-success {
    background: var(--success);
}

.status-rejected {
    background: var(--danger);
}
</style>

<?php include '../templates/layouts/admin/footer.php'; ?>
