<?php
$curSym = e($settings['currency_symbol'] ?? 'Rs.');

$odEnabled = ($settings['order_discount_enabled'] ?? '0') === '1';
$odType    = $settings['order_discount_type'] ?? 'percentage';

$foEnabled = ($settings['first_order_discount_enabled'] ?? '0') === '1';
$foType    = $settings['first_order_discount_type'] ?? 'percentage';
?>
<form method="POST" action="<?= url('discount-policies/update') ?>">
<?= csrf_field() ?>

<div class="page-header">
    <div>
        <h1>Discount Policies</h1>
        <p>Configure automatic discounts applied to customer orders.</p>
    </div>
    <button type="submit" class="btn btn-accent">
        <i class="bi bi-check-lg me-1"></i> Save Policies
    </button>
</div>

<div class="row g-4">
    <div class="col-12 col-xl-8">

        <!-- ── General Order Discount ─────────────────────────────────────── -->
        <div class="card mb-4">
            <div class="card-header">
                <i class="bi bi-percent text-accent"></i>
                <h6 class="card-title">Order Discount</h6>
            </div>
            <div class="card-body">
                <div class="form-check form-switch mb-3">
                    <input class="form-check-input" type="checkbox" role="switch"
                           id="odEnabled" name="order_discount_enabled" value="1"
                           <?= $odEnabled ? 'checked' : '' ?>>
                    <label class="form-check-label" for="odEnabled">
                        Apply a discount to <strong>every</strong> order
                    </label>
                </div>
                <div class="row g-3">
                    <div class="col-12 col-md-3">
                        <label class="form-label">Type</label>
                        <select name="order_discount_type" class="form-select">
                            <option value="percentage" <?= $odType === 'percentage' ? 'selected' : '' ?>>Percentage (%)</option>
                            <option value="fixed"      <?= $odType === 'fixed' ? 'selected' : '' ?>>Fixed Amount</option>
                        </select>
                    </div>
                    <div class="col-12 col-md-3">
                        <label class="form-label">Value</label>
                        <input type="number" name="order_discount_value" class="form-control"
                               value="<?= e($settings['order_discount_value'] ?? '0') ?>"
                               min="0" step="1" placeholder="e.g. 5">
                    </div>
                    <div class="col-12 col-md-3">
                        <label class="form-label">Min. Order</label>
                        <div class="input-group">
                            <span class="input-group-text"><?= $curSym ?></span>
                            <input type="number" name="order_discount_min_amount" class="form-control"
                                   value="<?= e($settings['order_discount_min_amount'] ?? '0') ?>"
                                   min="0" step="10" placeholder="0">
                        </div>
                    </div>
                    <div class="col-12 col-md-3">
                        <label class="form-label">Max. Discount <span class="text-secondary">(cap)</span></label>
                        <div class="input-group">
                            <span class="input-group-text"><?= $curSym ?></span>
                            <input type="number" name="order_discount_max_discount" class="form-control"
                                   value="<?= e($settings['order_discount_max_discount'] ?? '0') ?>"
                                   min="0" step="10" placeholder="0 = no cap">
                        </div>
                    </div>
                </div>
                <div class="form-text mt-2">
                    Applied to all orders whose subtotal meets the minimum. For percentage
                    type, "Max. Discount" caps the amount (0 = no cap).
                </div>
            </div>
        </div>

        <!-- ── First Order Discount ───────────────────────────────────────── -->
        <div class="card mb-4">
            <div class="card-header">
                <i class="bi bi-gift text-accent"></i>
                <h6 class="card-title">First Order Discount</h6>
            </div>
            <div class="card-body">
                <div class="form-check form-switch mb-3">
                    <input class="form-check-input" type="checkbox" role="switch"
                           id="foEnabled" name="first_order_discount_enabled" value="1"
                           <?= $foEnabled ? 'checked' : '' ?>>
                    <label class="form-check-label" for="foEnabled">
                        Give customers a discount on their <strong>first</strong> order
                    </label>
                </div>
                <div class="row g-3">
                    <div class="col-12 col-md-3">
                        <label class="form-label">Type</label>
                        <select name="first_order_discount_type" class="form-select">
                            <option value="percentage" <?= $foType === 'percentage' ? 'selected' : '' ?>>Percentage (%)</option>
                            <option value="fixed"      <?= $foType === 'fixed' ? 'selected' : '' ?>>Fixed Amount</option>
                        </select>
                    </div>
                    <div class="col-12 col-md-3">
                        <label class="form-label">Value</label>
                        <input type="number" name="first_order_discount_value" class="form-control"
                               value="<?= e($settings['first_order_discount_value'] ?? '0') ?>"
                               min="0" step="1" placeholder="e.g. 10">
                    </div>
                    <div class="col-12 col-md-3">
                        <label class="form-label">Min. Order</label>
                        <div class="input-group">
                            <span class="input-group-text"><?= $curSym ?></span>
                            <input type="number" name="first_order_min_amount" class="form-control"
                                   value="<?= e($settings['first_order_min_amount'] ?? '0') ?>"
                                   min="0" step="10" placeholder="0">
                        </div>
                    </div>
                    <div class="col-12 col-md-3">
                        <label class="form-label">Max. Discount <span class="text-secondary">(cap)</span></label>
                        <div class="input-group">
                            <span class="input-group-text"><?= $curSym ?></span>
                            <input type="number" name="first_order_max_discount" class="form-control"
                                   value="<?= e($settings['first_order_max_discount'] ?? '0') ?>"
                                   min="0" step="10" placeholder="0 = no cap">
                        </div>
                    </div>
                </div>
                <div class="form-text mt-2">
                    Applied automatically to a customer's very first order only.
                </div>
            </div>
        </div>

    </div>

    <div class="col-12 col-xl-4">
        <div class="card">
            <div class="card-header">
                <i class="bi bi-info-circle text-accent"></i>
                <h6 class="card-title">How it works</h6>
            </div>
            <div class="card-body">
                <ul class="mb-0 ps-3 small text-secondary">
                    <li class="mb-2">Discounts are applied to the order <strong>subtotal</strong>, before delivery fee and tax.</li>
                    <li class="mb-2">A percentage discount can be capped with "Max. Discount".</li>
                    <li class="mb-2">The first-order discount only applies to a customer's first successful order.</li>
                    <li>Both policies and any voucher can combine; total discount never exceeds the subtotal.</li>
                </ul>
            </div>
        </div>
    </div>
</div>
</form>
