@extends('admin.layouts.app')

@section('title', __('admin/billing-rules.title'))

@section('content')
<div class="container-fluid px-4">
    <x-admin.page-header
        :title="__('admin/billing-rules.title')"
        :subtitle="__('admin/billing-rules.subtitle')"
        icon="fas fa-filter"
        :buttons="[
            ['label' => __('admin/billing-rules.create'), 'route' => route('admin.billing.rules.create'),
             'icon' => 'fas fa-plus', 'color' => 'primary', 'permission' => 'manage billing rules'],
        ]"
    />

    <x-admin.alert />

    @can('manage settings')
    <x-admin.card class="mb-3">
        <button class="btn btn-link p-0 mb-2 d-flex align-items-center gap-2 text-decoration-none" type="button" data-bs-toggle="collapse" data-bs-target="#billingRulesSettings" aria-expanded="false">
            <i class="fas fa-cog"></i>
            <strong>{{ __('admin/billing-rules.settings_title') }}</strong>
            <i class="fas fa-chevron-down small ms-auto"></i>
        </button>
        <div class="collapse" id="billingRulesSettings">
            <form method="POST" action="{{ route('admin.billing.rules.settings.update') }}" class="pt-2">
                @csrf
                <div class="row g-3">
                    <div class="col-md-6">
                        <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" id="billing_enable_automatic" name="billing_enable_automatic" value="1" {{ ($settings['billing_enable_automatic'] ?? false) ? 'checked' : '' }}>
                            <label class="form-check-label" for="billing_enable_automatic">{{ __('admin/billing-rules.settings_enable_automatic') }}</label>
                        </div>
                        <div class="form-text small">{{ __('admin/billing-rules.settings_enable_automatic_help') }}</div>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">{{ __('admin/billing-rules.settings_default_module') }}</label>
                        <select class="form-select" name="billing_default_module">
                            @foreach ($modules as $module)
                                <option value="{{ $module->module_key }}" @selected(($settings['billing_default_module'] ?? 'gib') === $module->module_key)>{{ $module->display_name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">{{ __('admin/billing-rules.settings_auto_process_delay') }}</label>
                        <div class="input-group">
                            <input type="number" class="form-control" name="billing_auto_process_delay" value="{{ $settings['billing_auto_process_delay'] ?? 5 }}" min="1" max="60">
                            <span class="input-group-text">{{ __('admin/settings.minutes') }}</span>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-check form-switch mt-4">
                            <input class="form-check-input" type="checkbox" id="billing_retry_failed_jobs" name="billing_retry_failed_jobs" value="1" {{ ($settings['billing_retry_failed_jobs'] ?? true) ? 'checked' : '' }}>
                            <label class="form-check-label" for="billing_retry_failed_jobs">{{ __('admin/billing-rules.settings_retry_failed_jobs') }}</label>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">{{ __('admin/billing-rules.settings_max_retry_attempts') }}</label>
                        <input type="number" class="form-control" name="billing_max_retry_attempts" value="{{ $settings['billing_max_retry_attempts'] ?? 3 }}" min="1" max="10">
                    </div>
                    <div class="col-md-6">
                        <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" id="billing_notify_on_failure" name="billing_notify_on_failure" value="1" {{ ($settings['billing_notify_on_failure'] ?? true) ? 'checked' : '' }}>
                            <label class="form-check-label" for="billing_notify_on_failure">{{ __('admin/billing-rules.settings_notify_on_failure') }}</label>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">{{ __('admin/billing-rules.settings_failure_notification_email') }}</label>
                        <input type="email" class="form-control" name="billing_failure_notification_email" value="{{ $settings['billing_failure_notification_email'] ?? '' }}" placeholder="admin@example.com">
                    </div>
                </div>
                <div class="text-end mt-3">
                    <button type="submit" class="btn btn-primary btn-sm">
                        <i class="fas fa-save me-1"></i>{{ __('admin/common.save') }}
                    </button>
                </div>
            </form>
        </div>
    </x-admin.card>
    @endcan

    <x-admin.card>
        @if($rules->isEmpty())
            <div class="text-center py-5 text-muted">
                <i class="fas fa-filter fa-3x mb-3 opacity-25"></i>
                <p>{{ __('admin/billing-rules.no_rules') }}</p>
            </div>
        @else
            <div class="table-responsive">
                <table class="table table-hover align-middle" id="rulesTable">
                    <thead>
                        <tr>
                            <th style="width: 30px;"></th>
                            <th>{{ __('admin/billing-rules.fields_name') }}</th>
                            <th>{{ __('admin/billing-rules.fields_priority') }}</th>
                            <th>{{ __('admin/billing-rules.fields_events') }}</th>
                            <th>{{ __('admin/billing-rules.fields_action') }}</th>
                            <th>{{ __('admin/billing-rules.fields_module') }}</th>
                            <th>{{ __('admin/billing-rules.fields_delay_days') }}</th>
                            <th>{{ __('admin/billing-rules.fields_active') }}</th>
                            <th class="text-end"></th>
                        </tr>
                    </thead>
                    <tbody id="rulesTbody">
                        @foreach($rules as $rule)
                            <tr data-id="{{ $rule->id }}">
                                <td><span class="text-muted" style="cursor: grab;">⠿</span></td>
                                <td>
                                    <strong>{{ $rule->name }}</strong>
                                    @if($rule->is_fallback)
                                        <span class="badge bg-info ms-1">{{ __('admin/billing-rules.fallback_badge') }}</span>
                                    @endif
                                </td>
                                <td>{{ $rule->priority }}</td>
                                <td>
                                    @foreach($rule->events as $event)
                                        <span class="badge bg-secondary">{{ __('admin/billing-rules.events_'.$event) }}</span>
                                    @endforeach
                                </td>
                                <td>
                                    @if($rule->action === 'dispatch')
                                        <span class="badge bg-success">{{ __('admin/billing-rules.actions_dispatch') }}</span>
                                    @else
                                        <span class="badge bg-warning">{{ __('admin/billing-rules.actions_skip') }}</span>
                                    @endif
                                </td>
                                <td>{{ $rule->billingModule?->display_name ?? '—' }}</td>
                                <td>{{ $rule->dispatch_delay_days }}</td>
                                <td>
                                    @if($rule->is_active)
                                        <span class="badge bg-success">✓</span>
                                    @else
                                        <span class="badge bg-secondary">✗</span>
                                    @endif
                                </td>
                                <td class="text-end">
                                    <a href="{{ route('admin.billing.rules.edit', $rule) }}" class="btn btn-sm btn-outline-primary">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <form action="{{ route('admin.billing.rules.destroy', $rule) }}" method="POST" class="d-inline"
                                          onsubmit="return confirm('{{ __('admin/billing-rules.delete_confirm') }}');">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-sm btn-outline-danger">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            <div class="mt-3">{{ $rules->links() }}</div>
        @endif
    </x-admin.card>
</div>
@endsection
