@extends('admin.layouts.app')

@section('title', $title)

@section('content')
    <div class="col-md-12">
        <!-- Page Heading & Search Area -->
        <div class="row mb-3">
            <div class="col-md-9">
                <h4 class="c-grey-900">{{ $title }}</h4>
            </div>
            <div class="col-md-3">
                <form method="GET" action="{{ route('ch-admin.subscription.index') }}">
                    <div class="input-group">
                        <input type="text" name="search" class="form-control" placeholder="Search…" aria-label="Search" value="{{ request('search') }}">
                        <button class="btn btn-info" type="submit"><i class="ti ti-search"></i></button>
                    </div>
                    <!-- Hidden Inputs to Retain Filters -->
                    <input type="hidden" name="user_id" value="{{ request('user_id') }}">
                    <input type="hidden" name="status" value="{{ request('status') }}">
                </form>
            </div>
        </div>

        <div class="row">
            <div class="bgc-white bd bdrs-3 p-20 mB-20">
                <!-- Filters Section -->
                <div class="row mb-3">
                    <form method="GET" action="{{ route('ch-admin.subscription.index') }}" class="d-flex align-items-center gap-3 w-100">
                        <!-- Filter by Users -->
                        <div class="flex-shrink-0">
                            <select name="user_id" class="form-control">
                                <option value="">All Users</option>
                                @foreach($allSubscriptions->unique('user_id') as $subscription)
                                    <option value="{{ $subscription->user->id }}">{{ $subscription->user->name }}</option>
                                @endforeach
                            </select>
                        </div>

                        <!-- Filter by Status -->
                        <div class="flex-shrink-0">
                            <select name="status" class="form-control">
                                <option value="">All Status</option>
                                @foreach($allSubscriptions->unique('status') as $subscription)
                                    <option value="{{ $subscription->status }}">{{ getConstantLabel('subscription_status', $subscription->status) }}</option>
                                @endforeach
                            </select>
                        </div>

                        <!-- Action Buttons -->
                        <div class="flex-shrink-0">
                            <button type="submit" class="btn btn-info">Apply Filters</button>
                            @if(request()->hasAny(['search', 'status', 'user_id']))
                                <a href="{{ route('ch-admin.subscription.index') }}" class="btn btn-secondary">Reset</a>
                            @endif
                        </div>

                        <!-- Hidden Inputs to Retain Search -->
                        <input type="hidden" name="search" value="{{ request('search') }}">
                    </form>
                </div>

                <div class="row">
                    <div class="box-body table-responsive no-padding">
                        <table class="table table-striped">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Status</th>
                                <th>Start Date</th>
                                <th>End Date</th>
                                <th>Next Billing Date</th>
                                <th>Billing Cycle</th>
                                <th>Actions</th>
                            </tr>
                            </thead>
                            <tbody>
                            @forelse ($subscriptions as $subscription)
                                <tr>
                                    <th scope="row">{{ $subscription->id }}</th>
                                    <td>{!! $subscription->name !!}</td>
                                    <td>
                                        @php
                                            $statusDetails = getSubscriptionStatusDetails((int)$subscription->status);
                                        @endphp
                                        <span class="order-status badge {{ $statusDetails['class'] }}">{{ $statusDetails['label'] }}</span>
                                    </td>
                                    <td>{{ $subscription->trial_active ? $subscription->trial_start_date : $subscription->start_date }}</td>
                                    <td>{{ $subscription->trial_active ? $subscription->trial_end_date : $subscription->end_date }}</td>
                                    <td>{{ $subscription->next_billing_date }}</td>
                                    <td>{{ getConstantLabel('billing_cycle', $subscription->billing_cycle) }}</td>
                                    <td><a href="{{ route('ch-admin.subscription.edit', [$subscription->id]) }}" class="btn btn-sm btn-primary"><i class="ti-eye"></i></a></td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="9" class="text-center">No Subscriptions found.</td>
                                </tr>
                            @endforelse
                            </tbody>
                        </table>

                        @if($subscriptions->hasPages())
                            <div class="mt-4">
                                {{ $subscriptions->links() }}
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
