$(document).on('click','#customer_search',function(){
    customer_search()
});
$(document).on('click','#payment_button',function(){
    customer_subscribe()
});

function customer_search(){
    let search_by=$('#search_by').val();
    let customer_code=$('#customer_code').val();
    $('#customer_details .input_id').val('');
    $('#customer_details_area').hide();
    $('#bill_details_area').html('');
    if(customer_code!=''){
        $.ajax({
            type:'post',
            url:ajax_url,
            data:{
                customer_search:1,
                search_by:search_by,
                customer_code:customer_code
            },
            success:function(data){
                $('#customer_details_area').show();
                if(data.status==1){
                    let c=data.customer;
                    $('#customer_details .input_id').val(c.id);
                    $('#customer_details .name').html(c.customer_name);
                    $('#customer_details .id').html(c.id);
                    $('#customer_details .mobile').html(c.customer_mobile);
                    $('#customer_details .address').html(c.customer_address);
                    if(current_page=='bill_history'){
                        let serial=1;
                        $.each(data.subscription_info.subscribes,function(a,b){
                            let x=`<tr>
                    <td>${serial}</td>
                    <td>${b.start_date} - ${b.end_date}</td>
                    <td>${b.created_on}</td>
                    <td class="text-center">${b.amount}</td>
                    <td class="text-center"><button class="btn-sm btn-info">View</a></td>
                </tr>`
                
                $('#bill_details_area').append(x);
                serial++
                        });
                    }
                    else{
                        $('#subscription_range').html(data.subscription_info.next_range);
                    }
                    
                }
                swal_message_from_json(data.m);
            },
            error: function(data) { 
                swal_message('Some problem there'); 
            }
        });
    }
    else{
        swal_message('Please enter customer ID'); 
    }
}
function customer_subscribe(){
    let customer_code=$('#customer_details .input_id').val();
    if(customer_code!=''){
        $.ajax({
            type:'post',
            url:ajax_url,
            data:{
                customer_subscribe:1,
                customer_code:customer_code
            },
            success:function(data){
                if(data.status==1){
                    window.location=data.link;
                }
                else{
                swal_message_from_json(data.m);
                }
            },
            error: function(data) { 
                swal_message('Some problem there'); 
            }
        });
    }
    else{
        swal_message('Please enter customer ID and search first');
    }
}

function swal_message(msgData){
    swal({
        title: msgData,
        timer: 5000,
    });
}

function swal_message_from_json(msgData){
    var msgHtml='';
    $.each(msgData,function(c,ms){
        $.each(ms,function(tp,m){
            if(tp==1){
                if(msgHtml!=''){msgHtml=msgHtml+"\n";}
                msgHtml=msgHtml+m;
            }
        });
    });
    if(msgHtml!=''){
        swal_message(msgHtml);
    }
}