$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

$('#date_issued').datetimepicker();

function searchCounties() {
    var keyword = $("#search-county").val();

    $.ajax({
        type: "POST",
        data: {"_token": $('meta[name="csrf-token"]').attr('content'),"keyword":keyword},
        url: "/counties/search",
        success: function(data){
            data = $.parseJSON(data);
            var html = "";
            for(var i = 0; i < data.length; i++) {
                var county_name = data[i].county_name;
                var county_code = data[i].county_code;
                var county_id = data[i].county_id;
                html += '<div class="row t-content"><div class="col-4">'+county_code+'</div><div class="col-4">'+county_name+'</div><div class="col-4 edit-butts"><span><a href="/county/edit/'+county_id+'"><img src="/images/edit.png" width="15px" /></a><button rel="'+county_id+'" class="btn delcounty" data-toggle="modal" data-target="#countiesModal"><img src="/images/delete.png" width="15px" /></button></span></div></div>';
            }
            $("#counties-data").animate({opacity: '0.8'});
            $("#counties-data").html(html);
            $("#counties-data").animate({opacity: '1'});
        }
     });
}

function searchConstituencies() {
    var keyword = $("#search-constituency").val();

    $.ajax({
        type: "POST",
        data: {"_token": $('meta[name="csrf-token"]').attr('content'),"keyword":keyword},
        url: "/constituencies/search",
        success: function(data){
            data = $.parseJSON(data);
            var html = "";
            for(var i = 0; i < data.length; i++) {
                var constituency_name = data[i].constituency_name;
                var constituency_code = data[i].constituency_code;
                var constituency_id = data[i].constituency_id;
                var county_name = data[i].county_name;
                html += '<div class="row t-content"><div class="col-3">'+constituency_code+'</div><div class="col-3">'+constituency_name+'</div><div class="col-3">'+county_name+'</div><div class="col-3 edit-butts"><span><a href="/constituency/edit/'+constituency_id+'"><img src="/images/edit.png" width="15px" /></a><button rel="'+constituency_id+'" class="btn delconstituency" data-toggle="modal" data-target="#constituenciesModal"><img src="/images/delete.png" width="15px" /></button></span></div></div>';
            }
            $("#constituencies-data").animate({opacity: '0.8'});
            $("#constituencies-data").html(html);
            $("#constituencies-data").animate({opacity: '1'});
        }
     });
}

function searchCategories() {
    var keyword = $("#search-category").val();

    $.ajax({
        type: "POST",
        data: {"_token": $('meta[name="csrf-token"]').attr('content'),"keyword":keyword},
        url: "/categories/search",
        success: function(data){
            data = $.parseJSON(data);
            var html = "";
            for(var i = 0; i < data.length; i++) {
                var category_name = data[i].category_name;
                var category_id = data[i].category_id;
                html += '<div class="row t-content"><div class="col-6">'+category_name+'</div><div class="col-6 edit-butts"><span><a href="/category/edit/'+category_id+'"><img src="/images/edit.png" width="15px" /></a><button rel="'+category_id+'" class="btn delcategory" data-toggle="modal" data-target="#categoriesModal"><img src="/images/delete.png" width="15px" /></button></span></div></div>';
            }
            $("#categories-data").animate({opacity: '0.8'});
            $("#categories-data").html(html);
            $("#categories-data").animate({opacity: '1'});
        }
     });
}

function searchCountyAssemblies() {
    var keyword = $("#search-countyassembly").val();

    $.ajax({
        type: "POST",
        data: {"_token": $('meta[name="csrf-token"]').attr('content'),"keyword":keyword},
        url: "/countyassemblies/search",
        success: function(data){
            data = $.parseJSON(data);
            var html = "";
            for(var i = 0; i < data.length; i++) {
                var county_assembly_name = data[i].county_assembly_name;
                var county_assembly_code = data[i].county_assembly_code;
                var county_assembly_id = data[i].county_assembly_id;
                var county_name = data[i].county_name;
                html += '<div class="row t-content"><div class="col-3">'+county_assembly_code+'</div><div class="col-4">'+county_assembly_name+'</div><div class="col-3">'+county_name+'</div><div class="col-2 edit-butts"><span><a href="/countyassembly/edit/'+county_assembly_id+'"><img src="/images/edit.png" width="15px" /></a><button rel="'+county_assembly_id+'" class="btn delcountyassembly" data-toggle="modal" data-target="#countyassembliesModal"><img src="/images/delete.png" width="15px" /></button></span></div></div>';
            }
            $("#countyassemblies-data").animate({opacity: '0.8'});
            $("#countyassemblies-data").html(html);
            $("#countyassemblies-data").animate({opacity: '1'});
        }
     });
}

