<h4 class="fw-bold mb-4 text-dark"><i class="bi bi-cart3 me-2 text-primary"></i><?= __('shopping_cart') ?></h4>

<?php if (empty($cart['stores'])): ?>
  <div class="cargoo-card p-5 text-center my-4">
    <i class="bi bi-cart-x text-muted" style="font-size: 3.5rem;"></i>
    <h5 class="fw-bold text-dark mt-3"><?= __('cart_empty') ?></h5>
    <p class="text-muted small mb-4">คุณยังไม่มีสินค้าในรถเข็น เริ่มต้นค้นหาสินค้าที่คุณสนใจได้เลย</p>
    <a href="<?= url('/search') ?>" class="btn btn-cargoo-primary px-4 py-2">
      <i class="bi bi-bag me-1"></i> <?= __('continue_shopping') ?>
    </a>
  </div>
<?php else: ?>
  <div class="row g-4">
    <!-- Left Column: Stores & Cart Items -->
    <div class="col-lg-8">
      <div class="d-flex flex-column gap-3">
        <?php foreach ($cart['stores'] as $store): ?>
          <div class="cargoo-card overflow-hidden">
            <!-- Store Header -->
            <div class="cargoo-card-header bg-light d-flex align-items-center justify-content-between py-2 px-3">
              <a href="<?= url('/shop/' . $store['store_slug']) ?>" class="text-decoration-none fw-bold text-dark d-flex align-items-center gap-2">
                <i class="bi bi-shop text-primary"></i>
                <span><?= e($store['store_name']) ?></span>
              </a>
              <span class="small text-muted">ยอดรวมร้านนี้: <strong class="text-primary">฿<?= number_format($store['store_subtotal'], 2) ?></strong></span>
            </div>

            <!-- Store Items List -->
            <div class="p-3">
              <?php foreach ($store['items'] as $item): ?>
                <div class="d-flex flex-column flex-sm-row align-items-sm-center justify-content-between gap-3 py-3 border-bottom <?= !$item['is_available'] ? 'opacity-50' : '' ?>">
                  <!-- Select Checkbox & Product Image & Title -->
                  <div class="d-flex align-items-center gap-3">
                    <form action="<?= url('/cart/toggle-select') ?>" method="POST" class="m-0 form-cart-select">
                      <?= Security::csrfField() ?>
                      <input type="hidden" name="cart_item_id" value="<?= (int)$item['cart_item_id'] ?>">
                      <input type="hidden" name="product_id" value="<?= (int)$item['product_id'] ?>">
                      <input type="checkbox" name="is_selected" value="1" class="form-check-input cart-item-checkbox" <?= $item['is_selected'] ? 'checked' : '' ?> <?= !$item['is_available'] ? 'disabled' : '' ?> onchange="this.form.submit();">
                    </form>

                    <a href="<?= url('/product/' . $item['product_slug']) ?>" class="rounded-2 overflow-hidden border bg-light d-flex align-items-center justify-content-center" style="width: 70px; height: 70px; flex-shrink: 0;">
                      <?php if (!empty($item['main_image'])): ?>
                        <img src="<?= product_image_url($item['main_image']) ?>" alt="<?= e($item['product_name']) ?>" class="w-100 h-100 object-fit-contain">
                      <?php else: ?>
                        <i class="bi bi-image text-muted fs-4"></i>
                      <?php endif; ?>
                    </a>

                    <div>
                      <h6 class="fw-semibold text-dark mb-1 text-truncate" style="max-width: 250px;">
                        <a href="<?= url('/product/' . $item['product_slug']) ?>" class="text-dark text-decoration-none">
                          <?= e($item['product_name']) ?>
                        </a>
                      </h6>
                      
                      <!-- Selected Variation Badge -->
                      <?php if (!empty($item['variation_summary'])): ?>
                        <div class="mb-1">
                          <span class="badge bg-light text-secondary border fw-normal" style="font-size: 0.75rem;">
                            <i class="bi bi-tag-fill text-primary me-1"></i>ตัวเลือก: <?= e($item['variation_summary']) ?>
                          </span>
                        </div>
                      <?php endif; ?>

                      <span class="text-primary fw-bold small">฿<?= number_format((float)$item['price'], 2) ?></span>
                      <?php if (!$item['is_available']): ?>
                        <span class="badge bg-danger small ms-2">สินค้าหมด</span>
                      <?php endif; ?>
                    </div>
                  </div>

                  <!-- Quantity Controls & Subtotal & Delete -->
                  <div class="d-flex align-items-center justify-content-between justify-content-sm-end gap-3 ms-sm-auto">
                    <!-- Quantity Adjust Form -->
                    <form action="<?= url('/cart/update') ?>" method="POST" class="d-flex align-items-center m-0">
                      <?= Security::csrfField() ?>
                      <input type="hidden" name="cart_item_id" value="<?= (int)$item['cart_item_id'] ?>">
                      <input type="hidden" name="product_id" value="<?= (int)$item['product_id'] ?>">
                      
                      <div class="input-group input-group-sm" style="width: 110px;">
                        <button class="btn btn-outline-secondary" type="submit" name="quantity" value="<?= (int)$item['quantity'] - 1 ?>" <?= (int)$item['quantity'] <= 1 ? 'disabled' : '' ?>>-</button>
                        <input type="text" class="form-control text-center fw-bold bg-white" value="<?= (int)$item['quantity'] ?>" readonly>
                        <button class="btn btn-outline-secondary" type="submit" name="quantity" value="<?= (int)$item['quantity'] + 1 ?>" <?= (int)$item['quantity'] >= (int)$item['stock'] ? 'disabled' : '' ?>>+</button>
                      </div>
                    </form>

                    <!-- Item Total -->
                    <div class="text-end" style="min-width: 90px;">
                      <span class="fw-bold text-dark">฿<?= number_format($item['subtotal'], 2) ?></span>
                    </div>

                    <!-- Remove Button -->
                    <form action="<?= url('/cart/remove') ?>" method="POST" class="m-0">
                      <?= Security::csrfField() ?>
                      <input type="hidden" name="cart_item_id" value="<?= (int)$item['cart_item_id'] ?>">
                      <input type="hidden" name="product_id" value="<?= (int)$item['product_id'] ?>">
                      <button type="submit" class="btn btn-outline-danger btn-sm border-0" title="ลบสินค้า">
                        <i class="bi bi-trash"></i>
                      </button>
                    </form>
                  </div>
                </div>
              <?php endforeach; ?>
            </div>
          </div>
        <?php endforeach; ?>
      </div>
    </div>

    <!-- Right Column: Order Summary Card -->
    <div class="col-lg-4">
      <div class="cargoo-card p-4 position-sticky" style="top: 80px;">
        <h5 class="fw-bold text-dark mb-3"><?= __('order_summary') ?></h5>

        <div class="d-flex justify-content-between align-items-center mb-2 small text-muted">
          <span><?= __('items_selected', ['count' => $cart['selected_items_count']]) ?></span>
          <span class="text-dark fw-medium">฿<?= number_format($cart['selected_total'], 2) ?></span>
        </div>

        <div class="d-flex justify-content-between align-items-center mb-3 small text-muted">
          <span><?= __('shipping_fee') ?> (ประเมิน)</span>
          <span class="text-dark fw-medium">คำนวณในขั้นตอนถัดไป</span>
        </div>

        <hr class="my-3">

        <div class="d-flex justify-content-between align-items-center mb-4">
          <span class="fw-bold text-dark fs-6"><?= __('total_payment') ?></span>
          <span class="fs-4 fw-bold text-primary">฿<?= number_format($cart['selected_total'], 2) ?></span>
        </div>

        <?php if ($cart['selected_items_count'] > 0): ?>
          <a href="<?= url('/checkout') ?>" class="btn btn-cargoo-primary w-100 py-2 fs-6">
            <i class="bi bi-credit-card me-1"></i> <?= __('proceed_to_checkout') ?>
          </a>
        <?php else: ?>
          <button type="button" class="btn btn-secondary w-100 py-2 fs-6 disabled" disabled>
            กรุณาเลือกสินค้าในรถเข็น
          </button>
        <?php endif; ?>

        <div class="text-center mt-3">
          <a href="<?= url('/search') ?>" class="small text-muted text-decoration-none">
            <i class="bi bi-arrow-left me-1"></i> <?= __('continue_shopping') ?>
          </a>
        </div>
      </div>
    </div>
  </div>
<?php endif; ?>
