@extends('layouts/base_view')
@section('title')
<li class="breadcrumb-item">Configurations</li>
<li class="breadcrumb-item active">Roles</li>
@endsection
@section('content')
<!-- Main content -->
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <div class="card card-outline card-warning">
                    <div class="card-header">
                        <h3 class="card-title">Add data Role</h3>
                    </div>
                    <div class="card-body">
                        @if (count($errors) > 0)
                        <div class="alert alert-danger">
                            <ul>
                                @foreach ($errors->all() as $error)
                                <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                        @endif
                        <form method="POST" action="{{ route('roles.store') }}">
                            @csrf
                            <div class="form-group">
                                <label for="name">Role Name</label>
                                <input value="{{ old('name') }}" type="text" class="form-control form-control-sm" name="name" placeholder="Role Name" required>
                            </div>
                            @php
                            // Group permissions berdasarkan prefix
                            $groupedPermissions = [];
                            foreach ($permissions as $permission) {
                            [$group, $action] = explode('.', $permission->name, 2);
                            $groupedPermissions[$group][] = $permission;
                            }

                            // Fungsi pecah array jadi chunk (maksimal 5 item per kolom)
                            function chunkByColumns($items, $size = 5) {
                            return array_chunk($items, $size);
                            }
                            @endphp

                            <div class="mb-2">
                                <input type="checkbox" id="all_permission"> <strong>Select all permission</strong>
                            </div>

                            <div class="row align-items-stretch">
                                @foreach($groupedPermissions as $group => $items)
                                @php $chunks = chunkByColumns($items, 5); @endphp

                                <div class="col-md-3 d-flex"> {{-- 4 kolom --}}
                                    <div class="card card-outline card-warning flex-fill">
                                        <div class="card-header d-flex align-items-center p-2">
                                            <input type="checkbox" class="group-check mr-2" data-group="{{ $group }}">
                                            <span class="folder-icon mr-2" data-group="{{ $group }}" style="cursor: pointer;">📂</span>
                                            <h3 class="card-title mb-0">{{ ucfirst($group) }}</h3>
                                        </div>
                                        <div class="card-body p-2">
                                            @foreach($chunks as $chunk)
                                            <div class="child-row child-{{ $group }}">
                                                @foreach($chunk as $permission)
                                                <div class="form-check mb-1">
                                                    <input class="form-check-input permission permission-{{ $group }}"
                                                        type="checkbox"
                                                        name="permission[{{ $permission->name }}]"
                                                        value="{{ $permission->name }}">
                                                    <label class="form-check-label">
                                                        {{ $permission->name }}
                                                    </label>
                                                </div>
                                                @endforeach
                                            </div>
                                            @endforeach
                                        </div>
                                    </div>
                                </div>
                                @endforeach
                            </div>

                            <script>
                                document.addEventListener('DOMContentLoaded', function() {
                                    // Pilih semua
                                    document.getElementById('all_permission').addEventListener('change', function() {
                                        document.querySelectorAll('input.permission, input.group-check').forEach(cb => {
                                            cb.checked = this.checked;
                                        });
                                    });

                                    // Pilih semua dalam 1 grup
                                    document.querySelectorAll('.group-check').forEach(function(groupCb) {
                                        groupCb.addEventListener('change', function() {
                                            let group = this.dataset.group;
                                            document.querySelectorAll('.permission-' + group).forEach(cb => {
                                                cb.checked = this.checked;
                                            });
                                        });
                                    });

                                    // Expand / Collapse grup
                                    document.querySelectorAll('.folder-icon').forEach(function(icon) {
                                        icon.addEventListener('click', function() {
                                            let group = this.dataset.group;
                                            let rows = document.querySelectorAll('.child-' + group);
                                            let isHidden = rows[0]?.style.display === 'none';
                                            rows.forEach(row => {
                                                row.style.display = isHidden ? 'block' : 'none';
                                            });
                                            this.textContent = isHidden ? '📂' : '📁';
                                        });
                                    });

                                    // Auto-check group checkbox kalau semua child tercentang
                                    document.querySelectorAll('.group-check').forEach(function(groupCb) {
                                        let group = groupCb.dataset.group;
                                        let allChecked = true;
                                        document.querySelectorAll('.permission-' + group).forEach(cb => {
                                            if (!cb.checked) allChecked = false;
                                        });
                                        groupCb.checked = allChecked;
                                    });
                                });
                            </script>
                            <br>
                            <button type="submit" class="btn btn-outline-info btn-sm button-cek-permission" data-permission="roles.store"><i class="fas fa-save"></i> Save</button>
                            <a href="{{ route('roles.index') }}" class="btn btn-sm btn-outline-warning btn-sm"><i class="fas fa-arrow-left"></i> Back</a>
                        </form>
                    </div>
                    <!-- /.card-body -->
                </div>
            </div>
            <!-- /.col -->
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</section>
<script type="text/javascript">
    $(document).ready(function() {
        $('[name="all_permission"]').on('click', function() {

            if ($(this).is(':checked')) {
                $.each($('.permission'), function() {
                    $(this).prop('checked', true);
                });
            } else {
                $.each($('.permission'), function() {
                    $(this).prop('checked', false);
                });
            }

        });
    });
</script>
@endsection