function searchSubcategories() {
    var keyword = $("#search-subcategory").val();

    $.ajax({
        type: "POST",
        data: {"_token": $('meta[name="csrf-token"]').attr('content'),"keyword":keyword},
        url: "/subcategories/search",
        success: function(data){
            data = $.parseJSON(data);
            var html = "";
            for(var i = 0; i < data.length; i++) {
                var subcategory_name = data[i].sub_category_name;
                var category_name = data[i].category_name;
                var subcategory_id = data[i].sub_category_id;
                html += '<div class="row t-content"><div class="col-4">'+subcategory_name+'</div><div class="col-4">'+category_name+'</div><div class="col-4 edit-butts"><span><a href="/subcategory/edit/'+subcategory_id+'"><img src="/images/edit.png" width="15px" /></a><button rel="'+subcategory_id+'" class="btn delsubcategory" data-toggle="modal" data-target="#subcategoriesModal"><img src="/images/delete.png" width="15px" /></button></span></div></div>';
            }
            $("#subcategories-data").animate({opacity: '0.8'});
            $("#subcategories-data").html(html);
            $("#subcategories-data").animate({opacity: '1'});
        }
     });
}

function searchFinancialYears() {
    var keyword = $("#search-financialyear").val();

    $.ajax({
        type: "POST",
        data: {"_token": $('meta[name="csrf-token"]').attr('content'),"keyword":keyword},
        url: "/financialyears/search",
        success: function(data){
            data = $.parseJSON(data);
            var html = "";
            for(var i = 0; i < data.length; i++) {
                var financial_year_name = data[i].financial_year_name;
                var financial_year_id = data[i].financial_year_id;
                html += '<div class="row t-content"><div class="col-6">'+financial_year_name+'</div><div class="col-6 edit-butts"><span><a href="/financialyear/edit/'+financial_year_id+'"><img src="/images/edit.png" width="15px" /></a><button rel="'+financial_year_id+'" class="btn delfinancialyear" data-toggle="modal" data-target="#financialyearsModal"><img src="/images/delete.png" width="15px" /></button></span></div></div>';
            }
            $("#financialyears-data").animate({opacity: '0.8'});
            $("#financialyears-data").html(html);
            $("#financialyears-data").animate({opacity: '1'});
        }
     });
}

function searchInstitutions() {
    var keyword = $("#search-institution").val();

    $.ajax({
        type: "POST",
        data: {"_token": $('meta[name="csrf-token"]').attr('content'),"keyword":keyword},
        url: "/institutions/search",
        success: function(data){
            data = $.parseJSON(data);
            var html = "";
            for(var i = 0; i < data.length; i++) {
                var institution_name = (data[i].institution_name != null)?data[i].institution_name:"";
                var sub_category_name = (data[i].sub_category_name != null)?data[i].sub_category_name:"";
                var institution_id = data[i].institution_id;
                html += '<div class="row t-content"><div class="col-5">'+institution_name+'</div><div class="col-5">'+sub_category_name+'</div><div class="col-2 edit-butts"><span><a href="/institution/edit/'+institution_id+'"><img src="/images/edit.png" width="15px" /></a><button rel="'+institution_id+'" class="btn delinstitution" data-toggle="modal" data-target="#institutionsModal"><img src="/images/delete.png" width="15px" /></button></span></div></div>';
            }
            $("#institutions-data").animate({opacity: '0.8'});
            $("#institutions-data").html(html);
            $("#institutions-data").animate({opacity: '1'});
        }
     });
}

