@extends('admin.layouts.app')
@section('title', 'Configure ' . $whatsappPlugin->name)
@section('content')
<div class="action-bar">
    <a href="{{ route('admin.whatsapp-plugins.index') }}" class="btn btn-dark">&larr; Back</a>
    <span>Configure: <strong>{{ $whatsappPlugin->name }}</strong></span>
</div>
<div class="card">
    <form action="{{ route('admin.whatsapp-plugins.update', $whatsappPlugin) }}" method="POST">
        @csrf @method('PUT')

        <div class="form-group">
            <div style="display:flex;align-items:center;gap:10px;margin-bottom:15px">
                <label style="margin:0">
                    <input type="checkbox" name="is_active" value="1" {{ $whatsappPlugin->is_active ? 'checked' : '' }}>
                    Active
                </label>
                <small class="text-muted">Enable this chat plugin on the frontend</small>
            </div>
        </div>

        @if(count($fields))
        <h3 style="margin-bottom:15px;color:var(--text-muted);font-size:0.9rem;text-transform:uppercase;letter-spacing:1px">
            Plugin Configuration
        </h3>

        @foreach($fields as $field)
        <div class="form-group">
            <label for="config_{{ $field['key'] }}">
                {{ $field['label'] }}
                @if(!empty($field['required']))<span style="color:#e74c3c">*</span>@endif
            </label>

            @if(($field['type'] ?? 'text') === 'textarea')
                <textarea name="config[{{ $field['key'] }}]" id="config_{{ $field['key'] }}"
                    class="form-control" rows="3"
                    placeholder="{{ $field['placeholder'] ?? '' }}"
                    {{ !empty($field['required']) ? 'required' : '' }}>{{ old('config.' . $field['key'], $whatsappPlugin->config[$field['key']] ?? '') }}</textarea>
            @else
                <input type="text" name="config[{{ $field['key'] }}]" id="config_{{ $field['key'] }}"
                    class="form-control"
                    placeholder="{{ $field['placeholder'] ?? '' }}"
                    value="{{ old('config.' . $field['key'], $whatsappPlugin->config[$field['key']] ?? '') }}"
                    {{ !empty($field['required']) ? 'required' : '' }}>
            @endif

            @error('config.' . $field['key'])
                <div style="color:#e74c3c;font-size:0.85rem;margin-top:4px">{{ $message }}</div>
            @enderror
        </div>
        @endforeach
        @else
        <p class="text-muted">This plugin requires no configuration.</p>
        @endif

        @if($whatsappPlugin->is_configured)
        <div style="margin-top:15px;padding:12px;background:#1a2a1a;border:1px solid #2a4a2a;border-radius:8px">
            <small style="color:#6fcf97">
                Chat URL: <a href="{{ $whatsappPlugin->getChatUrl() }}" target="_blank" style="color:#6fcf97;text-decoration:underline">{{ $whatsappPlugin->getChatUrl() }}</a>
            </small>
        </div>
        @endif

        <div class="form-group" style="margin-top:20px">
            <button type="submit" class="btn btn-primary">Save Configuration</button>
        </div>
    </form>
</div>
@endsection
