<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

?>
<style type="text/css">
    .nav-tabcontent-active {
        display: block;
    }

    .nav-tabcontent-inactive {
        display: none;
    }
</style>
<script type="text/javascript">
    jQuery(document).ready(function () {
        var sHash = window.location.hash.substr(1);
		<?php
		if ( isset( $_GET['jumpTo'] ) && ! empty( $_GET['jumpTo'] ) ) {
			echo '	sHash = "' . urlencode( $_GET['jumpTo'] ) . '";' . "\n";
		}
		?>
        if (sHash != '') {
            amazilonToTab(sHash);
        }
    });

    amazilonToTab = function (activate) {
        jQuery('.amazilon-tabs .nav-tab-active').removeClass('nav-tab-active');
        jQuery('.amazilon-tabs #amazilon-tab-' + activate).addClass('nav-tab-active').blur();

        jQuery('.amazilon-tabcontent.nav-tabcontent-active').removeClass('nav-tabcontent-active').addClass('nav-tabcontent-inactive');
        jQuery('.amazilon-tabcontent#amazilon-tabcontent-' + activate).addClass('nav-tabcontent-active').removeClass('nav-tabcontent-inactive');

        window.location.hash = '#' + activate;
        return false;
    }
</script>
<?php
if ( isset( $_GET['msg'] ) ) {
	$aInfo = get_transient( 'bowsxWpApnInfo' );
	if ( $aInfo !== false && ! empty( $aInfo['msg'] ) ) {
		echo '<div class="notice notice-success"><p>' . esc_html( $aInfo['msg'] ) . '</p></div>';
	}
	switch ( $_GET['msg'] ) {
		case '1':
			echo '<div class="notice notice-success"><p>' . __( 'API-access has been checked successfully with the provided data.',
					'amazilon' ) . '</p></div>';
			break;
		case '2':
			echo '<div class="notice notice-error"><p>' . __( 'Quote Public-Key, Private-Key or Associate-Tag not valid; other - amazon-API not accessible',
					'amazilon' ) . '</p></div>';
			break;
	}
}
?>
<h2><?php _e( 'Amazilon', 'amazilon' );
	echo ' ';
	_e( 'Amazon PartnerNet', 'amazilon' );
	echo ' - ';
	_e( 'Settings', 'amazilon' ); ?></h2>
