<?php
    global $wpdb;

    $post = 0;
    $message = "";
    $table = "{$wpdb->prefix}gamasms_subscribes";
    $table2 = "{$wpdb->prefix}gamasms_subscribes_group";

    if(isset($_POST['btn-gamanet-subscribes-new'])){
       //print_r($_POST);
       $name = sanitize_text_field($_POST['name']);
       $mobile = sanitize_text_field($_POST['mobile']);
       $group_id = sanitize_text_field($_POST['group_id']);
       $option = sanitize_text_field($_POST['option']);
       
       if($option == "edit"){

           $myid = sanitize_text_field($_POST['myid']);
           $status = sanitize_text_field($_POST['status']);
           $data = [
             'name' => $name,
             'mobile' => $mobile,
             'group_id' => $group_id,
             'status' => $status,
          ];

          $result = $wpdb->update($table,$data,array('id'=>$myid));
          if($result){
              $message = "<br><p><div class='alert alert-info' role='alert'>Suscriptor actualizado</div></p>";
          }else{
              $message = "<br><p><div class='alert alert-info' role='alert'>No se pudo actualizar Suscriptor!</div></p>";
          }

       }

       if($option == "insert"){

           $data = [
             'id' => null,
             'creationdate' => null,
             'name' => $name,
             'mobile' => $mobile,
             'status' => '1',
             'activate_key' => null,
             'group_id' => $group_id
          ];

          $result = $wpdb->insert($table,$data);
          if($result){
              $message = "<p><div class='alert alert-info' role='alert'>Suscriptor creado</div></p>";
          }else{
              $message = "<p><div class='alert alert-info' role='alert'>No se pudo crear Suscriptor!</div></p>";
          }

       }


    }

    if(isset($_POST['btn-search'])){
        $search = sanitize_text_field($_POST['search'] );
        $sql2 = "select * from $table where name like '%".$search."%' order by name asc";
        $list_subscribes = $wpdb->get_results($sql2,ARRAY_A);
        // Para que no se produzcan errores cuando no hay datos
        if(empty($list_subscribes)){ 
                  $sql2 = "select * from $table where mobile like '".$search."%' order by name asc";
                  $list_subscribes = $wpdb->get_results($sql2,ARRAY_A);
        }

    }else{

        $sql2 = "select * from $table order by name asc";
        $list_subscribes = $wpdb->get_results($sql2,ARRAY_A);
        // Para que no se produzcan errores cuando no hay datos
        if(empty($list_subscribes)){ 
           $list_subscribes = array();
        }

    }


    $sql3 = "select * from $table2 order by name asc";
    $list_subscribes_group = $wpdb->get_results($sql3,ARRAY_A);
    // Para que no se produzcan errores cuando no hay datos
    if(empty($list_subscribes_group)){ 
       $list_subscribes_group = array();
    }

?>
<div class="wrap">
<?php
   echo '<h1 class="wp-heading-inline">'.get_admin_page_title().'</h1><a id="btn-gamanet-add-subscribes" class="page-title-action">Añadir nuevo</a> <a id="btn-gamanet-export" class="page-title-action">Exportar</a>';
   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>Nombre</th>
     <th>Celular</th>
     <th>Grupo</th>
     <th>Creacion</th>
     <th>Estado</th>
     <th>Acciones</th>
     </thead>
     <tbody id="the-list">
<?php
   foreach($list_subscribes as $key => $value){
       $id = $value['id'];
       $creationdate = $value['creationdate'];
       $name = $value['name'];
       $mobile = $value['mobile'];
       $group_id = $value['group_id'];
       $creationdate = $value['creationdate'];
       $status = $value['status'];
       if($status == "1"){
            $status_name = "Activo";
       }else{
            $status_name = "No activo";
       }

       $sql4 = "select * from $table2 where id='$group_id'";
       $list_subscribes_group2 = $wpdb->get_results($sql4,ARRAY_A);
       // Para que no se produzcan errores cuando no hay datos
       if(empty($list_subscribes_group2)){ 
           $list_subscribes_group2 = array();
           $group_name = '';
       }else{
           $group_name = $list_subscribes_group2[0]['name'];
       }

       echo '<tr><td>'.$name.'</td><td>'.$mobile.'</td><td>'.$group_name.'</td><td>'.$creationdate.'</td><td>'.$status_name.'</td><td><a data-id="'.$id.'" class="page-title-action delete">Borrar</a><a data-id="'.$id.'" class="page-title-action edit">Editar</a> <a data-from="'.$mobile.'" class="page-title-action sendmsg">Enviar SMS</a><</td>
          </tr>';

   }
?>
     </tbody>
</table>
</div>
<!-- Modal ModalSubscribes -->
<div id="ModalSubscribes" class="modal" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Suscriptor</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <form method="post">
      <div class="modal-body">
        <div class="form-group row">
          <label for="name" class="col-md-4 col-form-label">Nombre</label>
          <div class="col-md-8">
              <input type=hidden id="myid" name="myid" value=''>
              <input type=hidden id="option" name="option" value=''>
              <input name="name" id="name" class="form-control" type="text" value="" placeholder="Asigne un nombre al suscriptor" required>
           </div>
         </div>

        <div class="form-group row">
          <label for="mobile" class="col-md-4 col-form-label">Celular</label>
          <div class="col-md-8">
              <input name="mobile" id="mobile" class="form-control" type="text" value="" placeholder="Asigne un numero" required>
           </div>
         </div>

        <div class="form-group row">
          <label for="group_id" class="col-md-4 col-form-label">Grupo</label>
          <div class="col-md-8">
              <?php
                  if(count($list_subscribes_group)>0){ 
               ?>
              <select name="group_id" id="group_id" class="form-control" type="select">
              <?php
                  foreach($list_subscribes_group as $key => $value){
                     $id = $value['id'];
                     $name = $value['name'];
                     echo '<option value="'.$id.'">'.$name.'</option>';
                  }
               ?>
              </select>
              <?php
                  }else{
                      echo 'No hay grupos. <a href="./admin.php?page=gamasms/admin/GroupsMenu.php">Agregar</a>';

                  }
              ?>
           </div>
        </div>

        <div class="form-group row" id="div_status">
          <label for="status" class="col-md-4 col-form-label">Estado</label>
          <div class="col-md-8">
                <select name="status" id="status" class="form-control" type="select">
                       <option value="1">Activo</option>
                       <option value="0">No activo</option>
                </select>
           </div>
        </div>


      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
        <button type="submit" class="btn btn-primary" name="btn-gamanet-subscribes-new">Guardar</button>
      </div>
      </form>
    </div>
  </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="">
			</div>
			<div class="form-group">
                    <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> 