@extends('layouts.admin')

@section('title', 'Campaigns')

@section('content')
    <div class="d-flex flex-wrap justify-content-between align-items-center gap-1 mb-2">
        <div><span class="loot-status success">Offer operations</span><h1 class="loot-section-title mt-1 mb-25">Campaigns</h1><p class="loot-muted mb-0">Create curated campaigns connected to providers and control reward visibility.</p></div>
        <button class="btn btn-loot" data-bs-toggle="modal" data-bs-target="#campaignCreate">New campaign</button>
    </div>
    <div class="loot-admin-grid mb-2">
        <div class="loot-admin-card"><span>Active</span><strong>{{ $stats['active'] }}</strong></div>
        <div class="loot-admin-card"><span>Draft</span><strong>{{ $stats['draft'] }}</strong></div>
        <div class="loot-admin-card"><span>Paused</span><strong>{{ $stats['paused'] }}</strong></div>
        <div class="loot-admin-card"><span>Featured</span><strong>{{ $stats['featured'] }}</strong></div>
    </div>
    <section class="user-page-card">
        <form method="GET" class="admin-leads-filters">
            <div class="admin-filter-search"><i data-feather="search"></i><input class="form-control" name="search" value="{{ request('search') }}" placeholder="Search campaign"></div>
            <select name="status" class="form-select"><option value="">All statuses</option>@foreach(['draft','active','paused','ended'] as $status)<option value="{{ $status }}" {{ request('status') === $status ? 'selected' : '' }}>{{ ucfirst($status) }}</option>@endforeach</select>
            <button class="btn btn-loot">Apply</button><a class="btn btn-outline-light" href="{{ route('admin.campaigns') }}">Reset</a>
        </form>
        <form id="campaignBulk" data-bulk-form="campaigns" method="POST" action="{{ route('admin.campaigns.bulk') }}" class="admin-bulk-bar mb-1">
            @csrf
            <select class="form-select" name="action" required>
                <option value="">Bulk actions</option>
                <option value="activate">Activate</option>
                <option value="draft">Move to draft</option>
                <option value="pause">Pause</option>
                <option value="end">End</option>
                <option value="feature">Mark featured</option>
                <option value="unfeature">Remove featured</option>
                <option value="delete">Delete</option>
            </select>
            <button class="btn btn-outline-light" data-bulk-submit="campaigns" disabled>Apply</button>
            <span class="loot-muted" data-bulk-count="campaigns">0 selected</span>
            <button type="button" class="btn btn-outline-light ms-auto" data-bulk-export="campaigns" data-export-url="{{ route('admin.export.campaigns', request()->query()) }}"><i data-feather="download" class="me-50"></i>Export CSV</button>
        </form>
        <div class="table-responsive admin-leads-table-wrap">
            <table class="table admin-leads-table mb-0">
                <thead><tr><th class="admin-check-cell"><input class="form-check-input" type="checkbox" data-bulk-select-all="campaigns" aria-label="Select all campaigns"></th><th>Name</th><th>Provider</th><th>Status</th><th>Reward</th><th>Category</th><th>Device</th><th>Direct</th><th>Curation</th><th></th></tr></thead>
                <tbody>
                @forelse($campaigns as $campaign)
                    <tr>
                        <td class="admin-check-cell"><input class="form-check-input" type="checkbox" name="ids[]" value="{{ $campaign->id }}" form="campaignBulk" data-bulk-item="campaigns" aria-label="Select {{ $campaign->name }}"></td>
                        <td>
                            <div class="d-flex align-items-center gap-1">
                                @if($campaign->icon_path)
                                    <img src="{{ Storage::url($campaign->icon_path) }}" alt="{{ $campaign->name }}" style="width:42px;height:42px;border-radius:8px;object-fit:cover;">
                                @endif
                                <div><strong>{{ $campaign->name }}</strong><div class="loot-muted small">{{ Str::limit($campaign->description, 80) }}</div></div>
                            </div>
                        </td>
                        <td>{{ optional($campaign->offerwall)->name ?? 'Manual' }}</td>
                        <td>
                            <span class="loot-status {{ $campaign->status === 'active' ? 'success' : '' }}">{{ ucfirst($campaign->status) }}</span>
                            @if($campaign->featured)<span class="loot-status warning ms-50">Featured</span>@endif
                            @if($campaign->is_hot)<span class="loot-status danger ms-50">Hot</span>@endif
                        </td>
                        <td><strong>{{ format_coins_as_usd($campaign->reward_points) }}</strong><small class="d-block loot-muted">{{ number_format($campaign->reward_points) }} coins</small></td>
                        <td>{{ ucfirst($campaign->category ?? 'other') }}</td>
                        <td>{{ ucfirst($campaign->device) }}</td>
                        <td><span class="loot-status {{ $campaign->direct_url ? 'success' : '' }}">{{ $campaign->direct_url ? 'Ready' : 'Provider wall' }}</span></td>
                        <td>
                            <form method="POST" action="{{ route('admin.campaigns.update', $campaign) }}" title="{{ $campaign->curation_locked ? 'Unlock: allow auto-curation to manage this campaign again' : 'Lock: auto-curation will never change this campaign' }}">
                                @csrf
                                @method('PATCH')
                                <input type="hidden" name="offerwall_id" value="{{ $campaign->offerwall_id }}">
                                <input type="hidden" name="name" value="{{ $campaign->name }}">
                                <input type="hidden" name="reward_points" value="{{ $campaign->reward_points }}">
                                <input type="hidden" name="payout" value="{{ $campaign->payout }}">
                                <input type="hidden" name="status" value="{{ $campaign->status }}">
                                <input type="hidden" name="category" value="{{ $campaign->category }}">
                                <input type="hidden" name="device" value="{{ $campaign->device }}">
                                <input type="hidden" name="direct_launch_mode" value="{{ $campaign->direct_launch_mode }}">
                                <input type="hidden" name="featured" value="{{ $campaign->featured ? 1 : 0 }}">
                                <input type="hidden" name="is_hot" value="{{ $campaign->is_hot ? 1 : 0 }}">
                                <input type="hidden" name="curation_locked" value="{{ $campaign->curation_locked ? 0 : 1 }}">
                                <button class="btn btn-sm {{ $campaign->curation_locked ? 'btn-outline-warning' : 'btn-outline-light' }}">
                                    <i data-feather="{{ $campaign->curation_locked ? 'lock' : 'unlock' }}"></i>
                                </button>
                            </form>
                        </td>
                        <td><button class="btn btn-sm btn-outline-light" data-bs-toggle="modal" data-bs-target="#campaign{{ $campaign->id }}">Edit</button></td>
                    </tr>
                @empty
                    <tr><td colspan="10" class="text-center py-4 loot-muted">No campaigns yet.</td></tr>
                @endforelse
                </tbody>
            </table>
        </div>
        <div class="mt-2">{{ $campaigns->links() }}</div>
    </section>

    @include('admin.partials.campaign-form', ['id' => 'campaignCreate', 'campaign' => null, 'offerwalls' => $offerwalls])
    @foreach($campaigns as $campaign)
        @include('admin.partials.campaign-form', ['id' => 'campaign'.$campaign->id, 'campaign' => $campaign, 'offerwalls' => $offerwalls])
    @endforeach
@endsection
