<?php
    global $wpdb;
	global $gama_sms;

    $fg_limite_display = 40;
    $post = 0;
    $message = "";
    $table = "{$wpdb->prefix}gamasms_msg_incoming";
	
	$result_answers = $gama_sms->GetAnswersMsg();
	
	$array_data = $result_answers["data"];
	  foreach($array_data as $x => $val){
		$data = [
             'id' => null,
             'creationdate' => null,
             'msg_from' => $val["calledstation"],
             'msg_text' => $val["sms_text"],
             'msg_date' => $val["to_char"]
          ];

        $result = $wpdb->insert($table,$data);		   
		   
	  }
	
	if(isset($_GET['current_page'])){
	     $current_page = sanitize_text_field($_GET['current_page']);
	}
	
	if(!isset($current_page) || ($current_page == "") || ($current_page<0)){
		$current_page = 0;
	}
	
	$current_record = $current_page*$fg_limite_display;

    if(isset($_POST['btn-search'])){
        $search = strtolower(sanitize_text_field($_POST['search'] ));
        $sql2 = "select * from $table where msg_from like '%".$search."%' order by msg_date desc";
        $list_msg_incoming = $wpdb->get_results($sql2,ARRAY_A);
        // Para que no se produzcan errores cuando no hay datos
        if(empty($list_msg_incoming)){ 
                  $sql3 = "select * from $table where LOWER(msg_text) like '%".$search."%' order by msg_date desc";
                  $list_msg_incoming = $wpdb->get_results($sql3,ARRAY_A);
        }

    }else{
        $nb_record = $wpdb->get_var("select count(*) from $table");
		if($nb_record<=$fg_limite_display){
			$nb_record_max=1;
		}else{
			if($nb_record % $fg_limite_display == 0){
				$nb_record_max = (intval($nb_record/$fg_limite_display));
			}else{
				$nb_record_max = (intval($nb_record/$fg_limite_display)+1);				
			}
		}

         if($nb_record>=0){
			$sql2 = "select * from $table order by msg_date desc limit $current_record,$fg_limite_display";
			$list_msg_incoming = $wpdb->get_results($sql2,ARRAY_A);
			// Para que no se produzcan errores cuando no hay datos
			if(empty($list_msg_incoming)){ 
			   $list_msg_incoming = array();
        }
		 }

    }


?>
<div class="wrap">
<?php
   echo '<h1 class="wp-heading-inline">'.get_admin_page_title().'</h1>';
   echo  '<form method="post"><div class="search-box"><input type="search" name="search" style="margin-right:4px"><button type="submit" name="btn-search" class="button">Buscar</button></div></form>';
   echo $message;
?>
<br /><br />
<table class="wp-list-table widefat fixed striped pages">
     <thead>
     <th class="manage-column column-primary">#</th>
     <th class="manage-column"></>Fecha y Hora</th>
     <th class="manage-column">De</th>
     <th class="manage-column">Mensaje</th>
     <th class="manage-column">Acciones</th>
     </thead>
     <tbody id="the-list">
<?php
   $i=0;
   foreach($list_msg_incoming as $key => $value){
       $id = $value['id'];
       $creationdate = $value['msg_date'];
       $from = $value['msg_from'];
       $msg = $value['msg_text'];

       echo '<tr><td>'.$id.'</td><td>'.$creationdate.'</td><td>'.$from.'</td><td>'.$msg.'</td><td><a data-id="'.$id.'" class="page-title-action delete">Borrar</a> <a data-from="'.$from.'" class="page-title-action sendmsg">Enviar SMS</a></td>
          </tr>';

   }
?>
     </tbody>
</table>

<div class="tablenav bottom">

	<div class="alignleft actions bulkactions">
				<label for="bulk-action-selector-bottom" class="screen-reader-text">Selecciona acción en lote</label>
				<select name="action2" id="bulk-action-selector-bottom">
					<option value="-1">Acciones en lote</option>
					<option value="trash">Mover a la papelera</option>
				</select>
				<input type="submit" id="doaction2" class="button action" value="Aplicar">
	</div>
	<div class="alignleft actions">
	</div>
	<div class="tablenav-pages">
		<span class="displaying-num"><?php echo $nb_record; ?> elementos</span>
		<span class="pagination-links">
		<a class="next-page button" href="<?php  echo $PHP_SELF.'?page=gamasms%2Fadmin%2FMsgDetailsMenu.php&current_page=0'; ?>" <?php if($current_page==0) echo 'disabled' ?> ><span class="screen-reader-text">Página siguiente</span><span aria-hidden="true">«</span></a>
		<a class="last-page button" href="<?php  echo $PHP_SELF.'?page=gamasms%2Fadmin%2FMsgDetailsMenu.php&current_page='.($current_page-1); ?>" <?php if($current_page==0) echo 'disabled'; ?>><span class="screen-reader-text">Última página</span><span aria-hidden="true">‹</span></a>
		<span class="screen-reader-text">Página actual</span><span id="table-paging" class="paging-input"><span class="tablenav-paging-text"><?php echo ($current_page+1); ?> de <span class="total-pages"><?php echo $nb_record_max; ?></span></span></span>
		<a class="next-page button" href="<?php  echo $PHP_SELF.'?page=gamasms%2Fadmin%2FMsgDetailsMenu.php&current_page='.($current_page+1); ?>" <?php if(($nb_record_max-1) == $current_page) echo 'disabled' ?>><span class="screen-reader-text">Página siguiente</span><span aria-hidden="true">›</span></a>
		<a class="last-page button" href="<?php  echo $PHP_SELF.'?page=gamasms%2Fadmin%2FMsgDetailsMenu.php&current_page='.($nb_record_max-1); ?>" <?php if(($nb_record_max-1) == $current_page) echo 'disabled' ?>><span class="screen-reader-text">Última página</span><span aria-hidden="true">»</span></a>
		</span>
		</div>
		<br class="clear">
	</div>

</div>


<!-- Modal ModalSendMsg -->
<div id="ModalSendMsg" class="modal" role="dialog" data-backdrop="false">
    <div class="modal-dialog" role="document">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
		  <h5>Enviar mensaje SMS</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        </div>
        <div class="modal-body">
            <div class="form-group">
                    <label for="phone"> Para :</label>
                    <input type="text" class="form-control" name="phone" id="phone" placeholder="Ingrese telefono" value="">
                    <label for="text"> Mensaje: </label>
                    <textarea id="text" class="form-control" name="text" rows="4" placeholder="Escriba mensaje..."></textarea>
            </div>
        </div> 
	<div class="modal-footer">
			<button class="btn btn-secondary" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancelar</button>
            <button class="btn btn-primary" id="button-modalsendmsg"><span class="glyphicon glyphicon-ok"></span> Enviar</button>
	</div>
      </div>
      <!-- End Modal content-->
    </div>
</div> 

