@extends('layouts.app')

@section('page_title', 'Fuel Price Schedule')

@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">Fuel Price Schedule</h5>
</div>
    <div class="row justify-content-center">
        <div class="card-body p-3">
            <form id="fuelPriceScheduleForm">
                <!-- Import File Section -->
                <div class="mb-3">
                    <div class="custom-file" style="width: 400px;">
                        <input type="file" class="custom-file-input" id="fuelPriceFile" style="font-size: 0.9rem;">
                        <label class="custom-file-label" for="fuelPriceFile" style="font-size: 0.9rem; padding: 8px;">Choose File</label>
                    </div>
                </div>

                <!-- Date Input -->
                <div class="mb-3">
                    <label for="scheduleDate" class="form-label">Date</label>
                    <div class="input-group">
                        <input type="text" class="form-control" id="scheduleDate" placeholder="dd/mm/yyyy" required>
                        <div class="input-group-append">
                            <span class="input-group-text"><i class="fas fa-calendar-alt"></i></span>
                        </div>
                    </div>
                </div>

                <!-- Time Input -->
                <div class="mb-3">
                    <label for="scheduleTime" class="form-label">Time</label>
                    <div class="input-group">
                        <input type="text" class="form-control" id="scheduleTime" placeholder="--:-- --" required>
                        <div class="input-group-append">
                            <span class="input-group-text"><i class="fas fa-clock"></i></span>
                        </div>
                    </div>
                </div>

                <!-- Buttons -->
                <div class="d-flex justify-content-center gap-2 mt-4">
                    <button type="submit" class="btn btn-primary d-flex align-items-center" style="background-color: #E74610; border-color: #E74610; font-size: 0.9rem; padding: 8px 20px;">
                        SUBMIT <i class="fas fa-paper-plane ms-2"></i>
                    </button>
                    <button type="button" id="exportFuelPrices" class="btn btn-secondary d-flex align-items-center" style="font-size: 0.9rem; padding: 8px 20px;">
                        EXPORT FUEL PRICES <i class="fas fa-cloud-download-alt ms-2"></i>
                    </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%;
        }
        .custom-file {
            position: relative;
            display: inline-block;
            width: 100%;
            height: calc(2.25rem + 2px);
            margin-bottom: 0;
        }
        .custom-file-input {
            position: relative;
            z-index: 2;
            width: 100%;
            height: calc(2.25rem + 2px);
            margin: 0;
            opacity: 0;
        }
        .custom-file-label {
            position: absolute;
            top: 0;
            right: 0;
            left: 0;
            z-index: 1;
            height: calc(2.25rem + 2px);
            padding: 0.5rem 1rem;
            line-height: 1.5;
            color: #495057;
            background-color: #fff;
            border: 1px solid #ced4da;
            border-radius: 0.25rem;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .custom-file-label::after {
            content: "Choose File";
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            z-index: 3;
            padding: 0.5rem 1rem;
            line-height: 1.5;
            color: #495057;
            background-color: #e9ecef;
            border-left: 1px solid #ced4da;
            border-radius: 0 0.25rem 0.25rem 0;
        }
        .btn-primary:hover {
            background-color: #e07b30;
            border-color: #e07b30;
        }
        .btn-secondary {
            background-color: #6c757d;
            border-color: #6c757d;
        }
        .btn-secondary:hover {
            background-color: #5a6268;
            border-color: #5a6268;
        }
        .d-flex.justify-content-center {
            gap: 0.75rem; /* Space between buttons */
        }
        .input-group-text {
            background-color: #e9ecef;
            border: 1px solid #ced4da;
            border-radius: 0 0.25rem 0.25rem 0;
        }
    </style>

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

            const fileInput = document.getElementById('fuelPriceFile');
            const scheduleDate = document.getElementById('scheduleDate').value;
            const scheduleTime = document.getElementById('scheduleTime').value;

            // Validate file
            if (!fileInput.files.length) {
                alert('Please select a file to import.');
                return;
            }

            // Validate date format (dd/mm/yyyy)
            const datePattern = /^\d{2}\/\d{2}\/\d{4}$/;
            if (!datePattern.test(scheduleDate)) {
                alert('Please enter a valid date in the format dd/mm/yyyy.');
                return;
            }

            // Validate time format (hh:mm AM/PM)
            const timePattern = /^(0?[1-9]|1[0-2]):[0-5][0-9] (AM|PM)$/;
            if (!timePattern.test(scheduleTime)) {
                alert('Please enter a valid time in the format hh:mm AM/PM.');
                return;
            }

            // Simulate scheduling (frontend-only)
            const scheduleData = {
                file: fileInput.files[0]?.name,
                date: scheduleDate,
                time: scheduleTime
            };
            sessionStorage.setItem('fuelPriceSchedule', JSON.stringify(scheduleData));
            alert('Fuel price update scheduled successfully!');
            fileInput.value = ''; // Reset file input
            document.getElementById('scheduleDate').value = '';
            document.getElementById('scheduleTime').value = '';
        });

        document.getElementById('exportFuelPrices').addEventListener('click', function() {
            // Simulate export action (frontend-only)
            alert('Fuel prices exported successfully!');
        });

        // Update the file input label when a file is selected
        document.getElementById('fuelPriceFile').addEventListener('change', function(e) {
            const fileName = e.target.files.length > 0 ? e.target.files[0].name : 'No file chosen';
            document.querySelector('.custom-file-label').textContent = fileName;
        });

        // Load saved data if available
        const savedData = JSON.parse(sessionStorage.getItem('fuelPriceSchedule') || '{}');
        if (savedData.date) {
            document.getElementById('scheduleDate').value = savedData.date;
            document.getElementById('scheduleTime').value = savedData.time;
        }
    </script>
@endsection