(function ($) {
    "use strict";

    var initTelephone;

    var serviceStepar;

    var isBookingSuccess = false;

    var currentEmpList = [];

    let currency = "";

    let subtotal = 0;

    var bookingList = [];

    $(document).ready(function () {
        serviceStepar = $("#serviceStep").steps({
        headerTag: "h3",

        bodyTag: "section",

        transitionEffect: "slideLeft",

        autoFocus: true,

        onInit: function (event, currentIndex) {},

        onStepChanging: function (event, currentIndex, newIndex) {
            if (currentIndex > newIndex)
             return true;

            var branch = $("#cmn_branch_id");

            var selectedBranchValue = branch.select2("data")[0].text;

            // console.log(selectedBranchValue);

            var serviceId = $("#sch_service_id");

            var serviceTime = $(".time-slot.selected");
            

            if (currentIndex == 0) {
                // if (!selectedBranchValue.val()) {

                //     branch.addClass("border-red");

                // } 
                
                if (!serviceId.val()) {

                    serviceId.addClass("border-red");

                } else if (serviceTime.length < 1 || typeof $("input[name='service_time']:checked").val() == "undefined") {

                    Message.Warning("Please select service time");

                    $(".time-slot:first-child").addClass("border-red");

                } else {

                    return true;

                }
                return true;
                
            } else if (currentIndex == 1) {

                var firstName = $("#first_name");
                var lastName = $("#last_name");
                var email = $("#email");
                var phoneNumber = $("#phone_no");
                
                var remark = $("#remark");

                if ($('#bookingForSomeoneElse').is(':checked')) {

                    var fullNameBooking = $("#first_name_booking");
                    var lastNameBooking = $("#last_name_booking");
                    

                    if (!fullNameBooking.val().trim()) {

                        fullNameBooking.addClass("border-red");
                        console.log('Booking for Someone Else: First Name is required.');

                        return false;

                    } else if (!lastNameBooking.val().trim()) {

                        lastNameBooking.addClass("border-red");
                        console.log('Booking for Someone Else: Last Name is required.');

                        return false; 

                    } else {
                       
                        return true;
                        

                    }

                } else {

                    if (!firstName.val()) {

                        firstName.addClass("border-red");
    
                    } else if (!lastName.val()) {
    
                        lastName.addClass("border-red");
    
                    } else if (!isValidEmail(email.val())) {
    
                        email.addClass("border-red");
    
                    } else if (!phoneNumber.val()) {
    
                        phoneNumber.addClass("border-red");
    
                    } else if (!remark.val().trim()) {

                        remark.addClass("border-red");
                        console.log('Booking for Someone Else: Remark is required.');
                        console.log(firstName);

                        return false; 

                    } else {
                        // $("#step-2").data("email", email.val());
                        // console.log("Stored Email: " + $("#step-2").data("email"));
    
                        if (isBookingSuccess == false) {

                           
                            SiteManager.SaveBooking();
    
                        } else {
    
                            return true;
    
                        }
    
                    }

                }


            } else if (currentIndex == 2) {

                

                // Added
                var firstName = $("#first_name").text();
                var lastName = $("#last_name").text();
                var fullNameConfirmedSec = firstName.trim() + ' ' + lastName.trim();
                var fullNameConfirmed = $("#first_name").val() + ' ' + $("#last_name").val();

                console.log($("#first_name").val());
                console.log($("#last_name").val());
                console.log(fullNameConfirmedSec);
                
                var bookingDateConfirmed = $("#divServiceDate").data("xdsoft_datetimepicker").getValue();
                var bookingTimeConfirmed = $(".time-slot.selected").html();
                var bookingServiceConfirmed = $("#sch_service_category_id").val();
                var bookingLocationConfirmed = $("#cmn_branch_id").val();

                // For booking for someone else
                var fullNameBookingSomeoneElse = $("#first_name_booking").val() + ' ' + $("#last_name_booking").val();
                var phoneNumber = $("#phone_no").val();
                var email = $('#email').val();
                
                var remark = $("#remark");

                console.log("Email:", email);
                console.log("Full Name Confirmed:", fullNameConfirmed);
                console.log("Booking Date Confirmed:", bookingDateConfirmed);
                console.log("Booking Time Confirmed:", bookingTimeConfirmed);
                console.log("Booking Service Confirmed:", bookingServiceConfirmed);
                console.log("Booking Location Confirmed:", bookingLocationConfirmed);
                console.log("Full Name Booking for Someone Else:", fullNameBookingSomeoneElse);
                console.log("Phone Number:", phoneNumber);
                console.log("Remark:", remark.val());

                $(".fullNameConfirmed").text(fullNameConfirmed);
                $("#booking_date").text(bookingDateConfirmed);
                $("#booking_time").text(bookingTimeConfirmed);
                $("#booking_service").text(bookingServiceConfirmed);
                $("#booking_location").text(bookingLocationConfirmed);
                $("#full_name").text(fullNameBookingSomeoneElse);
                $("#booking_phone_number").text(phoneNumber);
                $("#email").text(email); 
                $("#remark").text($("#remark").val());

                
                return true;

               

            } else if (currentIndex == 3) {

                $(".fullNameConfirmed").text(firstName.val() + " " + lastName.val());

                return true;

            

            }
        },

        onFinished: function (event, currentIndex) {
            window.location = JsManager.BaseUrl() + "/client-dashboard";
        },

        labels: {
            loading: "Loading ...",
        },
        });

        // Add progress bar dynamically
        $(".steps").append('<div class="progress-bar"></div>');

        // Update progress bar based on the current step
        serviceStepar.on("stepChanged", function (event, currentIndex, priorIndex) {
        var stepCount = serviceStepar.getStepCount();
        var progress = ((currentIndex + 1) * 100) / stepCount;
        $(".progress-bar").width(progress + "%");
        });

        $(".form-control").on("click", function () {
        $(this).removeClass("border-red");
        });

        SiteManager.LoadBranchDropDown();

        SiteManager.LoadServiceTimeSlot();

        SiteManager.LoadServiceCategoryDropDown();

        SiteManager.PaymentType();
        SiteManager.LoadServiceDropDown();

        $("#sch_service_category_id").on("change", function () {
        SiteManager.LoadServiceTimeSlot($(this).val());
        });

        $("#sch_service_id").on("change", function () {
        SiteManager.LoadEmployeeDropDown($(this).val());
        });

        $("#iNextDate").on("click", function () {
        $("#divServiceDate").datetimepicker("destroy");

        var nextDate = moment($("#serviceDate").val(), "Y-M-D").add(1, "days");

        SiteManager.ServiceDatePicker(nextDate);
        });

        $("#iPrvDate").on("click", function () {
        var nextDate = moment($("#serviceDate").val(), "Y-M-D").subtract(
            1,
            "days"
        );

        if (nextDate >= moment(moment(new Date()).format("Y-M-D"), "Y-M-D")) {
            $("#divServiceDate").datetimepicker("destroy");

            SiteManager.ServiceDatePicker(nextDate);
        }
        });

        $(".serviceInput").on("change", function () {
        let selectedPropId = $(this).attr("id");

        if (selectedPropId == "cmn_branch_id") {
            $("#sch_employee_id").val("");

            $("#sch_service_category_id").val("");

            $("#sch_service_id").val("");
        } else if (selectedPropId == "sch_service_category_id") {
            $("#sch_employee_id").val("");

            $("#sch_service_id").val("");
        } else if (selectedPropId == "sch_service_id") {
            $("#sch_employee_id").val("");

            // SiteManager.LoadServiceTimeSlot(
            // $(this).val(),
            // $("#sch_employee_id").val()
            // );
        } else if (selectedPropId == "sch_employee_id") {
            // SiteManager.LoadServiceTimeSlot(
            // $("#sch_service_id").val(),
            // $(this).val()
            // );
        }
        });

        $(".iChangeDate").on("click", function () {
        SiteManager.LoadServiceTimeSlot(
            // $("#sch_service_id").val(),
            // $("#sch_employee_id").val()
        );
        });

        initTelephone = window.intlTelInput(document.querySelector("#phone_no"), {
        allowDropdown: true,

        autoHideDialCode: false,

        dropdownContainer: document.body,

        excludeCountries: [],

        formatOnDisplay: false,

        geoIpLookup: function (callback) {
            var jsonParam = "";

            var serviceUrl = "get-requested-country-code";

            JsManager.SendJson("GET", serviceUrl, jsonParam, onSuccess, onFailed);

            function onSuccess(jsonData) {
            if (jsonData.status == 1) {
                callback(jsonData.data);
            } else {
                callback("US");
            }
            }

            function onFailed(xhr, status, err) {}
        },

        hiddenInput: "full_number",

        initialCountry: "auto",

        nationalMode: true,

        placeholderNumberType: "MOBILE",

        separateDialCode: true,

        utilsScript: "js/lib/tel-input/js/utils.js",
        });

        var date = new Date();

        SiteManager.ServiceDatePicker(date);
    });

    $(document).on("click", ".payment-chose-div", function () {
        $(this).find("input").prop("checked", true);

        $(".payment-chose-div").removeClass("payment-chose");

        $(this).addClass("payment-chose");
    });

    $(document).on("click", ".divTimeSlot", function () {
        $(".divTimeSlot").removeClass("border-red");
    });

    $(document).on("click", "#add-service-btn", function () {
        var branch = $("#cmn_branch_id");

        var categoryId = $("#sch_service_category_id");

        var serviceId = $("#sch_service_id");

        var employeeId = $("#sch_employee_id");

        var serviceTime = $("input[name='service_time']");

        if (!branch.val()) {
        branch.addClass("border-red");
        } else if (!categoryId.val()) {
        categoryId.addClass("border-red");
        } else if (!serviceId.val()) {
        serviceId.addClass("border-red");
        } else if (!employeeId.val()) {
        employeeId.addClass("border-red");
        } else if (
        serviceTime.length < 1 ||
        typeof $("input[name='service_time']:checked").val() == "undefined"
        ) {
        Message.Warning("Select service time.");

        $(".divTimeSlot").addClass("border-red");
        } else {
        SiteManager.AddBookingSchedule();

        return true;
        }
    });

    $(document).on("click", ".divTimeSlot", function () {
        $(this).find("input").prop("checked", true);

        $(".divTimeSlot").removeClass("divTimeSlotActive");

        $(this).addClass("divTimeSlotActive");

        SiteManager.SetServiceProperty(
        $("#serviceDate").val(),
        $(this).find(".divStartTime").text()
        );
    });

    $(document).on("click", "#btn-apply-coupon", function () {
        SiteManager.GetCouponAmount();
    });

    var SiteManager = {
        GetCouponAmount: function () {
        var jsonParam = {
            couponCode: $("#coupon_code").val(),

            orderAmount: subtotal,
        };

        var serviceUrl = "get-coupon-amount";

        JsManager.SendJson("GET", serviceUrl, jsonParam, onSuccess, onFailed);

        function onSuccess(jsonData) {
            if (jsonData.status == 1) {
            $("#summary-discount").text(currency + "" + jsonData.data);

            $("#summary-total").text(
                currency +
                "" +
                parseFloat(
                    parseFloat(subtotal) - parseFloat(jsonData.data)
                ).toFixed(2)
            );
            }
        }

        function onFailed(xhr, status, err) {
            Message.Exception(xhr);
        }
        },

        PaymentType: function () {
        var jsonParam = "";

        var serviceUrl = "get-site-payment-type";

        JsManager.SendJson("GET", serviceUrl, jsonParam, onSuccess, onFailed);

        function onSuccess(jsonData) {
            if (jsonData.status == 1) {
            $.each(jsonData.data, function (i, v) {
                let typeIcon = '<img src="img/payment-cash.svg" />';

                let checkStatus = "";

                let activePayment = "";

                if (v.type == 2) {
                typeIcon = '<img src="img/payment-paypal.svg" />';

                checkStatus = "checked";

                activePayment = "payment-chose";
                } else if (v.type == 3) {
                typeIcon = '<img src="img/payment-stripe.svg" />';
                } else if (v.type == 4) {
                typeIcon = '<img src="img/payment-user-balance.svg" />';
                }

                $("#divPaymentMethod").append(
                '<div class="payment-chose-div float-start ' +
                    activePayment +
                    '">' +
                    "<input  " +
                    checkStatus +
                    ' type="radio" name="payment_type" id="payment_type" value="' +
                    v.id +
                    '" class="float-start payment-radio d-none" />' +
                    '<div class="float-start color-black p-2">' +
                    typeIcon +
                    "</div>" +
                    "</div>"
                );
            });
            }
        }

        function onFailed(xhr, status, err) {
            Message.Exception(xhr);
        }
        },

        ServiceDatePicker: function (startDate) {
        $("#divServiceDate").datetimepicker({
            format: "Y-m-d",

            inline: true,

            timepicker: false,

            minDate: new Date(),

            startDate: startDate._d,

            onChangeDateTime: function (dp, $input) {
            $("#serviceDate").val($input.val());

            SiteManager.SetServiceProperty($input.val());

            SiteManager.LoadServiceTimeSlot(
                // $("#sch_service_id").val(),
                // $("#sch_employee_id").val()
            );
            },
        });

        SiteManager.SetServiceProperty(startDate);
        },

        SetServiceProperty: function (startDate, time) {
        let longDate = moment(startDate).format("dddd, MMMM, DD, yyyy");

        $("#serviceDate").val(JsManager.DateFormatDefault(startDate));

        $("#divDaysName").text(longDate);

        if (time) {
            $("#iSelectedServiceText").text(
            "You've Selected " + time + " On " + longDate
            );
        } else {
            $("#iSelectedServiceText").text("You've Selected " + longDate);
        }
        },

        LoadServiceCategoryDropDown: function () {
            console.log("Attempting to load sersaya");
        var jsonParam = "";
 
        var serviceUrl = "get-site-service_mog";

        JsManager.SendJson("GET", serviceUrl, jsonParam, onSuccess, onFailed);

        function onSuccess(jsonData) {
            var data = jsonData.data;
            var select = document.getElementById("sch_service_category_id");
            select.innerHTML = '';
        
            // Create a default option if needed
            var defaultOption = document.createElement("option");
            defaultOption.value = ""; // Set the value attribute to empty string or any default value you want
            defaultOption.textContent = "Select One"; // Set the text content to the default text
            select.appendChild(defaultOption); // Append the default option to the select element
            
        
            // Loop through the JSON data and create options
            data.forEach(function (item) {
                var option = document.createElement("option");
                option.value = item.id; // Set the value attribute to the id
                option.textContent = item.title; // Set the text content to the title
                console.log(item.id);
                select.appendChild(option); // Append the option to the select element
            });
        }

        function onFailed(xhr, status, err) {
            Message.Exception(xhr);
        }
        },

        LoadServiceDropDown: function () {
        var jsonParam = "";

        var serviceUrl = "get-site-service_mog";

        JsManager.SendJson("GET", serviceUrl, jsonParam, onSuccess, onFailed);

        function onSuccess(jsonData) {
            JsManager.PopulateCombo(
            "#sch_service_id",
            jsonData.data,
            "Select One",
            ""
            );
        }

        function onFailed(xhr, status, err) {
            Message.Exception(xhr);
        }
        },

        LoadBranchDropDown: function () {
        var jsonParam = "";

        var serviceUrl = "get-site-branch";

        JsManager.SendJson("GET", serviceUrl, jsonParam, onSuccess, onFailed);

        function onSuccess(jsonData) {
            JsManager.PopulateCombo("#cmn_branch_id", jsonData.data);
        }

        function onFailed(xhr, status, err) {
            Message.Exception(xhr);
        }
        },

        LoadEmployeeDropDown: function (serviceId) {
        var jsonParam = {
            sch_service_id: serviceId,
            cmn_branch_id: $("#cmn_branch_id").val(),
        };

        var serviceUrl = "get-site-employee-service";

        JsManager.SendJson("GET", serviceUrl, jsonParam, onSuccess, onFailed);

        function onSuccess(jsonData) {
            currentEmpList = jsonData.data;

            JsManager.PopulateCombo(
            "#sch_employee_id",
            jsonData.data,
            "Select One",
            ""
            );
        }

        function onFailed(xhr, status, err) {
            Message.Exception(xhr);
        }
        },

        LoadServiceTimeSlot: function () {
            var employeeId = 11; // Hardcoded employee ID
            var serviceId = 1;// hardcode serviceId
            
            console.log("abcc");
            if (
                serviceId > 0 &&
                employeeId > 0 &&// Check if employeeId is greater than 0
                // $("#serviceDate").val() &&
               + ( $("#cmn_branch_id").val() ) > 0
            ) {
                console.log("123");
                JsManager.StartProcessBar();
        
                var jsonParam = {
                    sch_service_id: serviceId,
                    sch_employee_id: employeeId, // Use the hardcoded employee ID here
                    // date: $("#serviceDate").val(),
                     date: "2024-04-23",
                    cmn_branch_id: $("#cmn_branch_id").val(),
                };
                
                var serviceUrl = "get-site-service-time-slot";
        
                JsManager.SendJson("GET", serviceUrl, jsonParam, onSuccess, onFailed);
        
                function onSuccess(jsonData) {
                    if (jsonData.status == 1) {
                        $("#divServiceAvaiableTime").empty();
        
                        $.each(jsonData.data, function (i, v) {
                            let disabledClass = "";
                            let disabledServiceText = "";
                            if (v.is_avaiable == 0) {
                                disabledClass = "disabled-service";
                                disabledServiceText = "disabled-service-text";
                            }
                            let serviceTime = v.start_time + "-" + v.end_time;
        
                            $("#divServiceAvaiableTime").append(
                                '<div class="divTimeSlot ' +
                                disabledClass +
                                '" title="' +
                                serviceTime +
                                '">' +
                                '<div class="float-start w-100">' +
                                '<div class="float-start">' +
                                '<input type="radio" class="serviceTime d-none" name="service_time" value="' +
                                serviceTime +
                                '"/>' +
                                "</div>" +
                                '<div class="float-start cp divStartTime text-center w-100 ' +
                                disabledServiceText +
                                '" style="direction: ltr;">' +
                                moment("1990-01-01 " + v.start_time).format("hh:mm A") +
                                "</div>" +
                                "</div>" +
                                "</div>"
                            );
                        });
                    }
        
                    JsManager.EndProcessBar();
                }
        
                function onFailed(xhr, status, err) {
                    if (xhr.responseJSON.status == 5) {
                        $("#divServiceAvaiableTime").empty();
                        $("#divServiceAvaiableTime").append(
                            '<div class="mt-3">' + xhr.responseJSON.data + "</div>"
                        );
                    } else if (xhr.responseJSON.status == 2) {
                        //service is not available today
                    } else {
                        Message.Exception(xhr);
                    }
        
                    JsManager.EndProcessBar();
                }
            } else {
                $("#divServiceAvaiableTime").empty();
            }
        },
        
        
        SaveBooking: function () {
        return new Promise(function (resolve, reject) {
            
            if (Message.Prompt()) {
            console.log('bookingData');
            JsManager.StartProcessBar();
            
            let bookingData = {
                full_name: $("#first_name").val() + ' ' + $("#last_name").val(),
                email: $("#email").val(), 
                payment_type: "userBalance",

                items: [],
            };
            console.log(123);

            
           

            var firstName1 = $("#first_name").text();
            var lastName2 = $("#last_name").text();
           
            var fullNameConfirmed = $("#first_name").val() + ' ' + $("#last_name").val();
            var bookingDateConfirmed = $("#divServiceDate").data("xdsoft_datetimepicker").getValue();
            var bookingTimeConfirmed = $(".time-slot.selected").html();
            var bookingServiceConfirmed = $("#sch_service_category_id").val();
            var bookingLocationConfirmed = $("#cmn_branch_id").val();
            var fullNameBookingSomeoneElse = $("#first_name_booking").val() + ' ' + $("#last_name_booking").val();
            var phoneNumber = $("#phone_no").val();
            var email = $('#email').val();
            
            var remark = $("#remark").val();


            bookingData.items = [{

                cmn_branch_id: 1 ,


                sch_service_id: 1,

                service_date: "2024-03-28",

                service_time: "18:14:00",

                full_name : "saya",

                phone_no : 23222,

                email : "mathiarasu.mg@gmail.com",

                fullName_S : "",

                remark : "hi"
            }];
            console.log(bookingData.items);

            var jsonParam = { bookingData: bookingData };

            var serviceUrl = "save-site-service-booking_mog";

            console.log(1234);

            JsManager.SendJson(
                "POST",
                serviceUrl,
                jsonParam,
                onSuccess,
                onFailed
            );

            function onSuccess(jsonData) {
                console.log(123);
                if (jsonData.status == "1") {
                Message.Success("save");

                isBookingSuccess = true;

                if (jsonData.paymentType == "paypal") {
                    if ((jsonData.data.returnUrl.status = 201)) {
                    window.location.href =
                        jsonData.data.returnUrl.data.links[1].href;
                    } else {
                    //order will be cancel by redirect

                    SiteManager.CancelBooking(
                        jsonData.data.returnUrl.purchase_units[0].reference_id
                    );
                    }
                } else if (jsonData.paymentType == "stripe") {
                    window.location.href = jsonData.data.returnUrl.redirectUrl;
                } else if (jsonData.paymentType == "userBalance") {
                    window.location.href = jsonData.data.returnUrl.redirectUrl;
                } else {
                    //local payment done

                    serviceStepar.steps("next");

                    isBookingSuccess = false;
                }

                JsManager.EndProcessBar();
                } else {
                Message.Error("save");

                JsManager.EndProcessBar();
                }

                JsManager.EndProcessBar();
            }

            function onFailed(xhr, status, err) {
                console.log("abc");
                JsManager.EndProcessBar();

                Message.Exception(xhr);
            }

            isBookingSuccess = false;
            }
        });
        },

        CancelBooking: function (bookingId) {
        if (Message.Prompt()) {
            JsManager.StartProcessBar();

            var jsonParam = { serviceBookingId: bookingId };

            var serviceUrl = "site-cancel-booking";

            JsManager.SendJson("POST", serviceUrl, jsonParam, onSuccess, onFailed);

            function onSuccess(jsonData) {
            if (jsonData.status == "1") {
                Message.Success("Cancel Successfully");
            } else {
                Message.Error("save");
            }

            JsManager.EndProcessBar();
            }

            function onFailed(xhr, status, err) {
            JsManager.EndProcessBar();

            Message.Exception(xhr);

            return false;
            }
        }
        },

        GetCustomerLoginInfo: function () {
        JsManager.StartProcessBar();

        var jsonParam = "";

        var serviceUrl = "get-site-login-customer-info";

        JsManager.SendJson("POST", serviceUrl, jsonParam, onSuccess, onFailed);

        function onSuccess(jsonData) {
            if (jsonData.status == "1") {
            let data = jsonData.data;

            if (data.full_name)
                $("#full_name").val(data.full_name).attr("readonly", true);

            if (data.email) $("#email").val(data.email).attr("readonly", true);
            }

            JsManager.EndProcessBar();
        }

        function onFailed(xhr, status, err) {
            JsManager.EndProcessBar();

            Message.Exception(xhr);
        }
        },

        AddBookingSchedule: function () {
        if (bookingList.length > 0) {
            const chkVal = bookingList.filter(function (item, ind) {
            console.log(item.branchId);

            if (item.branchId != $("#cmn_branch_id").val()) {
                Message.Warning(
                "You can't add different branches service in the same order"
                );

                return true;
            }

            return (
                item.branchId == $("#cmn_branch_id").val() &&
                item.categoryId == $("#sch_service_category_id").val() &&
                item.serviceId == $("#sch_service_id").val() &&
                item.employeeId == $("#sch_employee_id").val() &&
                item.serviceTime == $("input[name='service_time']:checked").val() &&
                item.serviceDate == $("#serviceDate").val()
            );
            });

            if (chkVal.length > 0) {
            Message.Warning("This is already exists in your cart");

            return false;
            }
        }

        var currentEmp = currentEmpList.filter(function (emp) {
            return emp.id == $("#sch_employee_id").val();
        })[0];

        bookingList.push({
            branchId: $("#cmn_branch_id").val(),

            categoryId: $("#sch_service_category_id").val(),

            

            serviceTime: $(".time-slot.selected").text(),

            serviceDate: $("#divServiceDate").data("xdsoft_datetimepicker").getValue(),

            // currency: currentEmp.currency,
        });

        SiteManager.DrawServiceTable();

        $("#tbl-service-cart").removeClass("d-none");

        return bookingList;
        },

        RemoveBookingSchedule: function (ind) {
        if (bookingList[ind] != undefined) {
            bookingList = bookingList.filter(function (item, index) {
            return index != ind;
            });
        }

        SiteManager.DrawServiceTable();

        return bookingList;
        },

        DrawServiceTable: function () {
        $("#iSelectedServiceList").empty();

        $.each(bookingList, function (ind, item) {
            var $delItem = $(
            '<i class="fa fa-trash text-danger cursor-pointer"></i>'
            );

            $delItem.on("click", function () {
            SiteManager.RemoveBookingSchedule(ind);
            });

            var $wrap = $(
            "<tr>" +
                '<td class="text-center">' +
                (ind + 1) +
                "</td>" +
                "<td>" +
                item.service_name +
                "</td>" +
                "<td>" +
                item.employee_name +
                "</td>" +
                "<td>" +
                item.serviceDate +
                "</td>" +
                "<td>" +
                item.serviceTime +
                "</td>" +
                "<td>" +
                item.currency +
                " " +
                item.employee_rate +
                "</td>" +
                '<td class="text-center"></td>' +
                "</tr>"
            );

            $wrap.find("td:last-child").append($delItem);

            $("#iSelectedServiceList").append($wrap);
        });
        },

        // CalculateServiceSummary: function () {

        //     $("#divServiceSection").empty();

        //     subtotal=0;

        //     $.each(bookingList, function (i, v) {

        //         subtotal = parseFloat(parseFloat(subtotal) + parseFloat(v.employee_rate), 0);

        //         currency = v.currency;

        //         let servicehtml = '<div class="service-item">'

        //             + '<div class="w-70 float-start">'

        //             + '<div class="w-100 text-start">' + v.service_name + '</div>'

        //             + '<div class="w-100 text-start" style="font-size:11px">Date:' + v.serviceDate + ' Time:' + v.serviceTime + '</div>'

        //             + '</div>'

        //             + '<div class="float-end">' + v.currency + v.employee_rate.toFixed(2) + '</div>'

        //             + '</div>';

        //         $("#divServiceSection").append(servicehtml);

        //     });

        //     $("#divServiceSection").append('<div class="service-border-button"></div>');

        //     $("#summary-subtotal").text(currency + subtotal.toFixed(2));

        //     $("#summary-total").text(currency + subtotal.toFixed(2));

        // }
    };

    $(document).ready(function () {
        $(".cmn_branch_id").select2();

        // Branch
        $("#cmn_branch_id").change(function () {
            var selectedData = $(this).val();
            if (selectedData) {
                var selectedIds = selectedData.map(function (option) {
                    return option.id;
                });
                console.log(selectedIds);
            }
        });

        // Service
        $("#sch_service_category_id").change(function () {
            var selectedValue = $(this).val();
            console.log(selectedValue);
        });


        // Date
        function updateDateSlotNotice(selectedDate) {
            var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
            var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
            
            var date = selectedDate.getDate();
            var month = months[selectedDate.getMonth()];
            var year = selectedDate.getFullYear();
            var dayOfWeek = days[selectedDate.getDay()];
        

            $("#time-slot-span .time-slot-day").text(dayOfWeek);
            $("#time-slot-span .time-slot-date").text(month + ' ' + date + ', ' + year);
        }
    

        var selectedDate = $("#divServiceDate").data("xdsoft_datetimepicker").getValue();
        updateDateSlotNotice(selectedDate);
    

        $("#divServiceDate").on("change", function() {
            var newDate = $(this).data("xdsoft_datetimepicker").getValue();
            updateDateSlotNotice(newDate);
        });



        // Time
        $(".time-slot").click(function () {

            $(".time-slot").removeClass("selected");

            $(this).addClass("selected");

            var selectedTime = $(this).text();
            console.log("Selected Time:", selectedTime);
        });

        $(".bookingForSomeoneElseContainer").hide();

        $("#bookingForSomeoneElse").change(function () {

            if ($(this).is(":checked")) {

                $(".bookingForSomeoneElseContainer").show();

            } else {

                $(".bookingForSomeoneElseContainer").hide();
                
            }
        });
    });
})(jQuery);
