document.addEventListener( "DOMContentLoaded", function( event ) {

    let updateBtns = document.querySelectorAll( "[data-cms-update]" );
    updateBtns.forEach( function( button ) {
        button.addEventListener( "click", function() {
            let fn = this.getAttribute( "data-fn" );
            let data = {
                fn: fn
            }
            if ( !window.location.hostname.match( /dev\.coffee-cms\.com/ ) && ( fn === "create_filelist" || fn === "create_zip" ) ) {
                alert( "Not work" );
                return;
            }
            if ( window.location.hostname.match( /dev\.coffee-cms\.com/ ) && fn === "cms_update" ) {
                alert( "Not work" );
                return;
            }
            api2( data, function( r ){
                if ( r.result == "timeout" ) {
                    notify( __( "Connection lost", "update.mod.php"), 'info-error', 10000 );
                } else if ( r.info_text ) {
                    notify( r.info_text, r.info_class, 5000 );
                    if ( r.answer != null ) {
                        document.querySelector( "[data-"+data.fn+"-answer]" ).innerHTML = r.answer;
                    }
                    if ( r.reload ) {
                        window.location.reload( true );
                    }
                }
            });
        });
    });

    // show update from dev buttons
    document.querySelector( "[data-cms-update-show-dev]" ).addEventListener( "click", function() {
        let dev = document.querySelector( ".info.developers_only" );
        if ( dev ) {
            dev.classList.remove( "developers_only" );
        }
        this.remove();
    } );

});