function searchUsers() {
    var keyword = $("#search-user").val();
    
    $.ajax({
        type: "POST",
        data: {"_token": $('meta[name="csrf-token"]').attr('content'),"keyword":keyword},
        url: "/users/search",
        success: function(data){
            data = $.parseJSON(data);

            var html = "";
            for(var i = 0; i < data.length; i++) {
                var name = data[i].name;
                var institution_name = (data[i].institution_name != null)?data[i].institution_name:"";
                var phone_no = (data[i].msisdn != null)?data[i].msisdn:"";
                var user_id = data[i].user_id;
                var status = data[i].status;
                html += '<div class="row t-content"><div class="col-3">'+name+'</div><div class="col-3">'+institution_name+'</div><div class="col-2">'+phone_no+'</div><div class="col-2">'+status+'</div><div class="col-2 edit-butts"><span><a href="/user/edit/'+user_id+'"><img src="/images/edit.png" width="15px" /></a><button rel="'+user_id+'" class="btn deluser" data-toggle="modal" data-target="#usersModal"><img src="/images/delete.png" width="15px" /></button></span></div></div>';
            }
            $("#users-data").animate({opacity: '0.8'});
            $("#users-data").html(html);
            $("#users-data").animate({opacity: '1'});
        }
     });
}


function searchUserRoles() {
    var keyword = $("#search-userrole").val();

    $.ajax({
        type: "POST",
        data: {"_token": $('meta[name="csrf-token"]').attr('content'),"keyword":keyword},
        url: "/userroles/search",
        success: function(data){
            data = $.parseJSON(data);
            var html = "";
            for(var i = 0; i < data.length; i++) {
                var role = data[i].role;
                var user_role_id = data[i].user_role_id;
                html += '<div class="row t-content"><div class="col-6">'+role+'</div><div class="col-6 edit-butts"><span><a href="/userrole/edit/'+user_role_id+'"><img src="/images/edit.png" width="15px" /></a><button rel="'+user_role_id+'" class="btn deluserrole" data-toggle="modal" data-target="#userrolesModal"><img src="/images/delete.png" width="15px" /></button></span></div></div>';
            }
            $("#userroles-data").animate({opacity: '0.8'});
            $("#userroles-data").html(html);
            $("#userroles-data").animate({opacity: '1'});
        }
     });
}


function searchQuestionnaireParts() {
    var keyword = $("#search-questionnairepart").val();

    $.ajax({
        type: "POST",
        data: {"_token": $('meta[name="csrf-token"]').attr('content'),"keyword":keyword},
        url: "/questionnaireparts/search",
        success: function(data){
            data = $.parseJSON(data);
            var html = "";
            for(var i = 0; i < data.length; i++) {
                var part_name = data[i].part_name;
                var questionnaire_part_id = data[i].questionnaire_part_id;
                html += '<div class="row t-content"><div class="col-6">'+part_name+'</div><div class="col-6 edit-butts"><span><a href="/questionnairepart/edit/'+questionnaire_part_id+'"><img src="/images/edit.png" width="15px" /></a><button rel="'+questionnaire_part_id+'" class="btn delquestionnairepart" data-toggle="modal" data-target="#questionnairepartsModal"><img src="/images/delete.png" width="15px" /></button></span></div></div>';
            }
            $("#questionnaireparts-data").animate({opacity: '0.8'});
            $("#questionnaireparts-data").html(html);
            $("#questionnaireparts-data").animate({opacity: '1'});
        }
     });
}


