/**
 * Matador Jobs Pro
 *
 * @author Jeremy Scott, 2017
 *
 * @since   3.0.0
 */
const { domReady, apiFetch } = wp;

( function( $ ) {
    "use strict";

    $( function() {

        /* Setting Page -> Tab Menu */
        $( ".matador-nav-tabs a" ).on( "click", function( e ) {
            window.location.hash = $( e.target ).attr( "href" ).substr( 1 );
            $( ".matador-settings-tab" ).hide();
            $( ".nav-tab-active" ).removeClass( "nav-tab-active" );
            $( $( this ).attr( "href" ) ).show();
            $( this ).addClass( "nav-tab-active" );
            return false;
        });

        /* Display Settings Tabs Previous State on Form Submit */
        if ( 0 < window.location.hash.length ) {
            $( ".matador-settings-tab" ).hide();
            $( ".nav-tab-active" ).removeClass( "nav-tab-active" );
            $( window.location.hash ).show();
            $( "a[href=\"" + window.location.hash + "\"]" ).addClass( "nav-tab-active" );
        }

        /* Show/Hide Password Fields */
        $( ".show-password" ).on( "click", function( event ) {
            event.preventDefault();
            $( this ).toggleClass( "on", "addOrRemove" );
            var field = $( this ).parent( ".matador-field" ).find( "input" );

            if ( "password" === field.prop( "type" ) ) {
                field.attr( "type", "text" );
            } else {
                field.prop( "type", "password" );
            }
        });

        /* Bullhorn Authorize/Sync */
        $( "#matador_action" ).siblings( "button" ).on( "click", function( event ) {
            event.preventDefault();
            $( "#matador_action" ).attr( "value", $( this ).attr( "id" ) );
            $( this ).closest( "form" ).submit();
        });

        /* Show/Hide Page Fields */
        var applications_on = ".matador-field-applications_accept";
        var apply_field = ".matador-field-applications_apply_method";
        var confirmation_field = ".matador-field-applications_confirmation_method";

        if ( "0" === $( applications_on ).find( "input[type=radio]:checked" ).val() ) {
            $( "#matador-settings-section-applications_general" ).find( ".matador-field-group" ).each( function( index ) {
                if ( ! ( $( this ).hasClass( "matador-field-applications_accept" ) ||
					$( this ).hasClass( "matador-field-applications_apply_page" ) ) ) {
                    $( this ).hide();
                }
                if ( $( this ).hasClass( "matador-field-applications_apply_method" ) ) {
                    $( this ).find( "select" ).val( "custom" );
                }
            });
        }
        $( applications_on ).find( "input" ).on( "change", function() {
        	if ( "0" === $( applications_on ).find( "input[type=radio]:checked" ).val() ) {
                $( "#matador-settings-section-applications_general" ).find( ".matador-field-group" ).each( function( index ) {
                    if ( ! ( $( this ).hasClass( "matador-field-applications_accept" ) ||
						$( this ).hasClass( "matador-field-applications_apply_page" ) ) ) {
                        $( this ).hide();
                        if ( $( this ).hasClass( "matador-field-applications_apply_method" ) ) {
                            $( this ).find( "select" ).val( "custom" );
                        }
                    } else {
                        $( this ).show();
                    }
                });
            } else {
                $( "#matador-settings-section-applications_general" ).find( ".matador-field-group" ).each( function( index ) {
                    $( this ).show();
                });
                if ( "custom" !== $( confirmation_field ).find( "select" ).val() ) {
                    $( ".matador-field-applications_confirmation_page" ).hide();
                } else {
                    $( ".matador-field-applications_confirmation_page" ).show();
                }
            }
        });

        if ( $( apply_field ).length && "custom" !== $( apply_field ).find( "select" ).val() ) {
            $( ".matador-field-applications_apply_page" ).hide();
        }
        $( apply_field ).find( "select" ).on( "change", function() {
            if ( "custom" !== $( apply_field ).find( "select" ).val() ) {
                $( ".matador-field-applications_apply_page" ).hide();
            } else {
                $( ".matador-field-applications_apply_page" ).show();
            }
        });

        if ( $( confirmation_field ).length && "custom" !== $( confirmation_field ).find( "select" ).val() ) {
            $( ".matador-field-applications_confirmation_page" ).hide();
        }
        $( confirmation_field ).find( "select" ).on( "change", function() {
            if ( "custom" !== $( confirmation_field ).find( "select" ).val() ) {
                $( ".matador-field-applications_confirmation_page" ).hide();
            } else {
                $( ".matador-field-applications_confirmation_page" ).show();
            }
        });

    });

    $( "#matador-sync-job" ).on( "click", function( event ) {
        event.preventDefault();
        jQuery( "#matador-sync-job img" ).addClass( "spin" );
        var data = {
            "action": "matador_api_job_sync",
            "nonce": jQuery( this ).data( "nonce" ),
            "bhid": jQuery( this ).data( "bhid" )
        };

        // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
        jQuery.post( ajaxurl, data, function( response ) {
            jQuery( "#matador-sync-job img" ).removeClass( "spin" );
            window.location.reload( true );
        });
    });
}( jQuery ) );

const applicationScreeningBtnOnClick = () => {
    const applicationScreenBtns = document.querySelectorAll( ".matador-application-screen a.button" );
    if ( 0 < applicationScreenBtns.length ) {
        applicationScreenBtns.forEach( btn => btn.addEventListener( "click", function( e ) {
            e.preventDefault();
            apiFetch({
                path: e.target.href,
                method: "GET"
            }).then( ( response ) => {
                const syncStatusContainer = e.target.closest( ".matador-sync-status" );
                if ( syncStatusContainer ) {
                    syncStatusContainer.innerHTML = response.buttons;
                } else {

                    // We're on the application post edit screen
                    const screenBtns = document.querySelector( ".matador-application-screen" );
                    if ( screenBtns ) {
                        screenBtns.innerHTML = response.buttons;
                    }

                    if ( response.redirect_location ) {
                        window.location = response.redirect_location;
                    }
                }
                applicationScreeningBtnOnClick();
            }).catch( error => {
                console.error( "Error fetching application screen:", error );
            });

        }) );
    }
};

domReady( function() {
    applicationScreeningBtnOnClick();
});