<form action="options.php" method="post">
    <div class="block">
        <h2 class="nav-tab-wrapper">
            <p class="amazilon-tabs">
                <a class="nav-tab nav-tab-active" id="amazilon-tab-generaloptions" href="#generaloptions"
                   onclick="return amazilonToTab('generaloptions');"><?php _e( 'General', 'amazilon' ); ?></a>
                <a class="nav-tab" id="amazilon-tab-searchoptions" href="#searchoptions"
                   onclick="return amazilonToTab('searchoptions');"><?php _e( 'Search completion', 'amazilon' ); ?></a>
				<?php if ( current_user_can( 'amazilon_manage_rights' ) ) { ?>            <a class="nav-tab"
                                                                                             id="amazilon-tab-rightsoptions"
                                                                                             href="#rightsoptions"
                                                                                             onclick="return amazilonToTab('rightsoptions');"><?php _e( 'Rights Management',
					'amazilon' ); ?></a><?php } ?>
                <a class="nav-tab" id="amazilon-tab-expertoptions" href="#expertoptions"
                   onclick="return amazilonToTab('expertoptions');"
                   style="float: right; color: red; font-weight: lighter;"><?php _e( 'Expert Settings',
						'amazilon' ); ?></a>
            </p>
        </h2>
        <div class="amazilon-tabcontent nav-tabcontent-active" id="amazilon-tabcontent-generaloptions"><p>
            <table class="form-table" style="margin-left: 10px;"><?php do_settings_fields( 'amazilon-options',
					'amazilon-options' ); ?></table>
            </p></div>
        <div class="amazilon-tabcontent nav-tabcontent-inactive" id="amazilon-tabcontent-searchoptions"><p>
            <table class="form-table" style="margin-left: 10px;"><?php do_settings_fields( 'amazilon-options',
					'amazilon-searchoptions' ); ?></table>
            </p></div>
        <div class="amazilon-tabcontent nav-tabcontent-inactive" id="amazilon-tabcontent-rightsoptions">
            <p><?php if ( current_user_can( 'amazilon_manage_rights' ) ) { ?>
            <table class="form-table" style="margin-left: 10px;">
                <tr>
                    <th><?php _e( 'Role' ); ?></th>
                    <th><?php _e( 'Manage Rights', 'amazilon' ); ?></th>
                    <th><?php _e( 'Change Options', 'amazilon' ); ?></th>
                    <th><?php _e( 'Use Generators', 'amazilon' ); ?></th>
                    <th><?php _e( 'Use ShortCodes in Comments', 'amazilon' ); ?></th>
                    <th><?php _e( 'See Statistics / Errors', 'amazilon' ); ?></th>
                    <th><?php _e( 'Configure Dynamic Widget', 'amazilon' ); ?></th>
                </tr>
				<?php
				$aRoles = get_editable_roles();
				$oRole  = get_role( 'administrator' );
				foreach ( $aRoles AS $sRole => $aRoledata ) {
					$oRole = get_role( $sRole );
					echo '<tr>';
					echo '<td>' . $aRoledata['name'] . '</td>';

					echo '<td><input value="true" type="checkbox" name="amazilonCap[' . esc_attr( $sRole ) . '][amazilon_manage_rights]" id="amazilonCap[' . esc_attr( $sRole ) . '][amazilon_manage_rights]" ';
					if ( $sRole == 'administrator' ) {
						echo 'disabled="disabled" ';
					}
					echo checked( $oRole->has_cap( 'amazilon_manage_rights' ) );
					echo '/></td>';

					echo '<td><input value="true" type="checkbox" name="amazilonCap[' . esc_attr( $sRole ) . '][amazilon_change_options]" id="amazilonCap[' . esc_attr( $sRole ) . '][amazilon_change_options]" ';
					if ( $sRole == 'administrator' ) {
						echo 'disabled="disabled" ';
					}
					echo checked( $oRole->has_cap( 'amazilon_change_options' ) );
					echo '/></td>';

					echo '<td><input value="true" type="checkbox" name="amazilonCap[' . esc_attr( $sRole ) . '][amazilon_use_generators]" id="amazilonCap[' . esc_attr( $sRole ) . '][amazilon_use_generators]" ';
					if ( $sRole == 'administrator' ) {
						echo 'disabled="disabled" ';
					}
					echo checked( $oRole->has_cap( 'amazilon_use_generators' ) );
					echo '/></td>';

					echo '<td><input value="true" type="checkbox" name="amazilonCap[' . esc_attr( $sRole ) . '][amazilon_use_shortcodes_comments]" id="amazilonCap[' . esc_attr( $sRole ) . '][amazilon_use_shortcodes_comments]" ';
					if ( $sRole == 'administrator' ) {
						echo 'disabled="disabled" ';
					}
					echo checked( $oRole->has_cap( 'amazilon_use_shortcodes_comments' ) );
					echo '/></td>';

					echo '<td><input value="true" type="checkbox" name="amazilonCap[' . esc_attr( $sRole ) . '][amazilon_use_statistics]" id="amazilonCap[' . esc_attr( $sRole ) . '][amazilon_use_statistics]" ';
					if ( $sRole == 'administrator' ) {
						echo 'disabled="disabled" ';
					}
					echo checked( $oRole->has_cap( 'amazilon_use_statistics' ) );
					echo '/></td>';

					echo '<td><input value="true" type="checkbox" name="amazilonCap[' . esc_attr( $sRole ) . '][amazilon_modify_dynamic_widget]" id="amazilonCap[' . esc_attr( $sRole ) . '][amazilon_modify_dynamic_widget]" ';
					if ( $sRole == 'administrator' ) {
						echo 'disabled="disabled" ';
					}
					echo checked( $oRole->has_cap( 'amazilon_modify_dynamic_widget' ) );
					echo '/></td>';

					echo '</tr>';
				}
				?></table>
			<?php } else {
				_e( 'Sorry, you are not allowed to do that.' );
			} ?></p></div>
        <div class="amazilon-tabcontent nav-tabcontent-inactive" id="amazilon-tabcontent-expertoptions"><p
                    style="color: #F00;"><b><?php _e( 'Warning',
						'amazilon' ); ?></b>: <?php _e( 'Changing these options may result in unexpected behaviour and errors on your website if used wrong.',
					'amazilon' ); ?></p>
            <table class="form-table" style="margin-left: 10px;"><?php do_settings_fields( 'amazilon-options',
					'amazilon-expertoptions' ); ?></table>
        </div>
        <p><?php
			settings_fields( 'amazilon-options' );
			submit_button( __( 'Save Changes' ), 'primary', 'submit', false );
			?></p>
    </div>
</form>
