<?php
/**
 * WARNING:  DO NOT edit this file under any circumstances.
 *
 * Renders custom radio image control type for WP Customizer.
 *
 * @package CR_Login_Page_UI_Customizer
 * @author  Mangesh Nadekar
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 *
 * Class CRLPUIC_Radio_Images_Control renders custom radio image control type.
 *
 * @since 1.0.0
 */
class CRLPUIC_Radio_Images_Control extends WP_Customize_Control {

	/**
	 * The type of customize control being rendered.
	 *
	 * @access public
	 * @since  1.0.0
	 * @var    string
	 */
	public $type = 'crlpuic_radio_image';

	/**
	 * Add our JavaScript and CSS to the Customizer.
	 *
	 * @access public
	 * @since  1.0.0
	 * @return void
	 */
	public function enqueue() {

		parent::enqueue();

		wp_enqueue_script( 'crlpuic-login-customizer-controls' );
		wp_enqueue_style( 'crlpuic-login-customizer-script' );

	}

	/**
	 * Add custom JSON parameters to use in the JS template.
	 *
	 * @access public
	 * @since  1.0.0
	 */
	public function to_json() {

		parent::to_json();

		// Create the image URL.
		foreach ( $this->choices as $value => $args ) {

			$this->choices[ $value ]['url'] = esc_url( $args['url'] );
		}

		$this->json['choices'] = $this->choices;
		$this->json['link']    = $this->get_link();
		$this->json['value']   = $this->value();
		$this->json['id']      = $this->id;

		$templates = crlpuic_get_templates();

		foreach ( $templates as $t_name => $template ) {
				$count = count( $template );
				$index = 1;
				$data  = '{';
			foreach ( $template as $key => $value ) {
				if ( $index === $count ) {
					$data .= '"' . $key . '": "' . $value . '"';
				} else {
					$data .= '"' . $key . '": "' . $value . '",';
				}
				$index ++;
			}
				$data .= '}';

				$tpl_setting[ $t_name ] = $data;
		}
		$this->json['tpl_settings'] = $tpl_setting;
	}

	/**
	 * Don't render the content via PHP.  This control is handled with a JS template.
	 *
	 * @since  1.0.0
	 */
	protected function render_content() {}

	/**
	 * An Underscore (JS) template for this control's content.
	 *
	 * Class variables for this control class are available in the `data` JS object;
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
	 *
	 * @see    WP_Customize_Control::print_template()
	 *
	 * @access protected
	 * @since  1.0.0
	 * @return void
	 */
	public function content_template() {

		?>
		<# if ( ! data.choices ) {
			return;
		} #>

		<# if ( data.label ) { #>
			<span class="customize-control-title">{{{ data.label }}}</span>
		<# } #>

		<# if ( data.description ) { #>
			<span class="description customize-control-description">{{{ data.description }}}</span>
		<# } #>

		<div class="crlpuic-customize-control-radio-image">
		<# _.each( data.choices, function( args, choice ) { #>
			<label>
				<input type="radio" value="{{ choice }}" name="_customize-{{ data.type }}-{{ data.id }}" <# _.each( data.tpl_settings, function( setting,  key ) { if( key === choice) { #>  data-template="{{setting}}" <#  } } ) #> {{{ data.link }}} <# if ( choice === data.value ) { #> checked="checked" <# } #> />

				<span class="screen-reader-text">{{ args.label }}</span>

				<img src="{{ args.url }}" alt="{{ args.label }}" />
			</label>
		<# } ) #>
		</div>
		<?php
	}

}
