var max=7;
var offset=0;
var currentPage=1;

var app_url=$('#app_url').val();

if(!app_url)
    swal(' Application url not found!');



function peginate(search,max,offset){

    
 /*   $(".se-pre-con").fadeIn("slow");*/
    
    
    var url =  app_url+"organizational_structure/district_count";

    $.ajax({
        type: "GET",
        url: 'district_count',
        data: {search:search},
        dataType: "json",
        success: function (result) {

            if (result.msgType == true) {
              $('#no_distict').hide();
               $('#district_d').show();
                
                
                var total=result.data;
                var no_of_pages= Math.ceil(total/max);
                if(no_of_pages!=0) {
                    if (currentPage > no_of_pages) {
                        currentPage = no_of_pages;
                    }
                }
                console.info("total:"+total+" no_of_pages:"+no_of_pages+" currentPage:"+currentPage);
                if(total >0){
                    if(offset==total){
                        offset=offset-max;
                    }
                    paginateTab(no_of_pages,max);
                    list(search,max,offset);
                }else{
                    $('#no_distict').show();
                    $('#district_d').hide();
                }


            }else {
              $('#no_distict').show();
              $('#district_d').hide();
            }

        },
        error: function (jqXHR, textStatus, errorThrown) {

            callAjaxErrorFunction(jqXHR, textStatus, errorThrown);
        },
        complete: function (data){
            /*$(".se-pre-con").fadeOut("slow");*/
        }
    });  
}



function paginateTab(no_of_pages,max){
    if(currentPage==0){
        currentPage=1;
    }
    var $pagination =  $('#paginationDiv');
    var defaultOpts = {
        totalPages: no_of_pages
    };
    $pagination.twbsPagination(defaultOpts);
    $pagination.twbsPagination('destroy');
    $pagination.twbsPagination($.extend({}, defaultOpts, {
        startPage: currentPage,
        visiblePages: 4,
        totalPages: no_of_pages,
        onPageClick: function (event, page) {
            offset=(page*max)-max;
            currentPage=page;
        }
    })).on('page', function (event, page) {
        var search = null;
        list(search,max,offset);
    });
}





function list(search,max,offset){
    var url =  app_url+"organizational_structure/district_data";
    $(".se-pre-con").fadeIn("slow");
    $.ajax({
        type: "GET",
        url: 'district_data',
        data: {max:max,offset:offset,search:search},
        dataType: "json",


        success: function(data)
        {

            if(data.msgType==true){
                $('#district_data').empty();
                var i=offset+1;
                $.each(data.data, function(key, value) {

                    $('#district_data').append(' <tr><td>'+i+'</td><td>'+value['district_code']+'</td><td>'+value['district_name']+'</td><td>'+value['zp_code']+'</td><td></td></tr>');
                    i++;
                });
            }else{
                toastr.error(data.msg);
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {

            callAjaxErrorFunction(jqXHR, textStatus, errorThrown);
        },
        complete: function (data){
            $(".se-pre-con").fadeOut("slow");
        }
    });

}



$('#district_search').on('input',function(e){
    
    e.preventDefault();
    var searchValue=$(this).val();
    peginate(searchValue,max,offset);
    
});




 peginate(null,max,offset);




$("#district_form").validate({
    rules: {
        district_name: {
            required: true,
        },
        zip_code: {
            required: true,
            digits: true,
            minlength:3,
            maxlength:3,
        }
    },
    messages: {
        username: {
            required: "DistrictName is mandatory",
        },
        password: {
            required: "Zip Code is mandatory",
        }
    }
});




$('#district_form').on('submit',function(e){
    e.preventDefault();
    $("#district_form").validate();
    if($("#district_form").valid()){ 
            var district_name = $('#district_name').val();
            var zip_code = $('#zip_code').val();
            $.ajax({
                headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                type: "POST",
                url: 'district_save',
                data: {
                    district_name: district_name,zip_code:zip_code
                },
                dataType: "json",
                success: function (data) {
                    if (data.msgType == true) {
                        
                       peginate(null,max,offset); 
                      $('#modalRegisterForm').modal('hide');
                      toastr.success(data.msg);
                    }else{
                        if(data.error.district_name_err){
                           $('#district_name-error').append(data.error.district_name_err);
                           $('#district_name-error').show();
                        }else{
                           $('#district_name-error').empty();
                        }
                        
                        if(data.error.zip_code_err){
                            
                           $('#zip_code-error').append(data.error.zip_code_err);
                           $('#zip_code-error').show();
                        }else{
                           $('#zip_code-error').empty();
                        }
                    }

                },
                error: function (jqXHR, textStatus, errorThrown) {

                    callAjaxErrorFunction(jqXHR, textStatus, errorThrown);
                },
                complete: function (data) {
                    $('.page-loader-wrapper').fadeOut();
                }
            });
        
    }
});






