@extends('backoffice.layouts.app')

@section('title', 'Yetki Grupları')
@php
   $hiddenPermissionGroupIdsRaw = json_decode(setting('advanced_hidden_permission_group_ids', '[1,9]'), true);
   $hiddenPermissionGroupIds = is_array($hiddenPermissionGroupIdsRaw)
      ? array_values(array_unique(array_map('intval', $hiddenPermissionGroupIdsRaw)))
      : [1, 9];
@endphp

@section('content')

<div class="container-fluid">

   <div class="row mb-4">

      <div class="col-12">

         <div class="d-flex justify-content-between align-items-center">

            <div>

               <h2 class="mb-1"><i class="bi bi-shield-lock me-2"></i>Yetki Grupları</h2>

               <p class="text-muted">Kullanıcı yetki gruplarını yönetin</p>

            </div>

            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createGroupModal">

            <i class="bi bi-plus-circle me-2"></i>Yeni Yetki Grubu

            </button>

         </div>

      </div>

   </div>

   <!-- Table -->

   <div class="card border-0 shadow-sm">

      <div class="card-body p-0">

         <div class="table-responsive">

            <table id="groupsTable" class="table table-hover mb-0">

               <thead class="table-light">

                  <tr>

                     <th>Grup Adı</th>

                     <th>Açıklama</th>

                     <th>Kullanıcı Sayısı</th>

                     <th>Oluşturulma</th>

                     <th width="150" class="text-center">İşlemler</th>

                  </tr>

               </thead>

               <tbody></tbody>

            </table>

         </div>

      </div>

   </div>

</div>

<!-- Create Modal -->

