﻿@extends('layouts.app')

@section('content')
<div class="container m-auto p-4 sm:p-6 lg:p-8">
    <div class="bg-white rounded-lg shadow-lg p-6">
        <div class="flex justify-between items-center mb-6">
            <h1 class="text-2l font-bold text-gray-900">عرض إذن استلام</h1>
            <div class="flex space-y-2 space-y-reverse">
                <a href="{{ route('inventory.receipts.index') }}" class="inline-flex items-center p-4 py-2 bg-gray-200 text-gray-700 rounded-md hover:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500">
                    <svg class="h-5 w-5 ml-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18" />
                    </svg>
                    العودة للقائمة
                </a>
                
                @can('inventory.receipts.print')
                <a href="{{ route('inventory.receipts.print', $document->id) }}" target="_blank" class="inline-flex items-center p-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                    <svg class="h-5 w-5 ml-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z" />
                    </svg>
                    طباعة
                </a>
                @endcan
            </div>
        </div>

        <!-- Document Status -->
        <div class="mb-6">
            <div class="inline-block p-3 py-1 rounded-full text-sm font-semibold 
                @if($document->status == 'draft') bg-yellow-100 text-yellow-800
                @elseif($document->status == 'approved') bg-green-100 text-green-800
                @elseif($document->status == 'cancelled') bg-red-100 text-red-800
                @endif">
                @if($document->status == 'draft') مسودة
                @elseif($document->status == 'approved') معتمد
                @elseif($document->status == 'cancelled') ملغي
                @endif
            </div>
        </div>

        <!-- Document Details -->
        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
            <div>
                <h2 class="text-lg font-semibold text-gray-900 mb-3">بيانات المستند</h2>
                <div class="bg-gray-50 p-4 rounded-md">
                    <div class="grid grid-cols-2 gap-4">
                        <div>
                            <p class="text-sm font-medium text-gray-500">رقم المستند</p>
                            <p class="mt-1 text-sm text-gray-900">{{ $document->document_number }}</p>
                        </div>
                        <div>
                            <p class="text-sm font-medium text-gray-500">تاريخ المستند</p>
                            <p class="mt-1 text-sm text-gray-900">{{ $document->document_date->format('Y-m-d') }}</p>
                        </div>
                        <div>
                            <p class="text-sm font-medium text-gray-500">المخزن المستلم</p>
                            <p class="mt-1 text-sm text-gray-900">{{ $document->toWarehouse->name ?? 'غير محدد' }}</p>
                        </div>
                        <div>
                            <p class="text-sm font-medium text-gray-500">منشئ المستند</p>
                            <p class="mt-1 text-sm text-gray-900">{{ $document->creator->name ?? 'غير محدد' }}</p>
                        </div>
                        @if($document->status == 'approved')
                        <div>
                            <p class="text-sm font-medium text-gray-500">معتمد بواسطة</p>
                            <p class="mt-1 text-sm text-gray-900">{{ $document->approver->name ?? 'غير محدد' }}</p>
                        </div>
                        <div>
                            <p class="text-sm font-medium text-gray-500">تاريخ الاعتماد</p>
                            <p class="mt-1 text-sm text-gray-900">{{ $document->approved_at ? $document->approved_at->format('Y-m-d H:i') : 'غير محدد' }}</p>
                        </div>
                        @endif
                    </div>
                </div>
            </div>
            
            <div>
                <h2 class="text-lg font-semibold text-gray-900 mb-3">ملاحظات</h2>
                <div class="bg-gray-50 p-4 rounded-md h-full">
                    <p class="text-sm text-gray-900">{{ $document->notes ?? 'لا توجد ملاحظات' }}</p>
                </div>
            </div>
        </div>

        <!-- Document Items -->
        <div class="mb-6">
            <h2 class="text-lg font-semibold text-gray-900 mb-3">الأصناف</h2>
            <div class="overflow--auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">الصنف</th>
                            <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">الوحدة</th>
                            <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">الكمية</th>
                            <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">التكلفة</th>
                            <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">الإجمالي</th>
                            <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">ملاحظات</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        @php $totalCost = 0; @endphp
                        @forelse($document->items as $item)
                            @php $totalCost += $item->total_cost; @endphp
                            <tr>
                                <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $item->item->name ?? 'غير محدد' }}</td>
                                <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $item->unit->name ?? 'غير محدد' }}</td>
                                <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ number_format($item->quantity, 2) }}</td>
                                <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ number_format($item->unit_cost, 2) }}</td>
                                <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ number_format($item->total_cost, 2) }}</td>
                                <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $item->notes ?? '-' }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="p-6 py-4 whitespace-nowrap text-sm text-gray-500 text-center">لا توجد أصناف</td>
                            </tr>
                        @endforelse
                        <tr class="bg-gray-50">
                            <td colspan="4" class="p-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 text-left">الإجمالي</td>
                            <td class="p-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">{{ number_format($totalCost, 2) }}</td>
                            <td></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Actions -->
        @if($document->status == 'draft')
        <div class="flex justify-end space-y-2 space-y-reverse">
            @can('inventory.receipts.edit')
            <a href="{{ route('inventory.receipts.edit', $document->id) }}" class="inline-flex items-center p-4 py-2 bg-primary-600 text-white rounded-md hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500">
                <svg class="h-5 w-5 ml-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
                </svg>
                تعديل
            </a>
            @endcan
            
            @can('inventory.receipts.approve')
            <form method="POST" action="{{ route('inventory.receipts.approve', $document->id) }}" style="display: inline;">
                @csrf
                @method('PATCH')
                <button type="submit" onclick="return confirm('هل أنت متأكد من اعتماد هذا المستند؟');" class="inline-flex items-center px-4 py-2 bg-green-600 text-white rounded-md hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500">
                    <svg class="h-5 w-5 ml-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                    </svg>
                    اعتماد
                </button>
            </form>
            @endcan
            
            @can('inventory.receipts.delete')
            <form method="POST" action="{{ route('inventory.receipts.destroy', $document->id) }}" style="display: inline;">
                @csrf
                @method('DELETE')
                <button type="submit" onclick="return confirm('هل أنت متأكد من حذف هذا المستند؟');" class="inline-flex items-center px-4 py-2 bg-red-600 text-white rounded-md hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500">
                    <svg class="h-5 w-5 ml-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                    </svg>
                    حذف
                </button>
            </form>
            @endcan
        </div>
        @endif
    </div>
</div>
@endsection
