@extends('layout.app')
@section('content')
    <div class="card">
        <div class="card-body">
            <form action="{{ route('permissions.update', $permission->id) }}" method="POST">
                @method('put')
                @csrf
                <input type="hidden" name="status" value="{{ $permission->status ?? 'enable' }}">

                <div class="row">
                    <div class="form-group col-md-4">
                        <label for="name" class="block text-sm font-medium text-gray-700">
                            Permission Name*
                        </label>
                        <div class="mt-1 flex rounded-md shadow-sm">
                            <input type="hidden" value="{{ $permission->id }}">
                            <input type="text" name="name" id="name" value="{{ $permission->name }}" class="form-control"
                                placeholder="Permission Name" autocomplete="off">
                        </div>
                        @error('name')
                            <div class="text-red-500 mt-2 text-sm">
                                {{ $message }}
                            </div>
                        @enderror
                    </div>

                    <div class="form-group col-md-4 d-none">
                        <label for="status" class="block text-sm font-medium text-gray-700">
                            Permission Status*
                        </label>

                        <select id="status" class="select2 form-control">
                            <option value="">Select Status</option>
                            <option value="enable" @if ($permission->status == 'enable') {{ 'selected' }} @endif>Enable
                            </option>
                            <option value="disable" @if ($permission->status == 'disable') {{ 'selected' }} @endif>disable
                            </option>
                        </select>

                        @error('status')
                            <div class="text-red-500 mt-2 text-sm">
                                {{ $message }}
                            </div>
                        @enderror
                    </div>

                    <div class="form-group col-md-4">
                        <label for="group" class="block text-sm font-medium text-gray-700">
                            Group
                        </label>
                        <select name="group" id="group" class="form-control">
                            <option value="">— Default (from name) —</option>
                            @foreach($groups ?? [] as $group)
                            <option value="{{ $group }}" @if(($permission->group ?: \App\Services\PermissionGroupHelper::getGroupName($permission->name)) === $group) selected @endif>{{ $group }}</option>
                            @endforeach
                        </select>
                        <div class="mt-2">
                            <label for="new_group" class="text-muted small">Or write new group name:</label>
                            <input type="text" id="new_group" class="form-control form-control-sm mt-1" placeholder="Type new group name..."
                                autocomplete="off">
                        </div>
                        <small class="text-muted d-block mt-1">Select above or type a new group name in the box.</small>
                        @error('group')
                            <div class="text-red-500 mt-2 text-sm">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                <div class="form-group col-md-10">
                    <a href="{{ route('permissions.index') }}" class="btn-black "> <i class="fa fa-arrow-left"></i>
                        Back to Permission List</a>

                    <button type="submit" class="btn btn-primary float-right">
                        Save
                    </button>
                </div>
            </form>
        </div>
    </div>
@endsection

@push('js')
<script>
$(function() {
    var $group = $('#group');
    $group.select2({
        placeholder: '— Default (from name) —',
        allowClear: true,
        tags: true,
        width: '100%'
    });
    // When user writes in "new group" box, set that as the selected group (so it submits)
    $('#new_group').on('input change', function() {
        var val = $(this).val().trim();
        if (val) {
            if ($group.find('option[value="' + val.replace(/"/g, '&quot;') + '"]').length === 0) {
                var opt = new Option(val, val, true, true);
                $group.append(opt);
            }
            $group.val(val).trigger('change');
        } else {
            $group.val(null).trigger('change');
        }
    });
    // When user changes dropdown, clear the "new group" box so it's not confusing
    $group.on('change', function() {
        var sel = $(this).val();
        if ($('#new_group').val().trim() !== (sel || '')) {
            $('#new_group').val(sel || '');
        }
    });
});
</script>
@endpush
