@extends('layouts.app')

@section('page_title', 'Top-Up Settings')

@section('content')
<div class="card-header border-0 bg-transparent py-2">
    <h5 class="mb-0 fw-bold text-dark" style="font-size: 1.25rem;font-weight:500">Top-Up Settings</h5>
</div>
    <div class="row justify-content-center align-items-center">
        <div class="col-12 col-md-8 col-lg-6">
            <div class="card-body p-3">
                <form id="topUpSettingsForm">
                    <!-- Current Discount Display -->
                    <div class="mb-2">
                        <label class="form-label fw-bold" style="font-size: 1.5rem;">
                            Current Discount: <span style="color: #E74610;">2%</span>
                        </label>
                    </div>

                    <!-- Top Up Discount Input -->
                    <div class="mb-2">
                        <label for="discount" class="form-label" style="font-size: 1.2rem;">Top Up Discount (%)</label>
                        <div class="d-flex align-items-center">
                            <input type="number" class="form-control me-2" id="discount" placeholder="Enter discount percentage" value="3" step="0.01" required style="width: 300px; font-size: 1.2rem; padding: 10px;">
                            <button type="submit" class="btn btn-primary" style="background-color: #E74610; border-color: #E74610; font-size: 1rem; padding: 10px 20px;">SUBMIT</button>
                        </div>
                    </div>

                    <!-- Card Image -->
                    <div class="mt-3 text-center">
                        <img src="{{ asset('img/card.png') }}" alt="Loyalty Card" style="max-width: 300px;">
                    </div>
                </form>
            </div>
        </div>
    </div>

    <style>
        .card {
            border-radius: 5px;
            border: 1px solid #dee2e6;
        }
        .form-label {
            font-size: 1.2rem;
        }
        .form-control {
            font-size: 1.2rem;
        }
        .btn-primary:hover {
            background-color: #e07b30;
            border-color: #e07b30;
        }
        /* Ensure the input and button are closely aligned */
        .d-flex.align-items-center {
            gap: 0.5rem; /* Small gap between input and button */
        }
    </style>

    <script>
        document.getElementById('topUpSettingsForm').addEventListener('submit', function(e) {
            e.preventDefault();

            const discount = document.getElementById('discount').value;

            if (!discount || discount < 0) {
                alert('Please enter a valid discount percentage.');
                return;
            }

            // Simulate updating the discount (frontend-only)
            sessionStorage.setItem('topUpDiscount', discount);

            alert('Discount updated successfully!');
            window.location.reload(); // Reload to reflect the new "Current Discount"
        });

        // On page load, update the current discount display if stored in sessionStorage
        window.addEventListener('load', function() {
            const storedDiscount = sessionStorage.getItem('topUpDiscount') || '2';
            document.querySelector('.form-label.fw-bold span').textContent = `${storedDiscount}%`;
        });
    </script>
@endsection