<?php include("_config/dbconfig.php"); 
error_reporting(0);
 
if(isset($_GET['task']) && $_GET['task'] == "getClientNameDash"){
        
    $id  =  trim($_GET['event_id']);
    $ccs = $obj_jobs_class->getApprovedClientsName($id);
    
if(count($ccs)>0){
    ?> 
<h3> Clients Details <small> (Accepted <?php echo $ccs['client_accpt'] ?>, Hot <?php echo $ccs['client_hot'] ?> )</small></h3>
    <table class="lisitng_table table first_small" >
    <thead>
        <tr><th width="40" style="text-align: left;">Sr.</th>
            <th width="250">Client</th>
            <th></th> 
        </tr>
    </thead>
    <tbody>
<?php    $count =1; 
    foreach($ccs['client'] as $ac){ ?>
        <tr <?php if($ac['status_order']==1) echo "style='background:#BBEF7E;'"?>>
            <td><?php echo $count++; ?></td>
            <td>  <?php echo $ac['company_name']; ?>   </td>
            <td><?php 
                        if($ac['status_order']==1){
                                echo "Accepeted";
                        }else { echo "Hot";} ?> 
            </td>
        </tr>
<?php    } ?>
    <tr>
        <td></td><td></td><td></td>
    </tr>
    </tbody>
    </table>
    
<?php } ?>
    
<?php    }


if(isset($_GET['task']) && $_GET['task'] == "changeOrderStatus"){
        
    $id     =  trim($_GET['id']);
    $status =  trim($_GET['status']);
    $res = $obj_events_class ->updateMarketStatus($id, $status);
    
}

if(isset($_GET['task']) && $_GET['task'] == "checkCompany"){
        
      $company =  trim($_GET['comp']);
    
      if(isset($_GET['id'])){
          $id = $_GET['id'];
          $res = $obj_clients_class->checkCompany($company, $id);
          echo trim($res);
      }else {
            $res = $obj_clients_class->checkCompany($company);
            echo trim($res);
      }
}
 
 
if(isset($_GET['del_person']) && !empty($_GET['del_person'])){
        
    $id =  ($_GET['del_person']);
    $obj_clients_class->deletePerson($id);
}
 

if(isset($_GET['task']) && $_GET['task'] == 'delTask' ){
        
            $id   = ($_GET['id']);
            $obj_jobs_class->deleteAgentTasks($id);
}


if(isset($_GET['task']) && $_GET['task'] == 'changeStatustask' ){
        
            $id   = ($_GET['id']);
            $status     =  ($_GET['st_code']);
            
            $obj_jobs_class->updateAgentTasks($id, $status);
            
}

if(isset($_GET['task']) && $_GET['task'] == 'changeStatus' ){
        
            $asign_id   = ($_GET['asign']);
            $status     =  ($_GET['st_code']);
            if($status == 1){
                $status2     = 'accept';
            }
            
            if(isset($_GET['st_code'])){
                $st_code = $_GET['st_code'];
                $obj_jobs_class->updateClientsAssign($asign_id, $status2, $status, $st_code);
            }else {
                $obj_jobs_class->updateClientsAssign($asign_id, $status2, $status);
            }
            
            
}

if(isset($_GET['task']) && $_GET['task'] == 'deniedStatus' ){
        
        $assign_id = $_GET['assign_id'];
        $reason_id = $_GET['reason'];

        $obj_jobs_class->updateDeniedReasonsClient($assign_id,$reason_id);
        $obj_jobs_class->updateClientsAssign($assign_id, 'deny', '0');

}

if(isset($_GET['st']) && !empty($_GET['st']) && !empty($_GET['as']) && isset($_GET['as']) && $_GET['task'] == 'pipline' ){
        
            $asign_id   = base64_decode($_GET['as']);
            $status     =  base64_decode($_GET['st']);
            if($status == 1){
                $status2     = 'accept';
            }else if($status == 0){
                $status2     = 'deny';
            }else if($status == 4){
                $status2     = 'pipeline';
            }
            
            if(isset($_GET['st_code'])){
                    $st_code = $_GET['st_code'];
                    $obj_jobs_class->updateClientsAssign($asign_id, $status2, $status, $st_code);
            }else {
                    $obj_jobs_class->updateClientsAssign($asign_id, $status2, $status);
            }
            
         /*   echo "<script>window.location='assign_client_step2.php?event=".$_GET['event']."'</script>";*/
            die();
            
}