<div class="modal fade" id="createGroupModal" tabindex="-1">

   <div class="modal-dialog modal-lg">

      <div class="modal-content">

         <div class="modal-header">

            <h5 class="modal-title"><i class="bi bi-plus-circle me-2"></i>Yeni Yetki Grubu</h5>

            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>

         </div>

         <form id="createGroupForm">

            <div class="modal-body">

               <div class="mb-3">

                  <label class="form-label">Grup Adı <span class="text-danger">*</span></label>

                  <input type="text" name="name" class="form-control" required>

               </div>

               <div class="mb-3">

                  <label class="form-label">Açıklama</label>

                  <textarea name="description" class="form-control" rows="2"></textarea>

               </div>
               <div class="mb-3">
                  <div class="form-check form-switch">
                     <input class="form-check-input" type="checkbox" id="createBypassIpAuth" name="bypass_ip_auth" value="1">
                     <label class="form-check-label" for="createBypassIpAuth">IP Kisitini Atla</label>
                  </div>
                  <small class="text-muted">Bu gruptaki kullanicilar her IP'den giris yapabilir.</small>
               </div>

               <div class="mb-3">

                  <label class="form-label fw-bold">Yetkiler <span class="text-danger">*</span></label>

                  <div class="table-responsive">

                     <table class="table table-sm table-bordered">

                        <thead class="table-light">

                           <tr>

                              <th>Modül</th>

                              <th width="80" class="text-center">Görüntüle</th>

                              <th width="80" class="text-center">Oluştur</th>

                              <th width="80" class="text-center">Düzenle</th>

                              <th width="80" class="text-center">Sil</th>

                           </tr>

                        </thead>

                        <tbody id="permissionsTable">

                           <tr>

                              <td><strong>Dashboard</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[dashboard][read]" value="1" checked></td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                           </tr>

                           <tr>

                              <td><strong>Yatırımlar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[deposits][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[deposits][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[deposits][update]" value="1"></td>

                              <td class="text-center">-</td>

                           </tr>

                           <tr>

                              <td><strong>Çekimler</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[withdrawals][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[withdrawals][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[withdrawals][update]" value="1"></td>

                              <td class="text-center">-</td>

                           </tr>
                           <tr>

                              <td><strong>İşlem Logları</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[transaction_logs][read]" value="1"></td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                           </tr>

                           <tr>

                              <td><strong>Finans Gruplar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_groups][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_groups][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_groups][update]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_groups][delete]" value="1"></td>

                           </tr>

                           <tr>

                              <td><strong>Banka Hesaplar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[bank_accounts][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[bank_accounts][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[bank_accounts][update]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[bank_accounts][delete]" value="1"></td>

                           </tr>

                           <tr>

                              <td><strong>Kripto Hesablari</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[crypto_accounts][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[crypto_accounts][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[crypto_accounts][update]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[crypto_accounts][delete]" value="1"></td>

                           </tr>

                           <tr>

                              <td><strong>Finans Kullanıcılar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_users][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_users][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_users][update]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_users][delete]" value="1"></td>

                           </tr>

                           <tr>

                              <td><strong>Backoffice Kullanıcılar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[users][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[users][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[users][update]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[users][delete]" value="1"></td>

                           </tr>

                           <tr>

                              <td><strong>Hazır Cevaplar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[canned_responses][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[canned_responses][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[canned_responses][update]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[canned_responses][delete]" value="1"></td>

                           </tr>
                           <tr>
                              <td><strong>Blacklist</strong></td>
                              <td class="text-center"><input type="checkbox" name="permissions[blacklist][read]" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="permissions[blacklist][create]" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="permissions[blacklist][update]" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="permissions[blacklist][delete]" value="1"></td>
                           </tr>

                           <tr>
                              <td><strong>Export to Excel</strong></td>
                              <td class="text-center"><input type="checkbox" name="permissions[export_excel][read]" value="1"></td>
                              <td class="text-center">-</td>
                              <td class="text-center">-</td>
                              <td class="text-center">-</td>
                           </tr>
                           <tr>
                              <td><strong>Siteler</strong></td>
                              <td class="text-center"><input type="checkbox" name="permissions[sites][read]" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="permissions[sites][create]" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="permissions[sites][update]" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="permissions[sites][delete]" value="1"></td>
                           </tr>
                           <tr>
                              <td><strong>Payment Popups</strong></td>
                              <td class="text-center"><input type="checkbox" name="permissions[payment_popups][read]" value="1"></td>
                              <td class="text-center">-</td>
                              <td class="text-center"><input type="checkbox" name="permissions[payment_popups][update]" value="1"></td>
                              <td class="text-center">-</td>
                           </tr>

                           <tr>

                              <td><strong>Yetki Gruplar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[permissions][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[permissions][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[permissions][update]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[permissions][delete]" value="1"></td>

                           </tr>

                           <tr>

                              <td><strong>Sistem Ayarlar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[settings][read]" value="1"></td>

                              <td class="text-center">-</td>

                              <td class="text-center"><input type="checkbox" name="permissions[settings][update]" value="1"></td>

                              <td class="text-center">-</td>

                           </tr>

                           <tr>

                              <td><strong>Raporlar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[reports][read]" value="1"></td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                           </tr>

                           <tr>

                              <td><strong>Aktivite Loglar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[activity_logs][read]" value="1"></td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                           </tr>

                        </tbody>

                     </table>

                  </div>

                  <div class="mt-2">

                     <button type="button" class="btn btn-sm btn-outline-primary" onclick="selectAllPermissions()">

                     <i class="bi bi-check-all"></i> Tümünü Seç

                     </button>

                     <button type="button" class="btn btn-sm btn-outline-secondary" onclick="clearAllPermissions()">

                     <i class="bi bi-x"></i> Tümünü Temizle

                     </button>

                  </div>

               </div>

            </div>

            <div class="modal-footer">

               <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">İptal</button>

               <button type="submit" class="btn btn-primary"><i class="bi bi-check-circle me-2"></i>Oluştur</button>

            </div>

         </form>

      </div>

   </div>

</div>

<!-- Edit Modal -->

<div class="modal fade" id="editGroupModal" tabindex="-1">

   <div class="modal-dialog modal-lg">

      <div class="modal-content">

         <div class="modal-header">

            <h5 class="modal-title"><i class="bi bi-pencil me-2"></i>Yetki Grubunu Düzenle</h5>

            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>

         </div>

         <form id="editGroupForm">

            <input type="hidden" id="editGroupId">

            <div class="modal-body">

               <div class="mb-3">

                  <label class="form-label">Grup Adı</label>

                  <input type="text" name="name" id="editName" class="form-control" required>

               </div>

               <div class="mb-3">

                  <label class="form-label">Açıklama</label>

                  <textarea name="description" id="editDescription" class="form-control" rows="2"></textarea>

               </div>
               <div class="mb-3">
                  <div class="form-check form-switch">
                     <input class="form-check-input" type="checkbox" id="editBypassIpAuth" name="bypass_ip_auth" value="1">
                     <label class="form-check-label" for="editBypassIpAuth">IP Kisitini Atla</label>
                  </div>
                  <small class="text-muted">Bu gruptaki kullanicilar her IP'den giris yapabilir.</small>
               </div>

               <div class="mb-3">

                  <label class="form-label fw-bold">Yetkiler</label>

                  <div class="table-responsive">

                     <table class="table table-sm table-bordered">

                        <thead class="table-light">

                           <tr>

                              <th>Modül</th>

                              <th width="80" class="text-center">Görüntüle</th>

                              <th width="80" class="text-center">Oluştur</th>

                              <th width="80" class="text-center">Düzenle</th>

                              <th width="80" class="text-center">Sil</th>

                           </tr>

                        </thead>

                        <tbody id="editPermissionsTable">

                           <tr>

                              <td><strong>Dashboard</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[dashboard][read]" value="1" checked></td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                           </tr>

                           <tr>

                              <td><strong>Yatırımlar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[deposits][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[deposits][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[deposits][update]" value="1"></td>

                              <td class="text-center">-</td>

                           </tr>

                           <tr>

                              <td><strong>Çekimler</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[withdrawals][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[withdrawals][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[withdrawals][update]" value="1"></td>

                              <td class="text-center">-</td>

                           </tr>
                           <tr>

                              <td><strong>İşlem Logları</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[transaction_logs][read]" value="1"></td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                           </tr>

                           <tr>

                              <td><strong>Finans Gruplar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_groups][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_groups][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_groups][update]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_groups][delete]" value="1"></td>

                           </tr>

                           <tr>

                              <td><strong>Banka Hesaplar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[bank_accounts][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[bank_accounts][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[bank_accounts][update]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[bank_accounts][delete]" value="1"></td>

                           </tr>

                           <tr>

                              <td><strong>Kripto Hesablari</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[crypto_accounts][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[crypto_accounts][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[crypto_accounts][update]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[crypto_accounts][delete]" value="1"></td>

                           </tr>

                           <tr>

                              <td><strong>Finans Kullanıcılar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_users][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_users][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_users][update]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[finance_users][delete]" value="1"></td>

                           </tr>

                           <tr>

                              <td><strong>Backoffice Kullanıcılar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[users][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[users][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[users][update]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[users][delete]" value="1"></td>

                           </tr>

                           <tr>

                              <td><strong>Hazır Cevaplar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[canned_responses][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[canned_responses][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[canned_responses][update]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[canned_responses][delete]" value="1"></td>

                           </tr>
                           <tr>
                              <td><strong>Blacklist</strong></td>
                              <td class="text-center"><input type="checkbox" name="permissions[blacklist][read]" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="permissions[blacklist][create]" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="permissions[blacklist][update]" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="permissions[blacklist][delete]" value="1"></td>
                           </tr>

                           <tr>
                              <td><strong>Export to Excel</strong></td>
                              <td class="text-center"><input type="checkbox" name="permissions[export_excel][read]" value="1"></td>
                              <td class="text-center">-</td>
                              <td class="text-center">-</td>
                              <td class="text-center">-</td>
                           </tr>
                           <tr>
                              <td><strong>Siteler</strong></td>
                              <td class="text-center"><input type="checkbox" name="permissions[sites][read]" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="permissions[sites][create]" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="permissions[sites][update]" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="permissions[sites][delete]" value="1"></td>
                           </tr>
                           <tr>
                              <td><strong>Payment Popups</strong></td>
                              <td class="text-center"><input type="checkbox" name="permissions[payment_popups][read]" value="1"></td>
                              <td class="text-center">-</td>
                              <td class="text-center"><input type="checkbox" name="permissions[payment_popups][update]" value="1"></td>
                              <td class="text-center">-</td>
                           </tr>

                           <tr>

                              <td><strong>Yetki Gruplar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[permissions][read]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[permissions][create]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[permissions][update]" value="1"></td>

                              <td class="text-center"><input type="checkbox" name="permissions[permissions][delete]" value="1"></td>

                           </tr>

                           <tr>

                              <td><strong>Sistem Ayarlar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[settings][read]" value="1"></td>

                              <td class="text-center">-</td>

                              <td class="text-center"><input type="checkbox" name="permissions[settings][update]" value="1"></td>

                              <td class="text-center">-</td>

                           </tr>

                           <tr>

                              <td><strong>Raporlar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[reports][read]" value="1"></td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                           </tr>

                           <tr>

                              <td><strong>Aktivite Loglar</strong></td>

                              <td class="text-center"><input type="checkbox" name="permissions[activity_logs][read]" value="1"></td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                              <td class="text-center">-</td>

                           </tr>

                        </tbody>

                     </table>

                  </div>

                  <div class="mt-2">

                     <button type="button" class="btn btn-sm btn-outline-primary" onclick="selectAllPermissions('editPermissionsTable')">

                     <i class="bi bi-check-all"></i> Tümünü Seç

                     </button>

                     <button type="button" class="btn btn-sm btn-outline-secondary" onclick="clearAllPermissions('editPermissionsTable')">

                     <i class="bi bi-x"></i> Tümünü Temizle

                     </button>

                  </div>

               </div>

            </div>

            <div class="modal-footer">

               <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">İptal</button>

               <button type="submit" class="btn btn-primary"><i class="bi bi-check-circle me-2"></i>Güncelle</button>

            </div>

         </form>

      </div>

   </div>

</div>

<!-- Clone Modal -->

<div class="modal fade" id="cloneGroupModal" tabindex="-1">

   <div class="modal-dialog">

      <div class="modal-content">

         <div class="modal-header">

            <h5 class="modal-title"><i class="bi bi-files me-2"></i>Yetki Grubunu Kopyala</h5>

            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>

         </div>

         <form id="cloneGroupForm">
            <input type="hidden" id="cloneSourceGroupId">
            <div class="modal-body">
               <div class="mb-2 text-muted small">
                  Kaynak Grup: <span id="cloneSourceGroupName" class="fw-semibold">-</span>
               </div>
               <div class="mb-3">
                  <label class="form-label">Yeni Grup Adi <span class="text-danger">*</span></label>
                  <input type="text" name="name" id="cloneName" class="form-control" required>
               </div>
               <div class="mb-0">
                  <label class="form-label">Aciklama</label>
                  <textarea name="description" id="cloneDescription" class="form-control" rows="2"></textarea>
               </div>
            </div>
            <div class="modal-footer">
               <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Iptal</button>
               <button type="submit" class="btn btn-primary"><i class="bi bi-check-circle me-2"></i>Kopyala</button>
            </div>
         </form>

      </div>

   </div>

</div>

@endsection

@push('scripts')

<script>

   let table;

   

   

   

   

   

   

   

   

   

   

   

   function selectAllPermissions(tableId = 'permissionsTable') {

   

   

   

   

   

       $('#' + tableId + ' input[type="checkbox"]').prop('checked', true);

   

   

   

   

   

   }

   

   

   

   

   

   

   

   

   

   

   

   function clearAllPermissions(tableId = 'permissionsTable') {

   

   

   

   

   

       $('#' + tableId + ' input[type="checkbox"]').prop('checked', false);

   

   

   

   

   

   }

   

   

   

   

   

   

   

   

   

   

   

   $(document).ready(function() {

   

   

   

   

   

       const canCreatePermissions = {!! can_access('permissions', 'create') ? 'true' : 'false' !!};
       const canUpdatePermissions = {!! can_access('permissions', 'update') ? 'true' : 'false' !!};

   

   

   

   

   

       const canDeletePermissions = {!! can_access('permissions', 'delete') ? 'true' : 'false' !!};
       const hiddenPermissionGroupIds = @json($hiddenPermissionGroupIds);

   

   

   

   

   

       table = $('#groupsTable').DataTable({

   

   

   

   

   

           processing: true,

   

   

   

   

   

           serverSide: false,

   

   

   

   

   

           ajax: {
               url: '{{ route("backoffice.permissions.index") }}',
               dataSrc: function(response) {
                   let rows = [];
                   if (Array.isArray(response)) {
                       rows = response;
                   } else if (response && Array.isArray(response.data)) {
                       rows = response.data;
                   }
                   return rows.filter(function(group) {
                       return !hiddenPermissionGroupIds.includes(Number(group.id));
                   });
               }
           },

   

   

   

   

   

           columns: [

   

   

   

   

   

               { data: 'name' },

   

   

   

   

   

               { data: 'description' },

   

   

   

   

   

               {

   

   

   

   

   

                   data: 'users_count',

   

   

   

   

   

                   render: function(data) {

   

   

   

   

   

                       return '<span class="badge bg-info">' + data + ' kullanıcı</span>';

   

   

   

   

   

                   }

   

   

   

   

   

               },

   

   

   

   

   

               { data: 'created_at' },

   

   

   

   

   

               {

   

   

   

   

   

                   data: null,

   

   

   

   

   

                   orderable: false,

   

   

   

   

   

                   render: function(data) {

   

   

   

   

   

                       let btns = '<div class="btn-group btn-group-sm">';

   

   

   

   

   

                       if (canUpdatePermissions) {

   

   

   

   

   

                           btns += '<button class="btn btn-outline-primary btn-edit" data-id="' + data.id + '"><i class="bi bi-pencil"></i></button>';

   

   

   

   

   

                       }
                       if (canCreatePermissions) {

                           btns += '<button class="btn btn-outline-secondary btn-clone" data-id="' + data.id + '" title="Kopyala"><i class="bi bi-files"></i></button>';

                       }

   

   

   

   

   

                       if (canDeletePermissions) {

   

   

   

   

   

                           btns += '<button class="btn btn-outline-danger btn-delete" data-id="' + data.id + '"><i class="bi bi-trash"></i></button>';

   

   

   

   

   

                       }

   

   

   

   

   

                       return btns + '</div>';

   

   

   

   

   

                   }

   

   

   

   

   

               }

   

   

   

   

   

           ],

   

   

   

   

   

           order: [[0, 'asc']],

   

   

   

   

   

           pageLength: 25,

   

   

   

   

   

           language: { url: '//cdn.datatables.net/plug-ins/1.13.6/i18n/tr.json' }

   

   

   

   

   

       });

   

   

   

   

   

   

   

   

   

   

   

       // Create

   

   

   

   

   

       $('#createGroupForm').on('submit', function(e) {

   

   

   

   

   

           e.preventDefault();

   

   

   

   

   

   

   

   

   

   

   

           // Collect permissions manually (both checked and unchecked)

   

   

   

   

   

           const permissions = {};

   

   

   

   

   

           $('#permissionsTable input[type="checkbox"]').each(function() {

   

   

   

   

   

               const name = $(this).attr('name');

   

   

   

   

   

               if (name) {

   

   

   

   

   

                   // Extract module and action from name like "permissions[dashboard][read]"

   

   

   

   

   

                   const matches = name.match(/permissions\[([^\]]+)\]\[([^\]]+)\]/);

   

   

   

   

   

                   if (matches) {

   

   

   

   

   

                       const module = matches[1];

   

   

   

   

   

                       const action = matches[2];

   

   

   

   

   

   

   

   

   

   

   

                       if (!permissions[module]) {

   

   

   

   

   

                           permissions[module] = {};

   

   

   

   

   

                       }

   

   

   

   

   

                       permissions[module][action] = $(this).is(':checked');

   

   

   

   

   

                   }

   

   

   

   

   

               }

   

   

   

   

   

           });

   

   

   

   

   

   

   

   

   

   

   

           console.log('Creating with permissions:', permissions);

   

   

   

   

   

   

   

   

   

   

   

           $.ajax({

   

   

   

   

   

               url: '{{ route("backoffice.permissions.store") }}',

   

   

   

   

   

               method: 'POST',

   

   

   

   

   

               contentType: 'application/json',

   

   

   

   

   

               headers: {

   

   

   

   

   

                   'X-CSRF-TOKEN': '{{ csrf_token() }}'

   

   

   

   

   

               },

   

   

   

   

   

               data: JSON.stringify({

   

   

   

   

   

                   name: $('input[name="name"]', '#createGroupForm').val(),

   

   

   

   

   

                   description: $('textarea[name="description"]', '#createGroupForm').val(),

   

   

   

   

   

                   permissions: permissions,
                   bypass_ip_auth: $('#createBypassIpAuth').is(':checked')

   

   

   

   

   

               }),

   

   

   

   

   

               success: function(response) {

   

   

   

   

   

                   console.log('Create success:', response);

   

   

   

   

   

                   $('#createGroupModal').modal('hide');

   

   

   

   

   

                   $('#createGroupForm')[0].reset();

   

   

   

   

   

                   Swal.fire('Baarılı!', response.message, 'success');

   

   

   

   

   

                   table.ajax.reload();

   

   

   

   

   

               },

   

   

   

   

   

               error: function(xhr) {

   

   

   

   

   

                   console.error('Create error:', xhr);

   

   

   

   

   

                   if (xhr.status === 422 && xhr.responseJSON.errors) {

   

   

   

   

   

                       let errorMsg = '<ul class="text-start">';

   

   

   

   

   

                       Object.keys(xhr.responseJSON.errors).forEach(function(key) {

   

   

   

   

   

                           errorMsg += '<li>' + xhr.responseJSON.errors[key][0] + '</li>';

   

   

   

   

   

                       });

   

   

   

   

   

                       errorMsg += '</ul>';

   

   

   

   

   

                       Swal.fire('Dorulama Hatası!', errorMsg, 'error');

   

   

   

   

   

                   } else {

   

   

   

   

   

                       Swal.fire('Hata!', xhr.responseJSON.message || 'Bir hata olutu', 'error');

   

   

   

   

   

                   }

   

   

   

   

   

               }

   

   

   

   

   

           });

   

   

   

   

   

       });

   

   

   

   

   

   

   

   

   

   

   

       // Edit

   

   

   

   

   

       $(document).on('click', '.btn-edit', function() {

   

   

   

   

   

           const id = $(this).data('id');

   

   

   

   

   

           $.ajax({

   

   

   

   

   

               url: '{{ route("backoffice.permissions.show", ":id") }}'.replace(':id', id),

   

   

   

   

   

               success: function(response) {

   

   

   

   

   

                   const g = response.group;

   

   

   

   

   

                   $('#editGroupId').val(g.id);

   

   

   

   

   

                   $('#editName').val(g.name);

   

   

   

   

   

                   $('#editDescription').val(g.description);
                   $('#editBypassIpAuth').prop('checked', !!g.bypass_ip_auth);

   

   

   

   

   

   

   

   

   

   

   

                   // Clear all checkboxes first

   

   

   

   

   

                   $('#editPermissionsTable input[type="checkbox"]').prop('checked', false);

   

   

   

   

   

   

   

   

   

   

   

                   // Load permissions checkboxes

   

   

   

   

   

                   if (g.permissions) {

   

   

   

   

   

                       $.each(g.permissions, function(module, actions) {

   

   

   

   

   

                           $.each(actions, function(action, value) {

   

   

   

   

   

                               if (value === true || value === 1) {

   

   

   

   

   

                                   $('#editPermissionsTable input[name="permissions[' + module + '][' + action + ']"]').prop('checked', true);

   

   

   

   

   

                               }

   

   

   

   

   

                           });

   

   

   

   

   

                       });

   

   

   

   

   

                   }

   

   

   

   

   

   

   

   

   

   

   

                   $('#editGroupModal').modal('show');

   

   

   

   

   

               }

   

   

   

   

   

           });

   

   

   

   

   

       });

   

   

   

   

   

   

   

   

   

   

   

       // Update

   

   

   

   

   

       $('#editGroupForm').on('submit', function(e) {

   

   

   

   

   

           e.preventDefault();

   

   

   

   

   

           const id = $('#editGroupId').val();

   

   

   

   

   

   

   

   

   

   

   

           // Collect permissions manually (both checked and unchecked)

   

   

   

   

   

           const permissions = {};

   

   

   

   

   

           $('#editPermissionsTable input[type="checkbox"]').each(function() {

   

   

   

   

   

               const name = $(this).attr('name');

   

   

   

   

   

               if (name) {

   

   

   

   

   

                   // Extract module and action from name like "permissions[dashboard][read]"

   

   

   

   

   

                   const matches = name.match(/permissions\[([^\]]+)\]\[([^\]]+)\]/);

   

   

   

   

   

                   if (matches) {

   

   

   

   

   

                       const module = matches[1];

   

   

   

   

   

                       const action = matches[2];

   

   

   

   

   

   

   

   

   

   

   

                       if (!permissions[module]) {

   

   

   

   

   

                           permissions[module] = {};

   

   

   

   

   

                       }

   

   

   

   

   

                       permissions[module][action] = $(this).is(':checked');

   

   

   

   

   

                   }

   

   

   

   

   

               }

   

   

   

   

   

           });

   

   

   

   

   

   

   

   

   

   

   

           console.log('Sending permissions:', permissions);

   

   

   

   

   

   

   

   

   

   

   

           $.ajax({

   

   

   

   

   

               url: '{{ route("backoffice.permissions.update", ":id") }}'.replace(':id', id),

   

   

   

   

   

               method: 'PUT',

   

   

   

   

   

               contentType: 'application/json',

   

   

   

   

   

               headers: {

   

   

   

   

   

                   'X-CSRF-TOKEN': '{{ csrf_token() }}'

   

   

   

   

   

               },

   

   

   

   

   

               data: JSON.stringify({

   

   

   

   

   

                   name: $('#editName').val(),

   

   

   

   

   

                   description: $('#editDescription').val(),

   

   

   

   

   

                   permissions: permissions,
                   bypass_ip_auth: $('#editBypassIpAuth').is(':checked')

   

   

   

   

   

               }),

   

   

   

   

   

               success: function(response) {

   

   

   

   

   

                   console.log('Update success:', response);

   

   

   

   

   

                   $('#editGroupModal').modal('hide');

   

   

   

   

   

                   Swal.fire('Baarılı!', response.message, 'success');

   

   

   

   

   

                   table.ajax.reload();

   

   

   

   

   

               },

   

   

   

   

   

               error: function(xhr) {

   

   

   

   

   

                   console.error('Update error:', xhr);

   

   

   

   

   

                   if (xhr.status === 422 && xhr.responseJSON.errors) {

   

   

   

   

   

                       let errorMsg = '<ul class="text-start">';

   

   

   

   

   

                       Object.keys(xhr.responseJSON.errors).forEach(function(key) {

   

   

   

   

   

                           errorMsg += '<li>' + xhr.responseJSON.errors[key][0] + '</li>';

   

   

   

   

   

                       });

   

   

   

   

   

                       errorMsg += '</ul>';

   

   

   

   

   

                       Swal.fire('Dorulama Hatası!', errorMsg, 'error');

   

   

   

   

   

                   } else {

   

   

   

   

   

                       Swal.fire('Hata!', xhr.responseJSON.message || 'Bir hata olutu', 'error');

   

   

   

   

   

                   }

   

   

   

   

   

               }

   

   

   

   

   

           });

   

   

   

   

   

       });

   

   

   

   

   

   

   

   

   

   

   

       // Clone

       $(document).on('click', '.btn-clone', function() {

           const id = $(this).data('id');

           $.ajax({

               url: '{{ route("backoffice.permissions.show", ":id") }}'.replace(':id', id),

               success: function(response) {

                   const g = response.group;
                   $('#cloneSourceGroupId').val(g.id);
                   $('#cloneSourceGroupName').text(g.name || '-');
                   $('#cloneName').val((g.name || '') + ' Kopya');
                   $('#cloneDescription').val(g.description || '');
                   $('#cloneGroupModal').modal('show');

               },
               error: function(xhr) {
                   Swal.fire('Hata!', xhr.responseJSON?.message || 'Grup bilgisi alinamadi', 'error');
               }

           });

       });

       $('#cloneGroupForm').on('submit', function(e) {

           e.preventDefault();
           const id = $('#cloneSourceGroupId').val();

           $.ajax({

               url: '{{ route("backoffice.permissions.clone", ":id") }}'.replace(':id', id),
               method: 'POST',
               data: {
                   _token: '{{ csrf_token() }}',
                   name: $('#cloneName').val(),
                   description: $('#cloneDescription').val()
               },
               success: function(response) {
                   $('#cloneGroupModal').modal('hide');
                   $('#cloneGroupForm')[0].reset();
                   $('#cloneSourceGroupId').val('');
                   $('#cloneSourceGroupName').text('-');
                   Swal.fire('Basarili!', response.message, 'success');
                   table.ajax.reload();
               },
               error: function(xhr) {
                   if (xhr.status === 422 && xhr.responseJSON?.errors) {
                       let errorMsg = '<ul class="text-start">';
                       Object.keys(xhr.responseJSON.errors).forEach(function(key) {
                           errorMsg += '<li>' + xhr.responseJSON.errors[key][0] + '</li>';
                       });
                       errorMsg += '</ul>';
                       Swal.fire('Dogrulama Hatasi!', errorMsg, 'error');
                   } else {
                       Swal.fire('Hata!', xhr.responseJSON?.message || 'Bir hata olustu', 'error');
                   }
               }

           });

       });

       // Delete

   

   

   

   

   

       $(document).on('click', '.btn-delete', function() {

   

   

   

   

   

           const id = $(this).data('id');

   

   

   

   

   

           Swal.fire({

   

   

   

   

   

               title: 'Emin misiniz',

   

   

   

   

   

               text: 'Bu yetki grubunu silmek istediinizden emin misiniz',

   

   

   

   

   

               icon: 'warning',

   

   

   

   

   

               showCancelButton: true,

   

   

   

   

   

               confirmButtonText: 'Evet, Sil',

   

   

   

   

   

               cancelButtonText: 'İptal',

   

   

   

   

   

               confirmButtonColor: '#dc3545'

   

   

   

   

   

           }).then((result) => {

   

   

   

   

   

               if (result.isConfirmed) {

   

   

   

   

   

                   $.ajax({

   

   

   

   

   

                       url: '{{ route("backoffice.permissions.destroy", ":id") }}'.replace(':id', id),

   

   

   

   

   

                       method: 'POST',

   

   

   

   

   

                       data: { _token: '{{ csrf_token() }}', _method: 'DELETE' },

   

   

   

   

   

                       success: function(response) {

   

   

   

   

   

                           Swal.fire('Baarılı!', response.message, 'success');

   

   

   

   

   

                           table.ajax.reload();

   

   

   

   

   

                       },

   

   

   

   

   

                       error: function(xhr) {

   

   

   

   

   

                           Swal.fire('Hata!', xhr.responseJSON.message || 'Bir hata olutu', 'error');

   

   

   

   

   

                       }

   

   

   

   

   

                   });

   

   

   

   

   

               }

   

   

   

   

   

           });

   

   

   

   

   

       });

   

   

   

   

   

   });

   

   

   

   

   

</script>

@endpush



