<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
global $configverssion_id; global $configapp_version; global $userID; global $server; global $LANG; global $resellerID;
define('TITLE','Compose');
if(!isAdmin(getUser()) && (userData(getUser(),'phone') == '' || userData(getUser(),'first_name') == '') ) {
	header('location: profile?setting'); exit();
}
if(!isAdmin(getUser()) && userData($userID,'email_verified') != 0) {
	showVerify();
}
$today = date('Y-m-d'); $time = strtotime($today.'-1 months'); $lastMonth = date("Y-m", $time); $time = strtotime($today.'-1 years'); $lastYear = date("Y", $time);
if(userRole()>2) {
	header('location: index.php');	
}
hasModulePermission('composer');
//set defaults
$type = @$_GET['t'];
if(!isset($_GET['t'])) $type = 'sms';
$country_id = getSetting('defaultCountry');
$duration = 0;
$sender_id = '';
$message = '';
$to = '';
$language = 'en';
$phonebook_id = 0;
$marketinglist_id = 0;
if(isset($_REQUEST['to'])) {
	$to = mysqli_real_escape_string($server,$_REQUEST['to']);
}
if(isset($_REQUEST['draftid'])) {
	$did = mysqli_real_escape_string($server,$_REQUEST['draftid']);
	$query="SELECT * FROM draftmessages WHERE id = '$did'"; 
	$result = mysqli_query($server,$query) or die(mysqli_error($server));  
	$row = mysqli_fetch_assoc($result); 
	if($row['id'] > 0) {
		$type = $row['type'];
		$country_id = $row['country'];
		$duration = $row['duration'];
		$sender_id = $row['sender_id'];
		$message = $row['message'];
		$to = $row['recipients'];		
	} else {
		header('location: adraft');	
	}
}
if(isset($_REQUEST['schedid'])) {
	$did = mysqli_real_escape_string($server,$_REQUEST['schedid']);
	$query="SELECT * FROM scheduledmessages WHERE id = '$did'"; 
	$result = mysqli_query($server,$query) or die(mysqli_error($server));  
	$row = mysqli_fetch_assoc($result); 
	if($row['id'] > 0) {
		$type = $row['type'];
		$country_id = $row['country'];
		$duration = $row['duration'];
		$sender_id = $row['sender_id'];
		$message = $row['message'];
		$to = $row['recipients'];		
	} else {
		header('location: ascheduled');	
	}
}
if(isset($_REQUEST['senid'])) {
	$did = mysqli_real_escape_string($server,$_REQUEST['senid']);
	$query="SELECT * FROM sentmessages WHERE id = '$did'"; 
	$result = mysqli_query($server,$query) or die(mysqli_error($server));  
	$row = mysqli_fetch_assoc($result); 
	if($row['id'] > 0) {
		$type = $row['type'];
		$country_id = $row['country'];
		$duration = $row['duration'];
		$sender_id = $row['sender_id'];
		$message = $row['message'];
		$to = $row['recipients'];
		//add contacts		
		$phonebook_id = $row['phonebook_id'];
		$marketinglist_id = $row['marketinglist_id'];
		if(!empty($row['files'])) {
			$to .= ','.getFileContacts($files);
		}	
	} else {
		header('location: asent');	
	}
}
if(isset($_REQUEST['rsfai'])) {
	$did = mysqli_real_escape_string($server,$_REQUEST['rsfai']);
	$query="SELECT * FROM sentmessages WHERE id = '$did'"; 
	$result = mysqli_query($server,$query) or die(mysqli_error($server));  
	$row = mysqli_fetch_assoc($result); 
	if($row['id'] > 0) {
		$type = $row['type'];
		$country_id = $row['country'];
		$duration = $row['duration'];
		$sender_id = $row['sender_id'];
		$message = $row['message'];
		$to = '';
		$query2="SELECT * FROM messagedetails WHERE message_id = '$row[id]' AND status = 'failed'";
		$result2 = mysqli_query($server,$query2) or die(mysqli_error($server));
		while($rows2 = mysqli_fetch_assoc($result2)) {
			$to .= $rows2['recipient'].',';
		} $to = rtrim($to,',');
	} else {
		header('location: asent');	
	}
}
if(isset($_REQUEST['cust'])) {
	if(!isReseller($userID) && !isAdmin($userID)) {
		header('location: acompose');
	}
	$query2="SELECT * FROM users WHERE reseller = '0'";
	$result2 = mysqli_query($server,$query2) or die(mysqli_error($server));
	while($rows2 = mysqli_fetch_assoc($result2)) {
		$to .= $rows2['phone'].',';
	} $to = rtrim($to,',');
}
if(isset($_REQUEST['rscan'])) {
	$did = mysqli_real_escape_string($server,$_REQUEST['rscan']);
	$query="SELECT * FROM sentmessages WHERE id = '$did' AND status = 'canceled'"; 
	$result = mysqli_query($server,$query) or die(mysqli_error($server));  
	$row = mysqli_fetch_assoc($result); 
	if($row['id'] > 0) {
		$type = $row['type'];
		$country_id = $row['country'];
		$duration = $row['duration'];
		$sender_id = $row['sender_id'];
		$message = $row['message'];
		$to = '';
		$query2="SELECT * FROM messagedetails WHERE message_id = '$row[id]' AND status = 'canceled'";
		$result2 = mysqli_query($server,$query2) or die(mysqli_error($server));
		while($rows2 = mysqli_fetch_assoc($result2)) {
			$to .= $rows2['recipient'].',';
		} $to = rtrim($to,',');
	} else {
		header('location: asent');	
	}
}

