<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly
?>
    <script type="text/javascript">
        jQuery(document).ready(function () {
            jQuery('#amazilon-tabcontent-generator-2 .apn[type="checkbox"]').each(function (index, value) {
                jQuery(this).change(apnReload2);
            });
            jQuery('#amazilon-tabcontent-generator-2 select.apn').each(function (index, value) {
                jQuery(this).change(apnReload2);
            });
            jQuery('#amazilon-tabcontent-generator-2 .apn[type="text"]').each(function (index, value) {
                jQuery(this).keyup(apnReload2);
            });
            jQuery('#amazilon-tabcontent-generator-2 .amazilon-search-button').click(function () {
                apnSearch2();
                return false;
            });
            jQuery('#amazilon-tabcontent-generator-2 .amazilon-shorten-button').click(apnShorten2);
            var c = Cookies.get('amazilonAsin');
            if (typeof c !== 'undefined') {
                jQuery('#asin').val(c);
                apnReload2();
            }
        });
        apnSearch2 = function () {
            jQuery('#amazilon-tabcontent-generator-2 .amazilon-search-results').show();
            jQuery('#amazilon-tabcontent-generator-2 .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-generator-2 .amazilon-search').val(),
                'sIndex': jQuery('#amazilon-tabcontent-generator-2 .amazilon-search-sindex').val()
            };

            jQuery.post(ajaxurl, data, function (response) {
                jQuery('#amazilon-tabcontent-generator-2 .amazilon-search-results-inner').html(response);
                jQuery('#amazilon-tabcontent-generator-2 a.amazilon-search-result-entry').each(function (index, value) {
                    jQuery(this).click(apnSetAsin2).click(function () {
                        return false;
                    });
                });
                jQuery('#amazilon-tabcontent-generator-2 .amazilon-search-results-inner').fadeIn();
            });
        }
        apnSetAsin2 = function () {
            oAsin = jQuery('#amazilon-tabcontent-generator-2 .asin');
            if (jQuery('#amazilon-tabcontent-generator-2 .template').val() == 'vergleich') {
                oAsin.val(oAsin.val() + ',' + jQuery(this).data('asin'));
            } else {
                oAsin.val(jQuery(this).data('asin'));
            }
            oAsin.trigger('keyup');
            jQuery('#amazilon-tabcontent-generator-2 .amazilon-search-results').fadeOut();
        }

        apnShorten2 = function () {
            var data = {
                'action': 'amazilonShorten',
                'security': '<?php echo wp_create_nonce( 'amazilonCC' . date( 'Ym' ) ); ?>',
                'scode': jQuery('#amazilon-tabcontent-generator-2 .apnAusgabe').val()
            };

            jQuery.post(ajaxurl, data, function (response) {
                var j = jQuery.parseJSON(response);
                if (j.status == 'ok') {
                    jQuery('#amazilon-tabcontent-generator-2 input.amazilon-shortened-input').val(j.data);
                    jQuery('#amazilon-tabcontent-generator-2 label.amazilon-shortened-label').show();
                } else {
                    alert(j.msg);
                }
            });
        }

        apnReload2 = function () {
            jQuery('#amazilon-tabcontent-generator-2 label.amazilon-shortened-label').hide();
            var oAusgabe = jQuery('#amazilon-tabcontent-generator-2 .apnAusgabe');
            var sAsin = jQuery('#amazilon-tabcontent-generator-2 .asin').val();
            var iButton = parseInt(jQuery('#amazilon-tabcontent-generator-2 .buttonlayout').val());
            var sLinkText = jQuery('#amazilon-tabcontent-generator-2 .link_text').val();
            var sTracking = jQuery('#amazilon-tabcontent-generator-2 .tracking').val();
            if (sAsin == '' || sAsin.length < 10) {
                oAusgabe.val('<?php _e( 'Please insert the 10-digit ASIN.', 'amazilon' ); ?>');
                jQuery('#amazilon-tabcontent-generator-2 .amazilon-vorschau').hide();
                jQuery('#amazilon-tabcontent-generator-2 .apnInsert').hide();
                return;
            }
            if (sAsin.length == 10) {
                Cookies.set('amazilonAsin', sAsin);
            }

            var data = {
                'action': 'amazilonPreview',
                'security': '<?php echo wp_create_nonce( 'amazilonCC' . date( 'Ym' ) ); ?>',
                'template': 'button',
                'button': iButton,
                'asin': sAsin,
                'linktext': sLinkText,
                'tracking': sTracking
            };

            jQuery.post(ajaxurl, data, function (response) {
                jQuery('#amazilon-tabcontent-generator-2 .amazilon-vorschaubox').html(response);
            });
            jQuery('#amazilon-tabcontent-generator-2 .amazilon-vorschau').show();
            oAusgabe.val('');

            oAusgabe.val(oAusgabe.val() + '[apn asin="' + sAsin + '" typ="button"' + (parseInt(iButton) > 0 ? ' template="' + parseInt(iButton) + '"' : '') + (sTracking != '' ? ' tag="' + sTracking + '"' : '') + (sLinkText != '' ? ' true="' + sLinkText + '"' : '') + ' false=""]' + "\n");
            jQuery('#amazilon-tabcontent-generator-2 .apnInsert').show();
        }
        amazilonIsertShortCode2 = function (sStr, fieldId) {
            if (fieldId === undefined) {
                fieldId = '_mce';
            }

            if (fieldId == '_mce') {
                window.parent.tinyMCE.activeEditor.execCommand('mceInsertContent', 0, sStr);
            } else if (fieldId == '_qtags') {
                window.parent.QTags.insertContent(sStr);
            } else {
                jQuery(window.parent.jQuery('#' + fieldId)).val(JSON.stringify(oAmazilonTag));
            }
            window.parent.tb_remove();
        }

    </script>
    <h1 style="font-size:1.5em;"><?php _e( 'Generator for Button ShortCodes', 'amazilon' ); ?> - <?php _e( 'Amazilon',
			'amazilon' ); ?></h1>
    <div class="block">
        <p><?php _e( 'Put the product-ASIN into the field, fill the wanted fields (optional) and pick a layout from the list. Below the text box you will recieve the finished ShortCode. Mark, copy and insert the ShortCode into your article.',
				'amazilon' ); ?></p></div>
    <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 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-generator-2 .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>

    <div class="block">
        <p><?php _e( 'Put in the wanted <a href="https://amazilon.com/faq/#asin" target="_blank">Produkt-ASIN</a>.',
				'amazilon' ); ?>: <input type="text" class="apn asin" name="asin" value="" size="35" maxlength=""></p>
        <p class="noma"><b><?php _e( 'Select a button', 'amazilon' ); ?></b></p>
        <p><select name="buttonlayout" class="buttonlayout apn">
                <option value="0"><?php echo sprintf( __( 'Layout %s', 'amazilon' ), '1' ); ?></option>
                <option value="1"><?php echo sprintf( __( 'Layout %s', 'amazilon' ), '2' ); ?></option>
                <option value="2"><?php echo sprintf( __( 'Layout %s', 'amazilon' ), '3' ); ?></option>
                <option value="3"><?php echo sprintf( __( 'Layout %s', 'amazilon' ), '4' ); ?></option>
            </select></p>
        <div name="amazilon-vorschau" class="amazilon-vorschau" style="display: none;"><p><b><?php _e( 'Preview',
						'amazilon' ); ?>:</b><br/><span name="amazilon-vorschaubox" class="amazilon-vorschaubox"><a
                            name="amazilon-button" class="amazilon-button amazilon-button-0" href="#"> - </a></span></p>
        </div>

        <p class="noma"><b><?php _e( 'Text in the Button', 'amazilon' ); ?></b><br>
            <input type="text" class="apn link_text" name="link_text" value="" size=""
                   maxlength=""> <?php _e( 'Desired Linktext', 'amazilon' ); ?><br>
            <input type="text" class="apn tracking" name="tracking" value="" size=""
                   maxlength=""> <?php _e( 'divergent TrackingID (e.g.: AlternativeID-21)', 'amazilon' ); ?><br>
        </p>
        <p><?php _e( 'E.g. to show the price within the button', 'amazilon' ); ?>
			<?php _e( '[apnx asin="B00M8ABEIM" typ="button"]now buy for [apn asin="B00M8ABEIM" typ="preis"] at amazon[/apnx]',
				'amazilon' ); ?></p>

    </div>

