@extends('layouts.admin')

@section('title', 'Promotion Settings')

@section('admin-content')
    <!-- Page Header -->
    <div class="page-header">
        <div>
            <h3 class="page-title">Promotion Settings</h3>
            <p class="text-muted mb-0">Configure global settings for promotions</p>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="bi bi-check-circle me-2"></i> {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif

    <div class="card">
        <div class="card-body">
            <form action="{{ route('admin.promotions.settings.update') }}" method="POST">
                @csrf
                @method('PUT')

                <div class="row g-4">
                    @foreach($settings as $setting)
                        <div class="col-md-6">
                            <div class="p-3 border rounded bg-light h-100">
                                <label class="form-label fw-bold text-dark mb-1">
                                    {{ ucwords(str_replace('_', ' ', $setting->setting_key)) }}
                                </label>
                                <p class="text-muted small mb-3">{{ $setting->description }}</p>

                                @if($setting->setting_type === 'boolean')
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" 
                                               name="settings[{{ $loop->index }}][value]" 
                                               value="true"
                                               {{ $setting->setting_value === 'true' ? 'checked' : '' }}
                                               style="cursor: pointer;">
                                        <label class="form-check-label small text-muted">
                                            {{ $setting->setting_value === 'true' ? 'Enabled' : 'Disabled' }}
                                        </label>
                                        <input type="hidden" name="settings[{{ $loop->index }}][key]" value="{{ $setting->setting_key }}">
                                    </div>
                                @elseif($setting->setting_type === 'integer')
                                    <input type="number" class="form-control" 
                                           name="settings[{{ $loop->index }}][value]" 
                                           value="{{ $setting->setting_value }}" 
                                           min="0">
                                    <input type="hidden" name="settings[{{ $loop->index }}][key]" value="{{ $setting->setting_key }}">
                                @elseif($setting->setting_key === 'manual_payment_instructions')
                                    <textarea class="form-control" rows="4"
                                              name="settings[{{ $loop->index }}][value]">{{ $setting->setting_value }}</textarea>
                                    <input type="hidden" name="settings[{{ $loop->index }}][key]" value="{{ $setting->setting_key }}">
                                @else
                                    <input type="text" class="form-control" 
                                           name="settings[{{ $loop->index }}][value]" 
                                           value="{{ $setting->setting_value }}">
                                    <input type="hidden" name="settings[{{ $loop->index }}][key]" value="{{ $setting->setting_key }}">
                                @endif
                            </div>
                        </div>
                    @endforeach
                </div>

                <div class="mt-4 pt-3 border-top text-end">
                    <button type="submit" class="btn btn-primary-gradient px-4">
                        <i class="bi bi-save me-2"></i> Save Settings
                    </button>
                </div>
            </form>
        </div>
    </div>
@endsection
