<?php

class Intense_Field_Raw extends Intense_Field {
	protected function render() {
		$rows = isset( $this->args[ 'rows' ] ) ? ' rows="' . $this->args[ 'rows' ] . '"' : '';
		$columns = isset( $this->args[ 'columns' ] ) ? ' cols="' . $this->args[ 'columns' ] . '"' : '';

		return '<textarea class="dialog textarea" type="text" name="' . $this->id . '" id="' . $this->id . '" value="' . $this->value . '"' . $rows . $columns . '>' . $this->default . '</textarea><br><button id="' . $this->id . '_decode" data-field="' . $this->id . '" type="button" class="button decode">' . __( 'Decode Raw Value', 'intense' ) . '</button><br>' . $this->description;
	}

	public function render_init_script() {
		return '$(".dialog.textarea").typeWatch({
				callback: intenseRefreshPreview,
				wait: 500,
				highlight: false,
				captureLength: 0
			});			
			$(".button.decode").unbind("click").click(function() {				
				var $field = $("#" + $(this).data("field"));
				
				$field.val(rawurldecode(base64_decode(strip_tags($field.val()))));
			});';
	}

	public function render_value_script() {
		return '($(\'#' . $this->id . '\').val().length > 0 && $(\'#' . $this->id . '\').val() !== \'0\' ? \' ' . $this->id . '="\' + base64_encode(rawurlencode($(\'#' . $this->id . '\').val())) + \'"\' : "" ) + ';
	}

	public function map_visual_composer() {
		return array(
			"type" => "textarea_raw_html",
			//"holder" => "button",
			"class" => "",
			"heading" => $this->title,
			"param_name" => $this->id,
			"value" => !empty( $this->default ) ? $this->default : '',
			'description' => ( isset( $this->args['description'] ) ? $this->args['description'] : null ),
			"admin_label" => $this->composer_show_value,
		);
	}
}