jQuery(document).ready(function($){


    // اضافه کردن متغیر به متن ایمیل
    $(document).on('click', '.wooabc_fields_wrapper_email .wooabc_field', function () {
        var $el = $(this);

        var text = $el.attr('data-fieldid');
        text = '{{' + text + '}}';

        // کپی به clipboard
        if (navigator.clipboard && window.isSecureContext) {
            navigator.clipboard.writeText(text);
        } else {
            var tempInput = $('<textarea>');
            $('body').append(tempInput);
            tempInput.val(text).select();
            document.execCommand('copy');
            tempInput.remove();
        }

        // افکت بصری کپی شدن
        var originalBg = $el.css('background-color');

        $el.css('background-color', '#0ea04bff'); // سبز

        setTimeout(function () {
            $el.css('background-color', originalBg || '#626262');
        }, 2000);
    });



    // تنظیم متغیر در ارسال عادی
    $(document).on('click', '.dynamic_fields_placeholder .wooabc_field', function(){
        var text = $(this).attr('data-fieldid');
        text = '{{'+text+'}}';
        var teatarea_obj = $('#acf-field_69459e4803e9d');
        teatarea_obj.val(teatarea_obj.val() + text);
    });

    $(window).on('load', function () {
        $("#acf-field_69459e47ecdf2").trigger('change');
    });

    // تنظیم متغیر در ارسال پترن
    $("#acf-field_69459e47ecdf2").on('change', function(){
        pattern_variables_generator($(this).val());        
    });

    function pattern_variables_generator(value) {
        var fieldsArray = [
            "customer_name", 
            "first_name",
            "last_name", 
            "kerasno_mobile",
            "billing_phone",
            "profile_email",
            "billing_email",
            "cart_total",
            "cart_items",
            "cart_url",
            "cart_url_short",
            "site_name", 
            "site_url" 
        ];
        var payamakpanel_result = "";
        counter = 0;
        var resultArray = [];
        for (var fieldValue of value) {
            var isField = fieldsArray.includes(fieldValue);    
            if (!isField) {
                resultArray.push('<span>field' + fieldValue+ '</span>');
            } else {
                resultArray.push('<span>' + fieldValue + '</span>');
            }
            payamakpanel_result += '<span>' + "{"+counter+"}" + '</span>' ;
            counter++;
        }

        var IppanelSpanHtml =  resultArray.join("");
        var PayamakPanelSpanHtml = payamakpanel_result;
    
        $('#ippanel .panel_vars').html(IppanelSpanHtml);
        $('#payamakpanel .panel_vars').html(PayamakPanelSpanHtml);
    }

});