if(isset($_REQUEST['smid'])) {
	$did = mysqli_real_escape_string($server,$_REQUEST['smid']);
	$query="SELECT * FROM messagedetails WHERE id = '$did'"; 
	$result = mysqli_query($server,$query) or die(mysqli_error($server));  
	$row = mysqli_fetch_assoc($result); 
	if($row['id'] > 0) {
		$type = $row['type'];
		$country_id = $row['country'];
		$duration = $row['duration'];
		$sender_id = $row['sender_id'];
		$message = $row['message'];
		$to = $row['recipient'];		
	} else {
		header('location: asent');	
	}
}

if(isset($_REQUEST['inbid'])) {
	$did = mysqli_real_escape_string($server,$_REQUEST['inbid']);
	$query="SELECT * FROM inbox WHERE id = '$did'"; 
	$result = mysqli_query($server,$query) or die(mysqli_error($server));  
	$row = mysqli_fetch_assoc($result); 
	if($row['id'] > 0) {
		$to = $row['sender'];		
	} else {
		header('location: ainbox');	
	}
}

if(isset($_REQUEST['coid'])) {
	$did = mysqli_real_escape_string($server,$_REQUEST['coid']);
	$query="SELECT * FROM contacts WHERE id = '$did'"; 
	$result = mysqli_query($server,$query) or die(mysqli_error($server));  
	$row = mysqli_fetch_assoc($result); 
	if($row['id'] > 0) {
		$to = $row['phone'];
		$phonebookid = $row['phonebook_id'];	
	} else {
		header('location: aphonebook');	
	}
}
if(isset($_REQUEST['pbid'])) {
	$phonebook_id = mysqli_real_escape_string($server,$_REQUEST['pbid']);
}
if(isset($_REQUEST['mkcoid'])) {
	$did = mysqli_real_escape_string($server,$_REQUEST['mkcoid']);
	$query="SELECT * FROM marketingcontacts WHERE id = '$did'"; 
	$result = mysqli_query($server,$query) or die(mysqli_error($server));  
	$row = mysqli_fetch_assoc($result); 
	if($row['id'] > 0) {
		$to = $row['phone'];
		$marketinglist = $row['marketinglist'];	
	} else {
		header('location: marketinglist');	
	}
}
if(isset($_REQUEST['stspbc'])) {
	$to = @$_SESSION['contacts'];
}
if(isset($_REQUEST['mktid'])) {
	$marketinglist_id = mysqli_real_escape_string($server,$_REQUEST['mktid']);
}

