<?php
/**
 * Checkout Thank You Page
 * Converted from checkout-v1-thankyou.html
 * Order confirmation page after successful checkout
 */

$pageTitle = 'Cartzilla | Mulțumim pentru comandă!';
$pageDescription = 'Cartzilla - Magazin Online E-Commerce';

require_once __DIR__ . '/includes/header.php';
require_once __DIR__ . '/includes/product-card-helper.php';

// Initialize classes
$order = new Order();
$product = new Product();
$user = new User();
$auth = new Auth();

// Get order number from URL
$orderNumber = $_GET['order'] ?? null;

if (!$orderNumber) {
    // Redirect to home if no order number
    header('Location: /');
    exit;
}

// Get order by order number
$orderData = $order->getOrderByNumber($orderNumber);

if (!$orderData) {
    // Order not found
    http_response_code(404);
    require_once __DIR__ . '/404-electronics.php';
    exit;
}

// Get order items
$orderItems = $order->getOrderItems($orderData['id']);

// Get shipping address
$shippingAddress = null;
if ($orderData['shipping_address_id']) {
    $shippingAddress = $user->getAddressById($orderData['shipping_address_id']);
}

// Get user B2B group for pricing
$userId = $auth->getUserId();
$b2bGroup = $userId ? $user->getB2BGroup($userId) : null;

// Get related/recommended products
$relatedProducts = $product->getFeaturedProducts(4);

// Format delivery date (3 days from now)
$deliveryDate = date('l, j F', strtotime('+3 days'));
$days = ['Monday' => 'Luni', 'Tuesday' => 'Marți', 'Wednesday' => 'Miercuri', 'Thursday' => 'Joi', 'Friday' => 'Vineri', 'Saturday' => 'Sâmbătă', 'Sunday' => 'Duminică'];
$months = ['January' => 'ianuarie', 'February' => 'februarie', 'March' => 'martie', 'April' => 'aprilie', 'May' => 'mai', 'June' => 'iunie', 'July' => 'iulie', 'August' => 'august', 'September' => 'septembrie', 'October' => 'octombrie', 'November' => 'noiembrie', 'December' => 'decembrie'];
$dayName = $days[date('l', strtotime('+3 days'))] ?? date('l', strtotime('+3 days'));
$monthName = $months[date('F', strtotime('+3 days'))] ?? date('F', strtotime('+3 days'));
$formattedDeliveryDate = $dayName . ', ' . date('j', strtotime('+3 days')) . ' ' . $monthName;

