(function ($) {
    'use strict';

    $("#customer_email_approved").addClass("active show");
    $(document).ready(function ($) {
        var $ = jQuery.noConflict();
        $(".nt-emails").on("click", function (e) {
            e.preventDefault();
            var email_id = $(this).attr("data-email-id");
            var sms_id = $(this).attr("data-sms-id");
            var whatsapp_id = $(this).attr("data-whatsapp-id");
            $("#get-email-id").val(email_id);
            $("#get-sms-id").val(sms_id);
            $("#get-whatsapp-id").val(whatsapp_id);

           
        });
        $(document).on("click", ".nt-emails", function () {
            $('.nt-emails').find('.s-hf').removeClass('bookingor-bold-text');
            $(this).find('.s-hf').addClass('bookingor-bold-text');
            $("#get-all-emails-content").children().removeClass("active");
        });

        $(".tab-btn").on("click", function () {
            $(".tab-btn").removeClass("active-tbs");
            $(this).addClass("active-tbs");
        });
        $(".tb-btn").on("click", function () {
            $(".tb-btn").removeClass("active-tb");
            $(this).addClass("active-tb");
        });

        $(".tb-btn-sms").on("click", function () {
            $(".tb-btn-sms").removeClass("active-tb");
            $(this).addClass("active-tb");
        });

        $('.nt-emails').on('click', function () {
            var emailId = $(this).data('email-id');
            var smsId = $(this).data('sms-id');
            var whatsappId = $(this).data('whatsapp-id');
            $('.bookingor-ac-email').removeClass('active show');
            var $activePane = $('.tab-pane[data-email-id="' + emailId + '"]');
            $activePane.addClass('active show');
            $('.tab-pane[data-sms-id="' + smsId + '"]').addClass('active show');
            $('.tab-pane[data-whatsapp-id="' + whatsappId + '"]').addClass('active show');

            if ($activePane.hasClass('bookingor-required-license')) {
                $('.st-btns').hide();
            } else {
                $('.st-btns').show();
            }
        });

        function customer_email_notification(e) {
            e.preventDefault();
            var email_id = $("#get-email-id").val();
            var subject = $("#cs-email-subject-" + email_id).val();
            var body = $("#customer-email-text-" + email_id).find(".ql-editor").html();
            var status = $("#cs-email-status-" + email_id).val();
            var send_on_time = $("#send-on-time-" + email_id).val();
            var post_data = {
                subject: subject,
                body: body,
                email_id: email_id,
                status: status,
                action: 'bp_notification_email_update',
                nonce: TCN_BIND.nonce
            };
            if (send_on_time !== undefined) {
                post_data.send_on_time = send_on_time;
            }
            $.post({
                url: TCN_BIND.GET_URL,
                data: post_data,
                dataType: 'json',
                success: function (res) {
                    if (res.status === 'success') {
                        $.toast({
                            text: res.message,
                            icon: 'success',
                            position: 'bottom-right',
                          });
                    } else {}
                },
            });
        }



        function showEmail() {
            if ($(".email-disable-toggle").val() === "1") {
                $(".email-disable-toggle").attr("checked", true);
            } else {
                $('#cs-email-subject').attr('disabled', true);
                $(".email-disable-toggle").attr("checked", false);
                $(".email-disable-toggle").parent().next().text("Disable");
            }
        }
        showEmail();

        // Dynamic Cron Job logic
        function updateCronCommand(emailId) {
            var sendOnTimeSelect = $('#send-on-time-' + emailId);
            if (!sendOnTimeSelect.length) return;
            
            var timeVal = sendOnTimeSelect.val() || '09:00';
            var timeParts = timeVal.split(':');
            var min = parseInt(timeParts[1] || 0, 10);
            var hour = parseInt(timeParts[0] || 9, 10);
            
            var cronExpr = min + ' ' + hour + ' * * *';
            var baseUrl = $('#cron-command-' + emailId).data('base-url');
            var command = cronExpr + ' curl -s "' + baseUrl + '" > /dev/null 2>&1';
            
            $('#cron-command-' + emailId).text(command);
        }

        // Initialize cron commands for all templates
        $('.bookingor-ac-email').each(function() {
            var emailId = $(this).data('email-id');
            updateCronCommand(emailId);
        });

        // Event listener on time select change
        $(document).on('change', '[id^="send-on-time-"]', function() {
            var emailId = $(this).attr('id').replace('send-on-time-', '');
            updateCronCommand(emailId);
        });

        // Clipboard Copy Action
        $(document).on('click', '.btn-copy-cron', function() {
            var targetId = $(this).data('target');
            var text = $('#' + targetId).text().trim();
            var $btn = $(this);
            
            navigator.clipboard.writeText(text).then(function() {
                var originalHtml = $btn.html();
                $btn.html('<i class="fa fa-check" style="color: #34d399;"></i>');
                $.toast({
                    text: 'Copied to clipboard!',
                    icon: 'success',
                    position: 'bottom-right'
                });
                setTimeout(function() {
                    $btn.html(originalHtml);
                }, 2000);
            }).catch(function(err) {
                console.error('Failed to copy: ', err);
            });
        });

        $("#email-notification-all").on("submit", customer_email_notification);


        // Initialize default active notification tab inputs
        $("#pills-customer .nt-emails").first().click();
    });
})(jQuery);