@extends('admin.layouts.app')

@section('title', 'Universities Management')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h3 class="card-title mb-0">Universities Management</h3>
                    <a href="{{ route('admin.universities.create') }}" class="btn btn-primary">
                        <i class="fas fa-plus me-2"></i>Add New University
                    </a>
                </div>
                <div class="card-body">
                    @if(session('success'))
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            {{ session('success') }}
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    @endif

                    @if($universities->count() > 0)
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead class="table-dark">
                                    <tr>
                                        <th>Logo</th>
                                        <th>Name</th>
                                        <th>Address</th>
                                        <th>Contact</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($universities as $university)
                                        <tr>
                                            <td>
                                                @if($university->logo)
                                                    <img src="{{ asset('storage/' . $university->logo) }}" 
                                                         alt="{{ $university->name }}" 
                                                         class="rounded-circle"
                                                         style="width: 50px; height: 50px; object-fit: cover;">
                                                @else
                                                    <div class="bg-light rounded-circle d-flex align-items-center justify-content-center"
                                                         style="width: 50px; height: 50px;">
                                                        <i class="fas fa-university text-muted"></i>
                                                    </div>
                                                @endif
                                            </td>
                                            <td>
                                                <strong>{{ $university->name }}</strong>
                                            </td>
                                            <td>
                                                <small class="text-muted">{{ Str::limit($university->address, 50) }}</small>
                                            </td>
                                            <td>
                                                @if($university->phone)
                                                    <div><i class="fas fa-phone text-muted me-1"></i> {{ $university->phone }}</div>
                                                @endif
                                                @if($university->email)
                                                    <div><i class="fas fa-envelope text-muted me-1"></i> {{ $university->email }}</div>
                                                @endif
                                            </td>
                                            <td>
                                                <div class="btn-group" role="group">
                                                    <a href="{{ route('admin.universities.show', $university) }}" 
                                                       class="btn btn-sm btn-outline-info" title="View">
                                                        <i class="fas fa-eye"></i>
                                                    </a>
                                                    <a href="{{ route('admin.universities.edit', $university) }}" 
                                                       class="btn btn-sm btn-outline-warning" title="Edit">
                                                        <i class="fas fa-edit"></i>
                                                    </a>
                                                    <button type="button" 
                                                            class="btn btn-sm btn-outline-danger" 
                                                            title="Delete"
                                                            onclick="confirmDelete({{ $university->id }}, '{{ $university->name }}')">
                                                        <i class="fas fa-trash"></i>
                                                    </button>
                                                </div>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>

                        <!-- Pagination -->
                        <div class="d-flex justify-content-center mt-4">
                            {{ $universities->links() }}
                        </div>
                    @else
                        <div class="text-center py-5">
                            <i class="fas fa-university text-muted mb-3" style="font-size: 3rem;"></i>
                            <h4 class="text-muted">No Universities Found</h4>
                            <p class="text-muted mb-4">Start by adding your first university to the system.</p>
                            <a href="{{ route('admin.universities.create') }}" class="btn btn-primary">
                                <i class="fas fa-plus me-2"></i>Add First University
                            </a>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Delete Confirmation Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Confirm Delete</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>Are you sure you want to delete <strong id="universityName"></strong>?</p>
                <p class="text-muted small">This action cannot be undone.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <form id="deleteForm" method="POST" style="display: inline;">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
function confirmDelete(universityId, universityName) {
    document.getElementById('universityName').textContent = universityName;
    document.getElementById('deleteForm').action = `/admin/universities/${universityId}`;
    new bootstrap.Modal(document.getElementById('deleteModal')).show();
}
</script>
@endsection