if(isset($_GET['task']) && !empty($_GET['task']) && $_GET['task'] == 'add_assign_call_sms'){
                  $key=$_GET['key'];
				  $value=$_GET['value'];
				 $date= $obj_jobs_class->addClientsAssignDetails($key, $value,"");
				  echo $obj_jobs_class->change_date_hours($date);
        }
        


if(isset($_GET['task']) && !empty($_GET['task']) && $_GET['task'] == 'add_assign'){
     
    
    if(isset($_GET['call']) && count($_GET['call'])>0){

        //addClientsAssignDetails
        foreach($_GET['call'] as $key=>$call){
                  $obj_jobs_class->addClientsAssignDetails($key, 'call', $call);
        }
        
        /*foreach($_POST['sms'] as $key=>$sms){
                  $obj_jobs_class->addClientsAssignDetails($key, 'sms', $sms);
        }
        
        foreach($_POST['email'] as $key=>$email){
                  $obj_jobs_class->addClientsAssignDetails($key, 'email', $email);
        }*/
    }
    
    
      if(isset($_GET['sms']) && count($_GET['sms'])>0){
 
        foreach($_GET['sms'] as $key=>$sms){
                  $obj_jobs_class->addClientsAssignDetails($key, 'sms', $sms);
        }
        
    }
    
      if(isset($_GET['email']) && count($_GET['email'])>0){ 
        
        foreach($_GET['email'] as $key=>$email){
                  $obj_jobs_class->addClientsAssignDetails($key, 'email', $email);
        }
    }
    
    echo '1';
    
}
  

if(isset($_GET['task']) && !empty($_GET['task']) && $_GET['task'] == 'delete_task'){
    
    $obj_jobs_class->removeTask($_GET['id']);
    echo '1';
}

if(isset($_GET['task']) && !empty($_GET['task']) && $_GET['task'] == 'complete_task'){
    
   $obj_jobs_class->completeTask($_GET['id']);
    echo '1';
    
}

if(isset($_GET['task']) && !empty($_GET['task']) && $_GET['task'] == 'add_step3Task'){

if($_GET['stat']=='add'){
        
    $return = $obj_jobs_class->addEventStep3Task($_GET['event'], $_GET['client'],$_GET['agent'],$_GET['task_id'], 1 );   
    if($return == true){
        $width = $obj_jobs_class->getStep3TasksPercent($_GET['event'], $_GET['client'], $_GET['agent']);
    }else {
        $obj_jobs_class->removeEventStep3Task($_GET['event'], $_GET['client'],$_GET['agent'],$_GET['task_id']);   
        $width = $obj_jobs_class->getStep3TasksPercent($_GET['event'], $_GET['client'], $_GET['agent']);
    }
    echo $width;
    
    
}else if($_GET['stat']=='remove') {
    
    $return=  $obj_jobs_class->removeEventStep3Task($_GET['event'], $_GET['client'],$_GET['agent'],$_GET['task_id']);
    if($return == true){
        $width = $obj_jobs_class->getStep3TasksPercent($_GET['event'], $_GET['client'], $_GET['agent']);
    }else {
        $obj_jobs_class->addEventStep3Task($_GET['event'], $_GET['client'],$_GET['agent'],$_GET['task_id'], 1 );   
        $width = $obj_jobs_class->getStep3TasksPercent($_GET['event'], $_GET['client'], $_GET['agent']);
    }
    echo $width;
}
    
}

