<?php $__env->startSection('title','قائمة الصلاحيات'); ?>

<?php $__env->startSection('content'); ?>
<div class="app-content content ">
      <div class="content-overlay"></div>
      <div class="header-navbar-shadow"></div>
      <div class="content-wrapper container-xxl p-0">
        <div class="content-header row">
        </div>
    <div class="content-body">
	<!-- Dashboard Ecommerce Starts -->
		<section id="dashboard-ecommerce">
		<div class="content-header row">
          <div class="content-header-left col-md-9 col-12 mb-2">
            <div class="row breadcrumbs-top">
              <div class="col-12">
                <h2 class="content-header-title float-start mb-0">قائمة الصلاحيات</h2>
                <div class="breadcrumb-wrapper">
                  <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="<?php echo e(route('client.home')); ?>">لوحة التحكم</a>
                    </li>
                    <li class="breadcrumb-item active">الصلاحيات
                    </li>
                  </ol>
                </div>
              </div>
            </div>
          </div>
          <div class="content-header-right text-md-end col-md-3 col-12 d-md-block d-none">
            <div class="mb-1 breadcrumb-right">
			
              <div class="dropdown">
               				

            </div>
          </div>
        </div>
        <div class="col-xl-12 col-md-12 col-12">
          <div class=" card-statistics">
        <div class="card-body statistics-body">
          <div class="row">

            <?php $__currentLoopData = $roles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $role): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>  
          
          <div class="col-xl-4 col-lg-6 col-md-6">
            <div class="card">
              <div class="card-body">
                <div class="d-flex justify-content-between">
                  <span>عدد المستخدمين <?php echo e($role->users()->count()); ?></span>
                </div>
                <div class="d-flex justify-content-between align-items-end mt-1 pt-25">
                  <div class="role-heading">
                    <h4 class="fw-bolder"><?php echo e($role->name); ?></h4>
                    <a href="javascript:;" class="role-edit-modal" data-bs-toggle="modal" data-bs-target="#addRoleModal<?php echo e($role->id); ?>">
                      <small class="fw-bolder">تعديل الأذونات</small>
                    </a>
                  </div>
                </div>
              </div>
            </div>
          </div>
          
                    <!-- Add Role Modal -->
          <div class="modal fade" id="addRoleModal<?php echo e($role->id); ?>" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog modal-lg modal-dialog-centered modal-add-new-role">
              <div class="modal-content">
                <div class="modal-header bg-transparent">
                  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body px-5 pb-5">
                  <div class="text-center mb-4">
                    <h1 class="role-title"><?php echo e($role->name); ?></h1>
                    <p>تعديل وظائف الصلاحية</p>
                  </div>
                  <!-- Add role form -->
                  <form id="addRoleForm<?php echo e($role->id); ?>"  method="post" class="form" >
                    <?php echo csrf_field(); ?>
                  <input type="hidden" value="<?php echo e($role->id); ?>" name="role_id">

                    <div class="col-12">
                      <h4 class="mt-2 pt-50">الوظائف</h4>
                      <!-- Permission table -->
                      <div class="table-responsive">
                        <table class="table table-flush-spacing">
                          <tbody>
                              <tr>
                                  <?php $i = 0 ; $rolePermission =  $rolePermissions->where('role_id',$role->id)->pluck('id')->toArray();?>
                                  <?php $__currentLoopData = $permission; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index=> $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                  <?php if($index==0): ?>
                                  <td class="text-nowrap fw-bolder">
                                    <?php echo e($value->key); ?>

                                  </td>
                                  <?php endif; ?>

                                  <td class="text-nowrap fw-bolder">
                                  <label><?php echo e(Form::checkbox('permission[]', $value->name, in_array($value->id, $rolePermission) ? true : false, array('class' => 'name'))); ?>

                                         <?php echo e($value->name); ?></label>
                                  </td>
                                  <?php if($index < $permission->count()-1 ): ?>
                                  <?php if( $permission[$index+1]->key !== $value->key): ?>
                                </tr>
                                <tr>
                                  <td class="text-nowrap fw-bolder">
                                    <?php echo e($permission[$index+1]->key); ?>

                                  </td>
                                  <?php endif; ?>
                                  <?php endif; ?>

          
                                  <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                           
                              <tr>
                          </tbody>
                        </table>
                      </div>
                      <!-- Permission table -->
                    </div>
                    <div class="col-12 text-center mt-2">
                      <button type="submit" class="btn btn-success me-1">تعديل</button>
                      <button  type="reset" class="btn btn-outline-secondary close" data-bs-dismiss="modal" aria-label="Close">
                        إلغاء
                      </button>
                    </div>
                  </form>
                  <!--/ Add role form -->
                </div>
              </div>
            </div>
          </div>
          
          <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

          <div class="col-xl-4 col-lg-6 col-md-6">
            <div class="card">
              <div class="row">
                <div class="col-sm-5">
                  <div class="d-flex align-items-end justify-content-center h-100">
                    <img src="<?php echo e(asset('app-assets/images/faq-illustrations.svg')); ?>" class="img-fluid mt-2" alt="Image" width="85">
                  </div>
                </div>
                <div class="col-sm-7">
                  <div class="card-body text-sm-end text-center ps-sm-0">
                    <a href="javascript:void(0)" data-bs-target="#addRoleModal" data-bs-toggle="modal" class="stretched-link text-nowrap add-new-role">
                      <span class="btn btn-primary mb-1 waves-effect waves-float waves-light">إضافة صلاحية </span>
                    </a>
                    <p class="mb-0">إضافة صلاحية جديدة , ليست موجودة في القائمة السابقة</p>
                  </div>
                </div>
              </div>
            </div>
          </div>
          
          <div class="modal fade" id="addRoleModal" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog modal-lg modal-dialog-centered modal-add-new-role">
              <div class="modal-content">
                <div class="modal-header bg-transparent">
                  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body px-5 pb-5">
                  <div class="text-center mb-4">
                    <h1 class="role-title">صلاحية جديدة</h1>
                    <p>تحديد وظائف الصلاحية</p>
                  </div>
                  <!-- Add role form -->
                  <form id="addRole"  method="post"  >
                    <?php echo csrf_field(); ?>

                    <div class="col-12">
                      <div class="col-12">
                        <b class="form-label" for="modalRoleName"> اسم الصلاحية</b>
                        <input type="text"  name="name" class="form-control" placeholder="اسم الصلاحية">
                        <input type="hidden"  name="company_id" class="form-control" value="<?php echo e($company_id); ?>">

                      </div>
                      <h4 class="mt-2 pt-50">الوظائف</h4>
                      <!-- Permission table -->
                      <div class="table-responsive">
                        <table class="table table-flush-spacing">
                          <tbody>
                              <tr>
                                  <?php $__currentLoopData = $permission; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index=> $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                  <?php if($index==0): ?>
                                  <td class="text-nowrap fw-bolder">
                                    <?php echo e($value->key); ?>

                                  </td>
                                  <?php endif; ?>

                                  <td class="text-nowrap fw-bolder">
                                  <label><?php echo e(Form::checkbox('permission[]', $value->name, false, array('class' => 'name'))); ?>

                                         <?php echo e($value->name); ?></label>
                                  </td>
                                  <?php if($index < $permission->count()-1 ): ?>
                                  <?php if( $permission[$index+1]->key !== $value->key): ?>
                                </tr>
                                <tr>
                                  <td class="text-nowrap fw-bolder">
                                    <?php echo e($permission[$index+1]->key); ?>

                                  </td>
                                  <?php endif; ?>
                                  <?php endif; ?>

          
                                  <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                           
                              <tr>
                          </tbody>
                        </table>
                      </div>
                      <!-- Permission table -->
                    </div>
                    <div class="col-12 text-center mt-2">
                      <button type="submit" class="btn btn-success me-1">إضافة</button>
                      <button  type="reset" class="btn btn-outline-secondary close" data-bs-dismiss="modal" aria-label="Close">
                        إلغاء
                      </button>
                    </div>
                  </form>
                  <!--/ Add role form -->
                </div>
              </div>
            </div>
          </div>

          </div>
        </div>
      </div>
    </div>
	</section>

	</div>
      </div>
    </div>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('script'); ?>
