

<?php $__env->startSection('title', 'Manage Users'); ?>

<?php $__env->startSection('page-style'); ?>
<link href="https://cdn.datatables.net/2.1.8/css/dataTables.bootstrap5.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/responsive/3.0.3/css/responsive.dataTables.css">
<link href="https://cdn.datatables.net/buttons/3.2.0/css/buttons.dataTables.css" rel="stylesheet">
<?php $__env->stopSection(); ?>
      
<?php $__env->startSection('content'); ?>

	<div class="mb-2">
		<a href="<?php echo e(url('admin/users')); ?>" class="btn btn-kco-green me-1 mb-2"><i class="bx bx-group  bx-sm me-sm-1"></i>Users</a>
        <a href="<?php echo e(url('admin/roles')); ?>" class="btn btn-kco-dark-yellow mx-1 mb-2"><i class="bx bx-log-in-circle  bx-sm me-sm-1"></i>Roles</a>
        <a href="<?php echo e(url('admin/permissions')); ?>" class="btn btn-kco-light-brown mx-1 mb-2"><i class="bx bx-lock-open  bx-sm me-sm-1"></i>Permissions</a>
		</div>
     
	<?php if(session()->get('success')): ?>
		<div class="alert alert-success">
		  <?php echo e(session()->get('success')); ?>  
		</div>
	<?php endif; ?>
	
	   <?php if($errors->any()): ?>
    <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <li><?php echo e($error); ?></li>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </ul>
    </div>
	<?php endif; ?>
	 
    <div class="card card-datatable table-responsive">
	
        <div class="card-header">
			<div id="custom-dt-toolbar">
				<div class="row">
					<div class="col-md-6">
						<div class="dt-toolbar-left">
							<h5 class="card-title mb-0">Manage Users</h5>
						</div>
					</div>
					
					<div class="col-md-6">
						<div class="dt-toolbar-right">
							<button class="btn create-new btn-kco-green" type="button" data-bs-toggle="modal" data-bs-target="#addModal">
								<span><i class="bx bx-plus bx-sm me-sm-2"></i> 
								<span class="d-none d-sm-inline-block">Add User</span>
								</span>
							</button>
						</div>
					</div>
					
					</div>
				</div>
		</div><!--/.header-->
		
        <div class="card-body p-0">
            <table class="datatables-basic table border-top dataTable" style="width:100%">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>Name</th>
						<th>ID Number</th>
						<th>Phone Number</th>
                        <th>Email</th>
						<th>Role(s)</th>
						<th>Status</th>
                        <th width="120px">Action</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>
	
          <!-- Add Modal -->
          <div class="modal fade" id="addModal" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog" role="document">
              <div class="modal-content">
                <form action="<?php echo e(url('admin/users')); ?>" method="POST" class="needs-validation" novalidate>
				<?php echo csrf_field(); ?>
                <div class="modal-header">
                  <h5 class="modal-title" id="editModalLabel1">Add User</h5>
                  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="col mb-3">
                      <label for="addName" class="form-label">Name</label>
                      <input type="text" name="name" class="form-control" placeholder="Enter Name" value="<?php echo e(old('name')); ?>" required>
					  <span class="text-danger alert-sm"><?php echo e($errors->storeUser->first('name')); ?></span>
					      <div class="invalid-feedback">Name is required.</div>
                    </div>
					<div class="col mb-3">
                      <label for="addIdNumber" class="form-label">ID Number</label>
                      <input type="text" name="idNumber" class="form-control" placeholder="Enter National ID" value="<?php echo e(old('idNumber')); ?>" required>
					  <span class="text-danger alert-sm"><?php echo e($errors->storeUser->first('idNumber')); ?></span>
					  <span class="text-danger alert-sm"><?php echo e($errors->addUser->first('idNumber')); ?></span>
					      <div class="invalid-feedback">Enter National ID</div>
                    </div>
					<div class="col mb-3">
                      <label for="addPhoneNumber" class="form-label">Phone</label>
                      <input type="text" name="phoneNumber" class="form-control" placeholder="Enter Phone Number" value="<?php echo e(old('phoneNumber')); ?>" required>
					  <span class="text-danger alert-sm"><?php echo e($errors->storeUser->first('phoneNumber')); ?></span>
					  <span class="text-danger alert-sm"><?php echo e($errors->addUser->first('phoneNumber')); ?></span>
					      <div class="invalid-feedback">Enter Phone Number</div>
                    </div>
                    <div class="col mb-3">
                      <label for="addEmail" class="form-label">Email</label>
                      <input type="email" name="email" class="form-control" placeholder="xxxx@xxx.xx" value="<?php echo e(old('email')); ?>" required>
					  <span class="text-danger alert-sm"><?php echo e($errors->storeUser->first('email')); ?></span>
					  <span class="text-danger alert-sm"><?php echo e($errors->addUser->first('email')); ?></span>
					      <div class="invalid-feedback">Email is required.</div>
                    </div>
					<div class="col mb-3">
                      <label for="kraPin" class="form-label">KRA PIN</label>
                      <input type="text" name="kraPin" class="form-control" placeholder="Enter KRA PIN" value="<?php echo e(old('kraPin')); ?>">
					  <span class="text-danger alert-sm"><?php echo e($errors->storeUser->first('kraPin')); ?></span>
					      <div class="invalid-feedback">Enter KRA PIN</div>
                    </div>
					<div class="col mb-3">
                      <label for="addPassword" class="form-label">Password</label>
                      <input type="password" name="password" class="form-control" required>
					  <span class="text-danger alert-sm"><?php echo e($errors->storeUser->first('password')); ?></span>
					  <div class="invalid-feedback">Password is required.</div>
                    </div>
					<div class="col mb-3">
                      <label for="addRoles" class="form-label">Roles</label>
					  <br>
                      <?php $__currentLoopData = $allRoles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $role): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
					  <?php
					  $vchecked='';
					  $is_array=is_array(old('roles'));
							if($is_array==1){
							 if(in_array($role,old('roles'))){
								 $vchecked='checked';
								}
							}
					  ?>
						<div class="form-check form-check-inline">
									<label class="form-check-label">
									<input type="checkbox" class="form-check-input" value="<?php echo e($role); ?>" <?php echo e($vchecked); ?> name="roles[]"><?php echo e($role); ?>

									</label>
								</div>
					  <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
					  <br>
					  <span class="text-danger alert-sm"><?php echo e($errors->storeUser->first('roles')); ?></span>
                    </div>
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-label-secondary" data-bs-dismiss="modal">Close</button>
                  <button type="submit" class="btn btn-primary">Save</button>
                </div>
			  </form>
              </div>
            </div>
          </div>
	  
          <!-- Edit Modal -->
          <div class="modal fade" id="editModal" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog" role="document">
              <div class="modal-content">
			  <form action="<?php echo e(url('admin/users/updateuser')); ?>" method="POST" class="needs-validation" novalidate>
				<?php echo csrf_field(); ?>
                <div class="modal-header">
                  <h5 class="modal-title" id="editModalLabel1">Update User</h5>
                  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
				<input type="hidden" name="userid"  class="form-control edit_userid">
                    <div class="col mb-3">
                      <label for="editName" class="form-label">Name</label>
                      <input type="text" name="edit_name" class="form-control edit_name" placeholder="Enter Name" required>
					  <?php $__errorArgs = ['edit_name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <span class="text-danger"><?php echo e($message); ?></span> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
					      <div class="invalid-feedback">Name is required.</div>
                    </div>
					<div class="col mb-3">
                      <label for="addIdNumber" class="form-label">ID Number</label>
                      <input type="text" name="edit_idNumber" class="form-control edit_idNumber" placeholder="Enter National ID" required>
					  <?php $__errorArgs = ['edit_idNumber'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <span class="text-danger"><?php echo e($message); ?></span> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
					      <div class="invalid-feedback">Enter National ID</div>
                    </div>
					<div class="col mb-3">
                      <label for="addPhoneNumber" class="form-label">Phone</label>
                      <input type="text" name="edit_phoneNumber" class="form-control edit_phoneNumber" placeholder="Enter Phone Number" required>
					  <?php $__errorArgs = ['edit_phone'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <span class="text-danger"><?php echo e($message); ?></span> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
					      <div class="invalid-feedback">Enter Phone Number</div>
                    </div>
                    <div class="col mb-3">
                      <label for="editEmail" class="form-label">Email</label>
                      <input type="email" name="edit_email" class="form-control edit_email" placeholder="xxxx@xxx.xx" readonly required>
                    </div>
					<div class="col mb-3">
                      <label for="kraPin" class="form-label">KRA PIN</label>
                      <input type="text" name="edit_kraPin" class="form-control edit_kraPin" placeholder="Enter KRA PIN">
					  <?php $__errorArgs = ['edit_kraPin'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <span class="text-danger"><?php echo e($message); ?></span> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
					      <div class="invalid-feedback">Enter KRA PIN</div>
                    </div>
					<div class="col mb-3">
                      <label for="editPassword" class="form-label">Password</label>
                      <input type="password" name="edit_password" class="form-control edit_password">
					  <?php $__errorArgs = ['edit_password'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <span class="text-danger"><?php echo e($message); ?></span> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>
					<div class="col mb-3">
                      <label for="editRoles" class="form-label">Roles</label>
					  <br>
					  <!--
                      <?php $__currentLoopData = $allRoles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $role): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
						<div class="form-check form-check-inline">
									<label class="form-check-label">
									<input type="checkbox" class="form-check-input edit_roles" value="<?php echo e($role); ?>" name="roles[]"><?php echo e($role); ?>

									</label>
								</div>
					  <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
					  -->
					  <div id="editRoles"></div>
					  <?php $__errorArgs = ['edit_roles'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <span class="text-danger"><?php echo e($message); ?></span> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-label-secondary" data-bs-dismiss="modal">Close</button>
                  <button type="submit" class="btn btn-primary">Update</button>
                </div>
				</form>
              </div>
            </div>
          </div>
	  
	  <!-- End Offcanvas -->
      <div class="col-lg-3 col-md-6">
        <div class="mt-4">
          <div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasEnd" aria-labelledby="offcanvasEndLabel">
            <div class="offcanvas-header">
              <h5 id="offcanvasEndLabel" class="offcanvas-title">Offcanvas End</h5>
              <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
            </div>
            <div class="offcanvas-body my-auto mx-0 flex-grow-0">
              <p class="text-center">Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.</p>
              <button type="button" class="btn btn-primary mb-2 d-grid w-100">Continue</button>
              <button type="button" class="btn btn-outline-secondary d-grid w-100" data-bs-dismiss="offcanvas">Cancel</button>
            </div>
          </div>
        </div>
      </div>
	  
	  <?php if($errors->addUser->any() || $errors->storeUser->any()): ?>
    <script>
        $(document).ready(function() {
            $('#addModal').modal('show');
        });
    </script>
	<?php endif; ?>
	
	<?php if($errors->editUser->any()): ?>
    <script>
        $(document).ready(function() {
            $('#editModal').modal('show');
        });
    </script>
	<?php endif; ?>
	
<?php $__env->stopSection(); ?>

<?php $__env->startSection('page-script'); ?>


<script type="text/javascript">
(function () {
  'use strict';
  window.addEventListener('load', function () {
    // Fetch all the forms we want to apply custom Bootstrap validation styles to
    var forms = document.getElementsByClassName('needs-validation');
    // Loop over them and prevent submission
    var validation = Array.prototype.filter.call(forms, function (form) {
      form.addEventListener('submit', function (event) {
        if (form.checkValidity() === false) {
          event.preventDefault();
          event.stopPropagation();
        }
        form.classList.add('was-validated');
      }, false);
    });
  }, false);
})();

  document.addEventListener('livewire:navigated', () => {
console.log('dt navigated');	  
  $(function () {
     $.noConflict();   
    var table = $('.dataTable').DataTable({
		dom: 'Bfrltip',
		lengthMenu: [[10, 25, 50, 100, 500, 1000, -1], [10, 25, 50, 100, 500, 1000, "All"]],
		language: {
			"emptyTable": "No data available in table",
			"lengthMenu": "Show _MENU_ entries",
		},
		responsive: true,
        processing: true,
        serverSide: true,
        //ajax: "<?php echo e(route('users.index')); ?>",
		ajax:{
                     url: "<?php echo e(route('admin.users.getUsers')); ?>",
                     dataType: "json",
                     type: "POST",
                     data:{ _token: "<?php echo e(csrf_token()); ?>"}
                   },
        columns: [
            {data: 'id', name: 'id'},
            {data: 'name', name: 'name'},
			{data: 'idNumber', name: 'idNumber'},
			{data: 'phoneNumber', name: 'phoneNumber'},
            {data: 'email', name: 'email'},
			{data: 'assignedroles', name: 'assignedroles'},
			{data: 'status', name: 'status'},
            {data: 'options', name: 'options', orderable: false, searchable: false},
        ],
		buttons: [
					{
						extend: 'collection',
						text: "<i class='bx bx-download bx-sm me-sm-1'></i> Export",
						className: 'export_btns btn btn-outline-kco me-4',
						buttons: [
							{
								extend: 'csv',
								text: '<i class="bx bx-file me-1"></i> CSV',
								titleAttr: 'CSV'
							},
							{
								extend: 'excel',
								text: '<i class="bx bxs-file-export me-1"></i> Excel',
								titleAttr: 'Excel'
							},
							{
								extend: 'pdf',
								text: '<i class="bx bxs-file-pdf me-1"></i> PDF',
								titleAttr: 'PDF'
							},
						]
					},
					/*{
						extend: 'colvis',
						text: 'Display',
					}*/
				],
    });
	$('.export_btns').detach().prependTo('.dt-toolbar-right');
    $(".export_btns").addClass("dropdown-toggle");

	$('.dataTable').on('click', '.editdata', function (e) {
	  e.preventDefault();
	  var allRoles=<?php echo json_encode($allRoles,true); ?>;
	  var userRoles=$(this).data('userroles');
	  $('#editRoles').empty();
		  for (var i=0;i<allRoles.length;i++) {
			  if(jQuery.inArray(allRoles[i], userRoles) != -1) { checked='checked'; }
			  else{ checked=''; }
		  $('#editRoles').append('<div class="form-check form-check-inline">'+
									'<label class="form-check-label">'+
									'<input type="checkbox" class="form-check-input" value="'+allRoles[i]+'" name="edit_roles[]"' +checked+ '>'+allRoles[i]+
									'</label>'+
									'</div>');
		  }
		$(".edit_userid").val($(this).data('id'));
		$(".edit_name").val($(this).data('name'));
		$(".edit_idNumber").val($(this).data('idnumber'));
		$(".edit_phoneNumber").val($(this).data('phonenumber'));
		$(".edit_kraPin").val($(this).data('krapin'));
		$(".edit_email").val($(this).data('email'));
		let modal = bootstrap.Modal.getOrCreateInstance($('#editModal')); // Returns a Bootstrap modal instance //modal.show //modal.hide
		modal.show();
  });
  
  $('.dataTable').on('click', '.deactivate', function (e) {
	  e.preventDefault();
	  var href = $(this).attr('href');
      Swal.fire({
        title: 'Deactivate user?',
        text: "They will not be able to login",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, deactivate!'
      }).then((result) => {
        if (result.isConfirmed) {
          window.location.href = href;
        }
      });
  });
  
  $('.dataTable').on('click', '.activate', function (e) {
	  e.preventDefault();
	  var href = $(this).attr('href');
      Swal.fire({
        title: 'Activate user?',
        text: "They will be able to login",
        icon: 'success',
        showCancelButton: true,
        confirmButtonColor: '#71dd37',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, activate!'
      }).then((result) => {
        if (result.isConfirmed) {
          window.location.href = href;
        }
      });
  });
	
  });
  }, { once:true });
  
</script>
<?php $__env->stopSection(); ?> 
<?php echo $__env->make('layouts/contentNavbarLayout', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\xampp\htdocs\kcofbms\resources\views/users/user/index.blade.php ENDPATH**/ ?>