﻿@extends('layouts.app')

@section('title', \App\Helpers\TranslationHelper::transFromDatabase('Subscriptions Management', __('Subscriptions Management')))

@section('content')
<div class="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 py-8 px-4 md:px-8">
    <div class="max-w-7xl mx-auto">
        <div class="mb-8">
            <div class="flex items-center justify-between mb-6">
                <div>
                    <h1 class="text-3xl font-bold text-gray-900 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Subscriptions Management', __('Subscriptions Management')) }}</h1>
                    <p class="text-gray-600">{{ \App\Helpers\TranslationHelper::transFromDatabase('Manage and track company subscriptions', __('Manage and track company subscriptions')) }}</p>
                </div>
                @permission('subscriptions.create')
                <a href="{{ route('subscriptions.create') }}" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-blue-600 to-blue-700 text-white font-semibold rounded-lg hover:from-blue-700 hover:to-blue-800 transition-all duration-200 shadow-lg hover:shadow-xl transform hover:scale-105">
                    <i class="fas fa-plus ml-2"></i>
                    <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Add New Subscription', __('Add New Subscription')) }}</span>
                </a>
                @endpermission
            </div>

            @if(session('success'))
            <div class="bg-green-50 border-l-4 border-green-500 p-4 mb-6 rounded-r-lg">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-check-circle text-green-500 text-xl"></i>
                    </div>
                    <div class="mr-3">
                        <p class="text-green-700 font-medium">{{ session('success') }}</p>
                    </div>
                </div>
            </div>
            @endif

            @if(session('error'))
            <div class="bg-red-50 border-l-4 border-red-500 p-4 mb-6 rounded-r-lg">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-exclamation-circle text-red-500 text-xl"></i>
                    </div>
                    <div class="mr-3">
                        <p class="text-red-700 font-medium">{{ session('error') }}</p>
                    </div>
                </div>
            </div>
            @endif
        </div>

        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gradient-to-r from-gray-800 to-gray-900 text-white">
                        <tr>
                            <th class="px-6 py-4 text-right font-semibold text-sm">#</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Company', __('Company')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Plan', __('Plan')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Status', __('Status')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('End Date', __('End Date')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Days Remaining', __('Days Remaining')) }}</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Actions', __('Actions')) }}</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($subscriptions as $subscription)
                        <tr class="hover:bg-blue-50 transition-colors duration-150 border-b border-gray-100">
                            <td class="px-6 py-4 text-gray-600 font-medium">{{ $loop->iteration }}</td>
                            <td class="px-6 py-4">
                                <div class="font-semibold text-gray-900">{{ $subscription->company->name }}</div>
                                <div class="text-sm text-gray-500">{{ $subscription->company->email }}</div>
                            </td>
                            <td class="px-6 py-4">
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-100 text-blue-700">
                                    {{ $subscription->plan->name }}
                                </span>
                            </td>
                            <td class="px-6 py-4">
                                @if ($subscription->status === 'active')
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-700">
                                        <span class="w-2 h-2 bg-green-500 rounded-full ml-2"></span>
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Active', __('Active')) }}
                                    </span>
                                @elseif($subscription->status === 'trial')
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-yellow-100 text-yellow-700">
                                        <span class="w-2 h-2 bg-yellow-500 rounded-full ml-2"></span>
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Trial', __('Trial')) }}
                                    </span>
                                @elseif($subscription->status === 'suspended')
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-orange-100 text-orange-700">
                                        <span class="w-2 h-2 bg-orange-500 rounded-full ml-2"></span>
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Suspended', __('Suspended')) }}
                                    </span>
                                @else
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-700">
                                        <span class="w-2 h-2 bg-red-500 rounded-full ml-2"></span>
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Expired', __('Expired')) }}
                                    </span>
                                @endif
                            </td>
                            <td class="px-6 py-4 text-gray-900">
                                {{ $subscription->subscription_end_date->format('Y-m-d') }}
                            </td>
                            <td class="px-6 py-4">
                                @if ($subscription->isExpired())
                                    <span class="text-red-600 font-semibold">{{ \App\Helpers\TranslationHelper::transFromDatabase('Expired', __('Expired')) }}</span>
                                @else
                                    <span class="text-gray-900 font-semibold">{{ $subscription->daysUntilExpiration() }} {{ \App\Helpers\TranslationHelper::transFromDatabase('day', __('day')) }}</span>
                                @endif
                            </td>
                            <td class="px-6 py-4">
                                <div class="flex items-center justify-center gap-1">
                                    @permission('subscriptions.view')
                                    <a href="{{ route('subscriptions.show', $subscription) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-blue-100 text-blue-600 hover:bg-blue-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('View Details', __('View Details')) }}">
                                        <i class="fas fa-eye mr-1"></i>
                                        <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('View', __('View')) }}</span>
                                    </a>
                                    @endpermission

                                    @if($subscription->status === 'trial')
                                    @permission('subscriptions.edit')
                                    <form action="{{ route('subscriptions.activate', $subscription) }}" method="POST" class="inline">
                                        @csrf
                                        <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-green-100 text-green-600 hover:bg-green-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Activate', __('Activate')) }}">
                                            <i class="fas fa-check mr-1"></i>
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Activate', __('Activate')) }}</span>
                                        </button>
                                    </form>
                                    @endpermission
                                    @endif

                                    @if($subscription->status === 'active' || $subscription->status === 'expired')
                                    @permission('subscriptions.edit')
                                    <form action="{{ route('subscriptions.renew', $subscription) }}" method="POST" class="inline">
                                        @csrf
                                        <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-indigo-100 text-indigo-600 hover:bg-indigo-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Renew', __('Renew')) }}">
                                            <i class="fas fa-sync-alt mr-1"></i>
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Renew', __('Renew')) }}</span>
                                        </button>
                                    </form>
                                    @endpermission
                                    @endif

                                    @permission('subscriptions.edit')
                                    <a href="{{ route('subscriptions.edit', $subscription) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-amber-100 text-amber-600 hover:bg-amber-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Edit', __('Edit')) }}">
                                        <i class="fas fa-edit mr-1"></i>
                                        <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Edit', __('Edit')) }}</span>
                                    </a>
                                    @endpermission

                                    @permission('subscriptions.delete')
                                    <form action="{{ route('subscriptions.destroy', $subscription) }}" method="POST" class="inline">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-red-100 text-red-600 hover:bg-red-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Delete', __('Delete')) }}" onclick="return confirm('{{ \App\Helpers\TranslationHelper::transFromDatabase('Are you sure you want to delete this subscription?', __('Are you sure you want to delete this subscription?')) }}')">
                                            <i class="fas fa-trash mr-1"></i>
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Delete', __('Delete')) }}</span>
                                        </button>
                                    </form>
                                    @endpermission
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="7" class="px-6 py-12 text-center">
                                <div class="flex flex-col items-center justify-center">
                                    <i class="fas fa-inbox text-gray-400 text-4xl mb-3"></i>
                                    <p class="text-gray-600 font-medium">{{ \App\Helpers\TranslationHelper::transFromDatabase('No subscriptions registered currently', __('No subscriptions registered currently')) }}</p>
                                    <p class="text-gray-500 text-sm mt-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Start by adding a new subscription', __('Start by adding a new subscription')) }}</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection