@extends('layouts.app')
@section('title', \App\Helpers\TranslationHelper::transFromDatabase('Companies.ManageUsers', __('إدارة مستخدمي الشركة')) . ' - ' . $company->name)

@section('content')
<div class="min-h-screen bg-gray-50 py-8 px-4">
    <div class="max-w-7xl mx-auto">
        <!-- Header -->
        <div class="mb-8 flex justify-between items-center">
            <div>
                <h1 class="text-3xl font-bold text-gray-900">{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.ManageUsers', __('إدارة مستخدمي الشركة')) }}</h1>
                <p class="text-gray-600 mt-1">{{ $company->name }}</p>
            </div>
            <a href="{{ route('companies.show', $company) }}" class="px-4 py-2 bg-gray-600 hover:bg-gray-700 text-white rounded-lg">
                <i class="fas fa-arrow-right ml-1"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('CommonButtons.Back', __('العودة')) }}
            </a>
        </div>

        <!-- Messages -->
        @if(session('success'))
        <div class="bg-green-50 border-l-4 border-green-500 p-4 mb-6 rounded">
            <p class="text-green-700"><i class="fas fa-check ml-2"></i>{{ session('success') }}</p>
        </div>
        @endif
        @if(session('error'))
        <div class="bg-red-50 border-l-4 border-red-500 p-4 mb-6 rounded">
            <p class="text-red-700"><i class="fas fa-times ml-2"></i>{{ session('error') }}</p>
        </div>
        @endif

        <!-- Main Content -->
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            <!-- Form Panel -->
            <div class="bg-white rounded-lg shadow p-6">
                <h2 class="text-xl font-semibold mb-4">{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.AddNewUser', __('إضافة مستخدم جديد')) }}</h2>
                <form action="{{ route('companies.attach-user', $company) }}" method="POST" class="space-y-4">
                    @csrf
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.SelectUser', __('اختر المستخدم')) }}</label>
                        <select name="user_id" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                            <option value="">{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.SelectUserPlaceholder', __('-- اختر مستخدماً --')) }}</option>
                            @foreach($availableUsers as $user)
                            <option value="{{ $user->id }}">{{ $user->name }} ({{ $user->email }})</option>
                            @endforeach
                        </select>
                    </div>

                    <div class="flex items-center">
                        <input type="checkbox" id="is_owner" name="is_owner" value="1" class="h-4 w-4 text-blue-600">
                        <label for="is_owner" class="ml-2 text-sm text-gray-700">{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.SetAsOwner', __('تعيين كمالك للشركة')) }}</label>
                    </div>

                    <button type="submit" class="w-full px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg font-medium">
                        <i class="fas fa-plus ml-1"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('CommonButtons.Add', __('إضافة المستخدم')) }}
                    </button>
                </form>
            </div>

            <!-- Users List Panel -->
            <div class="lg:col-span-2 bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
                <div class="bg-gradient-to-r from-gray-800 to-gray-900 px-6 py-4">
                    <h2 class="text-xl font-semibold text-white">{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.UsersList', __('المستخدمون')) }} ({{ $company->users->count() }})</h2>
                </div>

                <div class="overflow-x-auto">
                    @if($company->users->count() > 0)
                    <table class="w-full">
                        <thead class="bg-gradient-to-r from-gray-800 to-gray-900 text-white">
                            <tr>
                                <th class="px-6 py-4 text-right font-semibold text-sm">#</th>
                                <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.UserName', __('الاسم')) }}</th>
                                <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.UserEmail', __('البريد الإلكتروني')) }}</th>
                                <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.UserStatus', __('الحالة')) }}</th>
                                <th class="px-6 py-4 text-center font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.UserActions', __('الإجراءات')) }}</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200">
                            @foreach($company->users as $user)
                            <tr class="hover:bg-blue-50 transition-colors duration-150 border-b border-gray-100">
                                <td class="px-6 py-4 text-gray-600 font-medium">{{ $loop->iteration }}</td>
                                <td class="px-6 py-4">
                                    <div class="font-semibold text-gray-900">{{ $user->name }}</div>
                                </td>
                                <td class="px-6 py-4 text-gray-600">{{ $user->email }}</td>
                                <td class="px-6 py-4 text-center">
                                    @if($company->owner_user_id === $user->id)
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-yellow-100 text-yellow-700">
                                        <i class="fas fa-crown ml-1 text-yellow-600"></i>
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.Owner', __('مالك')) }}
                                    </span>
                                    @else
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-100 text-blue-700">
                                        <span class="w-2 h-2 bg-blue-500 rounded-full ml-2"></span>
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.User', __('مستخدم')) }}
                                    </span>
                                    @endif
                                </td>
                                <td class="px-6 py-4 text-center">
                                    <div class="flex justify-center gap-2">
                                        <!-- عرض تفاصيل المستخدم -->
                                        <a href="{{ route('users.show', $user) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-blue-100 text-blue-600 hover:bg-blue-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('CommonButtons.View', __('عرض التفاصيل')) }}">
                                            <i class="fas fa-eye mr-1"></i>
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('CommonButtons.View', __('عرض')) }}</span>
                                        </a>

                                        <!-- تعديل المستخدم -->
                                        <a href="{{ route('users.edit', $user) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-amber-100 text-amber-600 hover:bg-amber-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('CommonButtons.Edit', __('تعديل')) }}">
                                            <i class="fas fa-edit mr-1"></i>
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('CommonButtons.Edit', __('تعديل')) }}</span>
                                        </a>

                                        <!-- تعيين كمالك -->
                                        @if($company->owner_user_id !== $user->id)
                                        <form action="{{ route('companies.set-owner', [$company, $user]) }}" method="POST" style="display:inline;" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.SetAsOwner', __('تعيين كمالك')) }}">
                                            @csrf
                                            <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-purple-100 text-purple-600 hover:bg-purple-200 transition-all duration-150 text-sm font-medium whitespace-nowrap">
                                                <i class="fas fa-crown mr-1"></i>
                                                <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.Owner', __('مالك')) }}</span>
                                            </button>
                                        </form>
                                        @else
                                        <button type="button" disabled class="inline-flex items-center px-3 py-2 rounded-lg bg-gray-100 text-gray-400 text-sm font-medium whitespace-nowrap cursor-not-allowed" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.AlreadyOwner', __('المستخدم الحالي مالك الشركة')) }}">
                                            <i class="fas fa-crown mr-1"></i>
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.Owner', __('مالك')) }}</span>
                                        </button>
                                        @endif

                                        <!-- حذف المستخدم -->
                                        <form action="{{ route('companies.detach-user', [$company, $user]) }}" method="POST" style="display:inline;" onsubmit="return confirm('{{ \App\Helpers\TranslationHelper::transFromDatabase('CommonMessages.ConfirmDelete', __('هل أنت متأكد من حذف هذا المستخدم؟')) }}');" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('CommonButtons.Delete', __('حذف')) }}">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-red-100 text-red-600 hover:bg-red-200 transition-all duration-150 text-sm font-medium whitespace-nowrap">
                                                <i class="fas fa-trash mr-1"></i>
                                                <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('CommonButtons.Delete', __('حذف')) }}</span>
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                    @else
                    <div class="px-6 py-12 text-center">
                        <div class="flex flex-col items-center justify-center">
                            <i class="fas fa-inbox text-gray-400 text-4xl mb-3"></i>
                            <p class="text-gray-600 font-medium">{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.NoUsersFound', __('لا توجد مستخدمون مرتبطون بهذه الشركة')) }}</p>
                            <p class="text-gray-500 text-sm mt-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.StartAddingUsers', __('ابدأ بإضافة مستخدم جديد من القسم الأيسر')) }}</p>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
