<?php

class Intense_Field_Icon extends Intense_Field_Dropdown {
	private function set_default_options() {
		
	}

	public function render_field() {
		// default value will start with __
		$this->class .= ' selectize icontype' . ( !empty( $this->args['default']) ? ' __' . $this->args['default'] : '');
		return parent::render_field();
	}

	public function render_init_script() {
		$ajax_nonce = wp_create_nonce( "intense-plugin-noonce" );

		return " var xhr = [];
				$('.selectize.icontype select').not('.repeater-item-model .selectize.icontype select').selectize({
    				sortField: 'text',
    				render: {
				        option: function(item, escape) {
				        	return '<div class=\"intense icon\" style=\"float: left; width: 22px; height:22px; margin: 1px; text-align: center;\">' + 
				        	item.svg + 
				        	'</div>';
				        },
				        item: function(item, escape) {
				        	return '<div><div class=\"intense icon\" style=\"float: left; width: 20px; height:20px; margin-right: 5px; text-align: center;\">' + 
				        	item.svg + '</div> ' + item.text  +
				        	'</div>';
				        }
			        },
			        load: function(callback) { }
				});

				$('.selectize.iconsource select').not('.repeater-item-model .selectize.iconsource select').change(function() {
					var source = $(this).val();
					var source_key = $(this).attr('id');	
					var icon_type_wrapper = $(this).parents('.iconsource').next('.icontype');
					var icon_type_id = icon_type_wrapper.find('select').attr('id');	
					var icon_type = jQuery('#' + icon_type_id).not('.repeater-item-model .selectize.icontype select')[0].selectize;				
					
			        icon_type.disable();
			        icon_type.clear();			        
			        icon_type.clearOptions();

			        icon_type.load(function(callback) {
			            xhr[icon_type_id] && xhr[icon_type_id].abort();
			            xhr[icon_type_id] = $.ajax({
			            	type: 'POST',
			                url: ajaxurl + '?action=intense_icon_type_action',
			                dataType: 'json',
			                cache: false,
			                data: {
				        		security: '" . $ajax_nonce . "',
				                source: source,
				                type_id: icon_type_id	                
				            },
			                success: function(results) {
			                	var select = jQuery('#' + results.type_id).not('.repeater-item-model .selectize.icontype select');
			                	var icon_type = select[0].selectize;							
			                	var classList = select.parents('.icontype').attr('class').split(/\s+/);
			                	var defaultVal = '';

								$.each( classList, function(index, item){									
								    if (item[0] === '_' && item[1] === '_') {
								       defaultVal = item.replace('__', '', 'gi');
								    }
								});	

			                	icon_type.enable();
								icon_type.refreshOptions();
								icon_type.clearOptions();
								icon_type.renderCache = {};								
			                    callback(results.icons);

			                    icon_type.setValue(defaultVal);			                    
			                },
			                error: function() {
			                    callback();
			                }
			            });
			        });
				});

				$(document).ready(function() {					
					$('.selectize.iconsource select:not(.initialized)').not('.repeater-item-model .selectize.iconsource select').addClass('initialized').change();					
				});
			";
	}	

	protected function render() {
		if ( empty( $this->args['class'] ) ) $this->args['class'] = '';
		if ( empty( $this->args['style'] ) ) $this->args['style'] = '';

		$this->set_default_options();

		return parent::render();
	}

	public function map_visual_composer() {
		return array(
			"type" => "textfield",
			//"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,
		);
	}
}
