<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
global $configverssion_id; global $configapp_version; global $userID; global $server; global $LANG; $resellerID = getReseller();
//=== List page stuffs =============
$page = 1; $setLimit = 30;
if(isset($_GET["page"])) $page = (int)$_GET["page"];
$pageLimit = ($page * $setLimit) - $setLimit; 
$pageurl=$_SERVER['REQUEST_URI']."?";
if(count($_GET)) { $pageurl = $_SERVER['REQUEST_URI'].'&'; }
$trans = array("s=d_o" => "", "s=d_n" => "", "s=r_h" => "", "s=r_l" => "", "s=s_r" => "", "s=s_u" => "", "s=s_q" => "", "s=s_c" => "", "s=s_f" => "", "s=s_p" => "");
$pageurl = strtr($pageurl,$trans);
$pageurl = str_replace('&&','&',$pageurl);
//====End ==============
define('TITLE','Chat');
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);
?>
<style>
.row .chts {padding:10px;}
#chat_messages {display:none;height:300px;overflow-x:auto;border:1px solid #ddd}
#chat_preloader {display:none;width:80%;position:absolute;top:40%;left:10%;text-align:center;}
#chat_preloader2 {width:80%;position:absolute;top:40%;left:10%;text-align:center;}
</style>
<div class="row"><div class="col s12"><h5><?=$LANG['SMS Chat']?></h5></div></row>
<div class="row">
    
    <!-- chat list -->
    <div class="col s12 m5 l4">
         <div class="card blue lighten-5" style="margin-bottom:0;">
         	 <div class="card-content">
            	<a href="javascript:void(0)" onclick="$('#start_chat').slideToggle();"><span class="green-text hoverable" style="font-size:16px;"><strong><i class="fa fa-plus-circle"></i> <?=$LANG['New Chat']?></strong></span></a>
            </div>
         </div>
		<div class="card card-white" id="start_chat" style="margin-top:-2px;margin-bottom:0;display:none;">
            <div class="card-content">
            	<h6 class="black-text"><?=$LANG['Start SMS Chat']?></h6>
                
                <form method="post" action="javascript:void(0)">
            	 <div class="row no-m-t no-m-b">
                     <div class="input-field col s12">
                     	<?php if(isAdmin(getUser())) { ?>
                          <input id="sender" type="number" value="" required class="validate">
                          <label for="sender"><?=$LANG['From Number (include country code)']?></label>
                        <?php } else { ?>
                        	<select id="sender" required class="validate">
                                <option value="0" disabled selected><?=$LANG['Select Shortcode to use']?></option>
                                <?php
								$query="SELECT * FROM shortcodes WHERE customer_id = '$userID' OR resold = '$userID' OR is_public = 1";$result = mysqli_query($server,$query) or die(mysqli_error($server));
								 while ($rows = mysqli_fetch_assoc($result)) { 
								 	echo '<option value="'.$rows['shortcode'].'">'.$rows['shortcode'].'</option>';
								 } ?>
                           </select>
                           <label><?=$LANG['Sender']?></label>
                        <?php } ?>  
                     </div>
                     <div class="input-field col s12">
                          <input id="receiver" type="number" value="" required class="validate">
                          <label for="receiver"><?=$LANG['Recipient (include country code)']?></label>
                     </div>
                     <div class="input-field col s12">
                          <textarea id="message" class="materialize-textarea" required length="960"></textarea>
                          <label for="message"><?=$LANG['Message']?></label>
                     </div>
                     <div class="input-field col s12">
                    	<button id="start-button" class="btn light-green darken-4" type="submit"><?=$LANG['Start Chat']?> </button>
                        <span id="start-spin" class="btn-flat" style="display: none;"><i class="fa fa-spinner fa-spin"></i> <strong><?=$LANG['Please wait...']?></strong></span>
                     </div>
               </div>
               </form>
            </div>
         </div>
         
         <div class="card card-white" style="margin-top:-2px;">
            <div class="card-content" style="max-height:500px;overflow-x:auto">
            	<div class="collection" id="chat_lists">
                <?php $query = "SELECT * FROM smschat WHERE `customer_id` = '$userID' GROUP BY number ORDER BY id DESC";
				if(isAdmin(getUser()))$query = "SELECT * FROM smschat WHERE `customer_id` = '0' GROUP BY number ORDER BY id DESC";
				$result = mysqli_query($server,$query) or die(mysqli_error($server));  
					while ($rows = mysqli_fetch_assoc($result)) { 
					$ago = "";
					if(countNewChats($rows['number'])>0) {
						$ago = '<span class="new badge red">'.countNewChats($rows['number']).'</span>';
					}
					?>
                    <a href="#!" class="collection-item" onclick="loadChat('<?=$rows['number']?>')"><?=$rows['number']?><br><small><?=timeElapsed(lastChatDate($rows['number']))?></small><?=$ago?></a>
                <?php } ?>    
                </div>
            </div><br><br>
         </div>
    </div>        
    
    
    <!-- Chat Holder -->
    <div class="col s12 m7 l8">
    	<div class="card blue lighten-5" style="margin-bottom:0;">
         	 <div class="card-content">
            	 <strong style="font-size:16px;"><i class="fa fa-comments"></i>&nbsp;&nbsp;&nbsp;<span id="chating_with" class="black-text" ></span></strong>
            </div>
         </div>
         <div class="card card-white" style="margin-top:-2px;">
            <div class="card-content" >
            	<div id="chat_int" class="center-align"><h5><i class="fa fa-info-circle"></i></h5><?=$LANG['chat_int']?></div>
            	<div id="chat_messages">
                	
                    <div id="chat_preloader"><?=$LANG['Loading messages']?><div class="progress"><div class="indeterminate"></div></div></div>
                                        
                </div>
                <div class="row" id="chat_composer" style="display:none">
                  <form  method="post" action="javascript:void(0)">
                	 <div class="input-field col s12 m8">
                          <textarea id="message2" class="materialize-textarea" required length="960"></textarea>
                          <label for="message2"><?=$LANG['Message']?></label>
                     </div>
                     <div class="input-field col s12 m4">
                    	<button id="send-button" class="btn light-green darken-4" 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['Sending...']?></strong></span>
                     </div>
                     <input type="hidden" id="sender2" value="">
                     <input type="hidden" id="receiver2" value="">
                  </form>   
                </div>
            </div>
         </div>
    </div>           
            
</div> <!-- main row -->            