@extends('admin.layouts.app')

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

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="h3">Pathway Management</h1>
                <div class="btn-group">
                    <a href="{{ route('admin.pathways.create') }}" class="btn btn-primary">
                        <i class="bi bi-plus-circle"></i> Add New Pathway
                    </a>
                </div>
            </div>
        </div>
    </div>

    @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(session('error'))
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            {{ session('error') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card bg-primary text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between">
                        <div>
                            <h4 class="mb-0">{{ $pathways->total() }}</h4>
                            <p class="mb-0">Total Pathways</p>
                        </div>
                        <div class="align-self-center">
                            <i class="bi bi-signpost fs-2"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-success text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between">
                        <div>
                            <h4 class="mb-0">{{ \App\Models\Pathway::active()->count() }}</h4>
                            <p class="mb-0">Active Pathways</p>
                        </div>
                        <div class="align-self-center">
                            <i class="bi bi-check-circle fs-2"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-warning text-dark">
                <div class="card-body">
                    <div class="d-flex justify-content-between">
                        <div>
                            <h4 class="mb-0">{{ \App\Models\Pathway::where('status', false)->count() }}</h4>
                            <p class="mb-0">Inactive Pathways</p>
                        </div>
                        <div class="align-self-center">
                            <i class="bi bi-x-circle fs-2"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-info text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between">
                        <div>
                            <h4 class="mb-0">{{ \App\Models\Pathway::whereNotNull('logo')->count() }}</h4>
                            <p class="mb-0">With Logos</p>
                        </div>
                        <div class="align-self-center">
                            <i class="bi bi-image fs-2"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters and Search -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="GET" action="{{ route('admin.pathways.index') }}" class="row g-3">
                <div class="col-md-4">
                    <label for="search" class="form-label">Search</label>
                    <input type="text" class="form-control" id="search" name="search" 
                           value="{{ request('search') }}" placeholder="Search by name or description...">
                </div>
                <div class="col-md-3">
                    <label for="status" class="form-label">Status</label>
                    <select class="form-select" id="status" name="status">
                        <option value="">All Status</option>
                        <option value="1" {{ request('status') == '1' ? 'selected' : '' }}>Active</option>
                        <option value="0" {{ request('status') == '0' ? 'selected' : '' }}>Inactive</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label for="sort_by" class="form-label">Sort By</label>
                    <select class="form-select" id="sort_by" name="sort_by">
                        <option value="sorting" {{ request('sort_by') == 'sorting' ? 'selected' : '' }}>Sort Order</option>
                        <option value="name" {{ request('sort_by') == 'name' ? 'selected' : '' }}>Name</option>
                        <option value="status" {{ request('sort_by') == 'status' ? 'selected' : '' }}>Status</option>
                        <option value="created_at" {{ request('sort_by') == 'created_at' ? 'selected' : '' }}>Created Date</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label for="sort_direction" class="form-label">Direction</label>
                    <select class="form-select" id="sort_direction" name="sort_direction">
                        <option value="asc" {{ request('sort_direction') == 'asc' ? 'selected' : '' }}>Ascending</option>
                        <option value="desc" {{ request('sort_direction') == 'desc' ? 'selected' : '' }}>Descending</option>
                    </select>
                </div>
                <div class="col-12">
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-search"></i> Filter
                    </button>
                    <a href="{{ route('admin.pathways.index') }}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle"></i> Clear
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Pathways Table -->
    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">Pathways List</h5>
        </div>
        <div class="card-body">
            @if($pathways->count() > 0)
                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th>Logo</th>
                                <th>Name</th>
                                <th>Description</th>
                                <th>Status</th>
                                <th>Sort Order</th>
                                <th>Created</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($pathways as $pathway)
                                <tr>
                                    <td>
                                        @if($pathway->logo)
                                            <img src="{{ $pathway->logo_url }}" alt="{{ $pathway->name }}" 
                                                 class="img-thumbnail" style="width: 50px; height: 50px; object-fit: cover;">
                                        @else
                                            <div class="bg-light d-flex align-items-center justify-content-center" 
                                                 style="width: 50px; height: 50px; border-radius: 4px;">
                                                <i class="bi bi-image text-muted"></i>
                                            </div>
                                        @endif
                                    </td>
                                    <td>
                                        <strong>{{ $pathway->name }}</strong>
                                    </td>
                                    <td>
                                        <span class="text-muted">
                                            {{ Str::limit($pathway->short_description, 50) }}
                                        </span>
                                    </td>
                                    <td>
                                        <div class="form-check form-switch">
                                            <input class="form-check-input status-toggle" type="checkbox" 
                                                   data-pathway-id="{{ $pathway->id }}"
                                                   {{ $pathway->status ? 'checked' : '' }}>
                                            <label class="form-check-label">
                                                <span class="badge bg-{{ $pathway->status ? 'success' : 'secondary' }}">
                                                    {{ $pathway->status_text }}
                                                </span>
                                            </label>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge bg-info">{{ $pathway->sorting }}</span>
                                    </td>
                                    <td>
                                        <small class="text-muted">
                                            {{ $pathway->created_at->format('M d, Y') }}
                                        </small>
                                    </td>
                                    <td>
                                        <div class="btn-group btn-group-sm" role="group">
                                            <a href="{{ route('admin.pathways.show', $pathway) }}" 
                                               class="btn btn-outline-info" title="View">
                                                <i class="bi bi-eye"></i>
                                            </a>
                                            <a href="{{ route('admin.pathways.edit', $pathway) }}" 
                                               class="btn btn-outline-primary" title="Edit">
                                                <i class="bi bi-pencil"></i>
                                            </a>
                                            <button type="button" class="btn btn-outline-danger delete-pathway" 
                                                    data-pathway-id="{{ $pathway->id }}" 
                                                    data-pathway-name="{{ $pathway->name }}" title="Delete">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                <div class="d-flex justify-content-between align-items-center mt-4">
                    <div>
                        <small class="text-muted">
                            Showing {{ $pathways->firstItem() }} to {{ $pathways->lastItem() }} 
                            of {{ $pathways->total() }} results
                        </small>
                    </div>
                    <div>
                        {{ $pathways->links() }}
                    </div>
                </div>
            @else
                <div class="text-center py-5">
                    <i class="bi bi-signpost text-muted" style="font-size: 3rem;"></i>
                    <h5 class="mt-3 text-muted">No pathways found</h5>
                    <p class="text-muted">Get started by creating your first pathway.</p>
                    <a href="{{ route('admin.pathways.create') }}" class="btn btn-primary">
                        <i class="bi bi-plus-circle"></i> Create Pathway
                    </a>
                </div>
            @endif
        </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 the pathway "<span id="pathwayName"></span>"?</p>
                <p class="text-danger"><small>This action cannot be undone.</small></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>
@endsection

@push('scripts')
<script>
$(document).ready(function() {
    // Status toggle functionality
    $('.status-toggle').change(function() {
        const pathwayId = $(this).data('pathway-id');
        const isChecked = $(this).is(':checked');
        const toggle = $(this);
        const badge = toggle.siblings('label').find('.badge');
        
        $.ajax({
            url: `/admin/pathways/${pathwayId}/toggle-status`,
            method: 'POST',
            data: {
                _token: $('meta[name="csrf-token"]').attr('content')
            },
            success: function(response) {
                if (response.success) {
                    badge.removeClass('bg-success bg-secondary')
                         .addClass(response.status ? 'bg-success' : 'bg-secondary')
                         .text(response.status_text);
                    
                    // Show success message
                    showAlert('success', response.message);
                }
            },
            error: function() {
                // Revert toggle state
                toggle.prop('checked', !isChecked);
                showAlert('danger', 'Failed to update pathway status.');
            }
        });
    });
    
    // Delete pathway functionality
    $('.delete-pathway').click(function() {
        const pathwayId = $(this).data('pathway-id');
        const pathwayName = $(this).data('pathway-name');
        
        $('#pathwayName').text(pathwayName);
        $('#deleteForm').attr('action', `/admin/pathways/${pathwayId}`);
        $('#deleteModal').modal('show');
    });
    
    // Helper function to show alerts
    function showAlert(type, message) {
        const alertHtml = `
            <div class="alert alert-${type} alert-dismissible fade show" role="alert">
                ${message}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        `;
        $('.container-fluid').prepend(alertHtml);
        
        // Auto-dismiss after 5 seconds
        setTimeout(function() {
            $('.alert').fadeOut();
        }, 5000);
    }
});
</script>
@endpush