?>

  <div class="row no-m-t no-m-b">
        <div class="s12">
<?php
	$vars=array("user_id"=>getUser(),"message_type"=>$type); $_SESSION['EventVals'] = $vars; global $hooks; $hooks->do_action('BeforeSMSCompose');
?>
      	</div>  
 </div>

 <div class="row no-m-t no-m-b">
 <!-- main area -->
 <form action="" id="composer-form">
 	<div class="col s12 m7 l8">
        <div class="card white">
        
        	<div class="card-content">
            	<div class="row no-m-t no-m-b">
            	 <div class="input-field col s12">
                       <select id="message_type" required class="validate">
                            <option value="" disabled selected><?=$LANG['Select the type of message you want to send']?></option>
                            <?php if(getSetting('TextEnabled',0)>0){?>
                            <option value="sms" <?php if($type=='sms') echo 'selected';?>><?=$LANG['Text Message']?></option>
                            <?php }?>
                            <?php if(getSetting('unicodeEnabled',0)>0){?>
                            <option value="unicode" <?php if($type=='unicode') echo 'selected';?>><?=$LANG['Unicode Message']?></option>
							<?php }?>
                            <?php if(getSetting('voiceEnabled',0)>0){?>
                            <option value="voice" <?php if($type=='voice') echo 'selected';?>><?=$LANG['Voice Message']?></option>
							<?php } ?>
                            <?php if(getSetting('watsappEnabled',0)>0){?>
                            <option value="whatsapp" <?php if($type=='whatsapp') echo 'selected';?>><?=$LANG['WhatsApp Message']?></option>
							<?php } ?>
                            <?php if(getSetting('mmsEnabled',0)>0){?>
                            <option value="mms" <?php if($type=='mms') echo 'selected';?>><?=$LANG['Multimedia Message']?></option>
							<?php } ?>
                            <?php if(getSetting('flashEnabled',0)>0){?>
                            <option value="flash" <?php if($type=='flash') echo 'selected';?>><?=$LANG['Flash Message']?></option>
							<?php } ?>
                       </select>
                       <label><?=$LANG['Message Type']?></label>
                 </div>
               </div>
               
               <div class="row no-m-t no-m-b">
            	 <div class="input-field col s12">
                       <select id="s_gateway_id" class="validate js-states browser-default">
                            <option value="0" selected><?=$LANG['Standard Routing Rule']?></option>
                            <?=getSelectRoutes($type)?>
                       </select>
                       <label class="active"><?=$LANG['Select Route']?></label>
                 </div>
               </div>

