@extends('layouts.master')
@section('title', 'Apply Supplier Credit - Quanta')
@section('content')

<div class="container-fluid py-4" style="font-family: 'Outfit', 'Inter', sans-serif;">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4 class="fw-bold mb-0 text-dark">
            <i class="fas fa-file-invoice-dollar me-2 text-info"></i> Apply Supplier Credit
        </h4>
        <a href="{{ route('supplier.documents.pdf', $creditNote->id) }}" class="btn btn-outline-secondary px-4 py-2 rounded-3 border-2">
            <i class="fas fa-arrow-left me-2"></i> Back to Document
        </a>
    </div>

    @if($errors->any())
        <div class="alert alert-danger border-0 shadow-sm rounded-3 mb-4 py-3">
            <ul class="mb-0">
                @foreach($errors->all() as $error)
                    <li><i class="fas fa-exclamation-triangle me-2"></i> {{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <div class="row">
        <!-- Document Information Card -->
        <div class="col-lg-5 mb-4 mb-lg-0">
            <div class="card border-0 shadow-sm rounded-4 overflow-hidden h-100">
                <div class="card-header bg-info text-white py-3 border-0" style="background: linear-gradient(135deg, #17a2b8, #117a8b);">
                    <h5 class="mb-0 fw-bold"><i class="fas fa-info-circle me-2"></i> Credit Note Details</h5>
                </div>
                <div class="card-body p-4 bg-light">
                    <div class="mb-4 text-center">
                        <span class="text-muted text-uppercase tracking-wider small fw-bold">Supplier Credit Value</span>
                        <h2 class="fw-bold text-info mt-1 mb-0">{{ number_format($creditNote->inclusive_total, 2) }}</h2>
                    </div>

                    <hr class="my-4" style="border-top: 1px dashed #ced4da;">

                    <div class="mb-3 d-flex justify-content-between">
                        <span class="text-muted">Document Number:</span>
                        <strong class="text-dark">{{ $creditNote->document_number }}</strong>
                    </div>
                    <div class="mb-3 d-flex justify-content-between">
                        <span class="text-muted">Supplier:</span>
                        <strong class="text-dark">{{ $creditNote->supplier->account_name }}</strong>
                    </div>
                    <div class="mb-3 d-flex justify-content-between">
                        <span class="text-muted">Document Date:</span>
                        <strong class="text-dark">{{ $creditNote->document_date ? $creditNote->document_date->format('Y-m-d') : '-' }}</strong>
                    </div>
                    <div class="mb-3 d-flex justify-content-between">
                        <span class="text-muted">Settlement Term:</span>
                        <strong class="text-dark">{{ $creditNote->settlement_term ?? '-' }}</strong>
                    </div>
                    <div class="mb-0 d-flex justify-content-between">
                        <span class="text-muted">Created By:</span>
                        <strong class="text-dark">{{ $creditNote->addedByUser->name ?? '-' }}</strong>
                    </div>
                </div>
            </div>
        </div>

        <!-- Application Form Card -->
        <div class="col-lg-7">
            <div class="card border-0 shadow-sm rounded-4 overflow-hidden h-100">
                <div class="card-header bg-dark text-white py-3 border-0" style="background: linear-gradient(135deg, #1f2937, #111827);">
                    <h5 class="mb-0 fw-bold"><i class="fas fa-link me-2"></i> Allocate to Outstanding Supplier Invoice</h5>
                </div>
                <div class="card-body p-4">
                    <form action="{{ route('supplier.documents.apply-credit-note-submit', $creditNote->id) }}" method="POST" id="applyCreditForm">
                        @csrf
                        
                        <div class="mb-4">
                            <label class="form-label fw-semibold text-dark mb-2">Select Target Supplier Invoice *</label>
                            <select name="invoice_id" class="form-control select2 rounded-3" required id="invoiceSelect">
                                <option value="">Select Supplier Invoice to Apply to</option>
                                @foreach($invoices as $invoice)
                                    <option value="{{ $invoice->id }}" data-outstanding="{{ $invoice->inclusive_total }}">
                                        {{ $invoice->document_number }} &nbsp;|&nbsp; Date: {{ $invoice->document_date ? $invoice->document_date->format('Y-m-d') : '-' }} &nbsp;|&nbsp; Total: {{ number_format($invoice->inclusive_total, 2) }}
                                    </option>
                                @endforeach
                            </select>
                            <div class="form-text text-muted mt-2">
                                <i class="fas fa-info-circle me-1"></i> Only processed or partially paid supplier invoices are listed.
                            </div>
                        </div>

                        <div id="allocationPreview" class="p-3 mb-4 rounded-3 bg-light border border-dashed d-none">
                            <h6 class="fw-bold mb-2 text-dark"><i class="fas fa-calculator me-2 text-primary"></i> Allocation Breakdown</h6>
                            <div class="row text-center mt-3">
                                <div class="col-4">
                                    <div class="small text-muted">Supplier Credit</div>
                                    <div class="fw-bold text-info mt-1">{{ number_format($creditNote->inclusive_total, 2) }}</div>
                                </div>
                                <div class="col-4 border-start border-end">
                                    <div class="small text-muted">Invoice Balance</div>
                                    <div class="fw-bold text-dark mt-1" id="previewOutstanding">0.00</div>
                                </div>
                                <div class="col-4">
                                    <div class="small text-muted">New Balance</div>
                                    <div class="fw-bold text-success mt-1" id="previewNewOutstanding">0.00</div>
                                </div>
                            </div>
                        </div>

                        <div class="d-flex justify-content-end pt-3 border-top">
                            <button type="submit" class="btn btn-info text-white px-4 py-2 rounded-3 fw-semibold transition-all">
                                <i class="fas fa-check-double me-2"></i> Apply Allocation
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

@endsection

@section('styles')
<style>
    .transition-all {
        transition: all 0.25s ease-in-out;
    }
    .transition-all:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(23, 162, 184, 0.35);
    }
    .select2-container--default .select2-selection--single {
        border-radius: 8px !important;
        border: 1px solid #ced4da !important;
        height: 42px !important;
        padding: 6px 12px !important;
    }
    .select2-container--default .select2-selection--single .select2-selection__arrow {
        height: 40px !important;
    }
    .select2-container--default .select2-selection--single .select2-selection__rendered {
        line-height: 28px !important;
    }
</style>
@endsection

@section('scripts')
<script>
    $(document).ready(function() {
        $('.select2').select2({
            placeholder: "Select Supplier Invoice to Apply to",
            allowClear: true,
            width: '100%'
        });

        $('#invoiceSelect').on('change', function() {
            const selected = $(this).find(':selected');
            if (selected.val()) {
                const outstanding = parseFloat(selected.data('outstanding'));
                const creditVal = parseFloat('{{ $creditNote->inclusive_total }}');
                const newOutstanding = Math.max(0, outstanding - creditVal);

                $('#previewOutstanding').text(outstanding.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }));
                $('#previewNewOutstanding').text(newOutstanding.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }));
                $('#allocationPreview').removeClass('d-none');
            } else {
                $('#allocationPreview').addClass('d-none');
            }
        });
    });
</script>
@endsection
