


jQuery(document).ready(function () {

    if(jQuery("#elex_wanabe_a_vendor").is(':checked')){
        var status = jQuery('#elex_wanabe_a_vendor').attr('data-attr-status');
        if (status == 'pending' || status == 'reject'){
            jQuery("#elex_wanabe_a_vendor").hide();
            jQuery(".label_wanna_be_a_vendor").hide();
        } 
       
        if( status == 'publish' || status == '' || status == 'draft' ){
            jQuery("#elex_wanabe_a_vendor").hide();
            jQuery(".label_wanna_be_a_vendor").hide();
            jQuery("#elex_admin_login_details_id").hide();
        }else{
            jQuery("#elex_admin_login_details_id").show();
        }
        
    }else{
        jQuery("#elex_admin_login_details_id").hide();

    }
    jQuery("#elex_wanabe_a_vendor").click(function(){
        if(jQuery(this).is(':checked')){
            jQuery("#elex_admin_login_details_id").show();

        }else{
            jQuery("#elex_admin_login_details_id").hide();

        }
    })
})

jQuery("#elex-multi-vendor-for-woocommerce-filetag-login").change(function () {
   
    changeImage(this);
});

function changeImage(input) {
    var reader;

    if (input.files && input.files[0]) {
        reader = new FileReader();

        reader.onload = function (e) {
            jQuery('#elexPreviewImageUploadLogin').attr('src', e.target.result);
            jQuery('.elex-multi-vendor-for-woocommerce-prod-img-name-div-login').removeClass('d-none');
            jQuery('.elex-multi-vendor-for-woocommerce-prod-img-name-login').html(input.files[0].name);

        }
        reader.readAsDataURL(input.files[0]);
    }
};
jQuery('.elex-multi-vendor-for-woocommerce-product-form-remove-image-login').click(function(){
    jQuery('#elexPreviewImageUploadLogin').attr('src', elex_mv_coupon_ajaxurl_object.plugin_path + 'images/default.jpg');
    jQuery('.elex-multi-vendor-for-woocommerce-prod-img-name-div-login').addClass('d-none');
    jQuery("#elex-multi-vendor-for-woocommerce-filetag-login").val(''); 
})





jQuery(document).ready(function(){

  
    jQuery("#elex_store_state").select2({
        placeholder: "Search State",
        minimumInputLength: 2,
       });
    // Function to load states based on the selected country
    function loadStates(countryCode) {
            var stateSelect = jQuery("#elex_store_state");
            // Clear the current options
            stateSelect.empty();
    
            // Load new options based on the selected country
          
            jQuery.ajax({
                url: elex_mv_coupon_ajaxurl_object.ajax_url,
                type: 'post',
                data: {
                    action: 'elex_mv_load_states',
                    country: countryCode,
                    nonce: elex_mv_coupon_ajaxurl_object.load_states_nonce,
                },
                success: function(result) {
                    if (result.data) {
                        stateSelect.parent().show();
    
                        stateSelect.next(".select2-container").show();
                        stateSelect.append(result.data);
                        stateSelect.trigger('change');
                    }else{
                        
                        stateSelect.parent().hide();
                        stateSelect.next(".select2-container").hide();
    
                    }
                    
                },
            });
        }
        var countryCode = jQuery('#elex_store_country').val();
        if(countryCode){
            loadStates(countryCode);
        }

        // Event listener to load states when the country changes
        jQuery("#elex_store_country").on('change', function() {
            var countryCode = jQuery(this).val();
    
            loadStates(countryCode);
        });

        
        var phoneEl = document.querySelector("#elex_store_phone");
        if (phoneEl) {
            var input_phone_number = window.intlTelInput(phoneEl, {
                autoPlaceholder: "polite",
            });

            var elex_store_dial_code =  jQuery("#elex_store_dial_code").val();
            var elex_store_country_iso =  jQuery("#elex_store_country_iso").val();
            var state = jQuery("#elex_store_state").attr("data-val");
            var state_name = jQuery("#elex_store_state").attr("data-state-name");
            input_phone_number.setCountry(elex_store_country_iso);
            jQuery("#elex_store_dial_code").val( input_phone_number.getSelectedCountryData().dialCode );
            jQuery("#elex_store_country_iso").val(input_phone_number.getSelectedCountryData().iso2);
            if(state !== undefined ){
                jQuery( '#elex_store_state' ).append( '<option value="' + state + '" selected="selected">'+state_name +'</option>' );
                jQuery("#elex_store_state").trigger('change');
            }

            phoneEl.addEventListener("countrychange", function() {
                jQuery("#elex_store_dial_code").val(input_phone_number.getSelectedCountryData().dialCode );
                jQuery("#elex_store_country_iso").val(input_phone_number.getSelectedCountryData().iso2);
            });
        }
})
