@extends('admin.layouts.app')

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

@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 Details</h1>
                <div>
                    <a href="{{ route('admin.pathways.edit', $pathway) }}" class="btn btn-primary me-2">
                        <i class="bi bi-pencil"></i> Edit
                    </a>
                    <a href="{{ route('admin.pathways.index') }}" class="btn btn-outline-secondary">
                        <i class="bi bi-arrow-left"></i> Back to Pathways
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">{{ $pathway->name }}</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <!-- Logo Section -->
                        @if($pathway->logo)
                        <div class="col-md-4 mb-3">
                            <div class="text-center">
                                <img src="{{ $pathway->logoUrl }}" alt="{{ $pathway->name }} Logo" 
                                     class="img-fluid rounded shadow-sm" style="max-width: 200px; max-height: 200px;">
                                <div class="mt-2">
                                    <small class="text-muted">{{ basename($pathway->logo) }}</small>
                                </div>
                            </div>
                        </div>
                        @endif

                        <!-- Details Section -->
                        <div class="{{ $pathway->logo ? 'col-md-8' : 'col-12' }}">
                            <div class="row">
                                <div class="col-sm-4">
                                    <strong>Name:</strong>
                                </div>
                                <div class="col-sm-8">
                                    {{ $pathway->name }}
                                </div>
                            </div>
                            <hr>

                            <div class="row">
                                <div class="col-sm-4">
                                    <strong>Status:</strong>
                                </div>
                                <div class="col-sm-8">
                                    <span class="badge {{ $pathway->status ? 'bg-success' : 'bg-danger' }}">
                                        {{ $pathway->statusText }}
                                    </span>
                                </div>
                            </div>
                            <hr>

                            <div class="row">
                                <div class="col-sm-4">
                                    <strong>Sort Order:</strong>
                                </div>
                                <div class="col-sm-8">
                                    {{ $pathway->sorting }}
                                </div>
                            </div>
                            <hr>

                            <div class="row">
                                <div class="col-sm-4">
                                    <strong>Created:</strong>
                                </div>
                                <div class="col-sm-8">
                                    {{ $pathway->created_at->format('M d, Y \a\t g:i A') }}
                                </div>
                            </div>
                            <hr>

                            <div class="row">
                                <div class="col-sm-4">
                                    <strong>Last Updated:</strong>
                                </div>
                                <div class="col-sm-8">
                                    {{ $pathway->updated_at->format('M d, Y \a\t g:i A') }}
                                </div>
                            </div>
                        </div>
                    </div>

                    @if($pathway->short_description)
                    <hr>
                    <div class="row">
                        <div class="col-12">
                            <strong>Description:</strong>
                            <div class="mt-2 p-3 bg-light rounded">
                                {{ $pathway->short_description }}
                            </div>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>

        <!-- Actions Sidebar -->
        <div class="col-lg-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">Actions</h5>
                </div>
                <div class="card-body">
                    <div class="d-grid gap-2">
                        <a href="{{ route('admin.pathways.edit', $pathway) }}" class="btn btn-primary">
                            <i class="bi bi-pencil"></i> Edit Pathway
                        </a>
                        
                        <button type="button" class="btn btn-{{ $pathway->status ? 'warning' : 'success' }}" 
                                onclick="toggleStatus({{ $pathway->id }})">
                            <i class="bi bi-toggle-{{ $pathway->status ? 'on' : 'off' }}"></i> 
                            {{ $pathway->status ? 'Deactivate' : 'Activate' }}
                        </button>
                        
                        <hr>
                        
                        <button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#deleteModal">
                            <i class="bi bi-trash"></i> Delete Pathway
                        </button>
                    </div>
                </div>
            </div>

            <!-- Statistics Card -->
            <div class="card mt-3">
                <div class="card-header">
                    <h5 class="card-title mb-0">Statistics</h5>
                </div>
                <div class="card-body">
                    <div class="row text-center">
                        <div class="col-6">
                            <div class="border-end">
                                <h4 class="text-primary">{{ $pathway->id }}</h4>
                                <small class="text-muted">Pathway ID</small>
                            </div>
                        </div>
                        <div class="col-6">
                            <h4 class="text-info">{{ $pathway->sorting }}</h4>
                            <small class="text-muted">Sort Order</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Delete Confirmation Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteModalLabel">Confirm Delete</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>Are you sure you want to delete the pathway "<strong>{{ $pathway->name }}</strong>"?</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 action="{{ route('admin.pathways.destroy', $pathway) }}" method="POST" class="d-inline">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-danger">Delete Pathway</button>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
function toggleStatus(pathwayId) {
    $.ajax({
        url: `/admin/pathways/${pathwayId}/toggle-status`,
        type: 'POST',
        data: {
            _token: '{{ csrf_token() }}'
        },
        success: function(response) {
            if (response.success) {
                // Reload the page to reflect changes
                location.reload();
            } else {
                alert('Error: ' + (response.message || 'Failed to update status'));
            }
        },
        error: function(xhr) {
            console.error('Error:', xhr);
            alert('An error occurred while updating the status');
        }
    });
}
</script>
@endpush