@extends('admin.layouts.app')

@section('title', 'View Content Section')

@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">Content Section: {{ $contentSection->title }}</h1>
                <div class="btn-group">
                    <a href="{{ route('admin.content-sections.edit', $contentSection) }}" class="btn btn-primary">
                        <i class="bi bi-pencil"></i> Edit
                    </a>
                    <a href="{{ route('admin.content-sections.index') }}" class="btn btn-secondary">
                        <i class="bi bi-arrow-left"></i> Back to List
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Section Details</h5>
                    <div>
                        @if($contentSection->is_active)
                            <span class="badge bg-success">Active</span>
                        @else
                            <span class="badge bg-secondary">Inactive</span>
                        @endif
                    </div>
                </div>
                <div class="card-body">
                    <dl class="row">
                        <dt class="col-sm-3">Section Key:</dt>
                        <dd class="col-sm-9">
                            <code>{{ $contentSection->section_key }}</code>
                        </dd>

                        <dt class="col-sm-3">Title:</dt>
                        <dd class="col-sm-9">{{ $contentSection->title }}</dd>

                        <dt class="col-sm-3">Content:</dt>
                        <dd class="col-sm-9">
                            @if($contentSection->content)
                                <div class="border rounded p-3 bg-light">
                                    {!! nl2br(e($contentSection->content)) !!}
                                </div>
                            @else
                                <em class="text-muted">No content provided</em>
                            @endif
                        </dd>

                        @if($contentSection->image_path)
                            <dt class="col-sm-3">Image:</dt>
                            <dd class="col-sm-9">
                                <div class="mb-2">
                                    <img src="{{ asset('storage/' . $contentSection->image_path) }}" 
                                         alt="{{ $contentSection->title }}" 
                                         class="img-fluid rounded" 
                                         style="max-height: 300px;">
                                </div>
                                <small class="text-muted">{{ $contentSection->image_path }}</small>
                            </dd>
                        @endif

                        <dt class="col-sm-3">Sort Order:</dt>
                        <dd class="col-sm-9">
                            <span class="badge bg-light text-dark">{{ $contentSection->sort_order }}</span>
                        </dd>

                        @if($contentSection->data && count($contentSection->data) > 0)
                            <dt class="col-sm-3">Additional Data:</dt>
                            <dd class="col-sm-9">
                                <pre class="bg-light p-3 rounded"><code>{{ json_encode($contentSection->data, JSON_PRETTY_PRINT) }}</code></pre>
                            </dd>
                        @endif

                        <dt class="col-sm-3">Created:</dt>
                        <dd class="col-sm-9">{{ $contentSection->created_at->format('F d, Y \a\t H:i') }}</dd>

                        <dt class="col-sm-3">Last Updated:</dt>
                        <dd class="col-sm-9">{{ $contentSection->updated_at->format('F d, Y \a\t H:i') }}</dd>
                    </dl>
                </div>
            </div>

            @if($contentSection->content)
                <div class="card mt-4">
                    <div class="card-header">
                        <h5 class="mb-0">Content Preview (Rendered)</h5>
                    </div>
                    <div class="card-body">
                        <div class="border rounded p-4">
                            {!! $contentSection->content !!}
                        </div>
                        <small class="text-muted mt-2 d-block">
                            <i class="bi bi-info-circle"></i> This is how the content will appear on the website
                        </small>
                    </div>
                </div>
            @endif
        </div>

        <div class="col-lg-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Quick Actions</h5>
                </div>
                <div class="card-body">
                    <div class="d-grid gap-2">
                        <a href="{{ route('admin.content-sections.edit', $contentSection) }}" class="btn btn-primary">
                            <i class="bi bi-pencil"></i> Edit Section
                        </a>
                        
                        @if($contentSection->is_active)
                            <form action="{{ route('admin.content-sections.update', $contentSection) }}" method="POST">
                                @csrf
                                @method('PUT')
                                <input type="hidden" name="section_key" value="{{ $contentSection->section_key }}">
                                <input type="hidden" name="title" value="{{ $contentSection->title }}">
                                <input type="hidden" name="content" value="{{ $contentSection->content }}">
                                <input type="hidden" name="sort_order" value="{{ $contentSection->sort_order }}">
                                <input type="hidden" name="is_active" value="0">
                                @if($contentSection->data)
                                    <input type="hidden" name="data" value="{{ json_encode($contentSection->data) }}">
                                @endif
                                <button type="submit" class="btn btn-warning w-100">
                                    <i class="bi bi-pause-circle"></i> Deactivate
                                </button>
                            </form>
                        @else
                            <form action="{{ route('admin.content-sections.update', $contentSection) }}" method="POST">
                                @csrf
                                @method('PUT')
                                <input type="hidden" name="section_key" value="{{ $contentSection->section_key }}">
                                <input type="hidden" name="title" value="{{ $contentSection->title }}">
                                <input type="hidden" name="content" value="{{ $contentSection->content }}">
                                <input type="hidden" name="sort_order" value="{{ $contentSection->sort_order }}">
                                <input type="hidden" name="is_active" value="1">
                                @if($contentSection->data)
                                    <input type="hidden" name="data" value="{{ json_encode($contentSection->data) }}">
                                @endif
                                <button type="submit" class="btn btn-success w-100">
                                    <i class="bi bi-play-circle"></i> Activate
                                </button>
                            </form>
                        @endif

                        <form action="{{ route('admin.content-sections.destroy', $contentSection) }}" 
                              method="POST" 
                              onsubmit="return confirm('Are you sure you want to delete this content section? This action cannot be undone.')">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-danger w-100">
                                <i class="bi bi-trash"></i> Delete Section
                            </button>
                        </form>
                    </div>
                </div>
            </div>

            <div class="card mt-3">
                <div class="card-header">
                    <h5 class="mb-0">Usage Information</h5>
                </div>
                <div class="card-body">
                    <h6>Template Usage:</h6>
                    <p class="small text-muted">
                        To use this content section in your Blade templates, you can retrieve it using:
                    </p>
                    <pre class="bg-light p-2 rounded small"><code>$section = ContentSection::where('section_key', '{{ $contentSection->section_key }}')->first();</code></pre>
                    
                    <h6 class="mt-3">Display in View:</h6>
                    <pre class="bg-light p-2 rounded small"><code>{{ '{' }}{{ '{' }} $section->title {{ '}' }}{{ '}' }}
{{ '{' }}!! $section->content !!{{ '}' }}</code></pre>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection