@extends('admin.layouts.app')

@section('title', 'إدارة مكاتب الغاز')
@section('page-title', 'إدارة مكاتب الغاز')
@section('page-icon', 'fas fa-building')

@section('content')
<div class="row g-3">
    <div class="col-12">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <div><i class="fas fa-building"></i> إدارة مكاتب الغاز</div>
                <div class="d-flex gap-2">
                    <a href="{{ route('admin.gas-offices.create') }}" class="btn btn-primary btn-sm">
                        <i class="fas fa-plus"></i> إضافة مكتب
                    </a>
                    <a href="{{ route('admin.gas-offices.stats') }}" class="btn btn-info btn-sm">
                        <i class="fas fa-chart-bar"></i> الإحصائيات
                    </a>
                </div>
            </div>
                <div class="card-body">
                    <!-- فلترة -->
                    <div class="row mb-3">
                        <div class="col-md-12">
                            <form method="GET" action="{{ route('admin.gas-offices.index') }}" class="form-inline">
                                <div class="form-group mr-3">
                                    <label for="search" class="mr-2">البحث:</label>
                                    <input type="text" name="search" id="search" class="form-control" 
                                           value="{{ request('search') }}" placeholder="اسم المكتب">
                                </div>
                                <div class="form-group mr-3">
                                    <label for="status" class="mr-2">الحالة:</label>
                                    <select name="status" id="status" class="form-control">
                                        <option value="">جميع الحالات</option>
                                        <option value="active" {{ request('status') == 'active' ? 'selected' : '' }}>نشط</option>
                                        <option value="inactive" {{ request('status') == 'inactive' ? 'selected' : '' }}>غير نشط</option>
                                    </select>
                                </div>
                                <div class="form-group mr-3">
                                    <label for="governorate" class="mr-2">المحافظة:</label>
                                    <input type="text" name="governorate" id="governorate" class="form-control" 
                                           value="{{ request('governorate') }}" placeholder="المحافظة">
                                </div>
                                <button type="submit" class="btn btn-primary">
                                    <i class="fas fa-search"></i> بحث
                                </button>
                                <a href="{{ route('admin.gas-offices.index') }}" class="btn btn-secondary">
                                    <i class="fas fa-times"></i> مسح
                                </a>
                            </form>
                        </div>
                    </div>

                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>اسم المكتب</th>
                                    <th>المالك</th>
                                    <th>الهاتف</th>
                                    <th>العنوان</th>
                                    <th>المناطق</th>
                                    <th>التقييم</th>
                                    <th>الحالة</th>
                                    <th>مفتوح</th>
                                    <th>الإجراءات</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($gasOffices as $office)
                                <tr>
                                    <td>{{ $office->id }}</td>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            @if($office->logo)
                                                <img src="{{ asset('storage/' . $office->logo) }}" 
                                                     alt="Logo" class="img-circle mr-2" style="width: 40px; height: 40px; object-fit: cover;">
                                            @endif
                                            <strong>{{ $office->name }}</strong>
                                        </div>
                                    </td>
                                    <td>{{ $office->owner->name ?? 'غير محدد' }}</td>
                                    <td>{{ $office->phone }}</td>
                                    <td>{{ Str::limit($office->address, 30) }}</td>
                                    <td>
                                        @if($office->service_areas)
                                            @foreach(array_slice($office->service_areas, 0, 2) as $area)
                                                <span class="badge badge-info">{{ $area }}</span>
                                            @endforeach
                                            @if(count($office->service_areas) > 2)
                                                <span class="badge badge-secondary">+{{ count($office->service_areas) - 2 }}</span>
                                            @endif
                                        @endif
                                    </td>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <span class="text-warning">
                                                @for($i = 1; $i <= 5; $i++)
                                                    @if($i <= floor($office->rating))
                                                        <i class="fas fa-star"></i>
                                                    @elseif($i - $office->rating < 1)
                                                        <i class="fas fa-star-half-alt"></i>
                                                    @else
                                                        <i class="far fa-star"></i>
                                                    @endif
                                                @endfor
                                            </span>
                                            <span class="ml-1">({{ $office->rating_count }})</span>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge badge-{{ $office->is_active ? 'success' : 'danger' }}">
                                            {{ $office->is_active ? 'نشط' : 'غير نشط' }}
                                        </span>
                                    </td>
                                    <td>
                                        <span class="badge badge-{{ $office->is_open ? 'success' : 'warning' }}">
                                            {{ $office->is_open ? 'مفتوح' : 'مغلق' }}
                                        </span>
                                    </td>
                                    <td>
                                        <div class="btn-group" role="group">
                                            <a href="{{ route('admin.gas-offices.show', $office) }}" 
                                               class="btn btn-info btn-sm" title="عرض">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{{ route('admin.gas-offices.edit', $office) }}" 
                                               class="btn btn-warning btn-sm" title="تعديل">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <form method="POST" action="{{ route('admin.gas-offices.toggle-status', $office) }}" 
                                                  class="d-inline">
                                                @csrf
                                                @method('PATCH')
                                                <button type="submit" class="btn btn-{{ $office->is_active ? 'secondary' : 'success' }} btn-sm" 
                                                        title="{{ $office->is_active ? 'إلغاء تفعيل' : 'تفعيل' }}">
                                                    <i class="fas fa-{{ $office->is_active ? 'ban' : 'check' }}"></i>
                                                </button>
                                            </form>
                                            <form method="POST" action="{{ route('admin.gas-offices.toggle-open', $office) }}" 
                                                  class="d-inline">
                                                @csrf
                                                @method('PATCH')
                                                <button type="submit" class="btn btn-{{ $office->is_open ? 'warning' : 'success' }} btn-sm" 
                                                        title="{{ $office->is_open ? 'إغلاق' : 'فتح' }}">
                                                    <i class="fas fa-{{ $office->is_open ? 'lock' : 'unlock' }}"></i>
                                                </button>
                                            </form>
                                            <form method="POST" action="{{ route('admin.gas-offices.destroy', $office) }}" 
                                                  class="d-inline" onsubmit="return confirm('هل أنت متأكد من حذف هذا المكتب؟')">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="btn btn-danger btn-sm" title="حذف">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                        </div>
                                    </td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="10" class="text-center">لا توجد مكاتب غاز</td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>

                    <!-- Pagination -->
                    <div class="d-flex justify-content-center">
                        {{ $gasOffices->appends(request()->query())->links('pagination::bootstrap-4') }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
