<?php
/**
 * Admin Abandoned Carts Page
 * List of abandoned carts with customer contact info
 */

$pageTitle = 'Coșuri abandonate';

require_once __DIR__ . '/includes/init.php';
require_once __DIR__ . '/includes/header.php';
require_once __DIR__ . '/../../includes/Cart.php';
require_once __DIR__ . '/../../includes/Product.php';

$db = Database::getInstance();
$cart = new Cart();
$product = new Product();

// Get abandoned carts (carts older than 24 hours without order)
$abandonedCarts = $db->fetchAll(
    "SELECT c.*, u.first_name, u.last_name, u.email, u.phone,
            (SELECT COUNT(*) FROM cart_items WHERE cart_id = c.id) as items_count
     FROM cart c
     LEFT JOIN users u ON c.user_id = u.id
     WHERE c.updated_at < DATE_SUB(NOW(), INTERVAL 24 HOUR)
     AND c.id NOT IN (SELECT DISTINCT cart_id FROM orders WHERE cart_id IS NOT NULL)
     ORDER BY c.updated_at DESC
     LIMIT 100"
);

// Get cart items for each abandoned cart
foreach ($abandonedCarts as &$abandonedCart) {
    $abandonedCart['items'] = $cart->getCartItems($abandonedCart['id']);
    $abandonedCart['total'] = 0;
    foreach ($abandonedCart['items'] as $item) {
        $prod = $product->getProductById($item['product_id']);
        if ($prod) {
            $price = $prod['sale_price'] ?? $prod['price'];
            $abandonedCart['total'] += $price * $item['quantity'];
        }
    }
}
unset($abandonedCart);
?>

<div class="content">
  <div class="px-3 px-xxl-4 py-3 py-lg-4">
    <div class="card mb-3">
      <div class="card-body">
        <div class="row flex-between-center">
          <div class="col-sm-auto mb-2 mb-sm-0">
            <h6 class="mb-0">Coșuri abandonate: <?php echo count($abandonedCarts); ?></h6>
          </div>
        </div>
      </div>
    </div>

    <div class="card">
      <div class="card-body p-0">
        <div class="table-responsive">
          <table class="table table-sm mb-0">
            <thead class="bg-body-tertiary">
              <tr>
                <th>Client</th>
                <th>Contact</th>
                <th>Produse</th>
                <th>Valoare</th>
                <th>Ultima actualizare</th>
                <th>Email-uri trimise</th>
                <th>Acțiuni</th>
              </tr>
            </thead>
            <tbody>
              <?php if (empty($abandonedCarts)): ?>
              <tr>
                <td colspan="7" class="text-center py-4">Nu există coșuri abandonate.</td>
              </tr>
              <?php else: ?>
              <?php foreach ($abandonedCarts as $cartData): ?>
              <tr>
                <td>
                  <?php if ($cartData['first_name']): ?>
                  <div><?php echo htmlspecialchars($cartData['first_name'] . ' ' . $cartData['last_name']); ?></div>
                  <?php else: ?>
                  <span class="text-600">Vizitator anonim</span>
                  <?php endif; ?>
                </td>
                <td>
                  <?php if ($cartData['email']): ?>
                  <div><?php echo htmlspecialchars($cartData['email']); ?></div>
                  <?php endif; ?>
                  <?php if ($cartData['phone']): ?>
                  <small class="text-600"><?php echo htmlspecialchars($cartData['phone']); ?></small>
                  <?php endif; ?>
                </td>
                <td>
                  <div><?php echo $cartData['items_count']; ?> produse</div>
                  <small class="text-600">
                    <?php 
                    $productNames = [];
                    foreach (array_slice($cartData['items'], 0, 3) as $item) {
                        $prod = $product->getProductById($item['product_id']);
                        if ($prod) {
                            $productNames[] = htmlspecialchars($prod['name']);
                        }
                    }
                    echo implode(', ', $productNames);
                    if (count($cartData['items']) > 3) {
                        echo '...';
                    }
                    ?>
                  </small>
                </td>
                <td><?php echo number_format($cartData['total'], 2); ?> RON</td>
                <td><?php echo date('d.m.Y H:i', strtotime($cartData['updated_at'])); ?></td>
                <td><?php echo $cartData['email_count'] ?? 0; ?></td>
                <td>
                  <?php if ($cartData['email']): ?>
                  <a class="btn btn-sm btn-falcon-default" href="mailto:<?php echo htmlspecialchars($cartData['email']); ?>?subject=Coșul tău de cumpărături" data-bs-toggle="tooltip" title="Trimite email">
                    <span class="fas fa-envelope"></span>
                  </a>
                  <?php endif; ?>
                </td>
              </tr>
              <?php endforeach; ?>
              <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>

<?php require_once __DIR__ . '/includes/footer.php'; ?>

