@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 === 'credit_note' ? 'إشعار دائن' : 'فاتورة مرتجعة' }}: {{ $return->return_number }}</h3>
                    <div class="card-tools d-flex gap-2">
                        @if($return->status === 'draft')
                            <form action="{{ route('customer-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('customer-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('customer-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
                        
                        {{-- ZATCA Submission Button --}}
                        <form action="{{ route('zatca.submission.return-invoice', $return->id) }}" method="POST" class="mr-1">
                            @csrf
                            <button type="submit" class="btn btn-sm btn-dark" onclick="return confirm('هل أنت متأكد من إرسال الإشعار لهيئة الزكاة والدخل؟')">
                                <i class="fas fa-paper-plane"></i> إرسال إلى ZATCA
                            </button>
                        </form>

                        <a href="{{ route('customer-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->customer->name }}</p>
                        </div>
                        <div class="col-md-3">
                            <strong>تاريخ المرتجع:</strong>
                            <p>{{ $return->return_date->format('Y-m-d') }}</p>
                        </div>
                        <div class="col-md-3">
                            <strong>النوع:</strong>
                            <p>
                                @if($return->return_type === 'credit_note')
                                    <span class="badge badge-info">إشعار دائن</span>
                                @else
                                    <span class="badge badge-warning">فاتورة مرتجعة</span>
                                @endif
                            </p>
                        </div>
                    </div>

                    <div class="row mb-4">
                        <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 badge-secondary">مسودة</span>
                                        @break
                                    @case('sent')
                                        <span class="badge badge-info">مرسلة</span>
                                        @break
                                    @case('approved')
                                        <span class="badge badge-success">معتمدة</span>
                                        @break
                                    @case('cancelled')
                                        <span class="badge badge-danger">ملغاة</span>
                                        @break
                                @endswitch
                            </p>
                        </div>
                        
                        <div class="col-md-3">
                            <strong>حالة الربط مع ZATCA:</strong>
                            <p>
                                @if($return->zatca_compliance_status == 'PASS' || $return->zatca_compliance_status == 'WARNING')
                                    <span class="badge badge-success">✅ تم الإرسال ({{ $return->zatca_compliance_status }})</span>
                                @elseif($return->zatca_compliance_status == 'REPORTED')
                                    <span class="badge badge-success">✅ تم الإبلاغ (REPORTED)</span>
                                @elseif($return->zatca_compliance_status == 'CLEARED')
                                    <span class="badge badge-success">✅ تم التخليص (CLEARED)</span>
                                @elseif($return->zatca_compliance_status)
                                    <span class="badge badge-danger">❌ {{ $return->zatca_compliance_status }}</span>
                                @else
                                    <span class="badge badge-secondary">⏳ لم يتم الإرسال بعد</span>
                                @endif
                            </p>
                        </div>
                    </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-primary">{{ 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-primary 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('customer-invoice-returns.index') }}" class="btn btn-secondary">
                                <i class="fas fa-arrow-right"></i> العودة
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
