<?php
namespace console\controllers;

use Yii;
use common\models;
use yii\console\Controller;
use common\models\Ticket;
use common\models\DoorLog;
use yii\helpers\VarDumper;
use common\models\Customer;
use common\models\Log;

class DoorlogController extends Controller{
	

	public $ticket;
	
	
// 	public function options($actionID)
// 	{
// 		return ['ticket'];
// 	}
	
// 	public function optionAliases()
// 	{
// 		return ['t' => 'ticket'];
// 	}
	
	 
	
	public function actionIn($ticket)
	{
		
		$log = new DoorLog();
		//5559	9719

		$log->id_card = 9719;
		$log->id_customer = 5559;
		$log->id_ticket_current = $ticket;
		$log->direction = 3;
		$log->id_key = 1; 
		$log->type = 0;
		$log->source_app = DoorLog::$SOURCE_APP_FITNESS_ADMIN;
		$log->id_account  = null;
		$log->card_flag = 0;
		$log->flag_out= 0;
		
		
		
		$log->save(false);
	}
	
	public function actionOut($ticket)
	{
		$log = new DoorLog();
		

		$log->id_card = 9719;
		$log->id_customer = 5559;
		$log->id_ticket_current = $ticket;
		$log->direction = 1;
		$log->id_key = null; 
		$log->type = 0;
		$log->source_app = DoorLog::$SOURCE_APP_FITNESS_ADMIN;
		$log->id_account  = null;
		$log->card_flag = 0;
		$log->flag_out= 0;
		
		
		$log->save(false);
	}
	
	protected function log($ticket){
		$subject = Ticket::find()->andWhere( ["id_ticket" => intval($ticket)])->one();
		
		
		$card = $subject->card;
		echo "in:" . $subject->usage_count. " | out:" . $subject->count_move_out . " | max: " .$subject->max_usage_count. " | card flag: " . $card->flag ."\n";
		
		
	}
	
	public function actionVisit($ticket) {
		$this->log($ticket);
		$this->actionIn($ticket);
		$this->log($ticket);
		$this->actionOut($ticket);
		$this->log($ticket);
	}
	
	
	public function actionFixmissing(){
		
		$tickets = Ticket::find()->andWhere(['in','id_ticket_type' ,[54,55,56,57]])->all();
		
		
		
		foreach ($tickets as $ticket){
			$cache = [];
			
			$doorlogs = DoorLog::find()->andWhere( [ 'id_ticket_current' => $ticket->id_ticket, 'direction' => 7 ])->all();
			
			
			foreach ($doorlogs as $doorlog ){
				//echo VarDumper::dump($doorlogs);
				
				$keydate =  substr($doorlog->created_at,0,10 );
				
				$d1 = strtotime($keydate);
				$d2 = $d1 + 60 *60 *24;
				
				$d1 = date("Y-m-d H:i:s",$d1);
				$d2 = date("Y-m-d H:i:s",$d2);
				
				$logs = Log::find()->andWhere(['id_ticket' => $ticket->id_ticket, 'type' => 30])->andWhere(['>=','created_at',$d1])->andWhere(['<','created_at',$d2])->all();
				
				
				if ( count($logs) == 0 ){
				
					if ( array_key_exists($keydate, $cache) ){
						$cache[$keydate]['count'] = $cache[$keydate]['count'] + 1;
					}else{
						$cache[$keydate]['count'] = 1;
						$cache[$keydate]['id'] = $doorlog->id_door_log;
					}
				
				} 
			
			}
			
			if ( count($cache) > 0 ){
			
				foreach ($cache as $item){
					
					echo $item['id'];
					echo "/";
					echo $item['count'];
					echo "\r\n";
					
					$t = Ticket::findOne($ticket->id_ticket);
					
					
					$elotte = $t->usage_count;
					$t->usage_count = $t->usage_count + 1;
					$t->count_move_out = $t->usage_count;
					
					$t->save(false);
					
					$log = new Log();
					$log->type = 30;
					$log->id_ticket = $ticket->id_ticket;
					$log->app = 'trigger_inc_ticket';
					$log->id_door_log = $item['id'];
					$log->message = "FixMissing: Bérlet használat (elotte: " . $elotte ." > utana: " . $t->usage_count . " max: ". $t->max_usage_count .")";
					$log->save(false);
					
				}
			 
			}
			
			
		}
		
		
		
		
	}
	

}