<?php

$sections['ImportExport'] = array(
	'title' => __( 'Import / Export', 'intense' ),	
	'icon' => 'el-icon-refresh',
	'icon_class' => 'icon-large',
	'fields' => array(		
		array(
			'id'        => 'intense_import_export',
			'type'      => 'callback',
			'title'     => __( '', 'intense' ),
			'callback'  => 'intense_import_export_field'
		),		
	)
);

if ( !function_exists( 'intense_import_export_field' ) ):
	function intense_import_export_field( $field, $value ) {
		$ajax_nonce = wp_create_nonce( "intense-plugin-noonce" );

		?>
		<style>
			#options-import label, #options-export label {
				font-weight: 700;
				font-size: 14px;
				width: 30%;
				display: inline-block;
			}

			#options-import input, #options-export input {
				width: auto;
				display: inline-block;
			}

			#options-import textarea, #options-export textarea {
				width: 100%;
				height: 250px;
				margin-top: 10px;
			}

			#export-results {
				display: none;
			}

			#import-export-container .dashicons-before:before {
        		font-size: 15px;
				width: 15px;
				height: 15px;
				margin-top: 5px;
				margin-left: 5px;
				-webkit-animation: 1.5s linear 0s normal none infinite spin;
				animation: 1.5s linear 0s normal none infinite spin;
        	}
		</style>
		<script>
		(function ($) {			
			var $container = $('<div id="import-export-container"></div>');
			var $info = '<?php _e("Import and Export your settings.", "intense"); ?><br><br>';
			var $optionsImport = $('<div id="options-import"><div class="intense-header redux-field-info"><?php _e('Import', 'intense'); ?></div><br><?php _e('Input your settings below and hit Import to restore your options from a backup. WARNING! This will overwrite all existing option values, please proceed with caution!', 'intense'); ?><br><br><label><?php _e('JSON', 'intense'); ?></label><textarea id="options-import-json"></textarea><div class="redux-field-info"><button class="button button-primary import"><?php _e('Import', 'intense'); ?></button><br><br></div></div>');
			var $optionsExport = $('<div id="options-export"><div class="intense-header redux-field-info"><?php _e('Export', 'intense'); ?></div><br><?php _e('Here you can copy your current option settings. Keep this safe as you can use it as a backup should anything go wrong, or you can use it to restore your settings on this site (or any other site).', 'intense'); ?><br><br><div class="redux-field-info"><button class="button-primary export"><?php _e('Export', 'intense'); ?></button></div><br><div id="export-results"><label><?php _e('JSON', 'intense'); ?></label><textarea id="intense-options-json"></textarea></div></div>');

			$container.append($info);
			$container.append($optionsImport);
			$container.append($optionsExport);

			$('#ImportExport_section_group').append($container);

			function intenseOptionsImport(e) {
				$.ajax({
					type: 'POST',
					cache: false,
					url: ajaxurl + '?action=intense_options_import_action',
					data: {
						security: '<?php echo $ajax_nonce; ?>',
						options: $('#options-import-json').val()
					},
					beforeSend: function() {
						intenseOptionsStartAction('button.import');
						$('#import-results').remove();						
					},
					success: function(data) {
						$container.find('button.import').after("<div id='import-results' style='display: inline-block; padding: 5px 5px'>" + data + '</div>');
						intenseOptionsEndAction();

						if ( data == 'Complete!' ) {
							window.location = window.location;
						}
					},
					dataType: 'JSON'
				});
				e.preventDefault();
			}

			function intenseOptionsExport(e) {							
				$.ajax({
					type: 'POST',
					cache: false,
					url: ajaxurl + '?action=intense_options_export_action',
					data: {
						security: '<?php echo $ajax_nonce; ?>'
					},
					beforeSend: function() {
						intenseOptionsStartAction('button.export');
						$('#export-results').slideUp();
					},
					success: function(data) {
						intenseOptionsEndAction();

						$('#intense-options-json').val(JSON.stringify(data, null, '\t'));
						$('#export-results').slideDown();
					},
					dataType: 'JSON'
				});
				e.preventDefault();
			}

			function intenseOptionsStartAction(element) {
				intenseOptionsEndAction();
				$(element).append('<i class="progress dashicons-before dashicons-update">');
			}

			function intenseOptionsEndAction() {
				$('.dashicons-update').remove();
			}

			$container.find('button.import').click(intenseOptionsImport);
			$container.find('button.export').click(intenseOptionsExport);
		})(jQuery);
		</script>
		<?php
	}

	add_action( 'wp_ajax_intense_options_export_action', 'intense_options_export_action' );
	function intense_options_export_action() {
		global $intense_visions_options;

		// check for rights
		if ( !current_user_can( 'edit_pages' ) && !current_user_can( 'edit_posts' ) )
			die( __( "You are not allowed to be here" ) );		

		check_ajax_referer( 'intense-plugin-noonce', 'security' );

		if ( !empty( $intense_visions_options ) ) {
			echo json_encode( $intense_visions_options );
		} else {
			echo json_encode( "No options set" );
		}

		die();
	}

	add_action( 'wp_ajax_intense_options_import_action', 'intense_options_import_action' );
	function intense_options_import_action() {
		global $intense_visions_options;

		// check for rights
		if ( !current_user_can( 'edit_pages' ) && !current_user_can( 'edit_posts' ) )
			die( __( "You are not allowed to be here" ) );		

		check_ajax_referer( 'intense-plugin-noonce', 'security' );

		if ( !empty( $_POST['options'] ) ) {
			$options = json_decode( stripslashes( $_POST['options'] ), true );
			
			require_once INTENSE_PLUGIN_FOLDER . '/inc/options/options.php';

			$intense_options = new Intense_Options();
			$intense_options->ReduxFramework->set_options($options);

			echo json_encode( 'Complete!' );
		} else {
			echo json_encode( "No options set" );
		}

		die();
	}

endif;