function searchQuestionnaireSections() {
    var keyword = $("#search-questionnairesection").val();

    $.ajax({
        type: "POST",
        data: {"_token": $('meta[name="csrf-token"]').attr('content'),"keyword":keyword},
        url: "/questionnairesections/search",
        success: function(data){
            data = $.parseJSON(data);
            var html = "";
            for(var i = 0; i < data.length; i++) {
                var section_name = (data[i].section_name != null)?data[i].section_name:"";
                var part_name = (data[i].part_name != null)?data[i].part_name:"";
                var questionnaire_section_id = data[i].questionnaire_section_id;
                html += '<div class="row t-content"><div class="col-5">'+section_name+'</div><div class="col-5">'+part_name+'</div><div class="col-2 edit-butts"><span><a href="/questionnairesection/edit/'+questionnaire_section_id+'"><img src="/images/edit.png" width="15px" /></a><button rel="'+questionnaire_section_id+'" class="btn delquestionnairesection" data-toggle="modal" data-target="#questionnairesectionsModal"><img src="/images/delete.png" width="15px" /></button></span></div></div>';
            }
            $("#questionnairesections-data").animate({opacity: '0.8'});
            $("#questionnairesections-data").html(html);
            $("#questionnairesections-data").animate({opacity: '1'});
        }
     });
}


function searchCirculars() {
    var keyword = $("#search-circular").val();

    $.ajax({
        type: "POST",
        data: {"_token": $('meta[name="csrf-token"]').attr('content'),"keyword":keyword},
        url: "/circulars/search",
        success: function(data){
            data = $.parseJSON(data);
            var html = "";
            for(var i = 0; i < data.length; i++) {
                var subject = data[i].subject;
                var ref_no = data[i].ref_no;
                var date_issued = new Date(data[i].date_issued);

                var dd = date_issued.getDate();
                var mm = date_issued.getMonth()+1; 
                var yyyy = date_issued.getFullYear();
                if(dd<10) 
                {
                    dd='0'+dd;
                } 
                if(mm<10) 
                {
                    mm='0'+mm;
                } 
                date_issued = dd+'-'+mm+'-'+yyyy;


                var src_circular_id = data[i].src_circular_id;
                html += '<div class="row t-content"><div class="col-4">'+subject+'</div><div class="col-4">'+ref_no+'</div><div class="col-2">'+date_issued+'</div><div class="col-2 edit-butts"><span><a href="/circular/edit/'+src_circular_id+'"><img src="/images/edit.png" width="15px" /></a><button rel="'+src_circular_id+'" class="btn delcircular" data-toggle="modal" data-target="#circularsModal"><img src="/images/delete.png" width="15px" /></button></span></div></div>';
            }
            $("#circulars-data").animate({opacity: '0.8'});
            $("#circulars-data").html(html);
            $("#circulars-data").animate({opacity: '1'});
        }
     });
}


$(document).on("click", ".delcounty", function () {
    var passedID = $(this).attr('rel');
    $("#del-county").val(passedID);
});

$(document).on("click", ".delconstituency", function () {
    var passedID = $(this).attr('rel');
    $("#del-constituency").val(passedID);
});

$(document).on("click", ".delcategory", function () {
    var passedID = $(this).attr('rel');
    $("#del-category").val(passedID);
});

$(document).on("click", ".delsubcategory", function () {
    var passedID = $(this).attr('rel');
    $("#del-subcategory").val(passedID);
});

$(document).on("click", ".delcountyassembly", function () {
    var passedID = $(this).attr('rel');
    $("#del-countyassembly").val(passedID);
});

$(document).on("click", ".delinstitution", function () {
    var passedID = $(this).attr('rel');
    $("#del-institution").val(passedID);
});

$(document).on("click", ".delfinancialyear", function () {
    var passedID = $(this).attr('rel');
    $("#del-financialyear").val(passedID);
});

$(document).on("click", ".deluser", function () {
    var passedID = $(this).attr('rel');
    $("#del-user").val(passedID);
});

$(document).on("click", ".deluserrole", function () {
    var passedID = $(this).attr('rel');
    $("#del-userrole").val(passedID);
});

$(document).on("click", ".delquestionnairepart", function () {
    var passedID = $(this).attr('rel');
    $("#del-questionnairepart").val(passedID);
});

$(document).on("click", ".delquestionnairesection", function () {
    var passedID = $(this).attr('rel');
    $("#del-questionnairesection").val(passedID);
});

$(document).on("click", ".delcircular", function () {
    var passedID = $(this).attr('rel');
    $("#del-circular").val(passedID);
});