@extends('layouts.app')

@section('title', 'عرض المرتجع')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            @if(session('success'))
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    {{ session('success') }}
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            @endif

            @if(session('error'))
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    {{ session('error') }}
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            @endif

            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">{{ $return->return_type === 'debit_note' ? 'إشعار مدين' : 'مرتجع مشتريات' }}: {{ $return->return_number }}</h3>
                    <div class="card-tools d-flex gap-2">
                        @if($return->status === 'draft')
                            <a href="{{ route('supplier-invoice-returns.edit', $return->id) }}" class="btn btn-sm btn-primary mr-1">
                                <i class="fas fa-edit"></i> تعديل
                            </a>
                            <form action="{{ route('supplier-invoice-returns.update-status', $return->id) }}" method="POST" class="mr-1">
                                @csrf
                                @method('PATCH')
                                <input type="hidden" name="status" value="approved">
                                <button type="submit" class="btn btn-sm btn-success">
                                    <i class="fas fa-check"></i> اعتماد المرتجع
                                </button>
                            </form>
                        @endif

                        @if($return->status !== 'cancelled')
                            <form action="{{ route('supplier-invoice-returns.update-status', $return->id) }}" method="POST" class="mr-1">
                                @csrf
                                @method('PATCH')
                                <input type="hidden" name="status" value="cancelled">
                                <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('هل أنت متأكد من إلغاء هذا المرتجع؟')">
                                    <i class="fas fa-times"></i> إلغاء
                                </button>
                            </form>
                        @endif

                        @if($return->status === 'approved' || $return->status === 'cancelled')
                            <form action="{{ route('supplier-invoice-returns.update-status', $return->id) }}" method="POST" class="mr-1">
                                @csrf
                                @method('PATCH')
                                <input type="hidden" name="status" value="draft">
                                <button type="submit" class="btn btn-sm btn-warning">
                                    <i class="fas fa-undo"></i> إعادة للمسودة
                                </button>
                            </form>
                        @endif

                        <a href="{{ route('supplier-invoice-returns.print', $return->id) }}" target="_blank" class="btn btn-sm btn-info mr-1">
                            <i class="fas fa-print"></i> طباعة
                        </a>

                        <a href="{{ route('supplier-invoice-returns.index') }}" class="btn btn-sm btn-secondary">
                            <i class="fas fa-arrow-right"></i> العودة
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <div class="row mb-4">
                        <div class="col-md-3">
                            <strong>رقم المرتجع:</strong>
                            <p>{{ $return->return_number }}</p>
                        </div>
                        <div class="col-md-3">
                            <strong>الرقم المرجعي:</strong>
                            <p>{{ $return->reference_number ?: '—' }}</p>
                        </div>
                        <div class="col-md-3">
                            <strong>المورد:</strong>
                            <p>{{ $return->supplier->name }}</p>
                        </div>
                        <div class="col-md-3">
                            <strong>تاريخ المرتجع:</strong>
                            <p>{{ $return->return_date->format('Y-m-d') }}</p>
                        </div>
                    </div>
                    <div class="row mb-4">
                        <div class="col-md-3">
                            <strong>النوع:</strong>
                            <p>
                                @if($return->return_type === 'debit_note')
                                    <span class="badge-blue">إشعار مدين</span>
                                @else
                                    <span class="badge-yellow">مرتجع مشتريات</span>
                                @endif
                            </p>
                        </div>
                        <div class="col-md-3">
                            <strong>الفاتورة الأصلية:</strong>
                            <p>{{ $return->invoice->invoice_number }}</p>
                        </div>
                        <div class="col-md-3">
                            <strong>الحالة:</strong>
                            <p>
                                @switch($return->status)
                                    @case('draft')
                                        <span class="badge-gray">مسودة</span>
                                        @break
                                    @case('sent')
                                        <span class="badge-blue">مرسلة</span>
                                        @break
                                    @case('approved')
                                        <span class="badge-green">معتمدة</span>
                                        @break
                                    @case('cancelled')
                                        <span class="badge-red">ملغاة</span>
                                        @break
                                @endswitch
                            </p>
                        </div>
                        @if($return->journalEntry)
                        <div class="col-md-3">
                            <strong>القيد المحاسبي:</strong>
                            <p>
                                <a href="{{ route('accounting.journal-entries.show', $return->journalEntry) }}" class="badge-blue text-decoration-none">
                                    <i class="fas fa-book"></i> {{ $return->journalEntry->reference_number }}
                                </a>
                            </p>
                        </div>
                        @endif
                    </div>

                    <div class="table-responsive mb-4">
                        <table class="table table-bordered table-hover">
                            <thead class="bg-light">
                                <tr>
                                    <th>#</th>
                                    <th>اسم الصنف</th>
                                    <th style="width: 100px;">الوحدة</th>
                                    <th>الوصف</th>
                                    <th style="width: 80px;">الكمية</th>
                                    <th style="width: 100px;">سعر الوحدة</th>
                                    <th style="width: 100px;">الخصم</th>
                                    <th style="width: 80px;">الضريبة</th>
                                    <th style="width: 100px;">الإجمالي</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($items as $index => $item)
                                <tr>
                                    <td>{{ $index + 1 }}</td>
                                    <td>{{ $item->invoiceItem->item->name ?? '-' }}</td>
                                    <td>{{ $item->invoiceItem->itemUnit && $item->invoiceItem->itemUnit->unit ? $item->invoiceItem->itemUnit->unit->name : '-' }}</td>
                                    <td>{{ $item->description }}</td>
                                    <td class="text-right">{{ number_format($item->quantity, 2) }}</td>
                                    <td class="text-right">{{ number_format($item->unit_price, 2) }}</td>
                                    <td class="text-right">{{ number_format($item->discount_amount, 2) }}</td>
                                    <td class="text-right">{{ number_format($item->tax_amount + ($item->tax_amount_2 ?? 0), 2) }}</td>
                                    <td class="text-right font-weight-bold text-warning">{{ number_format($item->total, 2) }}</td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="9" class="text-center text-muted">لا توجد بنود</td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>

                    <div class="row mb-4">
                        <div class="col-md-8"></div>
                        <div class="col-md-4">
                            <div class="card bg-light">
                                <div class="card-body">
                                    <div class="d-flex justify-content-between mb-2">
                                        <span>المبلغ الفرعي:</span>
                                        <strong>{{ number_format($return->subtotal, 2) }}</strong>
                                    </div>
                                    <div class="d-flex justify-content-between mb-2">
                                        <span>إجمالي الخصم:</span>
                                        <strong>{{ number_format($return->discount_amount, 2) }}</strong>
                                    </div>
                                    <div class="d-flex justify-content-between mb-2">
                                        <span>إجمالي الضريبة:</span>
                                        <strong>{{ number_format($return->tax_amount, 2) }}</strong>
                                    </div>
                                    <hr>
                                    <div class="d-flex justify-content-between">
                                        <span class="font-weight-bold">الإجمالي:</span>
                                        <strong class="text-warning h5">{{ number_format($return->total_amount, 2) }}</strong>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    @if($return->notes)
                    <div class="row mb-4">
                        <div class="col-md-12">
                            <h6>الملاحظات:</h6>
                            <p>{{ $return->notes }}</p>
                        </div>
                    </div>
                    @endif

                    <div class="row">
                        <div class="col-md-12">
                            <a href="{{ route('supplier-invoice-returns.index') }}" class="btn btn-secondary">
                                <i class="fas fa-arrow-right"></i> العودة
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