// Payment method display
$paymentMethods = [
    'cash_on_delivery' => 'Ramburs la livrare',
    'credit_card' => 'Card de credit',
    'paypal' => 'PayPal',
    'google_pay' => 'Google Pay'
];
$paymentMethodDisplay = $paymentMethods[$orderData['payment_method']] ?? $orderData['payment_method'];
?>

    <!-- Page content -->
    <main class="content-wrapper">
      <div class="row row-cols-1 row-cols-lg-2 g-0 mx-auto" style="max-width: 1920px">

        <!-- Thank you content column -->
        <div class="col d-flex flex-column justify-content-center py-5 px-xl-4 px-xxl-5">
          <div class="w-100 pt-sm-2 pt-md-3 pt-lg-4 pb-lg-4 pb-xl-5 px-3 px-sm-4 pe-lg-0 ps-lg-5 mx-auto ms-lg-auto me-lg-4" style="max-width: 740px">
            <div class="d-flex align-items-sm-center border-bottom pb-4 pb-md-5">
              <div class="d-flex align-items-center justify-content-center bg-success text-white rounded-circle flex-shrink-0" style="width: 3rem; height: 3rem; margin-top: -.125rem">
                <i class="ci-check fs-4"></i>
              </div>
              <div class="w-100 ps-3">
                <div class="fs-sm mb-1">Comanda #<?php echo htmlspecialchars($orderData['order_number']); ?></div>
                <div class="d-sm-flex align-items-center">
                  <h1 class="h4 mb-0 me-3">Mulțumim pentru comandă!</h1>
                  <?php if ($userId): ?>
                  <div class="nav mt-2 mt-sm-0 ms-auto">
                    <a class="nav-link text-decoration-underline p-0" href="/cont/comenzi">Vezi comanda</a>
                  </div>
                  <?php endif; ?>
                </div>
              </div>
            </div>
            <div class="d-flex flex-column gap-4 pt-3 pb-5 mt-3">
              <?php if ($shippingAddress): ?>
              <div>
                <h3 class="h6 mb-2">Livrare</h3>
                <p class="fs-sm mb-0">
                  <?php echo htmlspecialchars($shippingAddress['address_line1']); ?><br>
                  <?php if ($shippingAddress['address_line2']): ?>
                  <?php echo htmlspecialchars($shippingAddress['address_line2']); ?><br>
                  <?php endif; ?>
                  <?php echo htmlspecialchars($shippingAddress['city'] . ', ' . $shippingAddress['county'] . ' ' . $shippingAddress['postal_code']); ?>
                </p>
              </div>
              <?php endif; ?>
              <div>
                <h3 class="h6 mb-2">Timp</h3>
                <p class="fs-sm mb-0"><?php echo $formattedDeliveryDate; ?>, 09:00 - 17:00</p>
              </div>
              <div>
                <h3 class="h6 mb-2">Plată</h3>
                <p class="fs-sm mb-0"><?php echo htmlspecialchars($paymentMethodDisplay); ?></p>
              </div>
            </div>
            <?php if ($orderData['coupon_code']): ?>
            <div class="bg-success rounded px-4 py-4" style="--cz-bg-opacity: .2">
              <div class="py-3">
                <h2 class="h4 text-center pb-2 mb-1">🎉 Felicitări! Ai folosit codul promoțional!</h2>
                <p class="fs-sm text-center mb-4">Codul folosit: <strong><?php echo htmlspecialchars($orderData['coupon_code']); ?></strong></p>
              </div>
            </div>
            <?php endif; ?>
            <?php if ($orderData['loyalty_points_used'] > 0): ?>
            <div class="bg-warning rounded px-4 py-4 mt-3" style="--cz-bg-opacity: .2">
              <div class="py-3">
                <h2 class="h4 text-center pb-2 mb-1">⭐ Ai folosit <?php echo $orderData['loyalty_points_used']; ?> puncte fidelitate!</h2>
                <p class="fs-sm text-center mb-0">Ai economisit <?php echo number_format($orderData['loyalty_points_used'], 2); ?> RON folosind punctele tale.</p>
              </div>
            </div>
            <?php endif; ?>
            <p class="fs-sm pt-4 pt-md-5 mt-2 mt-sm-3 mt-md-0 mb-0">Ai nevoie de ajutor? <a class="fw-medium ms-2" href="/contact">Contactează-ne</a></p>
          </div>
        </div>


        <!-- Related products -->
        <div class="col pt-sm-3 p-md-5 ps-lg-5 py-lg-4 pe-lg-4 p-xxl-5">
          <div class="position-relative d-flex align-items-center h-100 py-5 px-3 px-sm-4 px-xl-5">
            <span class="position-absolute top-0 start-0 w-100 h-100 bg-body-tertiary rounded-5 d-none d-md-block"></span>
            <span class="position-absolute top-0 start-0 w-100 h-100 bg-body-tertiary d-md-none"></span>
            <div class="position-relative w-100 z-2 mx-auto pb-2 pb-sm-3 pb-md-0" style="max-width: 636px">
              <h2 class="h4 text-center pb-3">Ai putea să-ți placă și</h2>
              <div class="row row-cols-2 g-3 g-sm-4 mb-4">
                <?php if (!empty($relatedProducts)): ?>
                  <?php foreach ($relatedProducts as $relatedProduct): ?>
                    <div class="col">
                      <?php echo renderProductCard($relatedProduct, $b2bGroup); ?>
                    </div>
                  <?php endforeach; ?>
                <?php else: ?>
                  <div class="col-12">
                    <p class="text-center text-body-secondary">Nu sunt produse recomandate disponibile.</p>
                  </div>
                <?php endif; ?>
              </div>

              <a class="btn btn-lg btn-primary w-100" href="/catalog">
                Continuă cumpărăturile
                <i class="ci-chevron-right fs-lg ms-1 me-n1"></i>
              </a>
            </div>
          </div>
        </div>
      </div>
    </main>

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

