var merchant_hotel_actions = {"merchant_restaurant":"29","output_js_action":"30"};
var testest = "{booking_table_success_message}";

// check availability
$(document).on("click", ".check_availability_btn", function(evt){

    var form = $(this).parentsUntil("form");

    var is_continue = true;

    var locationprefix = $(form).find("[name='rest']").first().val();
    if(!locationprefix){
        $(".booking-error").remove();
        $("<span class='booking-error'>Please select a restaurant</span>").insertBefore(".check_availability_btn");
        is_continue = false;
        return;
    }

    var atdate = form.find("[name='table-date']").first().val();
    if(!atdate){
        $(".booking-error").remove();
        $("<span class='booking-error'>Please select a date</span>").insertBefore(".check_availability_btn");
        is_continue = false;
        return;
    } else {

        var checkdate = atdate.split(" ");
        var covers = form.find("[name='covers']").first().val();
        var session = $(form).find("[name='session']").first().val();


        /*
        if (atdate.includes("July") && (14 < checkdate[0] && checkdate[0] < 22) && covers >= 4 && session == 3 ) {
            $(".booking-error").remove();
            if (locationprefix == "RES") {
                $("<span class='booking-error'>During The Open, we require a non-refundable pre-payment deposit of £30 per person for booking, we will offer a set menu at £45.00, this is the minimum spend which can also go towards the A La Carte menu. Please call The Great Room on 028 90 234 888 to pay the deposit to secure the booking.</span>").insertBefore(".check_availability_btn");
            } else if (locationprefix == "BRT") {
                $("<span class='booking-error'>During The Open, we require a non-refundable pre-payment deposit of £20 per person for booking, we will offer a set menu at £32.50, this is the minimum spend which can also go towards the A La Carte menu.  Please call Berts on 028 90 262 713 to pay the deposit to secure the booking.</span>").insertBefore(".check_availability_btn");
            }
            is_continue = false;
            return;
        }*/



    }

    var covers = form.find("[name='covers']").first().val();
    if(!covers){
        $(".booking-error").remove();
        $("<span class='booking-error'>Please select the no. of people</span>").insertBefore(".check_availability_btn");
        is_continue = false;
        return;
    }

    var session = $(form).find("[name='session']").first().val();
    if(!session){
        $(".booking-error").remove();
        $("<span class='booking-error'>Please select a session</span>").insertBefore(".check_availability_btn");
        is_continue = false;
        return;
    }


    if(is_continue){
        var action_id = window["merchant_hotel_actions"]["merchant_restaurant"];

        var params = {
            "action": "GetSessionAvailability",
            "data": {
                "atdate": atdate,
                "covers": covers,
                "session": session,
                "locationprefix": locationprefix,
                "action": action_id
            }
        };

        $.post("/?ACT=" + action_id, params, function(response){
            $(".booking-error").remove();
            if(response.success){
                if(response.data.session_times.length == 0) {
                    if(atdate.includes("December")) {
                        $("<span class='booking-error'>Please contact the Christmas <nobr>Co-ordinators</nobr> directly on 028 9026 2710 / <a href='mailto:christmas@themerchanthotel.com'>christmas@themerchanthotel.com</a></span>").insertBefore(".check_availability_btn");
                    }
                    else if(locationprefix == "RES") {
                        $("<span class='booking-error'>Please contact The Great Room desk directly on 028 9026 7963 / <a href='mailto:thegreatroom@themerchanthotel.com'>thegreatroom@themerchanthotel.com</a></span>").insertBefore(".check_availability_btn");
                    }
                    else if(locationprefix == "BRT") {
                        $("<span class='booking-error'>Please contact Berts restaurant desk directly on 028 9026 2713 / <a href='mailto:berts@themerchanthotel.com'>berts@themerchanthotel.com</a></span>").insertBefore(".check_availability_btn");
                    }
                } else {
                    populate_restaurant_times(response.data.session_times, form);
                }
            } else {
                if(locationprefix == "RES" && atdate.includes("December")) {
                    $("<span class='booking-error'>Please contact the Christmas <nobr>Co-ordinators</nobr> directly on 028 9026 2710 / <a href='mailto:christmas@themerchanthotel.com'>christmas@themerchanthotel.com</a></span>").insertBefore(".check_availability_btn");
                }
                else if(locationprefix == "BRT" && atdate.includes("December")) {
                    $("<span class='booking-error'>Please contact the Christmas <nobr>Co-ordinators</nobr> directly on 028 9026 2710 / <a href='mailto:christmas@themerchanthotel.com'>christmas@themerchanthotel.com</a></span>").insertBefore(".check_availability_btn");
                } else {
                    $("<span class='booking-error'>Sorry, but there are no online bookings available for your selection.</span>").insertBefore(".check_availability_btn");
                }
            }
        }, "json");
    }


    evt.preventDefault();

});

function populate_restaurant_times(data, form){

    form.find("[name='time']").first().empty()
    var any_available = false;
    $.each(data, function(index, value){
        if(value.available){
            any_available = true;
            form.find("[name='time']").append("<option value='" + value.time + "'>" + value.time + "</option>");
        }
    });

    // need to move to next panel.
    if(any_available){
        console.log('Slide');
        $('.form-panels').animate({left: '-261px'});
        $('.booking-error').remove();
    } else {
        // otherwise display error booking_table_success_message
        $(".booking-error").remove();
        $("<span class='booking-error'>Sorry, no seats available</span>").insertBefore(".check_availability_btn");
    }

}

$(document).on("click", ".book_table_btn", function(evt){
    var form = $(this).parentsUntil("form");;
    var locationprefix = $(form).find("[name='rest']").first().val();
    var atdate = form.find("[name='table-date']").first().val();
    var attime = form.find("[name='time']").first().val();
    var covers = form.find("[name='covers']").first().val();
    var session = $(form).find("[name='session']").first().val();
    var title = $(form).find("[name='title']").first().val();
    var surname = $(form).find("[name='surname']").first().val();
    var telephone = $(form).find("[name='tel']").first().val();
    var email = $(form).find("[name='email']").first().val();

    var params = {
        "action": "BookTable",
        "data": {
            "atdate": atdate,
            "attime": attime,
            "covers": covers,
            "session": session,
            "title": title,
            "surname": surname,
            "telephone": telephone,
            "email": email,
            "locationprefix": locationprefix
        }
    };

    var action_id = window["merchant_hotel_actions"]["merchant_restaurant"];

    $.post("/?ACT=" + action_id, params, function(response){

        // alert user to booked table
        if(response.success){
            $('.form-panels').hide();
            $(".fieldset-form-panels").append(" <span class='booking-success'> Thank you for booking a table for " + response.data.covers + " at " + response.data.attime + " on " + response.data.atdate + ". <br><br>Your booking reference is " + response.data.booking_reference + ".</span>");
        } else {
            $(".booking-error").remove();
            $("<span class='booking-error'>Sorry, there was a problem booking your table. Please make sure you have entered your Surname and Telephone number.</span>").insertBefore(".booking-back-btn");
        }

    }, "json");

    evt.preventDefault();
});

