@extends('layout')

@section('title', __('store.clientarea_nav_services'))

@push('styles')
    @vite('resources/css/store/tables.css')
@endpush

@section('content')
<div class="container py-5">
    <div class="row">
        <div class="col-md-12">
            @php
                $isSwitched = session('active_client_id') && session('active_client_id') != auth('client')->id();
            @endphp
            <x-clientarea.page-header
                icon="fas fa-server"
                :title="__('store.clientarea_nav_services')"
                :subtitle="__('store.clientarea_services_subtitle')"
            >
                @if(!$isSwitched)
                    <a href="{{ route('store.products.index') }}" class="btn btn-primary">
                        <i class="fas fa-plus me-2"></i>{{ __('store.clientarea_nav_order_new') }}
                    </a>
                @endif
            </x-clientarea.page-header>


            @if($services->count() > 0)
                @php
                    $statusColors = [
                        'active' => 'success',
                        'pending' => 'warning',
                        'suspended' => 'danger',
                        'terminated' => 'secondary',
                        'cancelled' => 'secondary',
                        'fraud' => 'dark',
                        'completed' => 'info',
                        'expired' => 'secondary',
                    ];
                @endphp
                <div class="card">
                    <div class="card-body">
                        <table id="servicesTable" class="table table-hover mb-0">
                            <thead>
                                <tr>
                                    <th>{{ __('store.service_id_column') }}</th>
                                    <th>{{ __('store.service_name') }}</th>
                                    <th class="d-none d-md-table-cell">{{ __('store.product_group') }}</th>
                                    <th class="d-none d-lg-table-cell">{{ __('store.service_domain_column') }}</th>
                                    <th class="d-none d-xl-table-cell">{{ __('store.service_payment_period') }}</th>
                                    <th class="d-none d-xl-table-cell">{{ __('store.setup_date') }}</th>
                                    <th class="d-none d-md-table-cell">{{ __('store.service_next_due_date') }}</th>
                                    <th class="d-none d-md-table-cell">{{ __('store.service_days_remaining') }}</th>
                                    <th>{{ __('store.status') }}</th>
                                    <th class="text-end">{{ __('store.management') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                    @foreach($services as $service)
                                        @php
                                            $cycleKey = $service->billingcycle ? 'store.service_billing_cycle_' . strtolower(str_replace('-', '_', $service->billingcycle)) : null;
                                            $statusKey = 'store.service_status_' . strtolower($service->status);
                                            $nextDate = $service->next_due_date ?? $service->expire_date;
                                            $dueDate = $nextDate ? \Carbon\Carbon::parse($nextDate) : null;
                                            $daysRemaining = null;
                                            $isExpired = false;
                                            if ($dueDate) {
                                                $isExpired = $dueDate->isPast();
                                                $daysRemaining = $isExpired ? 0 : (int) floor(now()->diffInDays($dueDate, false));
                                            }
                                        @endphp
                                        <tr>
                                            <td class="fw-semibold text-muted">#{{ $service->id }}</td>
                                            <td>
                                                <strong>{{ $service->product->name ?? __('store.unknown_product') }}</strong>
                                            </td>
                                            <td class="d-none d-md-table-cell">
                                                <span class="badge bg-primary">
                                                    {{ $service->product->group->name ?? __('store.ungrouped') }}
                                                </span>
                                            </td>
                                            <td class="d-none d-lg-table-cell">
                                                @if($service->domain)
                                                    <span>{{ $service->domain }}</span>
                                                @else
                                                    <span class="text-muted">&mdash;</span>
                                                @endif
                                            </td>
                                            <td class="d-none d-xl-table-cell">
                                                @if($cycleKey && __($cycleKey) !== $cycleKey)
                                                    <strong>{{ __($cycleKey) }}</strong>
                                                @elseif($service->billingcycle)
                                                    <strong>{{ ucfirst(str_replace('_', ' ', $service->billingcycle)) }}</strong>
                                                @elseif($service->quantity > 1)
                                                    <strong>{{ $service->quantity }}x</strong>
                                                @else
                                                    <span class="text-muted">{{ __('store.standard_service') }}</span>
                                                @endif
                                            </td>
                                            <td class="d-none d-xl-table-cell" data-order="{{ $service->activation_date ? \Carbon\Carbon::parse($service->activation_date)->timestamp : ($service->create_date ? \Carbon\Carbon::parse($service->create_date)->timestamp : 0) }}">
                                                @if($service->create_date || $service->activation_date)
                                                    <span>{{ \Carbon\Carbon::parse($service->activation_date ?: $service->create_date)->format($dateFormat) }}</span>
                                                @else
                                                    <span class="text-muted">{{ __('store.not_activated') }}</span>
                                                @endif
                                            </td>
                                            <td class="d-none d-md-table-cell" data-order="{{ $dueDate ? $dueDate->timestamp : 0 }}">
                                                @if($dueDate)
                                                    <span>{{ $dueDate->format($dateFormat) }}</span>
                                                @else
                                                    <span class="text-muted">{{ __('store.no_expiry') }}</span>
                                                @endif
                                            </td>
                                            <td class="d-none d-md-table-cell" data-order="{{ $daysRemaining ?? 999999 }}">
                                                @if($dueDate)
                                                    @if($isExpired)
                                                        <span class="text-danger">{{ __('store.service_days_remaining_expired') }}</span>
                                                    @else
                                                        <span class="@if($daysRemaining <= 7) text-danger @elseif($daysRemaining <= 30) text-warning @endif">
                                                            {{ __('store.service_days_remaining_value', ['days' => $daysRemaining]) }}
                                                        </span>
                                                    @endif
                                                @else
                                                    <span class="text-muted">{{ __('store.service_days_remaining_unlimited') }}</span>
                                                @endif
                                            </td>
                                            <td>
                                                <span class="badge bg-{{ $statusColors[$service->status] ?? 'secondary' }}">
                                                    {{ __($statusKey) !== $statusKey ? __($statusKey) : ucfirst($service->status) }}
                                                </span>
                                                @if($service->is_trial && $service->trial_ends_at)
                                                    @php
                                                        $trialEndsAt = \Carbon\Carbon::parse($service->trial_ends_at);
                                                        $trialDaysLeft = (int) ceil(now()->diffInDays($trialEndsAt, false));
                                                    @endphp
                                                    @if($trialDaysLeft > 0)
                                                        <span class="badge bg-info ms-1">{{ __('store.service_trial_badge', ['days' => $trialDaysLeft]) }}</span>
                                                    @else
                                                        <span class="badge bg-warning text-dark ms-1">{{ __('store.service_trial_badge_ending') }}</span>
                                                    @endif
                                                @endif
                                            </td>
                                            <td class="text-end">
                                                <div class="dropdown">
                                                    <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown">
                                                        {{ __('store.management') }}
                                                    </button>
                                                    <ul class="dropdown-menu dropdown-menu-end">
                                                        <li><a class="dropdown-item" href="{{ route('store.auth.services.details', $service->id) }}"><i class="fas fa-eye me-2"></i>{{ __('store.view_details') }}</a></li>
                                                        @if($service->status === 'active' && $service->domain)
                                                            <li><a class="dropdown-item" href="{{ route('store.auth.domains.details', $service->domain) }}"><i class="fas fa-globe me-2"></i>{{ __('store.manage_domain') }}</a></li>
                                                        @endif
                                                        <li><a class="dropdown-item" href="{{ route('store.auth.invoices') }}?service={{ $service->id }}"><i class="fas fa-file-invoice me-2"></i>{{ __('store.view_invoices') }}</a></li>
                                                        @if($service->auto_renew)
                                                            <li><a class="dropdown-item text-warning" href="#" onclick="toggleAutoRenew({{ $service->id }}, false); return false;"><i class="fas fa-sync-alt me-2"></i>{{ __('store.disable_auto_renew') }}</a></li>
                                                        @else
                                                            <li><a class="dropdown-item text-success" href="#" onclick="toggleAutoRenew({{ $service->id }}, true); return false;"><i class="fas fa-sync-alt me-2"></i>{{ __('store.enable_auto_renew') }}</a></li>
                                                        @endif
                                                    </ul>
                                                </div>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                    </div>
                </div>
            @else
                <div class="card">
                    <div class="card-body text-center py-5">
                        <i class="fas fa-server fa-3x text-muted mb-3"></i>
                        <h5>{{ __('store.products_empty') }}</h5>
                        <p class="text-muted">{{ __('store.clientarea_no_services') }}</p>
                        @if(!$isSwitched)
                        <a href="{{ route('store.products.index') }}" class="btn btn-primary">
                            {{ __('store.clientarea_nav_order_new') }}
                        </a>
                        @endif
                    </div>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
// Translations
window.translations = window.translations || {};
window.translations.confirmEnableAutoRenew = '{{ __("store.confirm_enable_auto_renew") }}';
window.translations.confirmDisableAutoRenew = '{{ __("store.confirm_disable_auto_renew") }}';
window.translations.errorOccurred = '{{ __("store.error_occurred") }}';

// DataTables translations
window.dataTablesTranslations = {
    "sEmptyTable": "{{ __('store.datatable_empty_table') }}",
    "sInfo": "{{ __('store.datatable_info') }}",
    "sInfoEmpty": "{{ __('store.datatable_info_empty') }}",
    "sInfoFiltered": "{{ __('store.datatable_info_filtered') }}",
    "sLengthMenu": "{{ __('store.datatable_length_menu') }}",
    "sLoadingRecords": "{{ __('store.datatable_loading') }}",
    "sProcessing": "{{ __('store.datatable_processing') }}",
    "sSearch": "{{ __('store.datatable_search') }}",
    "sZeroRecords": "{{ __('store.datatable_zero_records') }}",
    "oPaginate": {
        "sFirst": "{{ __('store.datatable_first') }}",
        "sLast": "{{ __('store.datatable_last') }}",
        "sNext": "{{ __('store.datatable_next') }}",
        "sPrevious": "{{ __('store.datatable_previous') }}"
    },
    "all": "{{ __('store.datatable_all') }}"
};

window.csrfToken = '{{ csrf_token() }}';
</script>

@vite('resources/js/store/clientarea-services.js')
@endpush