<?php
namespace common\components;

use Yii;
use kartik\widgets\Typeahead;
use yii\helpers\Url;
use yii\web\JsExpression;


class CardNumberTypeahead extends Typeahead{
	
	public $item_template = '<div><p class="">{{txt}}</p></div>';
	
	public $options = ['placeholder' => 'Válassz bérletkártyát!'];
	
	public $pluginOptions = ['highlight'=>true , 'minLength' => 3 ];
	
	public $scrollable = true;
	
	public $display = 'number';
	
	public $pluginEvents = [	"typeahead:select" => "function(a,b) {   }",];
	
	public $onlyFreeCards = true;
			 
	
	
	public function init(){
		parent::init();
		$this->dataset = $this->createDefaultDataset();
	}
	
	protected function createDefaultDataset(){
		return [
    				[
    					'limit' => 20,
    					'remote' => [
    							'ttl' => 1,
    							'url' => Url::to(['card/list']) . '&onlyFree='.($this->onlyFreeCards ?  '1' :'0').'&search=%QUERY&' . rand(0, 100000000),
    							'wildcard' => '%QUERY'
    						],
    						'limit' => 10,
    						'display' => $this->display,
    						'templates' => [
	    						'notFound' => '<div class="text-danger" style="padding:0 8px">Unable to find number for selected query.</div>',
	    						'suggestion' => new JsExpression("Handlebars.compile('{$this->item_template}')")
    						]
    			]
		];
	}
	
}