<script>
$(".form").on('submit',(function(e) {

e.preventDefault();

$.ajax({

  url: "<?php echo e(route('client.roles.update')); ?>",

  type: "POST",

  data:  new FormData(this),

  dataType:  'json',

  contentType: false,

  cache: false,

  processData:false,
  
  beforeSend : function (){var ifConnected = window.navigator.onLine;if (!ifConnected) {toastr.info("يتم الآن إعادة الإتصال","لا يوجد إتصال بالإنترنت",{positionClass: "toast-top-right",tapToDismiss: !1,timeOut: 2e3,rtl: $("html").attr("data-textdirection")});}},

  success: function(response) {

    if(response.status){

            toastr.success("",response.message,{
			positionClass: "toast-top-right",
			tapToDismiss: !1,
			timeOut: 2e3,
			rtl: $("html").attr("data-textdirection")
		    });     
         
        // $('.close').click();

    }else{
        toastr.warning("",response.message,{
			positionClass: "toast-top-right",
			tapToDismiss: !1,
			timeOut: 2e3,
			rtl: $("html").attr("data-textdirection")
		    });   
    } 

  },
  error: function(response){
          $.each(response.responseJSON.errors, function (key, item) 
          {
            toastr.error("",item,{
			positionClass: "toast-top-right",
			tapToDismiss: !1,
			timeOut: 2e3,
			rtl: $("html").attr("data-textdirection")
		    });

          });
  },

 });

}));

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

e.preventDefault();

$.ajax({

  url: "<?php echo e(route('client.roles.store')); ?>",

  type: "POST",

  data:  new FormData(this),

  dataType:  'json',

  contentType: false,

  cache: false,

  processData:false,
  
  beforeSend : function (){var ifConnected = window.navigator.onLine;if (!ifConnected) {toastr.info("يتم الآن إعادة الإتصال","لا يوجد إتصال بالإنترنت",{positionClass: "toast-top-right",tapToDismiss: !1,timeOut: 2e3,rtl: $("html").attr("data-textdirection")});}},

  success: function(response) {

    if(response.status){

            toastr.success("",response.message,{
			positionClass: "toast-top-right",
			tapToDismiss: !1,
			timeOut: 2e3,
			rtl: $("html").attr("data-textdirection")
		    });     
         
         $('.close').click();

         setTimeout(window.location.reload(), 1500);

    }else{
        toastr.warning("",response.message,{
			positionClass: "toast-top-right",
			tapToDismiss: !1,
			timeOut: 2e3,
			rtl: $("html").attr("data-textdirection")
		    });   
    } 

  },
  error: function(response){
          $.each(response.responseJSON.errors, function (key, item) 
          {
            toastr.error("",item,{
			positionClass: "toast-top-right",
			tapToDismiss: !1,
			timeOut: 2e3,
			rtl: $("html").attr("data-textdirection")
		    });

          });
  },

 });

}));
   


</script>
<?php $__env->stopSection(); ?>


<?php echo $__env->make('client.layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH E:\xampp\htdocs\sales\resources\views/client/roles/index.blade.php ENDPATH**/ ?>