<?php
/**
 * ADMIN CUSTOMERS MANAGEMENT PAGE
 * File: admin/customers.php
 * Manage all customers, view details, and handle customer-related actions
 */

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

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

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

// Get filter parameters
$search = $_GET['search'] ?? '';
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$per_page = ADMIN_ITEMS_PER_PAGE;

// Initialize customer model
$customer_model = new Customer();

// Build filters
$filters = [];
if (!empty($search)) {
    $filters['search'] = $search;
}

// Get customers
try {
    $total_customers = $customer_model->count($filters);
    $pagination = paginate($total_customers, $per_page, $page);
    $customers = $customer_model->getAll($per_page, $pagination['offset'], $filters);
} catch (Exception $e) {
    log_message("Customers page error: " . $e->getMessage(), 'error');
    $customers = [];
    $pagination = ['total_pages' => 0, 'current_page' => 1];
}

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

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

</head>
<body>
    
    
    <!-- Main Content -->
    <div class="main-content">
        <?php include '../templates/layouts/admin/header.php'; ?>
        
        <div class="page-header">
            <h1>Customers Management</h1>
            <div style="font-size: 14px; color: #666;">
                <a href="index.php" style="color: var(--primary); text-decoration: none;">Home</a> / Customers
            </div>
        </div>
        
        <div class="content">
            <?php if ($flash_message): ?>
                <div class="alert alert-<?= $flash_message['type'] ?>">
                    <?= Security::escapeOutput($flash_message['message']) ?>
                </div>
            <?php endif; ?>
            
            <!-- Filter Section -->
            <div class="filter-section">
                <form method="GET">
                    <div class="filter-row">
                        <div class="filter-group">
                            <label>Search</label>
                            <input type="text" name="search" placeholder="Search by name, email, phone..." value="<?= Security::escapeOutput($search) ?>">
                        </div>
                        <div>
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-search"></i> Filter
                            </button>
                            <a href="customers.php" class="btn btn-secondary">Reset</a>
                        </div>
                    </div>
                </form>
            </div>
            
            <!-- Customers Table -->
            <div class="table-container">
                <div style="display: flex; justify-content: space-between; margin-bottom: 20px;">
                    <h3 style="margin: 0;">All Customers</h3>
                    <span style="color: #666;">Total: <?= $total_customers ?></span>
                </div>
                
                <table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Email</th>
                            <th>Phone</th>
                            <th>Location</th>
                            <th>Orders</th>
                            <th>Total Spent</th>
                            <th>Joined</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($customers)): ?>
                            <tr>
                                <td colspan="9" style="text-align: center; padding: 30px; color: #999;">
                                    No customers found
                                </td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($customers as $customer): ?>
                                <tr>
                                    <td><?= $customer['id'] ?></td>
                                    <td style="font-weight: 600;">
                                        <?= Security::escapeOutput($customer['first_name'] . ' ' . $customer['last_name']) ?>
                                    </td>
                                    <td><?= Security::escapeOutput($customer['email']) ?></td>
                                    <td><?= format_phone($customer['phone']) ?></td>
                                    <td><?= Security::escapeOutput(($customer['city'] ?? '') . ', ' . ($customer['state'] ?? '')) ?></td>
                                    <td><?= $customer['total_orders'] ?? 0 ?></td>
                                    <td><strong><?= format_money($customer['total_spent'] ?? 0) ?></strong></td>
                                    <td><?= format_date($customer['registered_at']) ?></td>
                                    <td>
                                        <a href="customer-details.php?id=<?= $customer['id'] ?>" class="btn-action">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
                
                <!-- Pagination -->
                <?php if ($pagination['total_pages'] > 1): ?>
                    <ul class="pagination">
                        <?php for ($i = 1; $i <= $pagination['total_pages']; $i++): ?>
                            <li class="<?= $i == $pagination['current_page'] ? 'active' : '' ?>">
                                <?php if ($i == $pagination['current_page']): ?>
                                    <span><?= $i ?></span>
                                <?php else: ?>
                                    <a href="?page=<?= $i ?><?= !empty($search) ? '&search=' . urlencode($search) : '' ?>"><?= $i ?></a>
                                <?php endif; ?>
                            </li>
                        <?php endfor; ?>
                    </ul>
                <?php endif; ?>
            </div>
        </div>
    </div>
</body>
</html>