@php use Illuminate\Support\Str; @endphp
@extends('admin.layouts.app')

@section('title', 'Modules')

@section('content')
<div class="action-bar">
    <h2 style="color:#fff;font-size:1.3rem;font-weight:700">Module Manager</h2>
    <div class="inline-flex">
        <a href="{{ route('admin.modules.refresh') }}" class="btn btn-primary btn-sm"
           onclick="return confirm('Refresh will reset all module settings. Continue?')">
            &#8635; Refresh Modules
        </a>
    </div>
</div>

<p style="color:#9a9ab0;margin-bottom:20px;font-size:0.9rem">
    Manage all admin modules. Install, uninstall, or toggle modules on/off.
    Modules from mecaprograms.com are auto-detected and can be installed with one click.
</p>

@foreach($groups as $group)
<div class="card" style="margin-bottom:20px">
    <div class="card-header">
        <span style="font-size:1.2rem;margin-right:8px">{!! $group['icon'] !!}</span>
        {{ $group['name'] }}
        <span style="color:#5a5a7a;font-size:0.8rem;font-weight:400;margin-left:10px">
            {{ $group['description'] }}
        </span>
    </div>
    <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(280px,1fr));gap:12px">
        @foreach($group['modules'] as $module)
        <div style="background:#0f0f1a;border:1px solid #2a2a3e;border-radius:10px;padding:14px 16px;display:flex;justify-content:space-between;align-items:center">
            <div style="display:flex;align-items:center;gap:10px">
                <span style="font-size:1.2rem">{!! $module['icon'] !!}</span>
                <div>
                    <div style="color:#e0e0e0;font-weight:600;font-size:0.9rem">{{ $module['name'] }}</div>
                    @if($module['is_installed'] && $module['is_active'] && $module['route'])
                    <a href="{{ route($module['route']) }}" style="color:#5a7af0;font-size:0.75rem;text-decoration:none">
                        &#8594; Manage
                    </a>
                    @else
                    <span style="color:#5a5a7a;font-size:0.75rem">
                        {{ $module['is_installed'] ? ($module['is_active'] ? 'Installed' : 'Inactive') : 'Not installed' }}
                    </span>
                    @endif
                </div>
            </div>
            <div class="inline-flex" style="gap:4px">
                @if(!$module['is_installed'])
                <a href="{{ route('admin.modules.install', $module['code']) }}" class="btn btn-primary btn-sm">Install</a>
                @else
                <a href="{{ route('admin.modules.toggle', $module['code']) }}"
                   class="btn btn-sm {{ $module['is_active'] ? 'btn-warning' : 'btn-primary' }}"
                   style="min-width:80px;text-align:center">
                    {{ $module['is_active'] ? 'Deactivate' : 'Activate' }}
                </a>
                <a href="{{ route('admin.modules.uninstall', $module['code']) }}"
                   class="btn btn-danger btn-sm"
                   onclick="return confirm('Uninstall {{ $module['name'] }}?')">Uninstall</a>
                @endif
            </div>
        </div>
        @endforeach
    </div>
</div>
@endforeach
@endsection
