<?php
namespace frontend\components;

use yii\base\Widget;
use common\models\AccountState;
use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\grid\GridView;
use yii\base\Object;
use yii\data\ArrayDataProvider;
use yii\helpers\Url;
use common\components\total\TotalDifferenceWidget;

/**
 * Display account state widget
 * 
 * @property  common\models\AccountState $model the account state object
 * @property int $index the current index in the list
 * */
class AccountStateBanknoteCountWidget extends Widget{
	
	 public $model;
	 public $layout;
	 public $index;
	 public $showDailyDownload = true;
	
	public function run(){
		
		$panelStyleClas = 'panel-default';
		if ( $this->model->hasDifferenceToPrevState()){
			if ( $this->model->hasPlus()){
				$panelStyleClas = 'panel-success';
			}else if ( $this->model->hasMinus() ){
				$panelStyleClas = 'panel-danger';
			}
		}
			
		$s = "";
		$s .= Html::beginTag("div",['class' => 'panel '.$panelStyleClas] );
		$s .= Html::beginTag("div",['class' => 'panel-heading    '] );
		$s .=  "Kassza művelet - " . $this->model->typeName;
		$s .= Html::endTag("div");
		$s .= Html::beginTag("div",['class' => 'panel-body    '] );
		
		
		
		$s .= $this->generateInfoRow();
// 		$s .= $this->generateNotes();

		if ( $this->model->hasDifferenceToPrevState()){
			$s .= "<h3>Különbözet</h3>";		
			$s .= TotalDifferenceWidget::widget(['model' => $this->model]);
		}
		
		
		$s .= $this->generateComment();
		
		$s .= Html::beginTag("div", ['class' => 'row', 'style' => 'margin-top: 6px;']);
		$s .= Html::beginTag("div", ['class' => 'col-md-12 text-right']);
		
		$s .= Html::a( Html::tag("span","",['class' =>'glyphicon glyphicon-download-alt']) ."&nbsp;Pdf", Url::to([ 'view', 'id' =>$this->model->id_account_state, 'output' =>'pdf']) ,['class' => 'btn btn-primary btn-pdf','style' =>'margin-bottom: 12px; margin-right: 6px;']);
		
		if ( $this->model->isTypeClose() ){
			if ( $this->showDailyDownload ){
				$s .= Html::a('<span class="glyphicon glyphicon-eye-open"></span>&nbsp;Napi összefoglaló', Url::toRoute(['mixed','id' =>$this->model->id_account_state]), [
		 			'title' => 'Napi összefoglaló',
		 			'class' => 'btn btn-success',
		 													'style' =>'margin-bottom: 12px; margin-right: 6px;'
		 	    ]);
			}
		}
		
		$s .= Html::a('<span class="glyphicon glyphicon-eye-open"></span>&nbsp;Részletek', Url::toRoute(['view','id' =>$this->model->id_account_state]), [
 			'title' => 'Részletek',
 			'class' => 'btn btn-success',
 													'style' =>'margin-bottom: 12px; margin-right: 6px;'
 	]);
		$s .= Html::a('<span class="glyphicon glyphicon-trash"></span>&nbsp;Törlés', Url::toRoute(['delete','id' =>$this->model->id_account_state]), [
 			'title' => \Yii::t('yii', 'Delete'),
 			'data-confirm' =>\Yii::t('yii', 'Are you sure to delete this item?'),
 			'data-method' => 'post',
 			'class' => 'btn btn-danger',
 													'style' =>'margin-bottom: 12px;'
 	]);
		
		$s .= Html::endTag("div");//end col
		$s .= Html::endTag("div");//end row
		
		$s .= Html::endTag("div");
		$s .= Html::endTag("div");
		
		echo $s;
	}

	
	protected function generateInfoRow(){
		$s = "";
	
		$s .= Html::beginTag("div", ['class' => 'row', 'style' => 'margin-top: 6px;']);
		$s .= Html::beginTag("div", ['class' => 'col-md-6']);
		$s .= DetailView::widget([
				'model' => $this->model,
				'attributes' => [
						[ 
								'attribute' => 'id_user',
								'value' =>  $this->model->userName
						],
						'created_at:datetime',
				]
		]);		
		 
		$s .= Html::endTag("div");
		$s .= Html::beginTag("div", ['class' => 'col-md-6']);
		$s .= DetailView::widget([
				'model' => $this->model,
				'attributes' => [
						'typeName',
						'accountName',
						'money:integer'
				]
		]);		
		 
		$s .= Html::endTag("div");
		$s .= Html::endTag("div");
		
		return $s;
		
	}
	
	
	protected function generateComment(){
		$s = "";
		if ( isset($this->model->comment) && !empty($this->model->comment)){
			
			$s .= Html::beginTag("div", ['class' => 'row', 'style' => 'margin-top: 6px;']);
			$s .= Html::beginTag("div", ['class' => 'col-md-12']);
			$s .= Html::beginTag("table", ['class' => 'table table-striped table-bordered']);
			$s .= Html::beginTag("thead", ['class' => ' ']);
			$s .= Html::beginTag("tr", ['class' => ' ']);
			$s .= Html::beginTag("th", ['class' => ' ']);
			$s .= $this->model->getAttributeLabel('comment');
			$s .= Html::endTag("tr");
			$s .= Html::endTag("th");
			$s .= Html::endTag("thead");
			$s .= Html::beginTag("tbody", ['class' => ' ']);
			$s .= Html::beginTag("tr", ['class' => ' ']);
			$s .= Html::beginTag("td", ['class' => ' ']);
			$s .= $this->model->comment;
			$s .= Html::endTag("td");
			$s .= Html::endTag("th");
			$s .= Html::endTag("tbody");
			$s .= Html::endTag("table");
			$s .= Html::endTag("div");
			$s .= Html::endTag("div");
			
		}
		return $s;
	}
	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' =>[
					[
						'value' => 'note',
						'label' => 'Címlet'
					],
					[
						'value' => 'count',
						'label' => 'Db'
					],
				]
		]);
		return $s;
	}
	
	 
	
}