$('#modalPago').on('shown.bs.modal', function (e) {
    getRewardData();
});

function getRewardData(){   
    /*Si campo telefono es diferente de vacio,
     * 1. Poner en 0 campo customer_buying
     * 2. Validar que tenga longuitud correct, 
     *      NO = Mando msj que formato de telefono es incorrecto 
     *      SI = obtener asignar id de cliente en campo customer_buying
     * 3.   
    */
    $.post('/Rewards/Controller/Rewards.php', {
        action: 'ajax',
        request: 'getRewardData',
        phone: $('#telefono').val(),
        balance: $('#total_balance').val()
        }, function(data) {
           if(data.response){                
                $.alert({ 
                    title:"<i class='fa fa-star' style='color:gold'></i> Rewards", 
                    content:data.msj,
                    type: 'orange',
                    columnClass: 'col-md-6 col-md-offset-3',
                    buttons: {   
                        Si: {
                            text: "Pagar con puntos",
                            btnClass: 'btn-warning',
                            //keys: ['enter'],
                            action: function(){
                                $("form[name=cobro] input").prop("disabled", false);    
                                $('#formaPago').val('10');                                                         
                                $('#monto').val(data.amount_to_apply); 
                                setCobroTemp();
                            }
                        },
                        No: {
                            text: "Cerrar",
                            btnClass: 'btn-default',
                            //keys: ['esc'],
                            action: function(){
                                 $(this).remove();
                            }
                        }
                    }                             
                });                
           }else{               
                $('#flashmessenger-pago').html(data.msj);
           }
        }, 
        'json');
}

/* EMAILING*/

$(function(){    
    $('._emailing').on('click',function(){ prepareEmailing(this);});
    $('._closeModalEmailing').on('click',function(){
        clearForm('emailing');
        $('.flashmessenger').html('');
        $('#modalEmailing').modal('hide');
    });
    
    $('#send_emailing').on('click',function(){emailing();});
});

function prepareEmailing(operation){   
    var operation_name = $(operation).data('operationname');
    var operation_id = $(operation).data('operationid');
    $('#operation_id').val(operation_id);
    $('#operation_name').val(operation_name);
    
     $.post('/Rewards/Controller/Rewards.php', {
        action: 'emailing',
        request: 'getDataOperation',
        operation_name:operation_name,
        operation_id:operation_id
    }, function(data) {
        if (data.response){ 
            $('#to').val(data.to);
            $('#subject').val(data.subject);
            $('#message').val(data.messageMail);
            $('._attachments').html(data.attachments);
            
            $('#modalEmailing').modal('show');
        }
    }, 'json');
}

function emailing(){
    var options = $('form[name=emailing]').serializeArray();
    var attachmentFiles = $('._attachementForEmail').serializeArray();
    
    $.post('/Rewards/Controller/Rewards.php', {
        action: 'emailing',
        request: 'emailing',
        options:options,
        attachmentFiles:attachmentFiles
    }, function(data) {
        if (data.response){ 
            clearForm('emailing');
            $('#modalEmailing').modal('hide');
            
            /*Esto es para las scripts donde este declarada la funcion callbackEmailing() */
            var array = [];
            array['data_sent'] = options;
            array['data_received'] = data;
            if (typeof callbackEmailing !== 'undefined' && typeof callbackEmailing === 'function'){callbackEmailing(array);}
            
            $('.flashmessenger').html(data.msg);
        }else{
             $('#flashmessenger-emailing').html(data.msg);
        }       
    }, 'json');
}
/* END EMAILING BOL*/