<?php

/*
*  ACF Flo Flex Builder Field Class
*
*  All the logic for this field type
*
*  @class 		acf_flo_flex_builder
*  @extends		acf_field
*  @package		ACF
*  @subpackage	Fields
*/

if (!class_exists('acf_flo_flex_builder') && class_exists('acf_field')) :

	class acf_flo_flex_builder extends acf_field {


		/*
	*  __construct
	*
	*  This function will setup the field type data
	*
	*  @type	function
	*  @date	5/03/2014
	*  @since	5.0.0
	*
	*  @param	n/a
	*  @return	n/a
	*/

		function __construct() {

			// vars
			$this->name = 'flo_flex_builder';
			$this->label = __("Flo Flex Builder", 'acf');
			$this->category = 'flothemes';
			$this->defaults = array(
				"default_value"	=> "",
				"json_value" => ""
			);


			// do not delete!
			parent::__construct();
		}


		/*
	*  input_admin_enqueue_scripts
	*
	*  description
	*
	*  @type	function
	*  @date	16/12/2015
	*  @since	5.3.2
	*
	*  @param	$post_id (int)
	*  @return	$post_id (int)
	*/

		function input_admin_enqueue_scripts() {

			// $dir = plugin_dir_url( __FILE__ );

		}


		/*
	*  render_field()
	*
	*  Create the HTML interface for your field
	*
	*  @param	$field - an array holding all the field's data
	*
	*  @type	action
	*  @since	3.6
	*  @date	23/01/13
	*/

		function render_field($field) {
			$dir = plugin_dir_url(__FILE__); ?>

			<?php

			$theme_data_transient = get_transient('flex_theme_data_transient');

			$html_font_size = 1.25;
			$block_padding = 3.75;

			if ($theme_data_transient) {
				$theme_data = json_decode($theme_data_transient);
				$html_font_size = $theme_data->html_font_size;
				$block_padding = $theme_data->block_padding;
			}

			?>

			<div class="flo-flex-builder__all-actions">
				<div class="flo-flex-builder__custom-image-wrap">
					<?php global $pagenow;
					if ($pagenow == 'admin.php' || $field["parent_layout"] === "5a3a34ec6d0cf") { ?>
						<img class="flo-flex-builder__custom-img" style="width: 100%; margin: 0 auto; height: auto" src="https://s3-us-west-2.amazonaws.com/flothemes-dashboard-images/flo-flex-builder/flo-flex-builder-block-footer-default.preview.jpg"><br>
					<?php } else { ?>
						<img class="flo-flex-builder__custom-img" style="max-height: 248px; width: auto; margin: 0 auto;" src="https://s3-us-west-2.amazonaws.com/flothemes-dashboard-images/flo-flex-builder/flo-flex-builder-block-preview.preview.jpg">
					<?php } ?>
					<img class="flo-flex-builder__screenshot-img-tag" src="" data-orig="">
				</div>
				<!-- start: iframe open trigger -->
				<span class="flo-flex-builder__open-trigger">OPEN BUILDER</span>
				<!-- end: iframe open trigger -->

				<!-- start: import/export buttons -->
				<div class="flo-flex-builder__import-export">
					<a href="https://docs.flothemes.com/flex-block/" target="_blank" class="flo-flex-builder__view-docs">VIEW DOCUMENTATION</a>
					<span class="flo-flex-builder__info-area">Upload your block file</span>
					<span class="flo-flex-builder__import-json-trigger">IMPORT</span>
					<input type="file" multiple="false" accept="application/json" onchange="flexHandleImport(this)" class="flo-flex-builder__import-file" />
					<span class="flo-flex-builder__export-json-trigger">EXPORT</span>
				</div>
				<!-- end: import/export buttons -->

				<!-- start: app iframe -->
				<?php if (defined('Flex_Dev_Mode') && true === Flex_Dev_Mode) { ?>
					<iframe class="flo-flex-builder__popup-wrap" data-src="http://localhost:8000"></iframe>
				<?php } else { ?>
					<iframe class="flo-flex-builder__popup-wrap" data-src="<?php echo plugins_url() . '/flo-flex-builder/admin/partials/acf-flo-flex-builder/app/index.html?ver=' . Flo_Flex_Builder_Version ?>"></iframe>
				<?php } ?>
				<!-- <iframe class="flo-flex-builder__popup-wrap" data-src="http://bucket-flexblock-gui.s3.amazonaws.com/index.html?remote=0"></iframe> -->
				<!-- end: app iframe -->

				<!-- start: data holder -->
				<textarea class="flo-flex-builder__data-holder" style="display: none;" name="<?php echo $field["name"]; ?>"><?php echo $field["value"]; ?></textarea>
				<input type="hidden" class="flo-flex-html-font-size-val" value="<?php echo $html_font_size; ?>" />
				<input type="hidden" class="flo-flex-block-padding-val" value="<?php echo $block_padding; ?>" />
				<!-- end: data holder -->
			</div>
<?php
		}

		/*
  *  render_field_settings()
  *
  *  Create extra options for your field. This is rendered when editing a field.
  *  The value of $field['name'] can be used (like bellow) to save extra data to the $field
  *
  *  @type	action
  *  @since	3.6
  *  @date	23/01/13
  *
  *  @param	$field	- an array holding all the field's data
  */

		function render_field_settings($field) {

			// display_format
			acf_render_field_setting($field, array(
				'label'			=> __('JSON Value', 'acf'),
				'instructions'	=> '',
				'type'			=> 'text',
				'name'			=> 'json_value',
				'placeholder'	=> ''
			));
		}

		/*
	*  format_value()
	*
	*  This filter is appied to the $value after it is loaded from the db and before it is returned to the template
	*
	*  @type	filter
	*
	*  @param	$value (mixed) the value which was loaded from the database
	*  @param	$post_id (mixed) the $post_id from which the value was loaded
	*  @param	$field (array) the field array holding all the field options
	*
	*  @return	$value (mixed) the modified value
	*/
		function format_value($value, $post_id, $field) {
		}
	}



	// initialize
	if (function_exists('acf_register_field_type')) {
		acf_register_field_type(new acf_flo_flex_builder());
	}

endif; // class_exists check

?>