@extends('layouts.app')

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

@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;">Add Top-Up</h5>
    </div>
    <div class="row justify-content-center">
        <div class="card-body p-3">
            <form id="addTopUpForm">
                <div class="mb-3">
                    <label for="freeCode" class="form-label">Free Code</label>
                    <input type="text" class="form-control" id="freeCode" placeholder="Enter free code" required>
                </div>
                <div class="mb-3">
                    <label for="name" class="form-label">Name</label>
                    <input type="text" class="form-control" id="name" placeholder="Enter name" required>
                </div>
                <div class="mb-3">
                    <label for="value" class="form-label">Value</label>
                    <input type="number" class="form-control" id="value" placeholder="Enter value" step="0.01" required>
                </div>
                <div class="mb-3">
                    <label for="type" class="form-label">Type</label>
                    <select class="form-select" id="type" required>
                        <option value="" disabled selected>Select type</option>
                        <option value="Prepaid">Prepaid</option>
                        <option value="Postpaid">Postpaid</option>
                        <option value="Bonus">Bonus</option>
                    </select>
                </div>
                <div class="d-flex justify-content-end mt-3">
                    <button type="button" class="btn btn-outline-secondary me-2" style="margin-right:5px">Cancel</button>
                    <button type="submit" class="btn btn-primary">Add Top-Up</button>
                </div>
            </form>
        </div>
    </div>

    <style>
        .card {
            border-radius: 5px;
            border: 1px solid #dee2e6;
        }
        .form-label {
            font-size: 0.95rem;
        }
        .form-control,
        .form-select {
            font-size: 0.9rem;
            width: 100%;
        }
    </style>

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

            const freeCode = document.getElementById('freeCode').value;
            const name = document.getElementById('name').value;
            const value = document.getElementById('value').value;
            const type = document.getElementById('type').value;

            if (!freeCode || !name || !value || !type) {
                alert('Please fill out all fields.');
                return;
            }

            // Simulate adding top-up (frontend-only)
            const newTopUp = {
                id: Date.now(),
                freeCode: freeCode,
                name: name,
                value: value,
                type: type
            };

            // Store in sessionStorage
            let topups = JSON.parse(sessionStorage.getItem('topups') || '[]');
            topups.push(newTopUp);
            sessionStorage.setItem('topups', JSON.stringify(topups));

            alert('Top-Up added successfully!');
            window.location.href = '/top-up';
        });

        // Cancel button click handler
        document.querySelector('.btn-outline-secondary').addEventListener('click', function() {
            window.location.href = '/top-up';
        });
    </script>
@endsection