<?php

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

?>
<h1><?php _e( 'Replace ASIN', 'amazilon' ); ?></h1>
<form method="post">
    <p>
        <label for="asin" style="display:inline-block;width: 8em;"><?php _e( 'ASIN', 'amazilon' ); ?>:</label> <input
                type="text" name="asin" id="asin" value="<?php if ( isset( $_REQUEST['asin'] ) ) {
			echo esc_html( $_REQUEST['asin'] );
		} ?>" placeholder="<?php _e( 'ASIN to replace', 'amazilon' ); ?>"/><input type="button" id="findSimilarButton"
                                                                                  value="<?php _e( 'Find similar products',
			                                                                          'amazilon' ); ?>"
                                                                                  style="display: none;"><br/>
        <label for="asinNew" style="display:inline-block;width: 8em;"><?php _e( 'ASIN to use', 'amazilon' ); ?>:</label>
        <input type="text" name="asinNew" id="asinNew" value="<?php if ( isset( $_REQUEST['asinNew'] ) ) {
			echo esc_html( $_REQUEST['asinNew'] );
		} ?>" placeholder="<?php _e( 'ASIN to use instead', 'amazilon' ); ?>"/><br/>
        <br/>
        <input type="submit" value="<?php _e( 'Save' ); ?>"/>
        <input type="hidden" name="action" value="e_add"/>
    </p>
</form>


<h2><?php _e( 'Search for product', 'amazilon' ); ?></h2>
<script type="text/javascript">
    jQuery(document).ready(function () {
        jQuery('#amazilon-tabcontent-xchangenew .amazilon-search-button').click(function () {
            apnSearchNew();
            return false;
        });
        jQuery('#amazilon-tabcontent-xchangenew #findSimilarButton').click(function () {
            apnSearchSimilar();
            return false;
        });
        jQuery('#amazilon-tabcontent-xchangenew #asin').keyup(apnTrigger);
        apnTrigger();
		<?php
		if ( isset( $_GET['findSimilar'] ) ) {
			echo 'jQuery( \'#amazilon-tabcontent-xchangenew #findSimilarButton\' ).click();';
		}
		?>
    });
    apnTrigger = function () {
        if (jQuery('#amazilon-tabcontent-xchangenew #asin').val().length == 10) {
            jQuery('#findSimilarButton').show();
        } else {
            jQuery('#findSimilarButton').hide();
        }
    }
    apnSearchNew = function () {
        jQuery('#amazilon-tabcontent-xchangenew .amazilon-search-results').show();
        jQuery('#amazilon-tabcontent-xchangenew .amazilon-search-results-inner').html('<img src="<?php echo admin_url( 'images/spinner.gif' );  ?>">');
        var data = {
            'action': 'amazilonSearch',
            'security': '<?php echo wp_create_nonce( 'amazilonCC' . date( 'Ym' ) ); ?>',
            'q': jQuery('#amazilon-tabcontent-xchangenew .amazilon-search').val(),
            'sIndex': jQuery('#amazilon-tabcontent-xchangenew .amazilon-search-sindex').val()
        };

        jQuery.post(ajaxurl, data, function (response) {
            jQuery('#amazilon-tabcontent-xchangenew .amazilon-search-results-inner').html(response);
            jQuery('#amazilon-tabcontent-xchangenew a.amazilon-search-result-entry').each(function (index, value) {
                jQuery(this).click(apnSetAsinNew).click(function () {
                    return false;
                });
            });
            jQuery('#amazilon-tabcontent-xchangenew .amazilon-search-results-inner').fadeIn();
        });
    }
    apnSearchSimilar = function () {
        jQuery('#amazilon-tabcontent-xchangenew .amazilon-search-results').show();
        jQuery('#amazilon-tabcontent-xchangenew .amazilon-search-results-inner').html('<img src="<?php echo admin_url( 'images/spinner.gif' );  ?>">');
        var data = {
            'action': 'amazilonSimilar',
            'security': '<?php echo wp_create_nonce( 'amazilonCC' . date( 'Ym' ) ); ?>',
            'q': jQuery('#amazilon-tabcontent-xchangenew #asin').val()
        };

        jQuery.post(ajaxurl, data, function (response) {
            jQuery('#amazilon-tabcontent-xchangenew .amazilon-search-results-inner').html(response);
            jQuery('#amazilon-tabcontent-xchangenew a.amazilon-search-result-entry').each(function (index, value) {
                jQuery(this).click(apnSetAsinNew).click(function () {
                    return false;
                });
            });
            jQuery('#amazilon-tabcontent-xchangenew .amazilon-search-results-inner').fadeIn();
        });
    }
    apnSetAsinNew = function () {
        oAsin = jQuery('#amazilon-tabcontent-xchangenew input[name=' + jQuery('#amazilon-tabcontent-xchangenew select[name=amazilon-search-fieldId]').val() + ']');
        oAsin.trigger('keyup');
        oAsin.val(jQuery(this).data('asin'));
        jQuery('#amazilon-tabcontent-xchangenew .amazilon-search-results').fadeOut();
    }
</script>
<div class="block">
    <p><b><?php _e( 'Search on Amazon', 'amazilon' ); ?></b><br/>
        <label for="amazilon-search" style="display: inline-block; width: 150px;"><?php _e( 'Searchterm',
				'amazilon' ); ?>: </label><input type="text" name="amazilon-search" class="amazilon-search" value=""
                                                 size="35" maxlength="" style="width: 350px;"><br/><label for="sIndex"
                                                                                                          style="width: 150px; display: inline-block;"><?php _e( 'Area',
				'amazilon' ); ?>: </label><select name="amazilon-search-sindex" class="amazilon-search-sindex"
                                                  style="width: 350px;"><?php
			$sIndexes = amazilonGetSearchindexes( 'de' );
			foreach ( $sIndexes AS $sIndex => $aEntry ) {
				echo '<option value="' . esc_attr( $sIndex ) . '">' . esc_attr( $aEntry['title'] ) . '</option>';
			}

			?></select><br/>
        <label for="amazilon-search-fieldId" style="display: inline-block; width: 150px;"><?php _e( 'Use as',
				'amazilon' ); ?>:</label><select name="amazilon-search-fieldId">
            <option value="asin"><?php _e( 'ASIN to replace', 'amazilon' ); ?></option>
            <option value="asinNew" selected="selected"><?php _e( 'ASIN to use instead', 'amazilon' ); ?></option>
        </select><br/>
        <label style="display: inline-block; width: 150px;">&nbsp;</label><a name="amazilon-search-button"
                                                                             class="amazilon-button-3 amazilon-search-button"
                                                                             href="#"
                                                                             style="width: initial;"><?php _e( 'Search on Amazon',
				'amazilon' ); ?></a></p>
</div>

<div name="amazilon-search-results" class="block amazilon-search-results" style="display: none; position: relative;"><a
            href="#" onclick="jQuery('#amazilon-tabcontent-xchangenew .amazilon-search-results').fadeOut();"><span
                class="tb-close-icon"></span></a>
    <p><b><?php _e( 'Search results', 'amazilon' ); ?></b></p>
    <p><?php _e( 'Just click to select', 'amazilon' ); ?>:</p>
    <p name="amazilon-search-results-inner" class="amazilon-search-results-inner"></p></div>