if(isset($_GET['task']) && !empty($_GET['task']) && $_GET['task'] == 'add_exiTask'){
    
    $task_title = $_POST['title_task'];
    $assign_id = $_POST['assign_id'];
    $event_id = $_POST['event_id'];
    $client_id = $_POST['client_id'];
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];
    $contact_person = $_POST['contact_person'];
    $agent_id = $_POST['agent_id'];
    $event = $obj_events_class->getEvent($event_id);
    $a_client = $obj_clients_class->getClients($client_id);
    
    if($task_title!='' && $start_date !='' && $end_date!='' && $contact_person!=''){
        
        $obj_jobs_class->addEventTask($assign_id, $event_id, $client_id, $task_title, $start_date, $end_date, $contact_person, $agent_id);
        $assign_id.' - '.$event_id.' - '.$client_id.' - '.$task_title.' - '.$start_date.' - '.$end_date.' - '.$contact_person.' - '.$agent_id;
    
 $tasks_events = $obj_jobs_class->getEventTasks($assign_id, $event_id);   
    
$modal = '
  <div class="modal-dialog client-modal" style="width:800px !important;">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">×</button>
        <h4 class="modal-title">Pending Tasks</h4>
      </div>
      <div class="modal-body">
	  <form name="task_form_'.$assign_id.'_" action="" method="POST" id="task_form_'.$assign_id.'_">
              
        <table style="table-layout: fixed; width: 100%; text-align:left;">
            <tbody>
            <tr><td width="50%" style="font-size:15px;"><small>Exhibition: </small> <strong> '.$event['event_name'].'</strong></td>
                <td width="50%"  style="border-top:none !important; font-size:15px; "><small>Company: </small> <strong>'.$a_client['company_name'].'</strong> </td>
            </tr>
        </table>';
 
    foreach($tasks_events as $tee ){
        
$modal .='<div class="listing_outline" id="task_id_'.$tee['task_id'].'_">
        <table style="table-layout: fixed; width: 100%; text-align:left;" class="tasks_listing_box">
            <tr class="task_title_row">
                <td width="100%" class="task_title" colspan="2">'.$tee['title'].' 
                <small class="small-gray">Created on : '.$tee['ts'].'</small>
                </td>
            </tr>
        <tr>
            <td width="50%" ><small>Start Date:</small> '.$obj_jobs_class->change_date3($tee['start_date']).'  </td>
            <td width="50%"><small>End Date: </small> '.$obj_jobs_class->change_date3($tee['end_date']).' </td>
        </tr>';


    

    foreach($a_client['persons'] as $ac_person){
        
        if($ac_person['person_id'] == $tee['person_id']){
            
            $modal .='<tr>
                    <td width="50%" ><small>Contact Person: </small> '.$ac_person['f_name'].' '.$ac_person['l_name'].'('.$ac_person['designation'].') </td>
                    <td width="50%" ><small>Email: </small> '.$ac_person['email'].'</td>
                </tr>
                <tr>
                    <td width="50%" ><small>Mobile: </small> '.$ac_person['mobile'].' </td>
                    <td width="50%" ><small>Land Line: </small> '.$ac_person['landline'].'</td>
                </tr>
                <tr id="last_row_task_'.$tee['task_id'].'_">
                    <td width="100%" >
                ';
            if(!empty($ac_person['line_ext'])){
                $modal .='<small>Ext: </small> '.$ac_person['line_ext'];
            }
            break;
        }
    }
    $modal .='</td><td>
                <input type="button" class="orange-btn" value="Task Complete" name="TaskCompleteSubmit" onclick="completeTask(\''.$tee['task_id'].'\', \''.$assign_id.'\' );  ">
                <input type="button" class="orange-btn" value="Delete Task " name="DeleteTaskSubmit" onclick="deleteTask('.$tee['task_id'].', '.$assign_id.')">
            </td>
        </tr>
        </tbody></table>
    </div>';        
    }
$modal .='</form>
      </div></div></div>';
echo $modal;
    }
}


if(isset($_GET['task']) && !empty($_GET['task']) && $_GET['task'] == 'add_comments'){

    $assign_id = $_POST['assign_id'];
    $event_id  = $_POST['event_id'];
    $client_id = $_POST['client_id'];
    $comments  = $_POST['comments'];
    $agent_id  = $_POST['agent_id'];
    $event = $obj_events_class->getEvent($event_id);
    $_client = $obj_clients_class->getClients($client_id);
    
    
    $coment_modal  = '<table style="table-layout: fixed; width: 100%; text-align:left;" class="modal-table">
            <tbody>
			<tr><td width="100%" ><small>Exhibition: </small><strong>'.$event['event_name'].' </strong></td></tr>
            <tr><td width="100%" style="border-top:none !important; "><small>Company:</small><strong> '.$a_client['company_name'].'</strong> </td></tr>';
    
    "$assign_id, $event_id, $client_id, $comments, $agent_id"; 
    if($comments != '' ){
        $obj_jobs_class->addEventComment($assign_id, $event_id, $client_id, $comments, $agent_id);
        $comment_events = $obj_jobs_class->getEventComments($assign_id, $event_id);
        foreach($comment_events as $cmn_eve){
        $coment_modal .= '
            

<tr><td width="100%">
                <span class="small-gray" style="width:100%;">'.$obj_jobs_class->change_date($cmn_eve['ts']).'</span><br>
                <i class="fa fa-angle-right"></i> '.$cmn_eve['comments'].'
            </td></tr>
            ';
        }
        
        echo $coment_modal .= '</tbody></table></form></div>';
    }
   
}


