﻿@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">
                @can('inventory.issues.index')
                <a href="{{ route('inventory.issues.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>
                @endcan
                
                @can('inventory.issues.print')
                <a href="{{ route('inventory.issues.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="bg-gray-50 p-4 rounded-md flex justify-between items-center">
                <div>
                    <span class="font-bold text-gray-700">حالة المستند:</span>
                    @if($document->status == 'draft')
                        <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-yellow-100 text-yellow-800">
                            مسودة
                        </span>
                    @elseif($document->status == 'approved')
                        <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                            معتمد
                        </span>
                    @elseif($document->status == 'cancelled')
                        <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-red-100 text-red-800">
                            ملغي
                        </span>
                    @endif
                </div>
                
                @if($document->status == 'draft')
                    <div class="flex space-y-2 space-y-reverse">
                        @can('inventory.issues.edit')
                        <a href="{{ route('inventory.issues.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.issues.approve')
                        <form action="{{ route('inventory.issues.approve', $document->id) }}" method="POST" onsubmit="return confirm('هل أنت متأكد من اعتماد هذا المستند؟');">
                            @csrf
                            @method('PATCH')
                            <button type="submit" class="inline-flex items-center p-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.issues.cancel')
                        <form action="{{ route('inventory.issues.cancel', $document->id) }}" method="POST" onsubmit="return confirm('هل أنت متأكد من إلغاء هذا المستند؟');">
                            @csrf
                            @method('PATCH')
                            <button type="submit" class="inline-flex items-center p-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="M6 18L18 6M6 6l12 12" />
                                </svg>
                                إلغاء
                            </button>
                        </form>
                        @endcan
                    </div>
                @endif
            </div>
        </div>

        <!-- Document Details -->
        <div class="mb-6 grid grid-cols-1 md:grid-cols-2 gap-6">
            <div class="bg-gray-50 p-4 rounded-md">
                <h2 class="text-lg font-bold text-gray-900 mb-4">بيانات المستند</h2>
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <p class="text-sm font-medium text-gray-500">رقم المستند</p>
                        <p class="text-base font-medium text-gray-900">{{ $document->document_number }}</p>
                    </div>
                    <div>
                        <p class="text-sm font-medium text-gray-500">تاريخ المستند</p>
                        <p class="text-base font-medium 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="text-base font-medium text-gray-900">{{ $document->fromWarehouse->name ?? 'غير محدد' }}</p>
                    </div>
                    @if($document->recipient_type === 'customer' && $document->recipient_id)
                    <div>
                        <p class="text-sm font-medium text-gray-500">العميل</p>
                        <p class="text-base font-medium text-gray-900">
                            @if($document->recipient && $document->recipient instanceof \App\Models\Customer)
                                {{ $document->recipient->name }}
                            @else
                                غير محدد
                            @endif
                        </p>
                    </div>
                    @endif
                    @if($document->reference_number)
                    <div>
                        <p class="text-sm font-medium text-gray-500">رقم المرجع</p>
                        <p class="text-base font-medium text-gray-900">{{ $document->reference_number }}</p>
                    </div>
                    @endif
                </div>
            </div>
            
            <div class="bg-gray-50 p-4 rounded-md">
                <h2 class="text-lg font-bold text-gray-900 mb-4">بيانات المستخدمين</h2>
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <p class="text-sm font-medium text-gray-500">منشئ المستند</p>
                        <p class="text-base font-medium text-gray-900">{{ $document->creator->name ?? 'غير محدد' }}</p>
                    </div>
                    <div>
                        <p class="text-sm font-medium text-gray-500">معتمد المستند</p>
                        <p class="text-base font-medium text-gray-900">{{ $document->approver->name ?? 'غير معتمد' }}</p>
                    </div>
                </div>
            </div>
        </div>

        @if($document->notes)
        <div class="mb-6 bg-gray-50 p-4 rounded-md">
            <h2 class="text-lg font-bold text-gray-900 mb-2">ملاحظات</h2>
            <p class="text-base text-gray-700">{{ $document->notes }}</p>
        </div>
        @endif

        <!-- Document Items -->
        <div class="mb-6">
            <h2 class="text-lg font-bold text-gray-900 mb-4">الأصناف</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="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">الصنف</th>
                            <th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">الوحدة</th>
                            <th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">الكمية</th>
                            <th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">تكلفة الوحدة</th>
                            <th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">الإجمالي</th>
                            <th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">مركز التكلفة</th>
                            <th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">ملاحظات</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        @forelse($document->items as $item)
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">{{ $item->item->name ?? 'غير محدد' }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $item->unit->name ?? 'غير محدد' }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ number_format($item->quantity, 2) }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ number_format($item->unit_cost, 2) }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ number_format($item->total_cost, 2) }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $item->costCenter ? $item->costCenter->code . ' - ' . $item->costCenter->name : '-' }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $item->notes ?? '-' }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 text-center">لا توجد أصناف في هذا المستند</td>
                            </tr>
                        @endforelse
                    </tbody>
                    <tfoot class="bg-gray-50">
                        <tr>
                            <td colspan="4" class="px-6 py-4 whitespace-nowrap text-sm font-bold text-gray-900 text-left">الإجمالي</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-bold text-gray-900">{{ number_format($document->items->sum('total_cost'), 2) }}</td>
                            <td colspan="2"></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
