<?php

$pageTitle =
    'Sửa 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>

                        <a
                            href="<?= BASE_URL ?>/?url=owner/promotions"
                            class="small text-decoration-none"
                        >
                            ← Khuyến mãi
                        </a>

                        <h1 class="mt-2">

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

                        </h1>

                    </div>

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


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

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

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


                    <div class="row g-4">

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

                            <section class="owner-card">

                                <h2>
                                    Thông tin khuyến mãi
                                </h2>


                                <div class="row g-3">

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

                                        <label class="form-label">
                                            Mã khuyến mãi
                                        </label>

                                        <input
                                            type="text"
                                            name="code"
                                            class="form-control text-uppercase"
                                            value="<?= htmlspecialchars(
                                                $promotion['code']
                                            ) ?>"
                                            required
                                        >

                                    </div>


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

                                        <label class="form-label">
                                            Loại giảm
                                        </label>

                                        <select
                                            name="discount_type"
                                            class="form-select"
                                        >

                                            <option
                                                value="percent"
                                                <?= $promotion[
                                                    'discount_type'
                                                ] === 'percent'
                                                    ? 'selected'
                                                    : ''
                                                ?>
                                            >
                                                Phần trăm (%)
                                            </option>

                                            <option
                                                value="fixed"
                                                <?= $promotion[
                                                    'discount_type'
                                                ] === 'fixed'
                                                    ? 'selected'
                                                    : ''
                                                ?>
                                            >
                                                Số tiền cố định
                                            </option>

                                        </select>

                                    </div>


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

                                        <label class="form-label">
                                            Giá trị giảm
                                        </label>

                                        <input
                                            type="number"
                                            name="discount_value"
                                            class="form-control"
                                            value="<?= htmlspecialchars(
                                                $promotion[
                                                    'discount_value'
                                                ]
                                            ) ?>"
                                            required
                                        >

                                    </div>


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

                                        <label class="form-label">
                                            Giảm tối đa
                                        </label>

                                        <input
                                            type="number"
                                            name="max_discount"
                                            class="form-control"
                                            value="<?= htmlspecialchars(
                                                $promotion[
                                                    'max_discount'
                                                ]
                                                ?? ''
                                            ) ?>"
                                        >

                                    </div>


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

                                        <label class="form-label">
                                            Đơn tối thiểu
                                        </label>

                                        <input
                                            type="number"
                                            name="min_order_value"
                                            class="form-control"
                                            value="<?= htmlspecialchars(
                                                $promotion[
                                                    'min_order_value'
                                                ]
                                            ) ?>"
                                        >

                                    </div>


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

                                        <label class="form-label">
                                            Giới hạn lượt dùng
                                        </label>

                                        <input
                                            type="number"
                                            name="usage_limit"
                                            class="form-control"
                                            min="<?= max(
                                                1,
                                                (int)
                                                $promotion[
                                                    'used_count'
                                                ]
                                            ) ?>"
                                            value="<?= htmlspecialchars(
                                                $promotion[
                                                    'usage_limit'
                                                ]
                                                ?? ''
                                            ) ?>"
                                        >

                                        <small class="text-muted">

                                            Đã dùng:

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

                                            lượt.

                                        </small>

                                    </div>

                                </div>

                            </section>


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

                                <h2>
                                    Thời gian áp dụng
                                </h2>


                                <div class="row g-3">

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

                                        <label class="form-label">
                                            Bắt đầu
                                        </label>

                                        <input
                                            type="datetime-local"
                                            name="start_at"
                                            class="form-control"
                                            value="<?= date(
                                                'Y-m-d\TH:i',
                                                strtotime(
                                                    $promotion[
                                                        'start_at'
                                                    ]
                                                )
                                            ) ?>"
                                            required
                                        >

                                    </div>


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

                                        <label class="form-label">
                                            Kết thúc
                                        </label>

                                        <input
                                            type="datetime-local"
                                            name="end_at"
                                            class="form-control"
                                            value="<?= date(
                                                'Y-m-d\TH:i',
                                                strtotime(
                                                    $promotion[
                                                        'end_at'
                                                    ]
                                                )
                                            ) ?>"
                                            required
                                        >

                                    </div>

                                </div>

                            </section>

                        </div>


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

                            <section class="owner-card">

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


                                <div class="form-check">

                                    <input
                                        class="form-check-input"
                                        type="checkbox"
                                        name="is_active"
                                        id="promotionActive"

                                        <?= (int)
                                            $promotion[
                                                'is_active'
                                            ] === 1
                                            ? 'checked'
                                            : ''
                                        ?>
                                    >

                                    <label
                                        class="form-check-label"
                                        for="promotionActive"
                                    >
                                        Kích hoạt mã
                                    </label>

                                </div>

                            </section>


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

                                <h2>
                                    Thống kê
                                </h2>

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

                                    <span>
                                        Đã sử dụng
                                    </span>

                                    <strong>

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

                                    </strong>

                                </div>

                            </section>


                            <button
                                type="submit"
                                class="btn btn-primary btn-lg w-100 mt-4"
                            >
                                Lưu thay đổi
                            </button>

                        </div>

                    </div>

                </form>

            </div>

        </div>

    </div>
<div class="col-12">

    <label class="form-label">
        Tên chương trình *
    </label>

    <input
        type="text"
        name="name"
        class="form-control"
        value="<?= htmlspecialchars(
            $promotion['name']
        ) ?>"
        required
    >

</div>

<div class="col-12">

    <label class="form-label">
        Mô tả
    </label>

    <textarea
        name="description"
        class="form-control"
        rows="3"
    ><?= htmlspecialchars(
        $promotion['description']
        ?? ''
    ) ?></textarea>

</div>
</main>


<?php

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

?>