jQuery( document ).ready(function( $ ) {
    'use strict';
    var pluginsUrl = backend_ajax_object.pluginsUrl;
    var mt_pluginUrl = pluginsUrl +"/mt-service-booking";
    var mt_icon_url = mt_pluginUrl +"/assets/img/service-booking.png";
    var mt_loading_icon_url = mt_pluginUrl +"/assets/img/loading.gif";

    $("li#toplevel_page_mt_admin_page .dashicons-admin-generic").append("<span class='mt-icon-wrapper'><img src='"+mt_icon_url+"' alt='mt'></span>");

    if($("#mt_service_full_day_reservation").is(":checked")) {
        $(".mt-time-slot-container").hide();
    }
    $("#mt_service_full_day_reservation").click(function() {
        if($(this).is(":checked")) {
            $(".mt-time-slot-container").slideUp();
        } else {
            $(".mt-time-slot-container").slideDown();
        }
    });

    if($("#mttexteditor_check").is(":checked")) {
        $("#mt-texteditor-container").show();
    } else {
        $("#mt-texteditor-container").hide();
    }
    $("#mttexteditor_check").click(function() {
        if($(this).is(":checked")) {
            $("#mt-texteditor-container").slideDown();
        } else {
            $("#mt-texteditor-container").slideUp();
        }
    });


    $(document).on('click','.mt-table-wrapper table tr', function(){
        $(this).addClass("mt-active-pending-order");
        $(this).siblings().removeClass("mt-active-pending-order");
        $(this).children(".mt-pending-order-checkbox").children("input").prop("checked", true);
        $(this).siblings().children(".mt-pending-order-checkbox").children("input").prop("checked", false);

    });


    $(document).on('click','.mt-active-pending-order .mt-approval-action', function(){
        $(".mt-admin-loading-gif").css("display","block");
        $("tbody#mtPendingBookings").css("opacity","0.3");
        var mt_booking_approval_nonce = $("#mt_booking_approval_nonce").val();
        var mt_booking_approval_action_value = $(this).children('.mt-booking-action-value').val();
        var mt_booking_update_availability = $(this).children('.mt-booking-update-avalability').val();
        var booked_slot_id = $(this).children('.mt-booked-slot-id').val();
        var mt_customer_email_address = $(this).children('.mt-customer-email-address').val();
        var mt_booking_approval_id = $(this).children('.mt-booking-approval-id').val();
        var mt_booking_approval_action_data = {
            'action': 'mt_booking_approval',
            'security': mt_booking_approval_nonce,
            'mt_booking_approval_action_value': mt_booking_approval_action_value,
            'mt_booking_update_availability': mt_booking_update_availability,
            'booked_slot_id': booked_slot_id,
            'mt_customer_email_address': mt_customer_email_address,
            'mt_booking_approval_id': mt_booking_approval_id
        };
        $.ajax({
            type: 'POST',
            url: backend_ajax_object.ajaxurl,
            data: mt_booking_approval_action_data,
            dataType:"json",
            success: function(response) {
                fetchPendingBookings();
            }

        });

    });
    

    var timeIntervalForCounting = 1000*60*2;

    jQuery("#toplevel_page_mt_admin_page .wp-menu-name").append("<span class='mt-pending-bookings-count'></span>");

    setInterval(function () {
        fetchPendingBookingsCounts();
        fetchPendingBookings();
    }, timeIntervalForCounting);

    fetchPendingBookings();

    function fetchPendingBookings(){
        var mt_fetch_pending_bookings_data = {
            'action': 'mt_fetch_pending_bookings'
        };
        $.ajax({
            type: 'POST',
            url: backend_ajax_object.ajaxurl,
            data: mt_fetch_pending_bookings_data,
            dataType:"json",
            success: function(data) {
                preparePendingBookingTable(data);
                $(".mt-admin-loading-gif").css("display","none");
                $("tbody#mtPendingBookings").css("opacity","1");
                fetchPendingBookingsCounts();
            }

        });

    }

    function preparePendingBookingTable(data) {
        $('#mtPendingBookings').empty();
        $.each(data, function(index, booking) {
            index = index + 1;
            var availability_per_slot = booking.available_orders;
            var availability_per_slot_to_int = parseInt(availability_per_slot);
            var update_availability = availability_per_slot_to_int - 1;
            var do_not_update_availability = availability_per_slot_to_int;
            if(availability_per_slot_to_int > 0){
                $('#mtPendingBookings').append("\
                <tr> \
                    <td class='mt-pending-order-checkbox'><input type='checkbox'/>&nbsp;</td>\
                    <td>"+ booking.id +"</td>\
                    <td> "+ booking.service_name +" </td>\
                    <td>"+ booking.customer_name +"</td>\
                    <td>"+ booking.customer_phone +"</td>\
                    <td>"+ booking.customer_email +"</td>\
                    <td>"+ booking.booked_date +"</td> \
                    <td>  "+ booking.booked_time_slot +"  </td>\
                    <td> \
                        <div class='mt-booking-status-wrapper'> \
                            <span> Pending </span> <br>\
                            <span> Available: "+ availability_per_slot_to_int +" </span>\
                            <div class='mt-approval-actions-wrapper'>\
                                <a class='mt-approval-action mt-approval-approve'>\
                                    <input class='mt-booking-approval-id' type='hidden' value='"+ booking.id +"' name='mt_booking_approval_id' >\
                                    <input class='mt-booking-action-value' type='hidden' value='1' name='mt_booking_approved' > Confirm \
                                    <input class='mt-booking-update-avalability' type='hidden' value='"+ update_availability +"' >\
                                    <input class='mt-booked-slot-id' type='hidden' value='"+ booking.booked_slot_id +"' >\
                                    <input class='mt-customer-email-address' type='hidden' value='"+ booking.customer_email +"' >\
                                </a>\
                                <a class='mt-approval-action mt-approval-action-trush'>\
                                    <input class='mt-booking-approval-id' type='hidden' value='"+ booking.id +"' name='mt_booking_approval_id' >\
                                    <input class='mt-booking-action-value' type='hidden' value='trush' name='mt_booking_trush' > Cancel \
                                    <input class='mt-booking-update-avalability' type='hidden' value='"+ do_not_update_availability +"' >\
                                    <input class='mt-customer-email-address' type='hidden' value='"+ booking.customer_email +"' >\
                                </a>\
                            </div> \
                        </div>\
                    </td>\
                </tr>"); 
            } else {
                $('#mtPendingBookings').append("\
                <tr>\
                    <td class='mt-pending-order-checkbox'><input type='checkbox'/>&nbsp;</td>\
                    <td>"+ booking.id +"</td>\
                    <td> "+ booking.service_name +" </td>\
                    <td>"+ booking.customer_name +"</td>\
                    <td>"+ booking.customer_phone +"</td>\
                    <td>"+ booking.customer_email +"</td>\
                    <td>"+ booking.booked_date +"</td> \
                    <td>  "+ booking.booked_time_slot +"  </td>\
                    <td> \
                        <span> Slot filled up </span>\
                        <div class='mt-approval-actions-wrapper'>\
                            <a class='mt-approval-action mt-approval-action-trush'>\
                                <input class='mt-booking-approval-id' type='hidden' value='"+ booking.id +"' name='mt_booking_approval_id' >\
                                <input class='mt-booking-action-value' type='hidden' value='trush' name='mt_booking_trush' > Cancel \
                                <input class='mt-booking-update-avalability' type='hidden' value='"+ do_not_update_availability +"' >\
                                <input class='mt-customer-email-address' type='hidden' value='"+ booking.customer_email +"' >\
                            </a>\
                        </div>\
                    </td>\
                </tr>"); 
            }

        });

    }

    fetchPendingBookingsCounts();

    function fetchPendingBookingsCounts(){
        var mt_fetch_pending_bookings_counts_data = {
            'action': 'mt_fetch_pending_bookings_counts'
        };
        $.ajax({
            type: 'POST',
            url: backend_ajax_object.ajaxurl,
            data: mt_fetch_pending_bookings_counts_data,
            success: function(result) {
                jQuery("#toplevel_page_mt_admin_page .wp-menu-name .mt-pending-bookings-count").text(result);
            }

        });

    }

    

    
      
});