﻿@extends('layouts.app')

@section('title', \App\Helpers\TranslationHelper::transFromDatabase('Branches.ManageTitle', __('إدارة فروع الشركة')))

@section('content')
<div class="container m-auto p-4 py-6">
    <div class="flex justify-between items-center mb-6">
        <h2 class="text-2l font-bold text-gray-800">{{ \App\Helpers\TranslationHelper::transFromDatabase('Branches.ManageTitle', __('إدارة فروع الشركة')) }}</h2>
        @permission('branches.create')
        <a href="{{ route('branches.create') }}" class="bg-primary-600 hover:bg-primary-700 text-white font-bold py-2 p-4 rounded-md flex items-center">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 ml-1" viewBox="0 0 20 20" fill="currentColor">
                <path fill-rule="evenodd" d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z" clip-rule="evenodd" />
            </svg>
            {{ \App\Helpers\TranslationHelper::transFromDatabase('Branches.CreateTitle', __('إضافة فرع جديد')) }}
        </a>
        @endpermission
    </div>

    @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="bg-white rounded-lg shadow-md overflow-hidden">
        <div class="overflow--auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Branches.Name', __('الاسم')) }}
                        </th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Branches.Address', __('العنوان')) }}
                        </th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Branches.Phone', __('رقم الهاتف')) }}
                        </th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Branches.Email', __('البريد الإلكتروني')) }}
                        </th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Branches.MainBranch', __('الفرع الرئيسي')) }}
                        </th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Branches.Status', __('الحالة')) }}
                        </th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Companies.UserActions', __('الإجراءات')) }}
                        </th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse($branches as $branch)
                    <tr>
                        <td class="p-6 py-4 whitespace-nowrap">
                            <div class="text-sm font-medium text-gray-900">{{ $branch->name }}</div>
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap">
                            <div class="text-sm text-gray-500">{{ $branch->address ?? \App\Helpers\TranslationHelper::transFromDatabase('CommonMessages.NotSpecified', __('غير محدد')) }}</div>
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap">
                            <div class="text-sm text-gray-500">{{ $branch->phone ?? \App\Helpers\TranslationHelper::transFromDatabase('CommonMessages.NotSpecified', __('غير محدد')) }}</div>
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap">
                            <div class="text-sm text-gray-500">{{ $branch->email ?? \App\Helpers\TranslationHelper::transFromDatabase('CommonMessages.NotSpecified', __('غير محدد')) }}</div>
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap">
                            @if($branch->is_main)
                            <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Common.Yes', __('نعم')) }}
                            </span>
                            @else
                            <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-gray-100 text-gray-800">
                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Common.No', __('لا')) }}
                            </span>
                            @endif
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap">
                            @if($branch->is_active)
                            <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Common.Active', __('نشط')) }}
                            </span>
                            @else
                            <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-red-100 text-red-800">
                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Common.Inactive', __('غير نشط')) }}
                            </span>
                            @endif
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                            <div class="flex items-center space-y-2 space-y-reverse">
                                <a href="{{ route('branches.show', $branch) }}" class="text-primary-600 hover:text-primary-900 ml-2" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('CommonButtons.View', __('عرض')) }}">
                                    <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>
                                @permission('branches.edit')
                                <a href="{{ route('branches.edit', $branch) }}" class="text-blue-600 hover:text-blue-900 ml-2" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('CommonButtons.Edit', __('تعديل')) }}">
                                    <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>
                                @endpermission
                                @permission('branches.delete')
                                @if(!$branch->is_main)
                                <form action="{{ route('branches.destroy', $branch) }}" method="POST" class="inline" onsubmit="return confirm('{{ \App\Helpers\TranslationHelper::transFromDatabase('CommonMessages.ConfirmDelete', __('هل أنت متأكد من حذف هذا الفرع؟')) }}');">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="text-red-600 hover:text-red-900" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('CommonButtons.Delete', __('حذف')) }}">
                                        <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
                                @endpermission
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="p-6 py-4 whitespace-nowrap text-center text-gray-500">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Branches.NoBranches', __('لا توجد فروع مسجلة حالياً')) }}
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection