<?php include("_config/dbconfig.php"); ?>
<?php include("_includes/head.php");

########################################################
# Login information for the SMS Gateway
########################################################
$username   = "eventage_sms"; // your username
$password   = "dell1234"; // your Password
$smsurl     = "http://sms.senseflow.pk:80/api/?"; // change smsdomain.com to your provided



########################################################
# Functions used to send the SMS message
########################################################
    function httpRequest($url){
        $args;
        $pattern = "/http...([0-9a-zA-Z-.]*).([0-9]*).(.*)/";
        preg_match($pattern,$url,$args);
        $in = "";
        $fp = fsockopen("$args[1]", $args[2], $errno, $errstr, 30);
        if (!$fp)
        {
           return("$errstr ($errno)");
        }
        else
        {
            $out = "GET /$args[3] HTTP/1.1\r\n";
            $out .= "Host: $args[1]:$args[2]\r\n";
            $out .= "User-agent: PHP Web SMS client\r\n";
            $out .= "Accept: */*\r\n";
            $out .= "Connection: Close\r\n\r\n";

            fwrite($fp, $out);
            while (!feof($fp))
            {
               $in.=fgets($fp, 128);
            }
        }
        fclose($fp);
        return($in);
    }
      
    function send_sms($phone, $msg, $debug=false,$sender){
        
        global $username,$password,$smsurl;
        $url  = 'username='.$username;
        $url  .= '&password='.$password;
        $url  .= '&receiver='.urlencode($phone);
		$url  .= '&sender='.urlencode($sender);
        $url  .= '&msgdata='.urlencode($msg);
        $urltouse =  $smsurl.$url;
        if($debug)
        {
            echo "Request: <br>$urltouse<br><br>";
        }
        //Open the URL to send the message
        $response = httpRequest($urltouse);
        if ($debug)
        {
            echo "Response: <br><pre>".
            str_replace(array("<",">"),array("&lt;","&gt;"),$response).
            "</pre><br>";
        }
        return($response); 
    }
    
    

?>
<title>Send SMS</title>

<style>
    .table-bordered > thead > tr > th, .table-bordered > thead > tr > td, .table-bordered  tr  td {
       // border-width: 1px !important; 
        //border: 1px solid #676767 !important; 
}
 
.table-bordered td, .table-bordered th{
    border: 1px solid #ccc !important;
}

.iner-table td, .iner-table  th{
    border: none;
    border-left: none !important;
    border-right: none !important;
    border-bottom: 1px dotted #ccc !important;
}

  .boxscroll {
	height: 500px;
	border: 0px solid #000;
	overflow: auto;
    }
</style>


<?php include("_includes/header.php");

if(isset($_SESSION['user_session'])){
$agent_id = $_SESSION['user_session'];
}else {
$agent_id = 0;	
}

$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;
}

$sms_tmp =  $obj_jobs_class->getSmsTemp();
  

 
  
if(isset($_GET['event']) && !empty($_GET['event'])){
    
    $exhibition_id = base64_decode($_GET['event']); 
    $event = $obj_events_class->getEvent($exhibition_id);
    $AssignClients =  $obj_jobs_class->getAssignClients($exhibition_id, $agent_id);
  
}
else {
    
     echo "<script>window.location='events.php'</script>";
     die();
}

if(isset($_POST['send_sms'])){
     
    $status     = $_POST['status'];
    $sms_text   = $_POST['temp_text'];
    $sender     = $_POST['sender'];
     
    foreach($AssignClients as $a_client){
        
        if($status == 'all'){
                    $ac_person1 = array();
                    $sms_text   = $_POST['temp_text'];
                    $sender     = $_POST['sender'];
            foreach($a_client['persons'] as $ac_person){

                $debug = false;
                $p_name = $ac_person['f_name'].' '.$ac_person['l_name'];

                $_exhi  = $event['event_name'];

                $sms_text = str_replace("%name%", $p_name, $sms_text);
                $sms_text = str_replace("%exhibition%", $_exhi, $sms_text);

                $message = $sms_text;
                $phonenum = explode(',', $ac_person['mobile']);
                if(is_array($phonenum)){
                    foreach ($phonenum as $pp){
                          $pp.'<br>';
                         send_sms($pp,$message,$debug, $sender);
                    }
                }else {   $phonenum.'<br>';
                        send_sms($phonenum,$message,$debug, $sender);
                } 
          }  
        }else {
                if($a_client['status_order'] == $status){ 
                    $ac_person1 = array();
                    $sms_text   = $_POST['temp_text'];
                    $sender     = $_POST['sender'];
                    foreach($a_client['persons'] as $ac_person){

                        $debug = false;
                        $p_name = $ac_person['f_name'].' '.$ac_person['l_name'];

                        $_exhi  = $event['event_name'];

                        $sms_text = str_replace("%name%", $p_name, $sms_text);
                        $sms_text = str_replace("%exhibition%", $_exhi, $sms_text);

                        $message = $sms_text;
                        $phonenum = explode(',', $ac_person['mobile']);
                        if(is_array($phonenum)){
                            foreach ($phonenum as $pp){
                                  $pp.'<br>';
                                send_sms($pp,$message,$debug, $sender);
                            }
                        }else {   $phonenum.'<br>';
                                send_sms($phonenum,$message,$debug, $sender);
                        } 
                      }
                }
                else if($a_client['pipeline'] ==    $status ){
                    $ac_person1 = array();
                    $sms_text   = $_POST['temp_text'];
                    $sender     = $_POST['sender'];
                     foreach($a_client['persons'] as $ac_person1){
                            $debug = false;
                            $p_name = $ac_person1['f_name'].' '.$ac_person1['l_name'];

                            $_exhi  = $event['event_name'];

                            $sms_text = str_replace("%name%", $p_name, $sms_text);
                            $sms_text = str_replace("%exhibition%", $_exhi, $sms_text);

                            $message = $sms_text;
                            $phonenum = explode(',', $ac_person1['mobile']);
                            if(is_array($phonenum)){
                                foreach ($phonenum as $pp){
                                      $pp.'<br>';
                                    send_sms($pp,$message,$debug, $sender);
                                }
                            }else {   $phonenum.'<br>';
                                    send_sms($phonenum,$message,$debug, $sender);
                            } 
                      }
                }
            }
      
    }    
      
       echo "<script>window.location='send_sms.php?event=".$_GET['event']."'</script>";
    die();      
         
    
  /*  echo '<pre>';
    print_r($_POST);
    echo '</pre>';*/
    
   // die();
}

$modal = '';  
?>

      
<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']);
                        
                        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><a href="assign_client_step2.php?event=<?= $_GET['event']?>">Assign Client Step 2</a></li>
              <li class="active">Send SMS</li>
        </ol>
    
    
    
<div class="col-sm-12 a_client">
     
	<div class="row">
      <div class="col-sm-12">
          
          
           <div class="table-result table-responsive col-sm-6 boxscroll">  

          <form action="<?php echo $link;?>" method="POST" id="assignForm">

		<table class="table table-bordered">
                    <thead>
                            <tr>
                            	<th>Sr. </th>
                                <th>Company Name </th>
				<th style="width:300px;">Contact Persons </th>
                            </tr>
                        </thead>
                	<tbody>
                        
<?php $i =1;

$count_comp = 0;  $count_deny = 0;  $count_pip= 0; $count_pen = 0;
$count_hot = 0;  $count_warm= 0;  $count_cold= 0; 

foreach($AssignClients as $a_client){
    
        $row_color = '';
        $assign_id = $a_client['assign_id'];
        $assign_CallSMSEmail = $obj_jobs_class->getClientsAssignDetails($a_client['assign_id']);
        if($a_client['status_order'] == 1){$row_color = 'style="background-color:#BBEF7E;"'; $count_comp +=1;}
        else if($a_client['status_order'] ==  0){$row_color = 'style="background-color:#FFAAAA"'; $count_deny +=1;}
        else if($a_client['status_order'] ==  4){$row_color = 'style="background-color:#FDF704"'; $count_pip +=1;}
        else if($a_client['status_order'] ==  2){$count_pen +=1;}
        
         if($a_client['pipeline']==80 && $a_client['status_order']==4 ){ $count_hot+=1;} ;
         if($a_client['pipeline']==50 && $a_client['status_order']==4){ $count_warm+=1;} ; 
         if($a_client['pipeline']==10 && $a_client['status_order']==4) { $count_cold+=1;};
            
    ?>
                            
<tr <?=$row_color?>>
    <td><?= $i++ ?></td>
    
    <td><?php echo $a_client['company_name'] ?> </td>
    
    <td><?php 
    if(isset($a_client['persons']))
    if(count($a_client['persons'])>0){
                foreach($a_client['persons'] as $ac_person){
                        echo $ac_person['f_name'].' '.$ac_person['l_name'].'('.$ac_person['designation'].') / ';
                }
        ?>
            <a data-toggle="modal" data-target="#persons_<?= $a_client['client_id']?>" style="cursor: pointer;" href="#">
                <i class='fa fa-list-ul'></i>
            </a>
       <?php  } ?>
                                

<div id="persons_<?= $a_client['client_id']?>" class="modal fade left-align" role="dialog" >

  <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">
	  
          <table   style="table-layout: fixed; width: 100%" class="ttttt iner-table">
             <tbody><tr>
                 <td width="50%" style="border-top:none !important; "><small>Company: </small> <strong> <?php echo $a_client['company_name'] ?></strong> </td>
                <td width="50%" style="border-top:none !important; "><small>Owner's Name: </small> <strong> <?php echo $a_client['owner_name'] ?> </strong> </td>
            </tr>
             
            <tr>  <td width="50%"><br></td>
                  <td width="50%"> <br></td>
            </tr>
            
            
<?php if(count($a_client['persons'])>0){$per=1; 
                foreach($a_client['persons'] as $ac_person){ ?> 
              
            <tr>
                <td colspan="2" width="100%" style="background-color: #eee;">  <strong>Contact Persons <?php echo $per++; ?></strong></td>
            </tr>
            
            <tr>
                <td width="50%"> <small>Name: </small> <strong> <?php echo $ac_person['f_name'].' '.$ac_person['l_name']; ?> </strong> </td>
                <td width="50%"><small>Designation: </small> <strong> <?php echo $ac_person['designation']; ?> </strong></td>
            </tr>
            <tr>
                <td width="50%" colspan="2"> <small>Email: </small> <strong>  <?php echo $ac_person['email']; ?> </strong> </td>
            </tr>
            
            <tr>
                <td width="50%"><small>Mobile: </small> <strong>  <?php echo $ac_person['mobile']; ?> </strong></td>
                <td width="50%"> <small>Land Line: </small> <strong>  <?php echo $ac_person['landline']; ?> </strong> </td>
            </tr>
            <tr>
                <td width="50%" colspan="2"><small>Ext: </small> <strong> <?php echo $ac_person['line_ext']; ?> </strong></td>
            </tr>
            
<?php  }      }   ?>     
            
        </tbody></table>
        
      </div>
      
    </div>

  </div>
</div>          

       </td> 
           
            </tr>
            <?php } ?>
            </tbody>
                </table>  
        </form>  

