@extends('layouts.admin')

@section('title', 'Manage Banners')

@section('admin-content')
<style>
    /* Specific Banner Styles */
    .banners-page .banner-preview {
        width: 140px;
        height: 80px;
        border-radius: 8px;
        overflow: hidden;
        border: 1px solid #eee;
        background: #f8f9fa;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .banners-page .banner-preview img {
        width: 100%;
        height: 100%;
        object-fit: cover;
    }
    .banners-page .position-badge {
        background: #e3f2fd;
        color: #1565c0;
        padding: 5px 10px;
        border-radius: 6px;
        font-size: 0.8rem;
        font-weight: 600;
        text-transform: capitalize;
    }
</style>

<div class="banners-page">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1">Manage Banners</h4>
            <p class="text-muted mb-0 small">Control website banners and advertisements</p>
        </div>
        <div class="d-flex gap-2">
            <button type="button" class="btn btn-primary shadow-sm btn-lg d-flex align-items-center gap-2" 
                    data-bs-toggle="modal" data-bs-target="#addBannerModal"
                    style="border-radius: 12px; font-size: 0.95rem;">
                <i class="bi bi-plus-lg"></i> Add New Banner
            </button>
        </div>
    </div>

    <!-- Stats Cards -->
    <div class="row g-4 mb-4">
        <div class="col-lg-4 col-md-6">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon primary">
                    <i class="bi bi-images"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $totalBanners }}</div>
                    <div class="stat-label">Total Banners</div>
                </div>
            </div>
        </div>
        <div class="col-lg-4 col-md-6">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon success">
                    <i class="bi bi-check-circle-fill"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $activeBanners }}</div>
                    <div class="stat-label">Active</div>
                </div>
            </div>
        </div>
        <div class="col-lg-4 col-md-6">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon secondary">
                    <i class="bi bi-slash-circle-fill"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $inactiveBanners }}</div>
                    <div class="stat-label">Inactive</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="filter-card mb-4">
        <form action="{{ route('admin.banners.index') }}" method="GET" class="row g-3 align-items-end">
            <div class="col-md-6">
                <label class="form-label small fw-bold text-muted mb-1">Search</label>
                <div class="input-group">
                    <span class="input-group-text bg-white border-end-0 ps-3" style="border-radius: 10px 0 0 10px;">
                        <i class="bi bi-search text-muted"></i>
                    </span>
                    <input type="text" name="q" class="form-control form-control-lg border-start-0" 
                           placeholder="Search by position or link..." 
                           value="{{ request('q') }}"
                           style="border-radius: 0 10px 10px 0;">
                </div>
            </div>
            <div class="col-md-4">
                <label class="form-label small fw-bold text-muted mb-1">Status</label>
                <select name="status" class="form-select form-select-lg" style="border-radius: 10px;">
                    <option value="all">All Status</option>
                    <option value="active" {{ request('status') == 'active' ? 'selected' : '' }}>Active</option>
                    <option value="inactive" {{ request('status') == 'inactive' ? 'selected' : '' }}>Inactive</option>
                </select>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary btn-lg w-100" style="border-radius: 10px;">
                    <i class="bi bi-funnel me-1"></i> Filter
                </button>
            </div>
        </form>
    </div>

    <!-- Banners Grid -->
    <div class="data-table">
        <div class="table-responsive">
            <table class="table mb-0">
                <thead>
                    <tr>
                        <th class="ps-4">Banner Preview</th>
                        <th>Position</th>
                        <th>Type</th>
                        <th>Link Destination</th>
                        <th class="text-center">Active Status</th>
                        <th>Created</th>
                        <th class="text-end pe-4">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($banners as $banner)
                        <tr>
                            <td class="ps-4">
                                <div class="banner-preview">
                                    <img src="{{ asset('storage/' . $banner->image) }}" alt="Banner">
                                </div>
                            </td>
                            <td>
                                <span class="position-badge">
                                    {{ ucwords(str_replace('_', ' ', $banner->position)) }}
                                </span>
                            </td>
                            <td>
                                @if($banner->type == 'blink')
                                    <span class="badge bg-warning text-dark border border-warning">
                                        <i class="bi bi-lightning-fill"></i> Blink Effect
                                    </span>
                                @else
                                    <span class="badge bg-light text-secondary border">Standard</span>
                                @endif
                            </td>
                            <td>
                                @if($banner->link)
                                    <a href="{{ $banner->link }}" target="_blank" class="text-decoration-none d-flex align-items-center gap-1">
                                        <i class="bi bi-link-45deg"></i>
                                        <span class="d-inline-block text-truncate" style="max-width: 150px;">
                                            {{ $banner->link }}
                                        </span>
                                    </a>
                                @else
                                    <span class="text-muted small italic">No link</span>
                                @endif
                            </td>
                            <td class="text-center">
                                <form action="{{ route('admin.banners.toggle', $banner->id) }}" method="POST">
                                    @csrf
                                    <div class="form-check form-switch d-inline-block">
                                        <input class="form-check-input" type="checkbox" role="switch" 
                                               onchange="this.form.submit()" 
                                               {{ $banner->active ? 'checked' : '' }} 
                                               style="cursor: pointer; width: 40px; height: 20px;">
                                    </div>
                                </form>
                            </td>
                            <td>
                                <div class="text-dark small fw-medium">{{ $banner->created_at->format('d M Y') }}</div>
                                <small class="text-muted">{{ $banner->created_at->diffForHumans() }}</small>
                            </td>
                            <td class="text-end pe-4">
                                <div class="d-flex gap-1 justify-content-end">
                                    <button type="button" class="action-btn primary" 
                                            data-bs-toggle="modal" data-bs-target="#editBannerModal{{ $banner->id }}"
                                            title="Edit Banner">
                                        <i class="bi bi-pencil"></i>
                                    </button>
                                    
                                    <form action="{{ route('admin.banners.destroy', $banner->id) }}" method="POST" 
                                          onsubmit="return confirm('Delete this banner permanently?');">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="action-btn danger" title="Delete">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="text-center py-5">
                                <div class="py-4">
                                    <i class="bi bi-images fs-1 text-muted opacity-50 d-block mb-3"></i>
                                    <h5 class="fw-bold text-muted">No Banners Found</h5>
                                    <p class="text-muted small mb-0">Create a new banner to get started</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        
        @if($banners->hasPages())
           <div class="p-3 border-top bg-light" style="border-radius: 0 0 16px 16px;">
                {{ $banners->appends(request()->query())->links() }}
            </div>
        @endif
    </div>