if(isset($_GET['getEditClient'])){
    
    $c_id = $_GET['client_id'];
    $persons = $obj_clients_class->getClientsPersons($c_id); 
    
?>
    
<div class="modal-dialog client-modal">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">×</button>
        <h4 class="modal-title">Contact Persons</h4>
      </div>
      <div class="modal-body">
          <form action="" method="post" id="edit_form">
                <input type="hidden" name="client_id" value="<?= $c_id ?>">
                <table style="table-layout: fixed; width: 100%" class="ttttt">
             <tbody> 
             
                 <tr><td colspan="2">
            
            
<?php if(count($persons)>0){$per=1; 
                foreach($persons as $ac_person){
                    $c_id = $ac_person['person_id'];
                ?> 
                         <table style="table-layout: fixed; width: 100%" class="ttttt" id="b_<?= $c_id ?>">
        <tr>
            <td width='50%'> 
                    <small> First Name: </small> 
                    <input type="text" name="f_name[<?= $c_id ?>]" value="<?php echo $ac_person['f_name'] ?>" >
            </td>
            <td width='50%'> 
                    <small> Last Name: </small> 
                    <input type="text" name="l_name[<?= $c_id ?>]" value="<?php echo $ac_person['l_name']; ?>" >
            </td>
            
        </tr>
        <tr>
        <td width='50%'> 
            <small>Designation: </small> 
            <input type="text" name="desig[<?= $c_id ?>]" value="<?php echo  $ac_person['designation']; ?>" >
        </td>
        <td width='50%'> 
        <small>Email: </small> 
        <input type="text" name="email[<?= $c_id ?>]" value="<?php echo str_replace(", ", ", ", $ac_person['email']); ?>">
        </td>
    </tr>
        <tr>
        <td width='50%'>
            <small>Mobile: </small> 
            <input type="text" name="mobile[<?= $c_id ?>]" value="<?php echo str_replace(", ", ", ",$ac_person['mobile']); ?>">
        </td>
                
        <td width='50%'> 
            <small>Land Line: </small>
            <input type="text" name="landline[<?= $c_id ?>]" value="<?php echo str_replace(", ", ", ",$ac_person['landline']); ?>">
        </td>
            </tr>
        <tr>
                <td width="50%" ><small>Ext: </small> <strong> <input type="text" name="ext[<?= $c_id ?>]" value="<?php echo $ac_person['line_ext']; ?>"> </strong></td>
                <td width="50%"> <a onclick="delPerson(<?= $c_id ?>);"> Delete </a> </td>
            </tr>
        <tr>  <td width="50%"><br></td>
            <td width="50%"> </td>
            </tr>
             </table>
<?php  }      }   ?>
                     </td></tr>
            <tr>  <td width="50%"><input type="submit" name="submit_edit" value="Update" > </td>
                  <td width="50%"> <br></td>
            </tr> 
            
        </tbody></table>
          </form>
      </div>
      
    </div>

  </div>


<script>
	$(document).ready(function(){
        $('#edit_form').submit(    function(e){
             e.preventDefault();
             var $form = $(this);
              $.ajax({
                   type: 'POST',
                   url: 'ajax.php?clientPersonUpdate',
                   data: $(this).serialize(),
                   success: function(response) {
						$("#persons_edit").modal('hide'); 
                   }
               });
           });  
        });
</script>

<?php    }     


if(isset($_GET['clientPersonUpdate'])){
        //echo 'clientPersonUpdate';
		
		echo "<pre>";
		print_r($_POST);
		echo "</pre>";
         foreach($_POST['f_name'] as $key=>$val){
            echo  $q = '  Update client_persons set f_name = "'.$_POST['f_name'][$key].'", l_name = "'.$_POST['l_name'][$key].'", designation = "'.$_POST['desig'][$key].'", email = "'.$_POST['email'][$key].'",  landline = "'.$_POST['landline'][$key].'", mobile = "'.$_POST['mobile'][$key].'" , line_ext = "'.$_POST['ext'][$key].'" where person_id= '.$key; //mobile
             $obj_clients_class->customQuery($q);
         }
}

if(isset($_GET['delPerson'])){
    
    $p = $_GET['p_id'];
    echo $q = '  Update client_persons set status = "Deactive" where person_id= '.$p;
    $obj_clients_class->customQuery($q);
    
}


if(isset($_GET['checkSMS'])){
    
    $event  = $_GET['event'];
    $agent = $_GET['agent'];
    $col = $_GET['type'];
    $date = date("Y-m-d H:i:s");
   
    $assg = $obj_jobs_class->getAssignClients($event, $agent);
    
    foreach($assg as $ag){
        $obj_jobs_class->addClientsAssignDetails($ag['assign_id'], $col, $date);
    }
    echo $obj_jobs_class->change_date_hours($date);
}

?> 