@extends('layouts.app')

@section('title', \App\Helpers\TranslationHelper::transFromDatabase('Edit Payment', __('Edit Payment')))

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">{{ \App\Helpers\TranslationHelper::transFromDatabase('Edit Payment', __('Edit Payment')) }} #{{ $payment->payment_number }}</h3>
                    <div class="card-tools">
                        <a href="{{ route('customer-payments.index') }}" class="btn btn-sm btn-secondary">
                            <i class="fas fa-arrow-right"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Return to Payments', __('Return to Payments')) }}
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <form action="{{ route('customer-payments.update', $payment) }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        @method('PUT')
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="customer_id">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customer Name', __('Customer Name')) }} <span class="text-danger">*</span></label>
                                    <select name="customer_id" id="customer_id" class="form-control @error('customer_id') is-invalid @enderror" required>
                                        <option value="">-- {{ \App\Helpers\TranslationHelper::transFromDatabase('Choose Customer', __('Choose Customer')) }} --</option>
                                        @foreach($customers as $customer)
                                            <option value="{{ $customer->id }}" {{ old('customer_id', $payment->customer_id) == $customer->id ? 'selected' : '' }}>
                                                {{ $customer->name }}
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('customer_id')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="customer_invoice_id">{{ \App\Helpers\TranslationHelper::transFromDatabase('Invoice Number', __('Invoice Number')) }} <span class="text-danger">*</span></label>
                                    <select name="customer_invoice_id" id="customer_invoice_id" class="form-control @error('customer_invoice_id') is-invalid @enderror" required>
                                        <option value="">-- {{ \App\Helpers\TranslationHelper::transFromDatabase('Choose Invoice', __('Choose Invoice')) }} --</option>
                                        @foreach($invoices as $invoice)
                                            <option value="{{ $invoice->id }}" {{ old('customer_invoice_id', $payment->customer_invoice_id) == $invoice->id ? 'selected' : '' }}>
                                                {{ $invoice->invoice_number }} - {{ number_format($invoice->remaining_amount + ($payment->customer_invoice_id == $invoice->id ? $payment->amount : 0), 2) }} ({{ \App\Helpers\TranslationHelper::transFromDatabase('Remaining', __('Remaining')) }})
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('customer_invoice_id')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="payment_number">{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Number', __('Payment Number')) }} <span class="text-danger">*</span></label>
                                    <input type="text" name="payment_number" id="payment_number" class="form-control @error('payment_number') is-invalid @enderror" value="{{ old('payment_number', $payment->payment_number) }}" required>
                                    @error('payment_number')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="payment_date">{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Date', __('Payment Date')) }} <span class="text-danger">*</span></label>
                                    <input type="date" name="payment_date" id="payment_date" class="form-control @error('payment_date') is-invalid @enderror" value="{{ old('payment_date', $payment->payment_date->format('Y-m-d')) }}" required>
                                    @error('payment_date')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="amount">{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Amount', __('Payment Amount')) }} <span class="text-danger">*</span></label>
                                    <input type="number" step="0.01" name="amount" id="amount" class="form-control @error('amount') is-invalid @enderror" value="{{ old('amount', $payment->amount) }}" required>
                                    @error('amount')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="payment_method">{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Method', __('Payment Method')) }} <span class="text-danger">*</span></label>
                                    <select name="payment_method" id="payment_method" class="form-control @error('payment_method') is-invalid @enderror" required>
                                        <option value="cash" {{ old('payment_method', $payment->payment_method) == 'cash' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Cash', __('Cash')) }}</option>
                                        <option value="bank_transfer" {{ old('payment_method', $payment->payment_method) == 'bank_transfer' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Bank Transfer', __('Bank Transfer')) }}</option>
                                        <option value="check" {{ old('payment_method', $payment->payment_method) == 'check' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Check', __('Check')) }}</option>
                                        <option value="credit_card" {{ old('payment_method', $payment->payment_method) == 'credit_card' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Credit Card', __('Credit Card')) }}</option>
                                        <option value="other" {{ old('payment_method', $payment->payment_method) == 'other' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Other', __('Other')) }}</option>
                                    </select>
                                    @error('payment_method')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="reference_number">{{ \App\Helpers\TranslationHelper::transFromDatabase('Reference Number', __('Reference Number')) }}</label>
                                    <input type="text" name="reference_number" id="reference_number" class="form-control @error('reference_number') is-invalid @enderror" value="{{ old('reference_number', $payment->reference_number) }}">
                                    @error('reference_number')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="attachment">{{ \App\Helpers\TranslationHelper::transFromDatabase('Attachment', __('Attachment')) }}</label>
                                    <input type="file" name="attachment" id="attachment" class="form-control-file @error('attachment') is-invalid @enderror">
                                    @error('attachment')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                    
                                    @if($payment->attachment)
                                    <div class="mt-2">
                                        <a href="{{ asset('storage/' . $payment->attachment) }}" target="_blank" class="btn btn-sm btn-info">
                                            <i class="fas fa-file"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('View Current File', __('View Current File')) }}
                                        </a>
                                    </div>
                                    @endif
                                </div>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label for="notes">{{ \App\Helpers\TranslationHelper::transFromDatabase('Notes', __('Notes')) }}</label>
                            <textarea name="notes" id="notes" rows="3" class="form-control @error('notes') is-invalid @enderror">{{ old('notes', $payment->notes) }}</textarea>
                            @error('notes')
                                <span class="invalid-feedback">{{ $message }}</span>
                            @enderror
                        </div>
                        
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Save Changes', __('Save Changes')) }}
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
    $(document).ready(function() {
        $('#customer_id').on('change', function() {
            const customerId = $(this).val();
            const currentInvoiceId = {{ $payment->customer_invoice_id ?? 'null' }};
            const currentAmount = {{ $payment->amount ?? 0 }};
            
            if (customerId) {
                $.ajax({
                    url: `{{ url('/api/customers') }}/${customerId}/invoices`,
                    type: 'GET',
                    dataType: 'json',
                    success: function(data) {
                        let options = '<option value="">-- {{ \App\Helpers\TranslationHelper::transFromDatabase('Choose Invoice', __('Choose Invoice')) }} --</option>';
                        
                        $.each(data, function(key, invoice) {
                            const remainingAmount = invoice.id == currentInvoiceId 
                                ? invoice.remaining_amount + currentAmount 
                                : invoice.remaining_amount;
                                
                            options += `<option value="${invoice.id}">${invoice.invoice_number} - ${remainingAmount.toFixed(2)} ({{ \App\Helpers\TranslationHelper::transFromDatabase('Remaining', __('Remaining')) }})</option>`;
                        });
                        
                        $('#customer_invoice_id').html(options);
                    }
                });
            } else {
                $('#customer_invoice_id').html('<option value="">-- {{ \App\Helpers\TranslationHelper::transFromDatabase('Choose Invoice', __('Choose Invoice')) }} --</option>');
            }
        });
    });
</script>
@endpush
@endsection
