@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Create Installment Plan for Reservation #{{ $reservation->id }}</div>

                <div class="card-body">
                    <div class="alert alert-info">
                        <strong>Reservation Details:</strong><br>
                        Customer: {{ $reservation->customer->name }}<br>
                        Room: {{ $reservation->room->room_number }}<br>
                        Total Amount: UGX {{ number_format($reservation->total_amount, 2) }}<br>
                        Duration: {{ $reservation->check_in->format('M d, Y') }} to {{ $reservation->check_out->format('M d, Y') }}
                    </div>

                    <form method="POST" action="{{ route('installments.store', $reservation->id) }}">
                        @csrf

                        <div class="form-group">
                            <label for="down_payment">Down Payment (UGX)</label>
                            <input type="number" step="0.01" class="form-control" id="down_payment" 
                                   name="down_payment" value="{{ old('down_payment', 0) }}" required>
                        </div>

                        <div class="form-group">
                            <label for="installment_count">Number of Installments</label>
                            <input type="number" class="form-control" id="installment_count" 
                                   name="installment_count" value="{{ old('installment_count', 3) }}" required min="1">
                        </div>

                        <div class="form-group">
                            <label for="frequency">Payment Frequency</label>
                            <select class="form-control" id="frequency" name="frequency" required>
                                <option value="weekly">Weekly</option>
                                <option value="bi-weekly">Bi-Weekly</option>
                                <option value="monthly" selected>Monthly</option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="start_date">Start Date</label>
                            <input type="date" class="form-control" id="start_date" 
                                   name="start_date" value="{{ old('start_date', date('Y-m-d')) }}" required>
                        </div>

                        <div class="form-group">
                            <label for="terms">Terms & Conditions</label>
                            <textarea class="form-control" id="terms" name="terms" rows="3">{{ old('terms') }}</textarea>
                        </div>

                        <div class="form-group mb-0">
                            <button type="submit" class="btn btn-primary">Create Installment Plan</button>
                            <a href="{{ route('reservations.show', $reservation->id) }}" class="btn btn-secondary">Cancel</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
document.getElementById('installment_count').addEventListener('change', function() {
    calculateInstallmentAmount();
});

document.getElementById('down_payment').addEventListener('input', function() {
    calculateInstallmentAmount();
});

function calculateInstallmentAmount() {
    const totalAmount = {{ $reservation->total_amount }};
    const downPayment = parseFloat(document.getElementById('down_payment').value) || 0;
    const installmentCount = parseInt(document.getElementById('installment_count').value) || 1;
    
    const remainingAmount = totalAmount - downPayment;
    const installmentAmount = remainingAmount / installmentCount;
    
    // Show preview
    const preview = document.getElementById('installment-preview');
    if (!preview) {
        const div = document.createElement('div');
        div.id = 'installment-preview';
        div.className = 'alert alert-warning mt-3';
        document.querySelector('.card-body').appendChild(div);
    }
    
    document.getElementById('installment-preview').innerHTML = `
        <strong>Installment Preview:</strong><br>
        Down Payment: UGX ${downPayment.toFixed(2)}<br>
        Remaining Amount: UGX ${remainingAmount.toFixed(2)}<br>
        Each Installment: UGX ${installmentAmount.toFixed(2)}<br>
        Total Installments: ${installmentCount}
    `;
}

// Initial calculation
calculateInstallmentAmount();
</script>
@endsection
