<?php include("_config/dbconfig.php"); ?>
<?php include("_includes/head.php");?>
<title>Assigning</title>
<style>
.a_client .head { border:0px solid #ddd; margin-bottom:25px; padding-bottom:20px;  }
.a_client .head .title { text-align:center; }
.a_client .head .title h3 { margin:15px 0; }
.a_client .category { margin-bottom:20px; }
.a_client .category .form-control { width:auto; border-radius:0; }
.a_client .category label { float: left; margin-right: 10px; line-height: 30px; }
.a_client .table-result { border:1px solid #ddd; min-height:400px; padding:15px; }

.a_client table tr.thead { border-top: 2px solid #ddd; }

.a_client table > thead > tr > th:first-child,
.a_client table > tbody > tr > td:first-child { text-align:center; }

.a_client .btn-box { border-top: 1px solid #ddd; margin-top: 50px; padding: 15px 0; text-align: right; }
.a_client .btn-box .btn { border-color:#F47721; color:#F47721; border-radius:0; }
.a_client .btn-box .btn:hover { background-color:#F47721; color:#FFF; }

@media screen and (max-width: 768px) {

@media (max-width: 767px) { 
.a_client .table-result { border:0; min-height:auto; padding:0; }
.table-responsive {    padding-bottom: 50px; }
}
</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;	
}


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);
        
        /*echo '<pre>';
          print_r($all_clients);
          echo '</pre>';*/
        
    }else {
        
        $all_clients = $obj_clients_class->getUnassignClients($idd);
    }
    
   // echo '<pre>';
    //print_r($all_categories);
   // echo '</pre>';
}

?>
      
<div class="col-sm-12 right_form">
    
<div class="col-sm-12 a_client">
	<div class="row">
      <div class="col-sm-12">
        <div class="col-sm-12 head">
            <div class="title">
                <h3><?php echo $event['event_name'] ?></h3>
            </div>
            <p style=" text-align: center;"> <?php echo $event['start_date'] ?> to <?php echo $event['end_date'] ?>  </p>
        </div>
        <div class="col-sm-6">
          
        </div>
	  </div>
    </div>
	<div class="row">
      <div class="col-sm-12">
          
          
	<div class="col-lg-6 col-md-6 col-sm-6">
            <form action="<?= $link ?>" method="POST" class="table-result">
                <div class="category">
                 
                    <label>Select Category </label>
                    <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 class="clearfix"></div>        
                    
            <div class="table-responsive">          
                      <table id=" " 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 class="clearfix"></div>      
<?php  if(count($all_clients)>0){?>                    
        <div class="col-sm-12">
        	<div class="btn-box">
                    <button type="submit" name="add_now" class="btn btn-default">Add Now</button>
            </div>
        </div>
<?php } ?>                    
                     <div class="clearfix"></div>      
                    
                    </form>
        </div>

    <div class=" col-lg-6 col-md-6 col-sm-6">
        <div class="table-result table-responsive">  

            <form action="<?= $link ?>" method="POST"  >

		<table class="table table-bordered">
                    <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 echo $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>

<?php  if(count($AssignClients)>0){?>                    
        <div class="col-sm-12">
        	<div class="btn-box">
                    <button type="submit" name="delete_now" class="btn btn-default">Delete Now</button>
            </div>
        </div>
<?php } ?>        

        </form>  

      </div>


            
            
            
            
        </div>
       
	  </div>
    </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>
