jQuery(document).ready(function($){
     console.log('SubscribesMenu.js Ready.....');
     //console.log(SolicitudesAjax);
     $('#btn-gamanet-add-subscribes').click(function(){
          $('#ModalSubscribes').modal('show');
          $('#option').val('insert');
          $('#name').val('');
          $('#mobile').val('');
          $('#div_status').hide();
     });


    $('#btn-gamanet-export').click(function(){
           var url = "../wp-content/plugins/gamasms/admin/export.php";
           $.ajax({
               type: "POST",
               url: url,
               data:{
                  'export-file-type'  : "csv"
               }
           })
           .done(function(data){
                   var myWindow = window.open();
                   myWindow.document.write(data);
            });

     });

     $(document).on('click',".delete",function(){
           var id = this.dataset.id;
           var url = SolicitudesAjax.url;
           var result = confirm("Esta seguro?");
           if(!result) return;

           $.ajax({
               type: "POST",
               url: url,
               data:{
                  action  : "peticionsubscribesajax",
                  nonce : SolicitudesAjax.seguridad,
                  id : id,
                  option : "delete"
               },
               success:function(){
                   location.reload();
                }
           });
     });

     $(document).on('click',".edit",function(){
           $('#ModalSubscribes').modal('show');
           var id = this.dataset.id;
           var url = SolicitudesAjax.url;
           
           $.ajax({
               type: "POST",
               url: url,
               data:{
                  action  : "peticionsubscribesajax",
                  nonce : SolicitudesAjax.seguridad,
                  id : id,
                  option  : "edit"
               },
               success:function(result){
                   console.log(result);
                   $('#div_status').show();
                   $('#myid').val(id);
                   $('#option').val('edit');
                   $('#name').val(result[0].name);
                   $('#mobile').val(result[0].mobile);
                   $('#group_id').val(result[0].group_id);
                   $('#status').val(result[0].status);
                   
                }
           });
           

     });
	 
	 $(document).on('click',".sendmsg",function(){
           var from = this.dataset.from;
		   $("#phone").val(from);
           $("#ModalSendMsg").modal('show');

     });
	 
	 
	      //form modal enviando mensaje
     $("#button-modalsendmsg").click(function(){

            var url = SolicitudesAjax.url;
            $("#button-modalsendmsg").prop("disabled",true);
            var msgphone = $('#phone').val();
            var msgtext = $('#text').val();
                        
            if(msgphone != "" && msgtext != "" ){
				
              $.ajax({
                  type: "POST",
                  url: url,
                  data:{
                    action  : "peticionincomingajax", // Se reusa la peticion ajax que ya exite en incoming
                    nonce : SolicitudesAjax.seguridad,
                    msgphone : msgphone,
				    msgtext : msgtext,
                    option : "sendmsg"
                  },
                  success:function(){
                    //location.reload();
				    $("#button-modalsendmsg").prop("disabled",false);
            	    $("#ModalSendMsg").modal("hide");
                  },
				  error:function(){
					$("#ModalSendMsg").modal("hide");
            		alert("Error conexion al servidor...  intente mas tarde.");
            		$("#button-modalsendmsg").prop("disabled",false);
					
				  }
              });
				
            }else{
                $("#ModalSendMsg").modal("hide");
                alert("Error: Hay Datos Obligatorios que faltan llenar.");
        	$("#button-modalsendmsg").prop("disabled",false);
            }
        }); //fine form


});