<?php include("_config/dbconfig.php"); ?>
<?php include("_includes/head.php");?>
<title>Assigning</title> 
<style type="text/css">
.boxscroll {
	height: 400px;
	border: 0px solid #000;
	overflow: auto;
}

</style>


<?php include("_includes/header.php");
$ee = explode("/", $_SERVER["PHP_SELF"]); 
$link  =  end($ee).'?'.$_SERVER["QUERY_STRING"] ;
if(isset($_SESSION['user_session'])){
    $agent_id = $_SESSION['user_session'];
}else {
    $agent_id = 0;	
}

$agent_id;
if(isset($_POST['delete_now'])){ 

    if(count($_POST['select'])>0)
    $sel_event = base64_decode($_GET['event']);
    foreach($_POST['select'] as $client_list){
        $obj_jobs_class->removeAssignClients($sel_event, $client_list,$agent_id);
    } 
    
    echo "<script>window.location='".$link."'</script>";
    die();
}


if(isset($_POST['add_now'])){
    
    $sel_event = base64_decode($_GET['event']);
    if(isset($_POST['select']) && count($_POST['select'])>0)
    foreach($_POST['select'] as $client_list){
        $obj_jobs_class->AssignClientsToEvent($sel_event, $client_list,$agent_id);
    }
    echo "<script>window.location='".$link."'</script>";
    die();
}


if(isset($_GET['event']) && !empty($_GET['event'])){
    $idd = base64_decode($_GET['event']); 
    $event = $obj_events_class->getEvent($idd);
    $all_categories = $obj_category_class->getAllCategories();
    
    $AssignClients =  $obj_jobs_class->getAssignClients($idd, $agent_id);
            
    
    //$all_clients = $obj_clients_class->getClients();
 
    if(isset($_GET['cat']) && !empty($_GET['cat'])){
        $category = base64_decode($_GET['cat']);
        $all_clients = $obj_clients_class->getClientsByCategory($category, $idd); 
    }else {
        
        $all_clients = $obj_clients_class->getUnassignClients($idd, $agent_id);
        
    }
  
}
else {
    
     echo "<script>window.location='events.php'</script>";
     die();
}
?>


