@extends('layouts.app')

@section('title', 'Plans')
@section('page-title', 'Plans')
@section('page-subtitle', 'Create, edit, disable, and monitor tenant subscription packages')

@section('content')
<div class="row g-3 mb-4">
    <div class="col-md-4">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body">
                <div class="text-muted small">Active plans</div>
                <div class="h3 mb-0">{{ $activePlans }}</div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body">
                <div class="text-muted small">Inactive plans</div>
                <div class="h3 mb-0">{{ $inactivePlans }}</div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body">
                <div class="text-muted small">Tenants on plans</div>
                <div class="h3 mb-0">{{ $assignedTenants }}</div>
            </div>
        </div>
    </div>
</div>

<div class="row g-4">
    <div class="col-lg-4">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <h2 class="h5">Create subscription plan</h2>
                <form method="POST" action="{{ route('super.plans.store') }}" class="vstack gap-2">
                    @csrf
                    @include('super.plans.partials.form', ['plan' => null])
                    <button class="btn btn-primary">Create plan</button>
                </form>
            </div>
        </div>
    </div>
    <div class="col-lg-8">
        <div class="d-flex justify-content-between align-items-center gap-2 mb-3">
            <div>
                <h2 class="h5 mb-1">Manage plans</h2>
                <div class="small text-muted">Plans already assigned to tenants can be disabled, but not deleted.</div>
            </div>
            <a href="{{ route('super.billing.index') }}" class="btn btn-sm btn-outline-secondary">
                <i class="bi bi-receipt me-1"></i> Billing
            </a>
        </div>

        @forelse($plans as $plan)
            <div class="card border-0 shadow-sm mb-3">
                <div class="card-header bg-white d-flex flex-column flex-md-row justify-content-between gap-2">
                    <div>
                        <div class="fw-semibold">{{ $plan->name }}</div>
                        <div class="small text-muted">{{ $plan->tenants_count }} tenant{{ $plan->tenants_count === 1 ? '' : 's' }} assigned</div>
                    </div>
                    <div class="d-flex flex-wrap gap-2 align-items-center">
                        <span class="badge {{ $plan->is_active ? 'text-bg-success' : 'text-bg-secondary' }}">
                            {{ $plan->is_active ? 'Active' : 'Inactive' }}
                        </span>
                        <span class="badge text-bg-light border">Monthly {{ number_format($plan->price_monthly, 2) }}</span>
                        <span class="badge text-bg-light border">Yearly {{ number_format($plan->price_yearly, 2) }}</span>
                    </div>
                </div>
                <div class="card-body">
                    <form method="POST" action="{{ route('super.plans.update', $plan) }}" class="row g-2 align-items-end">
                        @csrf @method('PATCH')
                        @include('super.plans.partials.form', ['plan' => $plan])
                        <div class="col-12 d-flex flex-wrap gap-2 align-items-center">
                            <button class="btn btn-outline-primary btn-sm">
                                <i class="bi bi-save me-1"></i> Save changes
                            </button>
                            @if($plan->tenants_count === 0)
                                <button form="delete-plan-{{ $plan->id }}" class="btn btn-outline-danger btn-sm">
                                    <i class="bi bi-trash me-1"></i> Delete
                                </button>
                            @else
                                <span class="small text-muted">Disable this plan to stop new tenants from selecting it.</span>
                            @endif
                        </div>
                    </form>

                    @if($plan->tenants_count === 0)
                        <form id="delete-plan-{{ $plan->id }}" method="POST" action="{{ route('super.plans.destroy', $plan) }}" class="d-none">
                            @csrf @method('DELETE')
                        </form>
                    @endif
                </div>
            </div>
        @empty
            <div class="card border-0 shadow-sm">
                <div class="card-body text-center text-muted py-5">No plans have been created yet.</div>
            </div>
        @endforelse
    </div>
</div>
@endsection
