<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-l text-gray-800 leading-tight">
            {{ __('تقرير تحليل العقود') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7l m-auto sm:p-6 lg:p-8">

            <div class="mb-6">
                <form method="GET" class="flex gap-2">
                    <select name="status" class="form-select text-sm" onchange="this.form.submit()">
                        <option value="all" {{ request('status') == 'all' ? 'selected' : '' }}>جميع العقود</option>
                        <option value="active" {{ request('status') == 'active' ? 'selected' : '' }}>نشطة</option>
                        <option value="completed" {{ request('status') == 'completed' ? 'selected' : '' }}>مكتملة</option>
                        <option value="terminated" {{ request('status') == 'terminated' ? 'selected' : '' }}>منهية</option>
                    </select>
                </form>
            </div>

            <div class="card">
                <div class="card-header">
                    <h2>قائمة العقود</h2>
                </div>
                <div class="overflow-auto">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>رقم العقد</th>
                                <th>العميل</th>
                                <th>قيمة العقد</th>
                                <th>المستحقات</th>
                                <th>المدفوع</th>
                                <th>المتبقي</th>
                                <th>الهامش</th>
                                <th>% الهامش</th>
                                <th>الحالة</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($contracts as $contract)
                                <tr>
                                    <td class="font-medium">{{ $contract['contract_number'] }}</td>
                                    <td>{{ $contract['client'] }}</td>
                                    <td>{{ number_format($contract['value'], 2) }}</td>
                                    <td>{{ number_format($contract['invoiced'], 2) }}</td>
                                    <td>{{ number_format($contract['paid'], 2) }}</td>
                                    <td>{{ number_format($contract['pending'], 2) }}</td>
                                    <td>
                                        <span class="{{ $contract['margin'] > 0 ? 'text-success-600' : 'text-danger-600' }}">
                                            {{ number_format($contract['margin'], 2) }}
                                        </span>
                                    </td>
                                    <td>
                                        <span class="{{ $contract['margin_percentage'] > 0 ? 'text-success-600' : 'text-danger-600' }}">
                                            {{ number_format($contract['margin_percentage'], 2) }}%
                                        </span>
                                    </td>
                                    <td>
                                        <span class="badge {{ $contract['status'] == 'completed' ? 'badge-success' : 'badge-primary' }}">
                                            {{ $contract['status'] }}
                                        </span>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="9" class="text-center text-neutral-500 py-8">لا توجد عقود</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>

        </div>
    </div>

</x-app-layout>
