<?php
include("lib/config.php");

$userID = isset($_GET["uid"])?$_GET["uid"]:'';
?>

<div class="box-content">
  <form method="post" name="frmDepartments" id="frmDepartments">
    <input type="hidden" name="action" value="departmentAccess">
    <input type="hidden" name="user_id" value="<?php echo(isset($userID))?$userID:'';?>">
   
    <div>
    <table class="table table-striped table-bordered dataTable no-footer">
        <thead>
          <tr>
            <th>Sr#</th>
            <th>Department</th>
            <th>Access</th>
          </tr>
        </thead>
        <tbody>
			  <?php 
                $counter_departments = 0;
                $selQry_dept = "SELECT * FROM ".TBL_DEPARTMENTS." WHERE status='1' ORDER BY id";
                $deptInfo =  $con->getMultiRow($selQry_dept, __FILE__,__LINE__);
                $totDepts = count($deptInfo);
                    for($d=0;$d<$totDepts;$d++)
                    {
						$counter_departments++;
						//----------------------------------------
						$checked = '';
						if(!empty($deptInfo[$d]['id']))
						{
							$selQry_dept_per = "SELECT * FROM ".TBL_USERS_DEPARTMENTS_PERMISSIONS." WHERE user_id='".$userID."' and dept_id='".$deptInfo[$d]['id']."' LIMIT 1";
							$deptPerInfo =  $con->getSingleRow($selQry_dept_per, __FILE__,__LINE__);							
							if(!empty($deptPerInfo['id'])){
								$checked = 'checked="checked"';
							}
						}
						//----------------------------------------
              ?>
      		  <tr>
                <td><?php echo $counter_departments;?>.</td>
                <td><?php echo $deptInfo[$d]['name'];?></td>
                <td><input type="checkbox" name="departments[]" id="department_<?php echo $deptInfo[$d]['id'];?>" value="<?php echo $deptInfo[$d]['id'];?>" <?php echo $checked?> /></td>
              </tr>
      		  <?php }?>
        </tbody>
      </table>
    </div>    
    <div style="margin-top:30px; text-align:right;">
      <button data-loading-text="Saving..." type="submit" class="btn btn-success"> <i class="fa fa-floppy-o"></i> Assign</button>
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </form>
</div>
<script>
$(document).ready(function(e){
    $("#frmDepartments").on('submit', function(e){
	    
		e.preventDefault();
		
        $.ajax({
            type: 'POST',
            url: 'users-config.php',
            data: new FormData(this),
			
            //data: userData,
            contentType: false,
            cache: false,
            processData:false,
			dataType: 'JSON',
            beforeSend: function(){
                //$('.submitBtn').attr("disabled","disabled");
                //$('#fupForm').css("opacity",".5");
            },
            success: function(msg){			
                //$('.statusMsg').html('');
				
                if(msg[0].message == 'ok'){
				
                    $('#frmDepartments')[0].reset();
					
					//$('.statusMMsg').html('<span style="font-size:18px;color:#34A853">Product(s) added successfully.</span>');
					$('#myModal').modal('hide');
					
					//getUsers(); 
					
					Lobibox.notify('success', {
						title: 'Success',
						msg: 'Permissions assigned successfully.'
					});					
                }
				else
				{   
					//$('.statusMMsg').html('<span style="font-size:18px;color:#EA4335">Some problem occurred, please try again.</span>');
					$('#myModal').modal('hide');
					
					Lobibox.notify('error', {
						title: 'Error',
						msg: 'Some problem occurred, please try again.'
					});
                }
            }
        });
    });
	
});
</script>