<div class="col-sm-12 right_form">
  <div class="title">
        <h1><?php echo $event['event_name'] ?>
            <p class="date_d"><?php if(!empty($event['start_date'])) echo $obj_jobs_class->change_date2($event['start_date']) ?>  <?php if(!empty($event['end_date'])) echo ' to '.$obj_jobs_class->change_date2( $event['end_date']); ?>  </p></h1>
    </div>  
    
    <ol class="breadcrumb white">
          <li><a href="dashboard.php">Dashboard</a></li>
          <li><a href="events.php">Exhibitions</a></li>
          <li class="active">Assign Client Step 1</li>
          <li class="right"><a href="assign_client_step2.php?event=<?php echo $_GET['event']?>">Assign Client Step 2 </a></li>
    </ol>
    
    

      <div class="col-sm-12 a_client">
        <div class="row">   
            
            
            <?php if($_SESSION['user_role_session']!="Admin"){?>
            
        	<div class="col-lg-6 col-md-6 col-sm-6">
                <form action="<?= $link ?>" method="POST" >
                   <?php  if(count($all_clients)>0){?>                    
                        <div class="">
                            <div class="btn-box" style="border:none; margin:0px; padding:0px; margin-bottom:10px;">
                                    <button type="submit" name="add_now" class="btn btn-default">Add Now</button>
                            </div>
                        </div>
				    <?php } ?>                    
                    <div class="clearfix"></div>   
                         

                <div  class="table-result">
                    <div class="category">
                     <div class="col-lg-3 col-md-6 col-sm-6">
                        <label>Select Category </label>
                     </div>
                     <div class="col-lg-9 col-md-6 col-sm-6">   
                        <select class="chosen-select"  onchange="redirect();" name="category" id="category">
                                <option value="">All</option>
                            <?php foreach($all_categories as $cats){
                                if(isset($cats['sub_cat']) && count($cats['sub_cat'])>0){
                                ?>
                                <optgroup label="<?php echo $cats['category'];?>">
                                <?php foreach($cats['sub_cat'] as $cats2){ $sel = '';
                                            if($cats2['cat_id']== $category){
                                                $sel = "selected='selected'";
                                            }
                                    ?>
                                    <option <?php echo $sel; ?> value="<?php echo base64_encode($cats2['cat_id']);?>"><?php echo $cats2['category'] ?></option>
                                <?php } ?>
                                </optgroup>
                            <?php }else {?>
    
                            <?php } }?>
                        </select>
                         </div>
                        
                </div>
                    <div class="clearfix"></div>        
                        
                <div class="table-responsive">          
                    
      <div style=" ">
          
          <div class="boxscroll" style="height:485px;">
                          <table  class="table table-bordered " cellspacing="0" width="100%">
                            <thead>
                                <tr>
                                    <th>Sr. </th>
                                    <th >    
                                        <input id="CheckAll" type="checkbox" name="chekkk" value="all" /> 
                                    </th>
                                    <th>Company Name </th>
                                    <th>Owner Name</th>
                                    <th>City</th>
                                </tr>
                            </thead>
                            
                            <tbody> 
                                
                            <?php $i=1; 
                            if(count($all_clients)>0){
                            foreach($all_clients as $cat){ ?>
                                <tr>
                                    <td><?php echo $i++;?></td>
                                    <td class="center"> <input type="checkbox"  class="all_client" name="select[<?php echo $cat['client_id']?>]" value="<?php echo $cat['client_id']?>">
                                        <i style="display: none;"  class="fa fa-check sellect"></i>
                                    </td>
                                    <td><?php echo $cat['company_name']?> </td>
                                    <td><?php echo $cat['owner_name']?></td>
                                    <td><?php echo $cat['city']?></td>
                                </tr>
                            <?php   } 
                            }else {
                                echo '';
                                
                            }
                            ?> 
                            </tbody>
                </table>
          </div>  
      </div>    
                </div>
                </div>
                
                         <div class="clearfix"></div>      
    
                        
                        </form>
            </div>
        
        <?php } ?>
        
            
            <?php if($_SESSION['user_role_session']=="Admin"){
                $ccls = 'col-lg-12 col-md-12 col-sm-12';
            }else {
                $ccls = 'col-lg-6 col-md-6 col-sm-6';
            }?>
        <div class="<?= $ccls ?>">
            
          
    
            <form action="<?= $link ?>" method="POST"  >
            

    <?php  if(count($AssignClients)>0 && $_SESSION['user_role_session']!="Admin"){?>                    
                <div class="btn-box" style="border:none; padding:0px; margin:0px; margin-bottom:10px;">
                        <button type="submit" name="delete_now" class="btn btn-default">Delete Now</button>
                </div>
    <?php } ?>
    
            <div class="table-result table-responsive right-b">  
               <div  class="boxscroll" style="height:520px;">    
                <table class="table table-bordered" id="box2">
                        <thead>
                                <tr>
                                    <th>Sr. </th>
                                    <th>Company Name </th>
                                    <th>Categories</th>
                                    <th>City</th>
                                     <th >    
                                        <input id="RemoveAll" type="checkbox" name="chekkk" value="all" /> 
                                    </th>
                                    
                                    
                                </tr>
                            </thead>
                            
                        <tbody>
                 
                               <?php $i=1; 
                            if(count($AssignClients)>0){
                            foreach($AssignClients as $cat){ ?>
                                <tr>
                                <td><?php echo $i++;?></td>
                                <td><?php echo $cat['company_name']?>   <?php   $cat['client_id']?> </td>
                                <td><?php echo $obj_category_class->getCategoriesName($cat['categories']);?></td>
                                <td><?php echo $cat['city']?></td>
                                <td class="center">
                                <input type="checkbox"  class="remove_all_client" name="select[<?php echo $cat['client_id']?>]" value="<?php echo $cat['client_id']?>">
                                    <i style="display: none;"  class="fa fa-check sellect_r"></i>
                                </td>
                                </tr>
                            <?php   } 
                            }else {
                                echo '';
                                
                            }
                            ?> 
                                
                                
                        </tbody>
                    </table>
            </div>
        	</div>
            </form>  
    
         
    
    
                
                
                
                
            
        </div>
	  </div>
	
</div>


<?php include("_includes/footer.php"); ?>

          
<script>
$(document).ready(function() {
         
    

//        $('#example').DataTable();
$('#example').dataTable( {
      "aoColumnDefs": [
          { 'bSortable': false, 'aTargets': [ 1 ] }
       ]
});


        $('#CheckAll').click(function(){
            
            console.log($(this).attr('checked'));
            if($(this).is(':checked')){
                $('input:checkbox').attr('checked',true);
                $('.all_client').css('display', 'none');
                $('.sellect').css('display', 'block');
            }
            else{  
                $('input:checkbox').attr('checked',false);
                $('.sellect').css('display', 'none');    
                $('.all_client').css('display', 'block')
            }
        });
        

        $('#RemoveAll').click(function(){
            console.log($(this).attr('checked'));
            if($(this).is(':checked')){
                $('input:checkbox.remove_all_client').attr('checked',true);
                $('.remove_all_client').css('display', 'none');
                $('.sellect_r').css('display', 'block');
            }
            else{  
                $('input:checkbox.remove_all_client').attr('checked',false);
                $('.sellect_r').css('display', 'none');    
                $('.remove_all_client').css('display', 'block')
            }
        });
        
        
    });



function redirect(){
        var cat = $('#category').val();
        window.location='assign_client.php?cat='+cat+'&event=<?php echo $_GET['event']?>';
}
</script>
