@extends('layouts.master')
@section('title', 'Customer Processing')

@section('content')
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-2">
        <h5 class="mb-0">Customer: {{ $customer->customer_account }} ({{ $customer->customer_code }})</h5>
        <a href="{{ route('processing.customers.index') }}" class="btn btn-outline-primary btn-sm">Back</a>
    </div>

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger">{{ session('error') }}</div>
    @endif

    <div class="card shadow-sm border-0 mb-3">
        <div class="card-header bg-primary text-white py-3">
            <h6 class="mb-0">Documents</h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>#</th>
                            <th>Type</th>
                            <th>Reference</th>
                            <th>Date</th>
                            <th>Status</th>
                            <th width="260px">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($documents as $doc)
                            <tr>
                                <td>{{ ($documents->currentPage() - 1) * $documents->perPage() + $loop->iteration }}</td>
                                <td>{{ $doc->document_type }}</td>
                                <td>{{ $doc->document_number }}</td>
                                <td>{{ optional($doc->document_date)->format('Y-m-d') }}</td>
                                <td>{{ $doc->status }}</td>
                                <td>
                                    <a href="{{ route('documents.pdf', $doc) }}" class="btn btn-secondary btn-sm">View</a>

                                    @if($doc->type === 'quotation' && $doc->status !== 'converted')
                                        <form method="POST" action="{{ route('processing.documents.convert', [$doc, 'sales_order']) }}" style="display:inline;">
                                            @csrf
                                            <button class="btn btn-outline-primary btn-sm" type="submit">Link → Sales Order</button>
                                        </form>
                                        <form method="POST" action="{{ route('processing.documents.convert', [$doc, 'invoice']) }}" style="display:inline;">
                                            @csrf
                                            <button class="btn btn-outline-primary btn-sm" type="submit">Link → Invoice</button>
                                        </form>
                                    @elseif($doc->type === 'sales_order')
                                        <form method="POST" action="{{ route('processing.documents.convert', [$doc, 'invoice']) }}" style="display:inline;">
                                            @csrf
                                            <button class="btn btn-outline-primary btn-sm" type="submit">Link → Invoice</button>
                                        </form>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="6">No documents found.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    {{ $documents->links() }}

    <div class="card shadow-sm border-0 mt-4">
        <div class="card-header bg-primary text-white py-3">
            <h6 class="mb-0">Linked Documents</h6>
        </div>
        <div class="card-body">
            @if($links->isEmpty())
                <div class="text-muted">No linked documents found.</div>
            @else
                <div class="table-responsive">
                    <table class="table table-bordered table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>Source</th>
                                <th>Target</th>
                                <th>Link Type</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($links as $l)
                                <tr>
                                    <td>{{ $l->source_type }} #{{ $l->source_id }}</td>
                                    <td>{{ $l->target_type }} #{{ $l->target_id }}</td>
                                    <td>{{ $l->link_type }}</td>
                                    <td>{{ $l->status }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection
