<?php
namespace common\components;

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


class CityZipTypeahead extends Typeahead{
	
	public $item_template = '<div><p class="">{{fullname}}</p></div>';
	
	public $options = ['placeholder' => 'Válassz irányítószám'];
	
	public $pluginOptions = ['highlight'=>true];
	
	public $scrollable = true;
	
	public $display = 'zip';
	
	public $pluginEvents = [	"typeahead:select" => "function(a,b) {   }",];
			 
	
	
	public function init(){
		parent::init();
		$this->dataset = $this->createDefaultDataset();
	}
	
	protected function createDefaultDataset(){
		return [
    				[
    					'limit' => 20,
    					'remote' => [
    							'ttl' => 1,
    							'url' => Url::to(['city/zip-list']) . '&search=%QUERY&' . rand(0, 100000000),
    							'wildcard' => '%QUERY'
    						],
    						'limit' => 10,
    						'display' => $this->display,
    						'templates' => [
	    						'notFound' => '<div class="text-danger" style="padding:0 8px">Nincs találat</div>',
	    						'suggestion' => new JsExpression("Handlebars.compile('{$this->item_template}')")
    						]
    			]
		];
	}
	
}