<?php
$maxlenhh = "";
if(getSetting('senderIDLimit',0)>0) {
	$maxlenhh = 'maxlength="'.getSetting('senderIDLimit',0).'"';
}
?>               
               <div class="row no-m-t no-m-b">
            	 <div class="input-field col s12">
                       <input id="sender_id" type="text" <?=$maxlenhh?> class="validate" value="<?=$sender_id?>">
                       <label for="sender_id"><?=$LANG['Sender ID']?></label>
                 </div>
               </div>
               <div class="row no-m-t no-m-b">
            	 <div class="input-field col s12">
                       <select id="sender_id2" class="validate js-states browser-default">
                            <option value="" selected><?=$LANG['Select Shortcode to use']?></option>
                            <?=getShortCodeList(getUser())?>
                       </select>
                       <label class="active"><?=$LANG['Or Use Shortcode']?></label>
                 </div>
               </div>

               <div class="row no-m-t no-m-b">
            	 <div class="input-field col s12">
                       <select id="country" required class="js-states browser-default"  style="width: 100%" >
                            <option <?php if($country_id=='0') echo 'selected';?> value="0"><?=$LANG['Multiple Destinations']?></option>
                            <?=getCountryList($country_id) ?>                            
                       </select>
                       <label class="active"><?=$LANG['Destination Country']?></label>
                 </div>
               </div>
               
               <div class="row no-m-t no-m-b">
                 <div class="input-field col s12">
                 	<h6 class="m-t-md"><?=$LANG['Recipient Source']?></h6>
                     <ul class="tabs tab-demo z-depth-1" style="width: 100%;">
                         <li class="tab col s3"><a onclick="showTL();hideFL();" href="#number" class=""><i class="fa fa-keyboard-o hide-on-med-and-up"></i> <span class="hide-on-small-only"><?=$LANG['Numbers']?></span></a></li>
                         <li class="tab col s3"><a onclick="hideTL();hideFL();" class="" href="#phonebook"><i class="fa fa-address-book hide-on-med-and-up"></i> <span class="hide-on-small-only"><?=$LANG['Addressbook']?></span></a></li>
                         <li class="tab col s3"><a onclick="hideTL();hideFL();" href="#list"><i class="fa fa-rss-square hide-on-med-and-up"></i> <span class="hide-on-small-only"><?=$LANG['Marketing List']?></span></a></li>
                         <li class="tab col s3"><a onclick="hideTL();showFL();" href="#file"><i class="fa fa-upload hide-on-med-and-up"></i> <span class="hide-on-small-only"><?=$LANG['File']?></span></a></li>
                     </ul>
                      <h6 class="m-t-md red-text darken-4" id="to_lab"><?=$LANG['type_recipinet_notice']?></h6>
                      <h6 class="m-t-md red-text darken-4" id="file_lab"><?=$LANG['upload_recipinet_notice']?></h6>
                 </div>
                 
                 <div id="number" class="input-field col s12">
                      <textarea id="to" class="materialize-textarea"><?=$to?></textarea>
                      <label for="to"><?=$LANG['Type Recipients']?></label>

                 </div>
                 
                 <div id="phonebook" class="input-field col s12">
                      <select multiple id="addressbook" class="js-states browser-default"  style="width: 100%" >
                            <?=listAddressbook(0,$phonebook_id) ?>                            
                       </select>
                       <label class="active"><?=$LANG['Addressbooks']?></label>
                 </div>

                 <div id="list" class="input-field col s12">
                     <select multiple id="marketing_list" class="js-states browser-default"  style="width: 100%" >
                            <?=listMarketingList(0,$marketinglist_id) ?>                            
                       </select>
                       <label class="active"><?=$LANG['Marketing Lists']?></label>
                 </div>
                 
                 <div id="file" class="file-field input-field col s12">
                    <div class="btn light-blue darken-4">
                    	<span><i class="fa fa-cloud-upload"></i></span>
                    	<input id="files" type="file" name="files" accept=".csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
                 	</div>
                 	<div class="file-path-wrapper">
                 		<input class="file-path validate" type="text" placeholder="<?=$LANG['Upload an Excel or CSV file']?>">
                        <input type="hidden" id="files_holder">
                   </div>
                   
                 </div>                 

               </div>
               
               <div class="row no-m-t no-m-b">
               <?php
					global $hooks; $hooks->do_action('InSMSComposeForm');
				?>               
				</div>
                
               <div class="row no-m-t no-m-b">
            	 <div class="input-field col s12">
                      <textarea id="message" class="materialize-textarea" length="960"><?=$message?></textarea>
                      <label id="mess_lab" for="message"><?=$LANG['Message']?></label>
                 </div>
               </div>
               
               <!-- voice file for voice -->
               <div class="row no-m-t no-m-b show-voice">
                 <h6 class="m-t-md gray-text darken-4" id="voice_lab"><b><?=$LANG['voice_media_notice']?></b></h6>
                <div class="file-field input-field col s12">
                    <div class="btn light-blue darken-4">
                    	<span><i class="fa fa-cloud-upload"></i></span>
                    	<input id="voice_file" type="file" name="voice_file" accept="audio/*">
                 	</div>
                 	<div class="file-path-wrapper">
                 		<input class="file-path validate" type="text" placeholder="<?=$LANG['Upload a WAV or MP3 file']?>">
                        <input type="hidden" id="voice_holder">
                   </div>
                 </div>
               </div>  
               <div class="row no-m-t no-m-b show-voice">
                <div id="list" class="input-field col s12">
                     <select id="duration" class="" >
                            <option <?php if($duration=='0') echo 'selected';?> value="0" disabled><?=$LANG['Select Message Duration']?></option>
                            <option <?php if($duration=='10') echo 'selected';?> value="10">10 <?=$LANG['seconds']?></option>
                            <option <?php if($duration=='15') echo 'selected';?> value="15">15 <?=$LANG['seconds']?></option>
                            <option <?php if($duration=='30') echo 'selected';?> value="30">30 <?=$LANG['seconds']?></option>
                            <option <?php if($duration=='45') echo 'selected';?> value="45">45 <?=$LANG['seconds']?></option>
                            <option <?php if($duration=='60') echo 'selected';?> value="60">1 <?=$LANG['minute']?></option>
                            <option <?php if($duration=='90') echo 'selected';?> value="90">1 <?=$LANG['minute']?>30 <?=$LANG['seconds']?></option>
                            <option <?php if($duration=='120') echo 'selected';?> value="120">2 <?=$LANG['minutes']?></option>
                            <option <?php if($duration=='150') echo 'selected';?> value="150">2 <?=$LANG['minutes']?> 30 <?=$LANG['seconds']?></option>
                            <option <?php if($duration=='180') echo 'selected';?> value="180">3 <?=$LANG['minutes']?></option>
                            <option <?php if($duration=='210') echo 'selected';?> value="210">3 <?=$LANG['minutes']?> 30 <?=$LANG['seconds']?></option>
                            <option <?php if($duration=='240') echo 'selected';?> value="240">4 <?=$LANG['minutes']?></option>
                            <option <?php if($duration=='270') echo 'selected';?> value="270">4 <?=$LANG['minutes']?> 30 <?=$LANG['seconds']?></option>
                            <option <?php if($duration=='300') echo 'selected';?> value="300">5 <?=$LANG['minutes']?></option>
                       </select>
                       <label><?=$LANG['Call Duration']?></label>
                 </div>
               </div>  
               <div class="row no-m-t no-m-b show-voice">
            	 <div class="input-field col s12">
                       <input id="language" type="text" class="validate" value="<?=$language?>">
                       <label for="language"><?=$LANG['Voice Language (eg. en for English, etc)']?></label>
                 </div>
               </div>
               
               <!-- multimedia file for mms -->
               <div class="row no-m-t no-m-b show-mms show-whatsapp">
                <h6 class="m-t-md gray-text darken-4" id="media_lab"><b><?=$LANG['upload_media_notice']?></b></h6>
                <div class="file-field input-field col s12">
                    <div class="btn light-blue darken-4">
                    	<span><i class="fa fa-cloud-upload"></i></span>
                    	<input id="media_file" name="media_file" type="file" accept="audio/*, video/*, image/*">
                 	</div>
                 	<div class="file-path-wrapper">
                 		<input class="file-path validate" type="text" placeholder="<?=$LANG['Add an Audio, Video or Image file']?>">
                        <input type="hidden" id="media_holder">
                   </div>
                 </div>
               </div>  
               
               <div class="row no-m-t no-m-b">
                  <div class="input-field col s12">
                  <label class=""><?=$LANG['Schedule Message']?></label><br>
                  </div>
                </div>
                
               <div class="row">  
                 <div class="input-field col s12 m6 l6">
                   <div class="switch m-b-md">
                     <label id="sch_swit" class="black-text"><?=$LANG['Send Now']?> <input type="checkbox" id="schedule"><span class="lever"></span><?=$LANG['Send Later']?></label>
                   </div>
                 </div>  
                 <div class="input-field col s12 m3 l3" id="sch_date" style="display: none;">
                 	<input id="date" type="text" class="datepicker" required value="<?=date('j F, Y')?>">
                    <label class="active" for="date"><?=$LANG['Date']?></label>
                 </div>
                 <div class="input-field col s12 m3 l3" id="sch_time" style="display: none;">
                 	<input id="time" type="text" class="masked validate" data-inputmask="'mask': 'h:s'" placeholder="" required value="<?=date('H:i')?>">
                    <label class="active" for="time"><?=$LANG['Time']?> (HH:mm)</label>
                 </div>
               </div>  
               <div id="rep_b" class="input-field col s12 m6" style="display:none;">
                     <select id="repeats" class="" >
                         <option value="0"><?=$LANG['No Repeat']?></option>
                         <option value="every hour" ><?=$LANG['Every Hour']?></option>
                         <option value="every 3 hours" ><?=$LANG['Every 3 Hours']?></option>
                         <option value="every 6 hours" ><?=$LANG['Every 6 Hours']?></option>
                         <option value="every 12 hours" ><?=$LANG['Every 12 Hours']?></option>
                         <option value="daily" ><?=$LANG['Daily']?></option>
                         <option value="weekly" ><?=$LANG['Weekly']?></option>
                         <option value="monthly" ><?=$LANG['Monthly']?></option>
                         <option value="yearly" ><?=$LANG['Yearly']?></option>
                     </select>
                     <label><?=$LANG['Repeat Message']?></label>
               </div>    
            </div>
			
            <!-- Hook -->
            <div class="row no-m-t no-m-b">
                 <div class="s12">
            <?php
			$vars=array("user_id"=>getUser(),"message_type"=>$type); $_SESSION['EventVals'] = $vars; global $hooks; $hooks->do_action('AfterSMSCompose');
			?>
             	</div>  
            </div>
            
            
            <div class="card-action">
                <a class="btn waves-effect waves-light red" href="dashboard"><?=$LANG['Cancel']?></a>
                <button id="save-button" class="btn blue" type="submit"><?=$LANG['Save']?> </button>
                <span id="save-spin" class="btn-flat" style="display: none;"><i class="fa fa-spinner fa-spin"></i> <strong><?=$LANG['Please wait']?>...</strong></span>
                <button id="send-button" class="btn green" type="submit"><?=$LANG['Send']?> </button>
                <span id="send-spin" class="btn-flat" style="display: none;"><i class="fa fa-spinner fa-spin"></i> <strong><?=$LANG['Please wait']?>...</strong></span>
                <span id="upload-spin" class="btn-flat" style="display: none;"><i class="fa fa-spinner fa-spin"></i> <strong><?=$LANG['Uploading your files']?> <span id="progress-wrp">0%</span>...</strong></span>
            </div>            
        </div>
    </div>
    </form>
    <!-- summery area -->
    <div class="col s12 m5 l4">
        <div class="card light-blue darken-4">
            <div class="card-content white-text">
            <?=$LANG['We will remove all Invalid characters and Duplicate numbers from your recipients list before sending your message.']?>
            </div>
        </div>
            
        <div class="card grey darken-4">
            <div class="card-content white-text">
                <h6 class="m-t-md right-align"><b><?=$LANG['Message Type']?></b></h6>
                <h5 class="right-align" id="ms_typ">Text</h5>
                <div class="divider"></div>
                <h6 class="m-t-md right-align"><b><?=$LANG['Characters']?></b></h6>
                <h5 class="right-align"><span id="char_count">0</span></h5>
                <div class="divider"></div>
                <h6 class="m-t-md right-align"><b><?=$LANG['Page Count']?></b></h6>
                <h5 class="right-align"><span id="pg_count">0</span> <?=$LANG['pages']?></h5>
                <div class="divider"></div>
                <h6 class="m-t-md right-align"><b><?=$LANG['Recipients']?></b></h6>
                <h5 class="right-align"><span id="to_count">0</span></h5>
            </div>
        </div>
    </div>
 </div>