<?php
if ( isset( $_GET['fieldId'] ) && ! empty( $_GET['fieldId'] ) ) {
	?>
    <textarea name="apnAusgabe" class="apnAusgabe" cols="104" rows="10" style="display: none;"></textarea>
    <input name="apnInsert" class="apnInsert" style="display: none;" type="button"
           value="<?php _e( 'Insert the template', 'amazilon' ); ?>"
           onclick="amazilonIsertShortCode2(jQuery('#amazilon-tabcontent-generator-2 .apnAusgabe').val(),'<?php echo esc_attr( $_GET['fieldId'] ); ?>');"/>
	<?php
} else {
	?>
    <h2><?php _e( 'Copy the ShortCode and insert wherever needed.', 'amazilon' ); ?></h2>
    <p style="position: relative; max-width: 50em;">
        <label style="display: none;" class="amazilon-shortened-label"><?php _e( 'Shortened', 'amazilon' ); ?>: <input
                    type="text" class="amazilon-shortened-input" style="width: 10em;" value=""/>
            <button class="amazilon-copy-button button button-primary button-clipboard"
                    data-clipboard-target="#amazilon-tabcontent-generator-2 .amazilon-shortened-input"><span
                        class="dashicons dashicons-clipboard"
                        style="margin-top: .2em;"></span> <?php _e( 'Copy to clipboard', 'amazilon' ); ?></button>
        </label>
        <span style="position: absolute; bottom: 2em; right: 1.5em; margin-right: 5%;">
    	<button class="button button-primary button-clipboard"
                data-clipboard-target="#amazilon-tabcontent-generator-2 .apnAusgabe"><span
                    class="dashicons dashicons-clipboard"
                    style="margin-top: .2em;"></span> <?php _e( 'Copy to clipboard', 'amazilon' ); ?></button>
    </span>
        <textarea class="apnAusgabe" rows="15" style="width: 95%;" spellcheck="false"></textarea>
    </p>
	<?php
}
?>