@extends('layouts.app')

@section('page_title', 'Add Fuel')

@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 Fuel</h5>
    </div>
    <div class="row justify-content-center">
        <div class="card-body p-3">
            <form id="addFuelForm">
                <div class="mb-3">
                    <label for="fuelName" class="form-label">Fuel Name</label>
                    <input type="text" class="form-control" id="fuelName" placeholder="Enter fuel name" required>
                </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 Fuel</button>
                </div>
            </form>
        </div>
    </div>

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

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

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

            if (!fuelName) {
                alert('Please fill out the required field.');
                return;
            }

            const currentDate = new Date().toISOString().split('T')[0]; // e.g., "2025-04-16"
            const newFuel = {
                id: Date.now(),
                fuelName,
                dateCreated: currentDate,
                dateModified: currentDate
            };

            let fuels = JSON.parse(sessionStorage.getItem('fuels') || '[]');
            fuels.push(newFuel);
            sessionStorage.setItem('fuels', JSON.stringify(fuels));

            alert('Fuel added successfully!');
            window.location.href = '/fuels';
        });

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