<?php

$pageTitle =
    'Khuyến mãi - PET SPA';

$pageStyles = [
    'owner.css'
];

$activeOwnerPage =
    'promotions';


$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="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">

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

                    <div>

                        <h1>
                            Khuyến mãi
                        </h1>

                        <p>
                            Quản lý mã giảm giá của cửa hàng.
                        </p>

                    </div>


                    <a
                        href="<?= BASE_URL ?>/?url=owner/promotion/create"
                        class="btn btn-primary"
                    >

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

                        Tạo mã giảm giá

                    </a>

                </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="owner-card mb-4">

                    <form
                        method="GET"
                        class="row g-3"
                    >

                        <input
                            type="hidden"
                            name="url"
                            value="owner/promotions"
                        >

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

                            <input
                                type="text"
                                name="search"
                                class="form-control text-uppercase"
                                placeholder="Tìm mã khuyến mãi..."
                                value="<?= htmlspecialchars(
                                    $search
                                ) ?>"
                            >

                        </div>

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

                            <button
                                class="btn btn-dark w-100"
                                type="submit"
                            >
                                Tìm kiếm
                            </button>

                        </div>

                    </form>

                </section>


                <section class="owner-card">

                    <div class="table-responsive">

                        <table
                            class="table align-middle owner-table"
                        >

                            <thead>

                                <tr>

                                    <th>Mã</th>
                                    <th>Giảm</th>
                                    <th>Đơn tối thiểu</th>
                                    <th>Lượt dùng</th>
                                    <th>Thời gian</th>
                                    <th>Trạng thái</th>
                                    <th></th>

                                </tr>

                            </thead>


                            <tbody>

                                <?php foreach (
                                    $promotions
                                    as $promotion
                                ): ?>


                                    <?php

                                    $now =
                                        new DateTime();

                                    $start =
                                        new DateTime(
                                            $promotion[
                                                'start_at'
                                            ]
                                        );

                                    $end =
                                        new DateTime(
                                            $promotion[
                                                'end_at'
                                            ]
                                        );


                                    if (
                                        !$promotion[
                                            'is_active'
                                        ]
                                    ) {

                                        $displayStatus =
                                            'Tạm tắt';

                                    } elseif (
                                        $now < $start
                                    ) {

                                        $displayStatus =
                                            'Sắp diễn ra';

                                    } elseif (
                                        $now > $end
                                    ) {

                                        $displayStatus =
                                            'Đã hết hạn';

                                    } else {

                                        $displayStatus =
                                            'Đang áp dụng';
                                    }

                                    ?>


                                    <tr>

                                        <td>

                                            <span
                                                class="promotion-code"
                                            >

                                                <?= htmlspecialchars(
                                                    $promotion['code']
                                                ) ?>

                                            </span>

                                        </td>


                                        <td>

                                            <strong>

                                                <?php if (
                                                    $promotion[
                                                        'discount_type'
                                                    ] === 'percent'
                                                ): ?>

                                                    <?= number_format(
                                                        (float)
                                                        $promotion[
                                                            'discount_value'
                                                        ],
                                                        0,
                                                        ',',
                                                        '.'
                                                    ) ?>%

                                                <?php else: ?>

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

                                                <?php endif; ?>

                                            </strong>

                                        </td>


                                        <td>

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

                                        </td>


                                        <td>

                                            <?= (int)
                                                $promotion[
                                                    'used_count'
                                                ]
                                            ?>

                                            /

                                            <?= $promotion[
                                                'usage_limit'
                                            ] !== null
                                                ? (int)
                                                    $promotion[
                                                        'usage_limit'
                                                    ]
                                                : '∞'
                                            ?>

                                        </td>


                                        <td>

                                            <small>

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

                                                <br>

                                                →

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

                                            </small>

                                        </td>


                                        <td>

                                            <span
                                                class="promotion-status"
                                            >

                                                <?= htmlspecialchars(
                                                    $displayStatus
                                                ) ?>

                                            </span>

                                        </td>


                                        <td>

                                            <div class="dropdown">

                                                <button
                                                    class="btn btn-sm btn-light"
                                                    data-bs-toggle="dropdown"
                                                >
                                                    ⋮
                                                </button>


                                                <ul
                                                    class="dropdown-menu dropdown-menu-end"
                                                >

                                                    <li>

                                                        <a
                                                            class="dropdown-item"
                                                            href="<?= BASE_URL ?>/?url=owner/promotion/edit&id=<?= (int)
                                                                $promotion['id']
                                                            ?>"
                                                        >
                                                            Chỉnh sửa
                                                        </a>

                                                    </li>


                                                    <li>

                                                        <form
                                                            method="POST"
                                                            action="<?= BASE_URL ?>/?url=owner/promotion/toggle"
                                                        >

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

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


                                                            <button
                                                                type="submit"
                                                                class="dropdown-item"
                                                            >

                                                                <?= (int)
                                                                    $promotion[
                                                                        'is_active'
                                                                    ] === 1
                                                                    ? 'Tạm tắt'
                                                                    : 'Bật lại'
                                                                ?>

                                                            </button>

                                                        </form>

                                                    </li>

                                                </ul>

                                            </div>

                                        </td>

                                    </tr>

                                <?php endforeach; ?>


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

                                    <tr>

                                        <td
                                            colspan="7"
                                            class="text-center text-muted py-5"
                                        >
                                            Chưa có mã khuyến mãi.
                                        </td>

                                    </tr>

                                <?php endif; ?>

                            </tbody>

                        </table>

                    </div>

                </section>

            </div>

        </div>

    </div>

</main>


<?php

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

?>