<?php

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

$pageStyles = [
    'owner.css'
];

$activeOwnerPage =
    'orders';


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

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


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


$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'
];


$nextStatus = match (
    $order['status']
) {

    'pending' =>
        'confirmed',

    'confirmed' =>
        'packing',

    'packing' =>
        'shipping',

    'shipping' =>
        'completed',

    default =>
        null
};


$nextStatusLabel =
    $nextStatus !== null

        ? (
            $statusNames[
                $nextStatus
            ]
            ?? $nextStatus
        )

        : null;


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

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

?>

<main class="owner-area">

    <div class="container-fluid">

        <div class="row g-4">

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

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

            </div>


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

                <!-- HEADER -->

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

                    <div>

                        <a
                            href="<?= BASE_URL ?>/?url=owner/orders"
                            class="small text-decoration-none"
                        >
                            ← Danh sách đơn hàng
                        </a>


                        <h1 class="mt-2">

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

                        </h1>


                        <p>

                            Đặt lúc

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

                        </p>

                    </div>


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

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

                    </span>

                </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; ?>


                <div class="row g-4">

                    <!-- LEFT -->

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

                        <!-- PRODUCTS -->

                        <section class="owner-card">

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


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

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

                                    <div>

                                        <strong>

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

                                        </strong>


                                        <small>

                                            SKU:

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

                                        </small>


                                        <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="owner-money-row"
                            >

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

                                <span>

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

                                </span>

                            </div>


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

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

                                <span>

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

                                </span>

                            </div>


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

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

                                <span>

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

                                </span>

                            </div>


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

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

                                <strong>

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

                                </strong>

                            </div>

                        </section>


                        <!-- TIMELINE -->

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

                            <h2>
                                Lịch sử đơn hàng
                            </h2>


                            <div class="owner-order-timeline">

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

                                    <div
                                        class="owner-timeline-item"
                                    >

                                        <span
                                            class="owner-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[
                                                        'changed_by_name'
                                                    ]
                                                )
                                            ): ?>

                                                <small>

                                                    Bởi:

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

                                                </small>

                                            <?php endif; ?>


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

                                                <p>

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

                                                </p>

                                            <?php endif; ?>

                                        </div>

                                    </div>

                                <?php endforeach; ?>

                            </div>

                        </section>

                    </div>


                    <!-- RIGHT -->

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

                        <!-- CUSTOMER -->

                        <section class="owner-card">

                            <h2>
                                Khách hàng
                            </h2>


                            <p class="mb-2">

                                <strong>

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

                                </strong>

                            </p>


                            <p class="mb-2">

                                <i
                                    class="bi bi-telephone me-2"
                                ></i>

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

                            </p>


                            <?php if (
                                !empty(
                                    $order[
                                        'customer_email'
                                    ]
                                )
                            ): ?>

                                <p class="mb-0">

                                    <i
                                        class="bi bi-envelope me-2"
                                    ></i>

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

                                </p>

                            <?php endif; ?>

                        </section>


                        <!-- SHIPPING -->

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

                            <h2>
                                Giao hàng
                            </h2>


                            <p>

                                <strong>

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

                                </strong>

                            </p>


                            <p class="mb-0">

                                <i
                                    class="bi bi-geo-alt me-2"
                                ></i>

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

                            </p>

                        </section>


                        <!-- PAYMENT -->

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

                            <h2>
                                Thanh toán
                            </h2>


                            <div
                                class="owner-info-row"
                            >

                                <span>
                                    Phương thức
                                </span>

                                <strong>

                                    <?= $order[
                                        'payment_method'
                                    ] === 'cod'

                                        ? 'COD'

                                        : 'Chuyển khoản'
                                    ?>

                                </strong>

                            </div>


                            <div
                                class="owner-info-row"
                            >

                                <span>
                                    Trạng thái
                                </span>


                                <?php if (
                                    $order[
                                        'payment_status'
                                    ] === 'paid'
                                ): ?>

                                    <strong
                                        class="text-success"
                                    >
                                        Đã thanh toán
                                    </strong>

                                <?php else: ?>

                                    <strong
                                        class="text-warning"
                                    >
                                        Chưa thanh toán
                                    </strong>

                                <?php endif; ?>

                            </div>


                            <?php if (
                                $order[
                                    'payment_method'
                                ] === 'bank_transfer'

                                && $order[
                                    'payment_status'
                                ] !== 'paid'

                                && $order['status']
                                !== 'cancelled'
                            ): ?>

                                <form
                                    method="POST"
                                    action="<?= BASE_URL ?>/?url=owner/order/paid"
                                    class="mt-3"
                                    onsubmit="return confirm('Xác nhận đã nhận tiền chuyển khoản?')"
                                >

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


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


                                    <button
                                        type="submit"
                                        class="btn btn-success w-100"
                                    >

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

                                        Xác nhận đã thanh toán

                                    </button>

                                </form>

                            <?php endif; ?>

                        </section>


                        <!-- UPDATE STATUS -->

                        <?php if (
                            $nextStatus !== null
                        ): ?>

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

                                <h2>
                                    Xử lý đơn hàng
                                </h2>


                                <form
                                    method="POST"
                                    action="<?= BASE_URL ?>/?url=owner/order/status"
                                >

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


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


                                    <input
                                        type="hidden"
                                        name="status"
                                        value="<?= htmlspecialchars(
                                            $nextStatus
                                        ) ?>"
                                    >


                                    <div class="mb-3">

                                        <label
                                            class="form-label"
                                        >
                                            Ghi chú
                                        </label>

                                        <textarea
                                            name="note"
                                            class="form-control"
                                            rows="3"
                                            placeholder="Ghi chú xử lý đơn..."
                                        ></textarea>

                                    </div>


                                    <button
                                        type="submit"
                                        class="btn btn-primary w-100"
                                    >

                                        Chuyển sang:

                                        <?= htmlspecialchars(
                                            $nextStatusLabel
                                        ) ?>

                                    </button>

                                </form>

                            </section>

                        <?php endif; ?>


                        <!-- CANCEL -->

                        <?php if (
                            in_array(
                                $order['status'],
                                [
                                    'pending',
                                    'confirmed',
                                    'packing'
                                ],
                                true
                            )

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

                            <section
                                class="owner-card mt-4 border-danger"
                            >

                                <h2 class="text-danger">
                                    Hủy đơn
                                </h2>


                                <form
                                    method="POST"
                                    action="<?= BASE_URL ?>/?url=owner/order/status"
                                    onsubmit="return confirm('Bạn chắc chắn muốn hủy đơn hàng này? Tồn kho sẽ được hoàn lại.')"
                                >

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


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


                                    <input
                                        type="hidden"
                                        name="status"
                                        value="cancelled"
                                    >


                                    <textarea
                                        name="note"
                                        class="form-control"
                                        rows="3"
                                        placeholder="Lý do hủy..."
                                        required
                                    ></textarea>


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

                                </form>

                            </section>

                        <?php endif; ?>

                    </div>

                </div>

            </div>

        </div>

    </div>

</main>


<?php

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

?>