﻿<x-app-layout>
    <x-slot name="header">
        <div class="flex justify-between items-center">
            <h2 class="font-semibold text-l text-gray-800 leading-tight">
                {{ __('إدارة المستخدمين') }}
            </h2>
            @if($isCompanyOwner || auth()->user()->hasPermission('users.create'))
            <a href="{{ route('users.create') }}" class="bg-primary-600 hover:bg-primary-700 text-white font-bold py-2 p-4 rounded">
                إضافة مستخدم جديد
            </a>
            @endif
        </div>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7l m-auto sm:p-6 lg:p-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 bg-white border-b border-gray-200">
                    @if(session('success'))
                        <div class="bg-green-100 border border-green-400 text-green-700 p-4 py-3 rounded relative mb-4" role="alert">
                            <span class="block sm:inline">{{ session('success') }}</span>
                        </div>
                    @endif

                    @if(session('error'))
                        <div class="bg-red-100 border border-red-400 text-red-700 p-4 py-3 rounded relative mb-4" role="alert">
                            <span class="block sm:inline">{{ session('error') }}</span>
                        </div>
                    @endif

                    <div class="overflow--auto">
                        <table class="min-w-full bg-white">
                            <thead class="bg-gray-100">
                                <tr>
                                    <th class="py-3 p-4 text-right">الاسم</th>
                                    <th class="py-3 p-4 text-right">الشركة</th>
                                    <th class="py-3 p-4 text-right">البريد الإلكتروني</th>
                                    <th class="py-3 p-4 text-right">رقم الهاتف</th>
                                    <th class="py-3 p-4 text-right">الفرع</th>
                                    <th class="py-3 p-4 text-right">الأدوار</th>
                                    <th class="py-3 p-4 text-right">الحالة</th>
                                    <th class="py-3 p-4 text-right">الإجراءات</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-200">
                                @forelse($users as $user)
                                    <tr>
                                        <td class="py-3 p-4">{{ $user->name }}</td>
                                        <td class="py-3 p-4">{{ $user->company ? $user->company->name : 'غير محدد' }}</td>
                                        <td class="py-3 p-4">{{ $user->email }}</td>
                                        <td class="py-3 p-4">{{ $user->phone ?? 'لم يتم تحديده' }}</td>
                                        <td class="py-3 p-4">{{ $user->branch ? $user->branch->name : 'غير محدد' }}</td>
                                        <td class="py-3 p-4">
                                            @foreach($user->roles as $role)
                                                <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-blue-100 text-blue-800 mr-1">
                                                    {{ $role->name }}
                                                </span>
                                            @endforeach
                                        </td>
                                        <td class="py-3 p-4">
                                            @if($user->is_active)
                                                <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                                                    نشط
                                                </span>
                                            @else
                                                <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-red-100 text-red-800">
                                                    غير نشط
                                                </span>
                                            @endif
                                        </td>
                                        <td class="py-3 p-4">
                                            <div class="flex items-center space-y-2 space-y-reverse">
                                                {{-- زر العرض --}}
                                                @if($isCompanyOwner || auth()->user()->hasPermission('users.view'))
                                                <a href="{{ route('users.show', $user) }}" class="text-blue-600 hover:text-blue-900 ml-3">
                                                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
                                                    </svg>
                                                </a>
                                                @endif

                                                {{-- زر التعديل --}}
                                                @if($isCompanyOwner || auth()->user()->hasPermission('users.edit'))
                                                <a href="{{ route('users.edit', $user) }}" class="text-yellow-600 hover:text-yellow-900 ml-3">
                                                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" 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>
                                                @endif

                                                {{-- زر الحذف --}}
                                                @if($isCompanyOwner || auth()->user()->hasPermission('users.delete'))
                                                <form action="{{ route('users.destroy', $user) }}" method="POST" class="inline-block">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="text-red-600 hover:text-red-900 ml-3" onclick="return confirm('هل أنت متأكد من حذف هذا المستخدم؟')">
                                                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" 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>
                                                @endif
                                            </div>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="8" class="py-3 p-4 text-center text-gray-500">
                                            لا يوجد مستخدمين مضافين حتى الآن
                                        </td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
