@extends('layouts.master')
@section('title', 'System Users')
@section('content')

<div class="container-fluid py-3">
    <div class="d-flex justify-content-end mb-2">
        <a href="{{ route('users.create') }}" class="btn btn-primary"> Add User</a>
    </div>

    <div class="card shadow-sm">
        <div class="card-header bg-primary text-white py-3">
            <h5 class="mb-0"><i class="fas fa-users"></i> System Users</h5>
        </div>

        <div class="card-body">
            @if(session('success'))
                <div class="alert alert-success">{{ session('success') }}</div>
            @endif

            @if($errors->any())
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        @foreach($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <form id="salesAnalysisForm">
                @csrf


            </form>
        </div>
    </div>
        <!-- Display Sales Analysis Data -->
        <div class="card shadow-sm mt-4">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover">
                <thead style="background-color: #0b7c85;" class="text-white">
                <tr>
                    <th style="width: 200px;">Name</th>
                    <th style="width: 190px;">Email</th>
                    <th style="width: 120px;">Phone</th>
                    <th style="width: 180px;">Role</th>
                    <th style="width: 180px;">Group</th>
                    <th style="width: 100px;">Status</th>
                    <th style="width: 180px;">Action</th>
                </tr>
                </thead>
                <tbody style="font-weight: bold;">
                    @forelse ($users as $user)
                    <tr>
                        <td>{{ $user->name }}</td>
                        <td>{{ $user->email }}</td>
                        <td>{{ $user->phone ?? 'N/A' }}</td>
                        <td>
                            @if($user->role)
                                {{ $user->role->name }}
                            @else
                                N/A
                            @endif
                        </td>
                        <td>
                            @if($user->group)
                                {{ $user->group->name }}
                            @else
                                N/A
                            @endif
                        </td>
                        <td>
                            @if($user->is_active ?? 1)
                                <span class="badge bg-success text-white">Active</span>
                            @else
                                <span class="badge bg-danger">Inactive</span>
                            @endif
                        </td>
                        <td>
                            <!-- Edit Button -->
                            <a href="{{ route('users.edit', $user->id) }}" class="btn btn-sm btn-secondary">
                                 Edit
                            </a>
                            <!-- Delete Form -->
                            @if ($user->email === 'admin@gmail.com')
                            <button class="btn btn-sm btn-danger" disabled title="Cannot delete admin user">Delete</button>
                            <!-- Disable delete button for admin user -->
                            <!-- Add a tooltip to explain why it's disabled -->

                            @else
                            <form action="{{ route('users.destroy', $user->id) }}" method="POST" style="display:inline-block;" onsubmit="return confirm('Are you sure you want to delete this user?');">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="btn btn-sm btn-danger">
                                    Delete
                                </button>
                            </form>
                            @endif
                            <a href="{{ route('user-permissions.addPermissions', $user->id) }}"
                            class="btn btn-sm btn-info"
                            title="Manage Permissions">
                          <i class="fas fa-user-shield"></i>
                            </a>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="text-center text-danger">No users found.</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
</div>

@endsection

@section('styles')
<style>
.btn-back {
    text-decoration: none;
    color: #1AB9D9;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    padding: 5px 15px;
    border-radius: 4px;
    transition: all 0.3s;
}
.btn-back:hover {
    background-color: rgba(26, 185, 217, 0.1);
    text-decoration: none;
}
.card {
    border-radius: 8px;
    overflow: hidden;
    border: none;
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.1);
}
.card-header {
    border-bottom: 1px solid rgba(0,0,0,0.1);
    padding: 12px 20px;
}
.table th {
    background-color: #f8f9fa;
}
.table-responsive {
    overflow-x: auto;
}
.table-active {
    background-color: rgba(0,0,0,.05) !important;
}
.btn-process-all {
    background-color: #ffffff !important;
    color: #1AB9D9 !important;
    border: 1px solid rgba(26,185,217,0.35) !important;
    font-weight: 600 !important;
    padding: 7px 14px !important;
    box-shadow: none !important;
    line-height: 1 !important;
}
.btn-process-all:hover {
    background-color: #f5fbfc !important;
    color: #1498b0 !important;
    border-color: rgba(20,152,176,0.45) !important;
}

@media print {
    .btn-back, .card-header .btn, form {
        display: none !important;
    }
    .card {
        box-shadow: none;
        border: 1px solid #ddd;
    }
    .table-responsive {
        overflow-x: visible;
    }
}
</style>
@endsection


