@extends('admin.layouts.app')
@section('title', 'Configure ' . $videoPlugin->name)
@section('content')
<div class="action-bar">
    <a href="{{ route('admin.video-plugins.index') }}" class="btn btn-dark">&larr; Back</a>
    <span>Configure: <strong>{{ $videoPlugin->name }}</strong></span>
</div>
<div class="card">
    <form action="{{ route('admin.video-plugins.update', $videoPlugin) }}" 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" {{ $videoPlugin->is_active ? 'checked' : '' }}>
                    Active
                </label>
                <small class="text-muted">Enable this 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"
                    {{ !empty($field['required']) ? 'required' : '' }}>{{ old('config.' . $field['key'], $videoPlugin->config[$field['key']] ?? '') }}</textarea>
            @elseif(($field['type'] ?? 'text') === 'select')
                <select name="config[{{ $field['key'] }}]" id="config_{{ $field['key'] }}" class="form-control"
                    {{ !empty($field['required']) ? 'required' : '' }}>
                    @foreach($field['options'] ?? [] as $value => $label)
                    <option value="{{ $value }}" {{ (old('config.' . $field['key'], $videoPlugin->config[$field['key']] ?? '') == $value) ? 'selected' : '' }}>{{ $label }}</option>
                    @endforeach
                </select>
            @elseif(($field['type'] ?? 'text') === 'password')
                <input type="password" name="config[{{ $field['key'] }}]" id="config_{{ $field['key'] }}"
                    class="form-control" value="{{ old('config.' . $field['key'], $videoPlugin->config[$field['key']] ?? '') }}"
                    {{ !empty($field['required']) ? 'required' : '' }}>
                @if($videoPlugin->config[$field['key']] ?? false)
                <small class="text-muted">Leave empty to keep current value</small>
                @endif
            @else
                <input type="text" name="config[{{ $field['key'] }}]" id="config_{{ $field['key'] }}"
                    class="form-control" value="{{ old('config.' . $field['key'], $videoPlugin->config[$field['key']] ?? '') }}"
                    {{ !empty($field['required']) ? 'required' : '' }}>
            @endif
        </div>
        @endforeach
        @else
        <p class="text-muted">This plugin requires no configuration.</p>
        @endif

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