@extends('layouts.app')

@section('content')
<div class="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 py-8 px-4 md:px-8">
    <div class="max-w-7xl mx-auto">
        <div class="mb-8">
            <div class="flex items-center justify-between mb-6">
                <div>
                    <h1 class="text-3xl font-bold text-gray-900 mb-2">💸 تقرير مصروفات مراكز التكاليف</h1>
                    <p class="text-gray-600">إظهار إجمالي المصروفات المحملة على كل مركز تكلفة</p>
                </div>
                <div class="flex gap-2">
                    <a href="{{ request()->fullUrlWithQuery(['export' => 1]) }}" class="inline-flex items-center px-4 py-2 bg-green-600 text-white font-semibold rounded-lg hover:bg-green-700 transition-all duration-200 shadow-md">
                        <i class="fas fa-file-excel ml-2"></i>
                        <span>تصدير Excel</span>
                    </a>
                    <a href="{{ route('reports.cost-center') }}" class="inline-flex items-center px-4 py-2 bg-gray-600 text-white font-semibold rounded-lg hover:bg-gray-700 transition-all duration-200 shadow-md">
                        <i class="fas fa-arrow-right ml-2"></i>
                        <span>رجوع</span>
                    </a>
                </div>
            </div>

            <!-- Filters -->
            <div class="bg-white rounded-xl shadow-md p-6 mb-8 border border-gray-200">
                <form action="{{ route('reports.cost-center.expenses') }}" method="GET" class="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-6 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">من تاريخ</label>
                        <input type="date" name="date_from" value="{{ $filters['date_from'] ?? '' }}" class="w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">إلى تاريخ</label>
                        <input type="date" name="date_to" value="{{ $filters['date_to'] ?? '' }}" class="w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">النوع</label>
                        <select name="type_id" class="w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
                            <option value="">الكل</option>
                            @foreach($types as $type)
                            <option value="{{ $type->id }}" {{ ($filters['type_id'] ?? '') == $type->id ? 'selected' : '' }}>{{ $type->name_ar ?: $type->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">الفئة</label>
                        <select name="category_id" class="w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
                            <option value="">الكل</option>
                            @foreach($categories as $category)
                            <option value="{{ $category->id }}" {{ ($filters['category_id'] ?? '') == $category->id ? 'selected' : '' }}>{{ $category->name_ar ?: $category->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">مركز التكلفة</label>
                        <select name="center_id" class="w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
                            <option value="">الكل</option>
                            @foreach($centers as $center)
                            <option value="{{ $center->id }}" {{ ($filters['center_id'] ?? '') == $center->id ? 'selected' : '' }}>{{ $center->name_ar ?: $center->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="flex items-end">
                        <button type="submit" class="w-full bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition-colors shadow-sm font-semibold">
                            <i class="fas fa-filter ml-2"></i> تصفية
                        </button>
                    </div>
                </form>
            </div>
        </div>

        @if(empty($report))
        <div class="bg-white rounded-xl shadow-lg p-12 text-center border border-gray-200">
            <i class="fas fa-inbox text-gray-400 text-5xl mb-4"></i>
            <p class="text-gray-600 font-medium text-lg">لا توجد بيانات للعرض</p>
        </div>
        @else
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            <div class="overflow-x-auto">
                <table class="w-full text-right">
                    <thead class="bg-gray-800 text-white">
                        <tr>
                            <th class="px-6 py-4 font-semibold text-sm">كود المركز</th>
                            <th class="px-6 py-4 font-semibold text-sm">اسم المركز</th>
                            <th class="px-6 py-4 font-semibold text-sm text-center">إجمالي المصروفات</th>
                            <th class="px-6 py-4 font-semibold text-sm text-center">عدد المستندات</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @foreach($report as $item)
                        <tr class="hover:bg-gray-50 transition-colors">
                            <td class="px-6 py-4 font-medium text-gray-900">{{ $item->code }}</td>
                            <td class="px-6 py-4 text-gray-700">{{ $item->name }}</td>
                            <td class="px-6 py-4 text-center font-bold text-red-600">
                                {{ number_format($item->total_expenses, 2) }} ر.س
                            </td>
                            <td class="px-6 py-4 text-center text-gray-600">
                                {{ $item->docs_count }}
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @endif
    </div>
</div>
@endsection
