<?php
namespace common\components\accountstate;
use yii\base\Widget;
use common\models\AccountState;
use yii\data\ArrayDataProvider;
use yii\grid\GridView;
use yii\helpers\Html;

/**
 * This is the model class for widget display bank notes.
 *
 * @property common\models\AccountState $model
 * */
class BankNotesWidget extends Widget{
	
	 public $model;
	
	
	public function init(){
		parent::init();
	}
	
	public function run(){
		echo $this->generateNotes();
	}
	
	protected function generateNotes(){
		$s = "";
		$s .= Html::beginTag("div", ['class' => 'row', 'style' => 'margin-top: 6px;']);
		$s .= Html::beginTag("div", ['class' => 'col-md-4']);
		$s .= $this->generateBanknoteGrid( [ 'banknote_5_ft','banknote_10_ft','banknote_20_ft','banknote_50_ft'  ]);
		$s .= Html::endTag("div");
		$s .= Html::beginTag("div", ['class' => 'col-md-4']);
		$s .= $this->generateBanknoteGrid( [ 'banknote_100_ft','banknote_200_ft','banknote_500_ft','banknote_1000_ft'  ]);
		$s .= Html::endTag("div");
		$s .= Html::beginTag("div", ['class' => 'col-md-4']);
		$s .= $this->generateBanknoteGrid( [ 'banknote_2000_ft','banknote_5000_ft','banknote_10000_ft','banknote_20000_ft'  ]);
		$s .= Html::endTag("div");
		$s .= Html::endTag("div");
		return $s;
	}
	
	protected function generateBanknoteGrid($attributes){
	
		return $this->generateBanknoteColumn( $this->mkColumnData($attributes));
	}
	
	protected function mkColumnData( $attributes){
	
		$values = AccountState::banknoteValues();
		$items = [];
		foreach ($attributes as $note){
			$value = $values[$note];
			$count = $this->model->$note;
			if ( !isset($count)  || empty($count)){
				$count = 0;
			}
			$value = \Yii::$app->formatter->asInteger($value);
			$item = [
					'note' => $value . " Ft",
					'count' =>   $count
			];
			$items[] = $item;
		}
	
		return $items;
	}
	
	protected function generateBanknoteColumn($data){
		$dp = new ArrayDataProvider(
				[
						'allModels' => $data,
						'sort' => false,
						'pagination' => false
				]
				);
		$s = GridView::widget([
				'dataProvider' => $dp,
				'layout' => '{items}',
				'options' => ['class' => 'grid-view notes-view'],
				'columns' =>[
						[
								'contentOptions' => ['class' => 'note-name'],
								'value' => 'note',
								'label' => 'Címlet'
						],
						[
								'value' => 'count',
								'label' => 'Db'
						],
				]
		]);
		return $s;
	}
	
}