</div>

<!-- Add Banner Modal -->
<div class="modal fade" id="addBannerModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow-lg" style="border-radius: 16px;">
            <div class="modal-header border-0 pb-0">
                <div>
                    <h5 class="modal-title fw-bold">Add New Banner</h5>
                    <p class="text-muted small mb-0">Upload and configure a new banner</p>
                </div>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="{{ route('admin.banners.store') }}" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="modal-body p-4">
                    <div class="mb-4 text-center">
                        <div class="border-2 border-dashed border-primary rounded-3 p-4 bg-light">
                            <i class="bi bi-cloud-arrow-up fs-1 text-primary opacity-50"></i>
                            <div class="mt-2 text-muted fw-bold small">Upload Banner Image</div>
                            <input type="file" name="image" class="form-control mt-3" accept="image/*" required>
                            <div class="form-text mt-2 small">Recommended: 1200x400px for Home Top</div>
                        </div>
                    </div>
                    
                    <div class="row g-3 mb-3">
                        <div class="col-md-6">
                            <label class="form-label fw-bold small text-muted">Position <span class="text-danger">*</span></label>
                            <select name="position" class="form-select form-select-lg rounded-3" required>
                                <option value="home_top">Home Top (Slider)</option>
                                <option value="home_top_right_1">Home Top Right (Top)</option>
                                <option value="home_top_right_2">Home Top Right (Bottom)</option>
                                <option value="home_middle">Home Middle</option>
                                <option value="product_sidebar">Product Sidebar</option>
                                <option value="product_middle">Product Middle (Horizontal)</option>
                                <option value="supplier_sidebar">Supplier Sidebar</option>
                                <option value="supplier_middle">Supplier Middle (Horizontal)</option>
                                <option value="sidebar">General Sidebar</option>
                                <option value="footer">Footer</option>
                                <option value="client_logo">Client Logo</option>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold small text-muted">Display Type <span class="text-danger">*</span></label>
                            <select name="type" class="form-select form-select-lg rounded-3" required>
                                <option value="standard">Standard</option>
                                <option value="blink">Blink Effect</option>
                                <option value="slider">Slider (Auto-rotate)</option>
                            </select>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold small text-muted">Link Destination (Optional)</label>
                        <div class="input-group">
                            <span class="input-group-text bg-light"><i class="bi bi-link-45deg"></i></span>
                            <input type="url" name="link" class="form-control form-control-lg" placeholder="https://example.com/promo">
                        </div>
                    </div>
                </div>
                <div class="modal-footer border-0 pt-0">
                    <button type="button" class="btn btn-light rounded-3" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary rounded-3 px-4 fw-bold">
                        <i class="bi bi-check-lg me-2"></i> Create Banner
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Edit Modals (Loop) -->
@foreach($banners as $banner)
<div class="modal fade" id="editBannerModal{{ $banner->id }}" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow-lg" style="border-radius: 16px;">
            <div class="modal-header border-0 pb-0">
                <div>
                    <h5 class="modal-title fw-bold">Edit Banner</h5>
                    <p class="text-muted small mb-0">Update banner details</p>
                </div>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="{{ route('admin.banners.update', $banner->id) }}" method="POST" enctype="multipart/form-data">
                @csrf
                @method('PUT')
                <div class="modal-body p-4">
                    <div class="row g-3 mb-3">
                        <div class="col-md-6">
                            <label class="form-label fw-bold small text-muted">Position</label>
                            <select name="position" class="form-select rounded-3" required>
                                @foreach(['home_top', 'home_top_right_1', 'home_top_right_2', 'home_middle', 'product_sidebar', 'product_middle', 'supplier_sidebar', 'supplier_middle', 'sidebar', 'footer', 'client_logo'] as $pos)
                                    <option value="{{ $pos }}" {{ $banner->position == $pos ? 'selected' : '' }}>
                                        {{ ucwords(str_replace('_', ' ', $pos)) }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold small text-muted">Type</label>
                            <select name="type" class="form-select rounded-3" required>
                                <option value="standard" {{ $banner->type == 'standard' ? 'selected' : '' }}>Standard</option>
                                <option value="blink" {{ $banner->type == 'blink' ? 'selected' : '' }}>Blink Effect</option>
                                <option value="slider" {{ $banner->type == 'slider' ? 'selected' : '' }}>Slider</option>
                            </select>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold small text-muted">Link URL</label>
                        <input type="url" name="link" class="form-control rounded-3" value="{{ $banner->link }}" placeholder="https://...">
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold small text-muted">Current Image</label>
                        <div class="rounded-3 overflow-hidden border mb-2 position-relative group">
                            <img src="{{ asset('storage/' . $banner->image) }}" class="w-100 object-fit-cover" style="max-height: 150px;">
                        </div>
                        <label class="form-label fw-bold small text-muted mt-2">Change Image (Optional)</label>
                        <input type="file" name="image" class="form-control" accept="image/*">
                    </div>
                </div>
                <div class="modal-footer border-0 pt-0">
                    <button type="button" class="btn btn-light rounded-3" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary rounded-3 px-4">Save Changes</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endforeach

<script>
    document.addEventListener('DOMContentLoaded', function() {
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        tooltipTriggerList.map(function (el) {
            return new bootstrap.Tooltip(el);
        });
    });
</script>
@endsection
