@extends('layout.admin')

@section('title', 'Add Sub Admin')

@section('content')
<div class="container py-4">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4 class="mb-0">Create Sub Admin</h4>
        <a href="{{ route('admin.sub-admins.index') }}" class="btn btn-outline-secondary">Back</a>
    </div>

    @if($errors->any())
        <div class="alert alert-danger">
            <ul class="mb-0 ps-3">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <form method="POST" action="{{ route('admin.sub-admins.store') }}" class="card shadow-sm border-0">
        @csrf
        <div class="card-body p-4">
            <div class="row g-3 mb-3">
                <div class="col-md-6">
                    <label class="form-label">Name</label>
                    <input type="text" name="name" class="form-control" value="{{ old('name') }}" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Email</label>
                    <input type="email" name="email" class="form-control" value="{{ old('email') }}" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Password</label>
                    <input type="password" name="password" class="form-control" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Confirm Password</label>
                    <input type="password" name="password_confirmation" class="form-control" required>
                </div>
            </div>

            @php
                $oldPermissions = old('project_permissions', []);
                if (!is_array($oldPermissions)) {
                    $oldPermissions = [];
                }
            @endphp

            <div class="border rounded p-3">
                <div class="fw-semibold mb-2">Project Permissions (required)</div>
                <div class="text-muted small mb-2">
                    Parent permission দিলে পুরো মডিউল অ্যাক্সেস পাবে, আর submenu permission দিলে শুধু সেই অংশ দেখাবে।
                </div>
                <div class="d-flex flex-wrap gap-2 mb-3">
                    <button type="button" class="btn btn-sm btn-outline-success" id="permSelectAll">
                        ✓ সব সিলেক্ট
                    </button>
                    <button type="button" class="btn btn-sm btn-outline-secondary" id="permUnselectAll">
                        ✕ সব আনসিলেক্ট
                    </button>
                </div>
                <div class="row g-2">
                    @foreach($projects as $project)
                        <div class="col-md-6">
                            <label class="w-100 border rounded p-2 d-flex gap-2 align-items-start">
                                <input
                                    type="checkbox"
                                    name="project_permissions[]"
                                    value="{{ $project['key'] }}"
                                    class="form-check-input mt-1 project-permission-checkbox"
                                    {{ in_array($project['key'], $oldPermissions, true) ? 'checked' : '' }}
                                >
                                <span>
                                    <span class="fw-semibold d-block">{{ $project['label'] }}</span>
                                    <small class="text-muted">{{ $project['description'] }}</small>
                                </span>
                            </label>
                        </div>
                    @endforeach
                </div>
            </div>
        </div>
        <div class="card-footer bg-white d-flex justify-content-end gap-2">
            <a href="{{ route('admin.sub-admins.index') }}" class="btn btn-light border">Cancel</a>
            <button type="submit" class="btn btn-primary">Create Sub Admin</button>
        </div>
    </form>
</div>

<script>
    (function () {
        const selectAllBtn = document.getElementById('permSelectAll');
        const unselectAllBtn = document.getElementById('permUnselectAll');
        const boxes = document.querySelectorAll('.project-permission-checkbox');

        const setAll = (checked) => {
            boxes.forEach((box) => {
                box.checked = checked;
            });
        };

        selectAllBtn?.addEventListener('click', () => setAll(true));
        unselectAllBtn?.addEventListener('click', () => setAll(false));
    })();
</script>
@endsection