<?php echo $modal;?>
          
      </div>
	   
    <form class="col-sm-6 form-box" id="agent_form" method="post" action="" enctype="multipart/form-data" >
        
                <div class="row"> 
                      <div class="col-md-12 col-sm-12 form-group">
                           <label for=" ">Select Sender</label>
                           
                    <select name="sender" id="sender"  class="form-control" required="required">
                           <option value="">---Select---</option> 
                        <option value="EVENTAGE"> EVENTAGE </option>
                        <option value="MARMOMACC"> MARMOMACC </option>
                        <option value="Deco Fair"> Deco Fair </option>
                        <option value="Saudi Build"> Saudi Build </option>
                        <option value="Food Africa"> Food Africa </option>
                        <option value="FoodexSaudi"> FoodexSaudi </option>
                        <option value="Spring Fair"> Spring Fair </option>
                        <option value="Cosmobeaute"> Cosmobeaute </option>
                        <option value="SIAL"> SIAL </option>
                        <option value="SEAFEX"> SEAFEX </option>
                        <option value="INDEX"> INDEX </option>
                        <option value="HKTDC"> HKTDC </option>
                        <option value="ExpoProtect"> ExpoProtect </option>
                        <option value="Aspire"> Aspire </option>
                        <option value="HomeTex"> HomeTex </option>
                        <option value="NHSExpo"> NHSExpo </option>
                        <option value="AutoExpo"> AutoExpo </option>                               
                    </select>   
                    </div>
                    <div class="col-md-12 col-sm-12 form-group">
                                <label for=" ">Select Status</label>
                                <select class="form-control" name="status" required="required" >
                                        <option value="">---Select---</option> 
                                        <option value="all">All (<?php echo count($AssignClients)?>)</option> 
                                        <option value="1">Accept (<?=  $count_comp ?>)</option> 
                                        <option value="2">Pending (<?=  $count_pen ?>)</option> 
                                        <option value="80">Hot (<?=  $count_hot ?>)</option>
                                        <option value="50">Warm (<?=  $count_warm ?>)</option>
                                        <option value="10">Cold (<?=  $count_cold ?>)</option>
                                </select> 
                        </div>
                    
                    
                    
                        <div class="col-md-12 col-sm-12 form-group">
                                <label for=" ">Select Templete</label>
                                <select class="chosen-select" name="category" id="sms_select" required="required" onchange="setSms();">
                                    <option value="">---Select---</option> 
                                    <?php foreach ($sms_tmp as $ssm){?>
                                    <option value="<?php echo $ssm['text']?>"><?php echo $ssm['title'] ;?></option> 
                                    <?php }?>
                                </select> 
                        </div>
                        
                    <div class="clearfix"></div>
                    
                        <div class="col-md-12 col-sm-12 form-group">
                                <label for=" ">Templete Text</label>
                                <textarea id="sms_box" type="text" rows="5" class="form-control" required="required" name="temp_text"></textarea>
                        </div> 
                      
                    <div class="clearfix"></div> 
                                         
                <div class="col-sm-12 form-group">
                <button type="submit" class="btn btn-default" name="send_sms"> Send </button>
            </div>
                                            
		</div>
    </form>
           
   
	  </div>
    </div>
</div>	
		
</div>


<?php include("_includes/footer.php"); ?>
<script>
    
    function completeTask(id){
        $.post('ajax.php?id='+id+'&task=complete_task', function(data){
            console.log(data);
             if(data=='1'){
                     location.reload();
                  }
        })
    }




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

        $('#CallAll').click(function(){
            
            console.log($(this).attr('checked'));
            if($(this).is(':checked')){
                $('input:checkbox.all_call').attr('checked',true);
                $('.all_call').css('display', 'none');
                $('.sellect_call').css('display', 'inline-block');
            }
            else{  
                $('input:checkbox.all_call').attr('checked',false);
                $('.sellect_call').css('display', 'none');    
                $('.all_call').css('display', 'inline-block')
            }
        });
        
        $('#SMSAll').click(function(){
            
            console.log($(this).attr('checked'));
            if($(this).is(':checked')){
                $('input:checkbox.all_sms').attr('checked',true);
                $('.all_sms').css('display', 'none');
                $('.sellect_sms').css('display', 'inline-block');
            }
            else{  
                $('input:checkbox.all_sms').attr('checked',false);
                $('.sellect_sms').css('display', 'none');    
                $('.all_sms').css('display', 'inline-block')
            }
        });

	$('#EmailAll').click(function(){
            
            console.log($(this).attr('checked'));
            if($(this).is(':checked')){
                $('input:checkbox.all_email').attr('checked',true);
                $('.all_email').css('display', 'none');
                $('.sellect_email').css('display', 'inline-block');
            }
            else{  
                $('input:checkbox.all_email').attr('checked',false);
                $('.sellect_email').css('display', 'none');    
                $('.all_email').css('display', 'inline-block')
            }
        });
    });

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

function setSms(){
    // 
        var stat = $("#sms_select").val();
        if(stat !=''){
            $("#sms_box").val(stat);
        }
}

 
</script>


<script>
$(document).ready(function(){
    $(".start_date").datepicker({
        dateFormat: 'yy-mm-dd',
        numberOfMonths: 1,
        onSelect: function(selected) {
          $(".end_date").datepicker("option","minDate", selected)
        }
    });
    $(".end_date").datepicker({
        dateFormat: 'yy-mm-dd',
        numberOfMonths: 1,
        onSelect: function(selected) {
           //$("#start_date").datepicker("option","maxDate", selected)
        }
    });  
});
</script>
 