@extends('layouts.app')

@section('content')
@php
    $statusClass = match($creditNote->status ?? 'open') {
        'applied'  => 'bg-success',
        'voided'   => 'bg-dark',
        default    => 'bg-info'
    };
    $currSymbol = $creditNote->invoice?->currency?->symbol ?? '';
@endphp

<div class="row">
    <div class="col-sm-12">
        <div class="page-title-box">
            <div class="row">
                <div class="col">
                    <h4 class="page-title">Credit Note #{{ $creditNote->id }}</h4>
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="{{ route('finance.credit-notes.index') }}">Credit Notes</a></li>
                        <li class="breadcrumb-item active">#{{ $creditNote->id }}</li>
                    </ol>
                </div>
                <div class="col-auto align-self-center d-flex gap-2">
                    @if($creditNote->invoice)
                    <a href="{{ route('finance.invoices.show', $creditNote->invoice_id) }}" class="btn btn-sm btn-outline-primary">
                        <i class="ti ti-file-invoice me-1"></i>View Invoice
                    </a>
                    @endif
                    <a href="{{ route('finance.credit-notes.index') }}" class="btn btn-sm btn-outline-secondary">
                        <i class="ti ti-arrow-left me-1"></i>Back
                    </a>
                    <button class="btn btn-sm btn-outline-danger btn-delete-cn" data-id="{{ $creditNote->id }}">
                        <i class="ti ti-trash me-1"></i>Delete
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-lg-8">
        <div class="card shadow-sm">
            <div class="card-body p-5">

                {{-- Header --}}
                <div class="row mb-4 align-items-start">
                    <div class="col-md-6">
                        <h4 class="fw-bold mb-1 text-warning">Credit Note</h4>
                        <p class="text-muted mb-0 small">Issued to:</p>
                        <h5 class="fw-bold">{{ $creditNote->client?->name ?? $creditNote->invoice?->client?->name ?? 'N/A' }}</h5>
                        @if($creditNote->invoice?->client?->email)
                            <p class="text-muted small mb-0"><i class="ti ti-mail me-1"></i>{{ $creditNote->invoice->client->email }}</p>
                        @endif
                        @if($creditNote->invoice?->client?->address)
                            <p class="text-muted small mb-0 mt-1">{{ $creditNote->invoice->client->address }}</p>
                        @endif
                    </div>
                    <div class="col-md-6 text-md-end">
                        <div class="display-6 fw-bold text-warning mb-1">
                            {{ $currSymbol }}{{ number_format($creditNote->amount ?? $creditNote->total_amount ?? 0, 2) }}
                        </div>
                        <span class="badge {{ $statusClass }} fs-6 px-3 py-2">{{ ucfirst($creditNote->status ?? 'open') }}</span>
                    </div>
                </div>

                <hr>

                {{-- Details --}}
                <div class="row">
                    <div class="col-md-6">
                        <table class="table table-borderless table-sm">
                            <tr>
                                <td class="text-muted small fw-bold text-uppercase" style="font-size:11px; width:140px;">Credit Note #</td>
                                <td class="fw-semibold">#{{ $creditNote->id }}</td>
                            </tr>
                            <tr>
                                <td class="text-muted small fw-bold text-uppercase" style="font-size:11px;">Date</td>
                                <td>{{ $creditNote->credit_note_date ? \Carbon\Carbon::parse($creditNote->credit_note_date)->format('d M Y') : ($creditNote->created_at?->format('d M Y')) }}</td>
                            </tr>
                            <tr>
                                <td class="text-muted small fw-bold text-uppercase" style="font-size:11px;">Status</td>
                                <td><span class="badge {{ $statusClass }}">{{ ucfirst($creditNote->status ?? 'open') }}</span></td>
                            </tr>
                            @if($creditNote->reason)
                            <tr>
                                <td class="text-muted small fw-bold text-uppercase" style="font-size:11px;">Reason</td>
                                <td>{{ $creditNote->reason }}</td>
                            </tr>
                            @endif
                            @if($creditNote->notes ?? null)
                            <tr>
                                <td class="text-muted small fw-bold text-uppercase" style="font-size:11px;">Notes</td>
                                <td class="text-muted small">{{ $creditNote->notes }}</td>
                            </tr>
                            @endif
                        </table>
                    </div>
                    <div class="col-md-6">
                        @if($creditNote->invoice)
                        <div class="p-3 bg-light rounded">
                            <p class="text-muted small fw-bold text-uppercase mb-2" style="font-size:11px;">Applied to Invoice</p>
                            <a href="{{ route('finance.invoices.show', $creditNote->invoice_id) }}" class="fw-bold text-primary d-block mb-1">
                                {{ $creditNote->invoice->invoice_number }}
                            </a>
                            <div class="row small">
                                <div class="col-6">
                                    <span class="text-muted">Invoice Total:</span><br>
                                    <strong>{{ $currSymbol }}{{ number_format($creditNote->invoice->total_amount, 2) }}</strong>
                                </div>
                                <div class="col-6">
                                    <span class="text-muted">Invoice Date:</span><br>
                                    <strong>{{ $creditNote->invoice->invoice_date?->format('d M Y') }}</strong>
                                </div>
                            </div>
                        </div>
                        @endif
                    </div>
                </div>

                {{-- Credit Summary --}}
                <div class="mt-4 p-3 bg-warning-subtle rounded border-start border-4 border-warning">
                    <div class="d-flex justify-content-between align-items-center">
                        <span class="fw-bold">Credit Amount</span>
                        <span class="fw-bold fs-4 text-warning">
                            {{ $currSymbol }}{{ number_format($creditNote->amount ?? $creditNote->total_amount ?? 0, 2) }}
                        </span>
                    </div>
                </div>

            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="card shadow-sm border-warning border-top border-3">
            <div class="card-body p-4 text-center">
                <div class="mb-3">
                    <div class="mx-auto bg-warning-subtle rounded-circle d-flex align-items-center justify-content-center" style="width:70px;height:70px;">
                        <i class="ti ti-receipt-refund font-32 text-warning"></i>
                    </div>
                </div>
                <h5 class="fw-bold mb-1">Credit Note</h5>
                <p class="text-muted small mb-3">Issued on {{ $creditNote->created_at?->format('d M Y') }}</p>
                <div class="fs-2 fw-bold text-warning mb-3">
                    {{ $currSymbol }}{{ number_format($creditNote->amount ?? $creditNote->total_amount ?? 0, 2) }}
                </div>

                @if($creditNote->invoice)
                <div class="d-grid gap-2">
                    <a href="{{ route('finance.invoices.show', $creditNote->invoice_id) }}" class="btn btn-warning btn-sm text-white">
                        <i class="ti ti-file-invoice me-1"></i>View Related Invoice
                    </a>
                    <a href="{{ route('finance.credit-notes.create') }}" class="btn btn-outline-warning btn-sm">
                        <i class="ti ti-plus me-1"></i>New Credit Note
                    </a>
                </div>
                @endif
            </div>
        </div>
    </div>
</div>

@endsection

@section('scripts')
<script>
$(document).ready(function () {
    $('.btn-delete-cn').on('click', function () {
        const id = $(this).data('id');
        Swal.fire({
            title: 'Delete this credit note?',
            text: 'This action cannot be undone.',
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#d33',
            cancelButtonColor: '#aaa',
            confirmButtonText: 'Yes, delete it!'
        }).then(result => {
            if (result.isConfirmed) {
                $.ajax({
                    url: "{{ url('finance/credit-notes') }}/" + id,
                    type: 'DELETE',
                    data: { _token: "{{ csrf_token() }}" },
                    success: function (res) {
                        Swal.fire('Deleted!', res.message, 'success')
                            .then(() => window.location.href = "{{ route('finance.credit-notes.index') }}");
                    },
                    error: function () {
                        Swal.fire('Error', 'Could not delete credit note.', 'error');
                    }
                });
            }
        });
    });
});
</script>
@endsection
