<?php
if (Auth('admin')->User()->dashboard_style == 'light') {
    $text = 'dark';
} else {
    $text = 'light';
}
?>
@extends('layouts.app')
@section('content')
    @include('admin.topmenu')
    @include('admin.sidebar')
    <div class="main-panel">
        <div class="content">
            <div class="page-inner">
                <div class="page-header">
                    <h4 class="page-title text-{{ $text }}">Giveaways</h4>
                    <ul class="breadcrumbs">
                        <li class="nav-home"><a href="{{ route('admin.dashboard') }}"><i class="flaticon-home"></i></a></li>
                        <li class="separator"><i class="flaticon-right-arrow"></i></li>
                        <li class="nav-item"><span class="text-{{ $text }}">Giveaways</span></li>
                    </ul>
                </div>

                @if(session('success'))
                    <div class="alert alert-success alert-dismissible fade show">
                        {{ session('success') }}
                        <button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
                    </div>
                @endif
                @if(session('error'))
                    <div class="alert alert-danger alert-dismissible fade show">
                        {{ session('error') }}
                        <button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
                    </div>
                @endif

                <div class="mb-3 d-flex justify-content-between align-items-center">
                    <div>
                        <a href="{{ route('admin.giveaways.index') }}" class="btn btn-sm {{ !$currentStatus ? 'btn-primary' : 'btn-outline-primary' }}">All</a>
                        <a href="{{ route('admin.giveaways.index', ['status' => 'draft']) }}" class="btn btn-sm {{ $currentStatus == 'draft' ? 'btn-secondary' : 'btn-outline-secondary' }}">Draft</a>
                        <a href="{{ route('admin.giveaways.index', ['status' => 'active']) }}" class="btn btn-sm {{ $currentStatus == 'active' ? 'btn-success' : 'btn-outline-success' }}">Active</a>
                        <a href="{{ route('admin.giveaways.index', ['status' => 'ended']) }}" class="btn btn-sm {{ $currentStatus == 'ended' ? 'btn-info' : 'btn-outline-info' }}">Ended</a>
                        <a href="{{ route('admin.giveaways.index', ['status' => 'cancelled']) }}" class="btn btn-sm {{ $currentStatus == 'cancelled' ? 'btn-danger' : 'btn-outline-danger' }}">Cancelled</a>
                    </div>
                    <a href="{{ route('admin.giveaways.create') }}" class="btn btn-primary">
                        <i class="fas fa-plus"></i> Create Giveaway
                    </a>
                </div>

                <div class="card">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table id="ShipTable" class="display table table-striped table-hover">
                                <thead>
                                    <tr>
                                        <th>Image</th>
                                        <th>Title</th>
                                        <th>Prize</th>
                                        <th>Value</th>
                                        <th>Requirement</th>
                                        <th>Status</th>
                                        <th>Entries</th>
                                        <th>Dates</th>
                                        <th>Winner</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($giveaways as $giveaway)
                                    <tr>
                                        <td>
                                            @if($giveaway->image)
                                                <img src="{{ asset('dash/' . $giveaway->image) }}" alt="{{ $giveaway->title }}" style="height:40px; border-radius:4px;">
                                            @else
                                                <span class="text-muted">—</span>
                                            @endif
                                        </td>
                                        <td>
                                            <a href="{{ route('admin.giveaways.show', $giveaway) }}" class="text-{{ $text }}">
                                                <strong>{{ Str::limit($giveaway->title, 30) }}</strong>
                                            </a>
                                            @if($giveaway->featured)
                                                <span class="badge badge-warning ml-1"><i class="fas fa-star"></i></span>
                                            @endif
                                        </td>
                                        <td>
                                            @switch($giveaway->prize_type)
                                                @case('car')
                                                    <span class="badge badge-primary">Car</span>
                                                    @break
                                                @case('cash')
                                                    <span class="badge badge-success">Cash</span>
                                                    @break
                                                @case('investment_credit')
                                                    <span class="badge" style="background:#6f42c1;color:#fff">Investment</span>
                                                    @break
                                                @default
                                                    <span class="badge badge-secondary">Other</span>
                                            @endswitch
                                        </td>
                                        <td>${{ number_format($giveaway->prize_value, 2) }}</td>
                                        <td>
                                            @switch($giveaway->entry_requirement)
                                                @case('anyone')
                                                    <span class="badge badge-light">Anyone</span>
                                                    @break
                                                @case('vip_only')
                                                    <span class="badge badge-warning">VIP Only</span>
                                                    @break
                                                @case('purchase_required')
                                                    <span class="badge badge-info">Purchase Req.</span>
                                                    @break
                                                @case('vip_and_purchase')
                                                    <span class="badge badge-dark">VIP + Purchase</span>
                                                    @break
                                            @endswitch
                                        </td>
                                        <td>
                                            @switch($giveaway->status)
                                                @case('draft')
                                                    <span class="badge badge-secondary">Draft</span>
                                                    @break
                                                @case('active')
                                                    <span class="badge badge-success">Active</span>
                                                    @break
                                                @case('ended')
                                                    <span class="badge badge-info">Ended</span>
                                                    @break
                                                @case('cancelled')
                                                    <span class="badge badge-danger">Cancelled</span>
                                                    @break
                                            @endswitch
                                        </td>
                                        <td>
                                            {{ $giveaway->entries_count }} / {{ $giveaway->max_entries ?? '∞' }}
                                        </td>
                                        <td>
                                            <small>
                                                {{ $giveaway->starts_at ? $giveaway->starts_at->format('M d, Y') : '—' }}<br>
                                                {{ $giveaway->ends_at ? $giveaway->ends_at->format('M d, Y') : '—' }}
                                            </small>
                                        </td>
                                        <td>
                                            @if($giveaway->winner)
                                                <span class="text-success"><i class="fas fa-trophy"></i> {{ $giveaway->winner->user->name ?? 'N/A' }}</span>
                                            @else
                                                <span class="text-muted">—</span>
                                            @endif
                                        </td>
                                        <td>
                                            <div class="btn-group btn-group-sm">
                                                <a href="{{ route('admin.giveaways.show', $giveaway) }}" class="btn btn-primary" title="View">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="{{ route('admin.giveaways.edit', $giveaway) }}" class="btn btn-info" title="Edit">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <form action="{{ route('admin.giveaways.destroy', $giveaway) }}" method="POST" class="d-inline" onsubmit="return confirm('Delete this giveaway?')">
                                                    @csrf @method('DELETE')
                                                    <button class="btn btn-danger" title="Delete">
                                                        <i class="fas fa-trash"></i>
                                                    </button>
                                                </form>
                                            </div>
                                        </td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                        <div class="mt-3">
                            {{ $giveaways->appends(['status' => $currentStatus])->links() }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
