<?php

$pageTitle =
    'Giỏ hàng - PET SPA';

$pageDescription =
    'Giỏ hàng của bạn tại PET SPA.';

$pageScript =
    'cart.js';


$successMessage =
    $_SESSION[
        'flash_success'
    ]
    ?? null;


$errorMessage =
    $_SESSION[
        'flash_error'
    ]
    ?? null;


unset(
    $_SESSION[
        'flash_success'
    ],
    $_SESSION[
        'flash_error'
    ]
);


require BASE_PATH
    . '/app/views/layouts/header.php';


require BASE_PATH
    . '/app/views/layouts/navbar.php';

?>


<main>

    <section class="section">

        <div class="container">


            <!-- BREADCRUMB -->

            <nav aria-label="breadcrumb">

                <ol class="breadcrumb">

                    <li class="breadcrumb-item">

                        <a href="<?= BASE_URL ?>/">
                            Trang chủ
                        </a>

                    </li>

                    <li
                        class="breadcrumb-item active"
                    >
                        Giỏ hàng
                    </li>

                </ol>

            </nav>


            <div
                class="d-flex justify-content-between align-items-center mb-4"
            >

                <div>

                    <h1 class="fw-bold mb-1">
                        Giỏ hàng
                    </h1>

                    <p class="text-muted mb-0">

                        <?= (int)
                            $cartCount
                        ?>

                        sản phẩm trong giỏ.

                    </p>

                </div>

            </div>


            <!-- FLASH -->

            <?php if ($successMessage): ?>

                <div
                    class="alert alert-success alert-dismissible fade show"
                >

                    <i
                        class="bi bi-check-circle me-2"
                    ></i>

                    <?= htmlspecialchars(
                        $successMessage
                    ) ?>


                    <button
                        type="button"
                        class="btn-close"
                        data-bs-dismiss="alert"
                    ></button>

                </div>

            <?php endif; ?>


            <?php if ($errorMessage): ?>

                <div
                    class="alert alert-danger alert-dismissible fade show"
                >

                    <?= htmlspecialchars(
                        $errorMessage
                    ) ?>

                    <button
                        type="button"
                        class="btn-close"
                        data-bs-dismiss="alert"
                    ></button>

                </div>

            <?php endif; ?>


            <?php if (empty($items)): ?>

                <!-- EMPTY CART -->

                <div
                    class="empty-cart text-center"
                >

                    <div
                        class="empty-cart-icon"
                    >
                        🛒
                    </div>

                    <h2>
                        Giỏ hàng đang trống
                    </h2>

                    <p class="text-muted">

                        Hãy chọn thêm những sản phẩm
                        phù hợp cho Boss nhé.

                    </p>


                    <a
                        href="<?= BASE_URL ?>/?url=products"
                        class="btn btn-primary btn-lg"
                    >

                        Khám phá sản phẩm

                    </a>

                </div>

            <?php else: ?>


                <div class="row g-4">


                    <!-- ==========================================
                         CART ITEMS
                    =========================================== -->

                    <div class="col-lg-8">


                        <div class="cart-items">

                            <?php foreach (
                                $items
                                as $item
                            ): ?>


                                <?php

                                $imageUrl = null;

                                if (
                                    !empty(
                                        $item[
                                            'featured_image'
                                        ]
                                    )
                                ) {

                                    $imageUrl =
                                        BASE_URL
                                        . '/'
                                        . ltrim(
                                            $item[
                                                'featured_image'
                                            ],
                                            '/'
                                        );
                                }

                                ?>


                                <article
                                    class="cart-item"
                                >


                                    <!-- IMAGE -->

                                    <a
                                        href="<?= BASE_URL ?>/?url=product&slug=<?= urlencode(
                                            $item['slug']
                                        ) ?>"
                                        class="cart-item-image"
                                    >

                                        <?php if ($imageUrl): ?>

                                            <img
                                                src="<?= htmlspecialchars(
                                                    $imageUrl
                                                ) ?>"
                                                alt="<?= htmlspecialchars(
                                                    $item['name']
                                                ) ?>"
                                            >

                                        <?php else: ?>

                                            <div
                                                class="cart-image-placeholder"
                                            >
                                                🐾
                                            </div>

                                        <?php endif; ?>

                                    </a>


                                    <!-- INFO -->

                                    <div
                                        class="cart-item-info"
                                    >

                                        <h3>

                                            <a
                                                href="<?= BASE_URL ?>/?url=product&slug=<?= urlencode(
                                                    $item['slug']
                                                ) ?>"
                                            >

                                                <?= htmlspecialchars(
                                                    $item[
                                                        'name'
                                                    ]
                                                ) ?>

                                            </a>

                                        </h3>


                                        <?php if (
                                            !empty(
                                                $item['sku']
                                            )
                                        ): ?>

                                            <small
                                                class="text-muted"
                                            >

                                                SKU:

                                                <?= htmlspecialchars(
                                                    $item['sku']
                                                ) ?>

                                            </small>

                                        <?php endif; ?>


                                        <div
                                            class="cart-item-price"
                                        >

                                            <?= number_format(
                                                (float)
                                                $item[
                                                    'current_price'
                                                ],
                                                0,
                                                ',',
                                                '.'
                                            ) ?>đ

                                        </div>


                                        <?php if (
                                            $item['status']
                                            !== 'active'

                                            || (int)
                                            $item[
                                                'stock_quantity'
                                            ] <= 0
                                        ): ?>

                                            <div
                                                class="text-danger small mt-2"
                                            >

                                                Sản phẩm hiện đã hết hàng.

                                            </div>

                                        <?php endif; ?>

                                    </div>


                                    <!-- QUANTITY -->

                                    <div
                                        class="cart-item-actions"
                                    >

                                        <form
                                            action="<?= BASE_URL ?>/?url=cart/update"
                                            method="POST"
                                            class="cart-update-form"
                                        >

                                            <input
                                                type="hidden"
                                                name="_token"
                                                value="<?= htmlspecialchars(
                                                    $_SESSION[
                                                        'csrf_token'
                                                    ]
                                                ) ?>"
                                            >

                                            <input
                                                type="hidden"
                                                name="product_id"
                                                value="<?= (int)
                                                    $item[
                                                        'product_id'
                                                    ]
                                                ?>"
                                            >


                                            <div
                                                class="quantity-control"
                                            >

                                                <button
                                                    type="button"
                                                    class="quantity-minus"
                                                >
                                                    −
                                                </button>


                                                <input
                                                    type="number"
                                                    name="quantity"
                                                    value="<?= (int)
                                                        $item[
                                                            'quantity'
                                                        ]
                                                    ?>"
                                                    min="1"
                                                    max="<?= max(
                                                        1,
                                                        (int)
                                                        $item[
                                                            'stock_quantity'
                                                        ]
                                                    ) ?>"
                                                >


                                                <button
                                                    type="button"
                                                    class="quantity-plus"
                                                >
                                                    +
                                                </button>

                                            </div>


                                            <button
                                                type="submit"
                                                class="btn btn-sm btn-light mt-2"
                                            >
                                                Cập nhật
                                            </button>

                                        </form>


                                        <div
                                            class="cart-line-total"
                                        >

                                            <?= number_format(
                                                (float)
                                                $item[
                                                    'line_total'
                                                ],
                                                0,
                                                ',',
                                                '.'
                                            ) ?>đ

                                        </div>


                                        <form
                                            action="<?= BASE_URL ?>/?url=cart/remove"
                                            method="POST"
                                        >

                                            <input
                                                type="hidden"
                                                name="_token"
                                                value="<?= htmlspecialchars(
                                                    $_SESSION[
                                                        'csrf_token'
                                                    ]
                                                ) ?>"
                                            >

                                            <input
                                                type="hidden"
                                                name="product_id"
                                                value="<?= (int)
                                                    $item[
                                                        'product_id'
                                                    ]
                                                ?>"
                                            >


                                            <button
                                                type="submit"
                                                class="btn btn-link text-danger p-0"
                                                onclick="return confirm('Xóa sản phẩm này khỏi giỏ hàng?')"
                                            >

                                                <i
                                                    class="bi bi-trash"
                                                ></i>

                                                Xóa

                                            </button>

                                        </form>

                                    </div>

                                </article>

                            <?php endforeach; ?>


                        </div>


                        <!-- CART ACTION -->

                        <div
                            class="d-flex justify-content-between gap-3 mt-4 flex-wrap"
                        >

                            <a
                                href="<?= BASE_URL ?>/?url=products"
                                class="btn btn-outline-dark"
                            >

                                <i
                                    class="bi bi-arrow-left me-2"
                                ></i>

                                Tiếp tục mua hàng

                            </a>


                            <form
                                action="<?= BASE_URL ?>/?url=cart/clear"
                                method="POST"
                            >

                                <input
                                    type="hidden"
                                    name="_token"
                                    value="<?= htmlspecialchars(
                                        $_SESSION[
                                            'csrf_token'
                                        ]
                                    ) ?>"
                                >

                                <button
                                    type="submit"
                                    class="btn btn-outline-danger"
                                    onclick="return confirm('Bạn muốn xóa toàn bộ giỏ hàng?')"
                                >

                                    Xóa toàn bộ giỏ

                                </button>

                            </form>

                        </div>

                    </div>


                    <!-- ==========================================
                         SUMMARY
                    =========================================== -->

                    <div class="col-lg-4">

                        <aside
                            class="cart-summary"
                        >

                            <h3>
                                Tóm tắt đơn hàng
                            </h3>


                            <div
                                class="cart-summary-row"
                            >

                                <span>
                                    Tạm tính
                                </span>

                                <strong>

                                    <?= number_format(
                                        $subtotal,
                                        0,
                                        ',',
                                        '.'
                                    ) ?>đ

                                </strong>

                            </div>


                            <div
                                class="cart-summary-row"
                            >

                                <span>
                                    Phí giao hàng
                                </span>

                                <span>
                                    Tính ở bước thanh toán
                                </span>

                            </div>


                            <hr>


                            <div
                                class="cart-summary-total"
                            >

                                <span>
                                    Tổng tạm tính
                                </span>

                                <strong>

                                    <?= number_format(
                                        $subtotal,
                                        0,
                                        ',',
                                        '.'
                                    ) ?>đ

                                </strong>

                            </div>


                            <a
                                href="<?= BASE_URL ?>/?url=checkout"
                                class="btn btn-primary btn-lg w-100 mt-4"
                            >

                                Tiến hành thanh toán

                                <i
                                    class="bi bi-arrow-right ms-2"
                                ></i>

                            </a>


                            <?php if (
                                !isset(
                                    $_SESSION['user']
                                )
                            ): ?>

                                <p
                                    class="small text-muted mt-3 mb-0 text-center"
                                >

                                    Bạn có thể chọn hàng trước.
                                    Hệ thống sẽ yêu cầu đăng nhập
                                    khi xác nhận đơn hàng.

                                </p>

                            <?php endif; ?>

                        </aside>

                    </div>

                </div>

            <?php endif; ?>

        </div>

    </section>

</main>


<?php

require BASE_PATH
    . '/app/views/layouts/footer.php';

?>