﻿
$(document).ready(function () {

    /**********************************************
    Escort seach script
    **********************************************/
    var SortByPrice = "";
    var SortByPopularity = "";
    var NumberPerPage = "";

    // price per hour dropdown change
    $("#PricePerHourDropDown").change(function () {
        // if the user selects order by price, set popularity to 0
        SortSearchResults($('#PricePerHourDropDown option:selected').attr('value'), "0", $('#NumberPerPageDropDown option:selected').attr('value'));
    });

    // popularity dropdown change
    $("#EscortPopularityDropDown").change(function () {
        // if the user selects popularity, set the price to 0
        SortSearchResults("0", $('#EscortPopularityDropDown option:selected').attr('value'), $('#NumberPerPageDropDown option:selected').attr('value'));
    });

    // numberperpage dropdown change
    $("#NumberPerPageDropDown").change(function () {
        CallEscortSortSearchResults();
    });

    /**********************************************
    Agency seach script
    **********************************************/
    var SortByPrice = "";
    var SortByPopularity = "";
    var NumberPerPage = "";

    // price per hour dropdown change
    $("#AgencyAveragePricePerHourDropDown").change(function () {
        // if the user selects order by price, set popularity to 0
        SortagencySearchResults($('#AgencyAveragePricePerHourDropDown option:selected').attr('value'), "0", "0", $('#AgencyNumberPerPageDropDown option:selected').attr('value'));
    });

    // popularity dropdown change
    $("#AgencyPopularityDropDown").change(function () {
        // if the user selects popularity, set the price to 0
        SortagencySearchResults("0", $('#AgencyPopularityDropDown option:selected').attr('value'), "0", $('#AgencyNumberPerPageDropDown option:selected').attr('value'));
    });

    // NoOfEscortsDropDown dropdown change
    $("#NoOfEscortsDropDown").change(function () {
        // if the user selects popularity, set the price to 0
        SortagencySearchResults("0", "0", $('#NoOfEscortsDropDown option:selected').attr('value'), $('#AgencyNumberPerPageDropDown option:selected').attr('value'));
    });

    // numberperpage dropdown change
    $("#AgencyNumberPerPageDropDown").change(function () {
        CallagenciesortSearchResults();
    });

    // when we select number per page, we want to keep any sort values already set for escort search
    function CallEscortSortSearchResults() {

        var SortByPrice = $('#PricePerHourDropDown option:selected').attr('value');
        var SortByPopularity = $('#EscortPopularityDropDown option:selected').attr('value');
        var NumberPerPage = $('#NumberPerPageDropDown option:selected').attr('value');

        SortSearchResults(SortByPrice, SortByPopularity, NumberPerPage);
    }

    // when we select number per page, we want to keep any sort values already set for agency search
    function CallagenciesortSearchResults() {

        var SortByPrice = $('#AgencyAveragePricePerHourDropDown option:selected').attr('value');
        var SortByPopularity = $('#AgencyPopularityDropDown option:selected').attr('value');
        var NumberPerPage = $('#AgencyNumberPerPageDropDown option:selected').attr('value');

        SortSearchResults(SortByPrice, SortByPopularity, NumberPerPage);
    }


    // fabukescorts.dev/EscortSearch/UKEscorts/Female/Both/options/HourlyRateFrom=150-HourlyRateTo=200-escortAgeFrom=18-escortAgeTo=26
    function SortSearchResults(SortByPrice, SortByPopularity, NumberPerPage) {

        //console.log(SortByPrice);
        //console.log(SortByPopularity);
        //console.log(NumberPerPage);

        var data =
        {
            "data": [
             { "URL": window.location.toString(), "SortByPrice": SortByPrice },
             { "SortByPopularity": SortByPopularity, "NumberPerPage": NumberPerPage }
          ] // end of JSON sort array
        }


        // get the generated URL from the method that links to SortEscortSearchResults
        $.ajax({
            type: "POST",
            url: "/SortEscortSearchResults/",
            dataType: "json",
            data: data,
            success: function (data) {
                $.each(data, function (i, item) {

                    // if the URL has been returned - open it
                    if (item != null) {
                        window.location.href = (item);
                    }
                });
            },
            error: function () { },
            beforeSend: function () { },
            complete: function () { },
            error: null,
            cache: false
        });
    }

    // fabukescorts.dev/EscortAgencies/SearchEscortAgencies/options/
    function SortagencySearchResults(SortByAvgPrice, SortByPopularity, NoOfEscorts, NumberPerPage) {

        //console.log(SortByPrice);
        //console.log(SortByPopularity);
        //console.log(NumberPerPage);

        var data =
        {
            "data": [
             { "URL": window.location.toString(), "SortByAvgPrice": SortByAvgPrice },
             { "SortByPopularity": SortByPopularity, "NumberPerPage": NumberPerPage },
             { "NoOfEscorts": NoOfEscorts }             
             ] // end of JSON sort array
        }

        // get the generated URL from the method that links to SortEscortSearchResults
        $.ajax({
            type: "POST",
            url: "/SortAgencyResults/",
            dataType: "json",
            data: data,
            success: function (data) {
                $.each(data, function (i, item) {

                    // if the URL has been returned - open it
                    if (item != null) {
                        window.location.href = (item);
                    }
                });
            },
            error: function () { },
            beforeSend: function () { },
            complete: function () { },
            error: null,
            cache: false
        });
    }

});
