<?php

$pageTitle =
    'Đơn '
    . $order['order_code']
    . ' - PET SPA';

$pageStyles = [
    'customer.css'
];

$activeCustomerPage =
    'orders';


$statusNames = [

    'pending' => 'Chờ xác nhận',
    'confirmed' => 'Đã xác nhận',
    'packing' => 'Đang đóng gói',
    'shipping' => 'Đang giao',
    'completed' => 'Hoàn thành',
    'cancelled' => 'Đã hủy'
];


$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 class="customer-area">

    <div class="container">

        <div class="row g-4">

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

                <?php require BASE_PATH
                    . '/app/views/layouts/customer-sidebar.php';
                ?>

            </div>


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

                <div class="customer-page-header">

                    <a
                        href="<?= BASE_URL ?>/?url=customer/orders"
                        class="small"
                    >
                        ← Quay lại đơn hàng
                    </a>

                    <h1 class="mt-2">

                        <?= htmlspecialchars(
                            $order['order_code']
                        ) ?>

                    </h1>

                </div>


                <?php if ($successMessage): ?>

                    <div class="alert alert-success">

                        <?= htmlspecialchars(
                            $successMessage
                        ) ?>

                    </div>

                <?php endif; ?>


                <?php if ($errorMessage): ?>

                    <div class="alert alert-danger">

                        <?= htmlspecialchars(
                            $errorMessage
                        ) ?>

                    </div>

                <?php endif; ?>


                <section class="customer-card">

                    <div
                        class="d-flex justify-content-between align-items-center flex-wrap gap-3"
                    >

                        <div>

                            <small class="text-muted">
                                Trạng thái
                            </small>

                            <div>

                                <span
                                    class="order-status status-<?= htmlspecialchars(
                                        $order['status']
                                    ) ?>"
                                >

                                    <?= htmlspecialchars(
                                        $statusNames[
                                            $order['status']
                                        ]
                                        ?? $order['status']
                                    ) ?>

                                </span>

                            </div>

                        </div>


                        <div>

                            <small class="text-muted">
                                Ngày đặt
                            </small>

                            <div>

                                <?= date(
                                    'd/m/Y H:i',
                                    strtotime(
                                        $order['created_at']
                                    )
                                ) ?>

                            </div>

                        </div>

                    </div>

                </section>


                <!-- PRODUCTS -->

                <section class="customer-card mt-4">

                    <h2>
                        Sản phẩm
                    </h2>


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

                        <div class="order-detail-item">

                            <div>

                                <strong>

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

                                </strong>

                                <small>

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

                                    ×

                                    <?= (int)
                                        $item['quantity']
                                    ?>

                                </small>

                            </div>


                            <strong>

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

                            </strong>

                        </div>

                    <?php endforeach; ?>


                    <hr>


                    <div class="order-money-row">

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

                        <span>

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

                        </span>

                    </div>


                    <div class="order-money-row">

                        <span>
                            Giảm giá
                        </span>

                        <span>

                            -<?= number_format(
                                (float)
                                $order['discount_amount'],
                                0,
                                ',',
                                '.'
                            ) ?>đ

                        </span>

                    </div>


                    <div class="order-money-row">

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

                        <span>

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

                        </span>

                    </div>


                    <div class="order-grand-total">

                        <span>
                            Tổng thanh toán
                        </span>

                        <strong>

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

                        </strong>

                    </div>

                </section>


                <!-- SHIPPING -->

                <section class="customer-card mt-4">

                    <h2>
                        Thông tin giao hàng
                    </h2>

                    <p>

                        <strong>

                            <?= htmlspecialchars(
                                $order['recipient_name']
                            ) ?>

                        </strong>

                    </p>

                    <p>

                        <?= htmlspecialchars(
                            $order['recipient_phone']
                        ) ?>

                    </p>

                    <p>

                        <?= htmlspecialchars(
                            $order['shipping_address']
                        ) ?>

                    </p>

                </section>


                <!-- STATUS LOG -->

                <section class="customer-card mt-4">

                    <h2>
                        Theo dõi đơn hàng
                    </h2>


                    <div class="order-timeline">

                        <?php foreach (
                            $statusLogs
                            as $log
                        ): ?>

                            <div class="timeline-item">

                                <span
                                    class="timeline-dot"
                                ></span>

                                <div>

                                    <strong>

                                        <?= htmlspecialchars(
                                            $statusNames[
                                                $log['new_status']
                                            ]
                                            ?? $log['new_status']
                                        ) ?>

                                    </strong>

                                    <small>

                                        <?= date(
                                            'd/m/Y H:i',
                                            strtotime(
                                                $log['created_at']
                                            )
                                        ) ?>

                                    </small>


                                    <?php if (
                                        !empty(
                                            $log['note']
                                        )
                                    ): ?>

                                        <p>

                                            <?= htmlspecialchars(
                                                $log['note']
                                            ) ?>

                                        </p>

                                    <?php endif; ?>

                                </div>

                            </div>

                        <?php endforeach; ?>

                    </div>

                </section>


                <!-- CANCEL -->

                <?php if (
                    $order['status']
                    === 'pending'

                    && $order['payment_status']
                    !== 'paid'
                ): ?>

                    <section
                        class="customer-card customer-danger-zone mt-4"
                    >

                        <h2>
                            Hủy đơn hàng
                        </h2>

                        <p class="text-muted">

                            Bạn chỉ có thể tự hủy khi đơn vẫn đang chờ xác nhận.

                        </p>


                        <form
                            method="POST"
                            action="<?= BASE_URL ?>/?url=customer/order/cancel"
                            onsubmit="return confirm('Bạn chắc chắn muốn hủy đơn hàng này?')"
                        >

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

                            <input
                                type="hidden"
                                name="order_id"
                                value="<?= (int)
                                    $order['id']
                                ?>"
                            >


                            <textarea
                                name="reason"
                                class="form-control"
                                rows="3"
                                placeholder="Nhập lý do hủy..."
                                required
                            ></textarea>


                            <button
                                type="submit"
                                class="btn btn-outline-danger mt-3"
                            >
                                Hủy đơn hàng
                            </button>

                        </form>

                    </section>

                <?php endif; ?>

            </div>

        </div>

    </div>

</main>


<?php

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

?>