var freeTrial = {}
var supporter = {}
var innovator = {}
var created_date;
var modifed_date;
var expiryDate;

$(document).ready(function () {
    freeTrial = {
        'memberShipType':'Free',
        'benefits':['Access all solutions','Access all drawings & figures'],
        'noofProjects':'1',
        'userAccounts':'1',
        'costPerYear':'Free for 30 days',
    }; 

    supporter = {
        'memberShipType':'Supporter',
        'benefits':['Access all solutions, drawings & figures','Access all design resources',
                    'Create 1 project/year & access printable scorecard', '1 user/project', 'Submit projects for pre-certification, & isUD Certification with on-site audit',
                    '20% discount on re-certification if subscription is active for all 5 years'],
        'noofProjects':'1',
        'userAccounts':'1', 
        'costPerYear':'$500/year',
        'totalCost':'500/year',
        'years':'1',
    };
    
    innovator = {
        'memberShipType':'Innovator',
        'benefits':['Access all solutions, drawings & figures','Access all design resources',
                    'Create 2 projects/year & access printable scorecard', 'Up to 3 users/project', 'Submit projects for pre-certification,& isUD Certification with on-site audit',
                    '20% discount on re-certification if subscription is active for all 5 years','Access to free and discounted online course'],
        'noofProjects':'2',
        'userAccounts':'3', 
        'costPerYear':'$1000/year',
        'totalCost':'1000/year',
        'years':'1',
    };

    $.post("API.php", { dashboard: "SUBSCRIPTION_TYPE"}, function (data) {
        if(data.response){
            created_date = data.ts_created;
            modifed_date = created_date.toString().split(" ")[0];
            expiryDate = GetFreeTrialExpiryDate(modifed_date);
            var expiry = "Expires: "+"<b>" + expiryDate.month + "/" + expiryDate.day + "/" + expiryDate.year + "</b>";
            $('#expiry-date').html(expiry);
        }
    },"json");

    attachOverviewBtnEventListener(supporter, innovator);

});


function attachOverviewBtnEventListener(supporter, innovator){
    localStorage.setItem("currentMemberShip", "free");
    $(".cl-supporter-mem").click(function(ev){
        localStorage.setItem("memberShipCat", JSON.stringify(supporter));
        window.location.href = 'memberShipSubscription.php';
    });

    $(".cl-innovator-mem").click(function(ev){
        localStorage.setItem( 'memberShipCat', JSON.stringify(innovator));
        window.location.href = 'memberShipSubscription.php';
    });

    $('#slct-for-supporter').change(function(){ 
        var value = $(this).val();
        updateSupporterTotalCost(value);
    });

    $('#slct-for-innovator').change(function(){ 
        var value = $(this).val();
        updateInnovatorTotalCost(value);
    });
}

function updateSupporterTotalCost(years){
    const base_cost = 500;
    $('#cost-in-dollars-supporter').html('$'+base_cost * years);
    supporter['totalCost'] = base_cost * years;
    supporter['years'] = years;
}

function updateInnovatorTotalCost(years){
    const base_cost = 1000;
    $('#cost-in-dollars-innovator').html('$'+base_cost * years);
    innovator['totalCost'] = base_cost * years;
    innovator['years'] = years;
}

function GetFreeTrialExpiryDate(modifed_date) 
{
    var expiryDate = new Date(modifed_date)
    console.log(expiryDate)
    if(leapYear(expiryDate.getFullYear())){
        expiryDate.setDate(expiryDate.getDate() + 31)
    }else{
        // Writing it like this to make it more readable
        expiryDate.setDate(expiryDate.getDate() - 1 + 31)
    }
    var day = expiryDate.getDate()
    var month = expiryDate.getMonth() + 1
    var year = expiryDate.getFullYear()
    return {
        month,
        day,
        year
    }
}

var leapYear = function(year)
{
  return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
};