<?php

namespace common\models;

use common\components\Helper;
use Yii;
use yii\helpers\Url;
use yii\helpers\VarDumper;
use common\models\BaseFitnessActiveRecord;
use yii\helpers\Console;

/**
 * This is the model class for table "log".
 *
 * @property integer $id_log
 * @property integer $type
 * @property string $message
 * @property string $url
 * @property string $app
 * @property integer $id_user
 * @property integer $id_transfer
 * @property integer $id_money_movement
 * @property integer $id_ticket
 * @property integer $id_sale
 * @property integer $id_customer
 * @property integer $id_account
 * @property integer $id_account_state
 * @property integer $id_key
 * @property integer $id_product
 * @property integer $id_door_log
 * @property string $created_at
 * @property string $updated_at
 */
class Log extends BaseFitnessActiveRecord
{
   
	public static $TYPE_INFO = 10;
	public static $TYPE_ERR = 20;
	public static $TYPE_TICKET_USAGE_FIRST = 30;
	public static $TYPE_TICKET_USAGE_MULTIPLE = 40;
	public static $TYPE_LOGIN = 50;
	public static $TYPE_DEFAULT_ACCOUNT= 60;
	public static $TYPE_CREATE_CUSTOMER= 70;
	public static $TYPE_PROCUREMENT_UPDATE = 80;
	public static $TYPE_TICKET_COUNT_MOVE_OUT = 90;
	public static $TYPE_WASTE = 100;
	public static $TYPE_NEWSLETTER_SUBSCRIBE = 110;
	public static $TYPE_NEWSLETTER_UNSUBSCRIBE = 120;
	public static $TYPE_NEWSLETTER_SENT = 130;
	public static $TYPE_TICKET_EXPIRE_SENT = 140;
	public static $TYPE_NEWSLETTER_SEND_START = 150;
	public static $TYPE_NEWSLETTER_SEND_END = 160;
	public static $TYPE_KEY_ASSIGN = 170;
	public static $TYPE_KEY_UNASSIGN = 180;
	public static $TYPE_TOWEL_IN = 190;
	public static $TYPE_TOWEL_OUT = 200;
	public static $TYPE_CRUD = 210;
	public static $TYPE_TICKET_UPDATED_BY_ADMIN = 220;


	public static  function getTypes(){
	    return [
	        
	       Log::$TYPE_INFO => "Info",
	 Log::$TYPE_ERR => "Hiba",
	 Log::$TYPE_TICKET_USAGE_FIRST => "Bérlet használat",
	 Log::$TYPE_TICKET_USAGE_MULTIPLE => "Többszöri bérlet használat",
	 Log::$TYPE_LOGIN => "Bejelentkezés",
	 Log::$TYPE_DEFAULT_ACCOUNT=> "Alapértelmezett kassza",
	 Log::$TYPE_CREATE_CUSTOMER=> "Új vendég",
	 Log::$TYPE_PROCUREMENT_UPDATE => "Beszerzés módosítás",
	 Log::$TYPE_TICKET_COUNT_MOVE_OUT => "Ki mozgás",
	 Log::$TYPE_WASTE => "Selejt",
	 Log::$TYPE_NEWSLETTER_SUBSCRIBE => "Feliratkozás hirlevélre",
	 Log::$TYPE_NEWSLETTER_UNSUBSCRIBE => "Leiratkozás hírlevélről",
	 Log::$TYPE_NEWSLETTER_SENT => "Hirlevél elküldve",
	 Log::$TYPE_TICKET_EXPIRE_SENT => "Bérlet lejáart figyelmeztetés elküldve",
	 Log::$TYPE_NEWSLETTER_SEND_START => "Hirlevél küldés start",
	 Log::$TYPE_NEWSLETTER_SEND_END => "Hirlevél küldés vége",
	 Log::$TYPE_KEY_ASSIGN => "Kulcs kiadás",
	 Log::$TYPE_KEY_UNASSIGN => "Kulcs visszaadás",
	 Log::$TYPE_TOWEL_IN => "Törölköző ki",
	 Log::$TYPE_TOWEL_OUT => "Törölköző vissza",
	 Log::$TYPE_CRUD => "CRUD",
	 Log::$TYPE_TICKET_UPDATED_BY_ADMIN => "Bérlet módosítás"
	        
        ];
}


    public function getTypeName(){
        $types = Log::getTypes();
        return Helper::getArrayValue($types,$this->type,null);
    }

	/**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'log';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id_log' => Yii::t('common/log', 'Azonosító'),
            'type' => Yii::t('common/log', 'Típus'),
            'message' => Yii::t('common/log', 'Üzenet'),
            'url' => Yii::t('common/log', 'Url'),
            'app' => Yii::t('common/log', 'Alkalmazás'),
            'id_user' => Yii::t('common/log', 'Felhasználó'),
            'id_transfer' => Yii::t('common/log', 'Tranzakció'),
            'id_money_movement' => Yii::t('common/log', 'Pénzmozgás'),
            'id_ticket' => Yii::t('common/log', 'Bérlet'),
            'id_sale' => Yii::t('common/log', 'Termékeladás'),
            'id_customer' => Yii::t('common/log', 'Vendég'),
            'id_account' => Yii::t('common/log', 'Kassza'),
            'id_account_state' => Yii::t('common/log', 'Id Account State'),
            'id_key' => Yii::t('common/log', 'Kulcs'),
            'id_product' => Yii::t('common/log', 'Termék'),
            'id_door_log' => Yii::t('common/log', 'Kapu log'),
            'created_at' => Yii::t('common/log', 'Dátum idő'),
            'updated_at' => Yii::t('common/log', 'Módosítás'),
        		'start' => 'Időszak kezdete',
        		'end' => 'Időszak vége'
        ];
    }
    
    
    public static function info($message ){
    	self::log(['type' =>self::$TYPE_INFO, 'message' => $message]);
    }
    
    /**
     * example
     * Log::log([
        			'type' =>Log::$TYPE_LOGIN,
        			'message' => $message
        	]);
     * @param array $config
     */
    public static function log( $config ){
    	$model = new Log($config);
    	$model->app = \Yii::$app->name;
    	$model->url = Url::canonical();
    	$model->id_user = \Yii::$app->user->id;
    	$model->save(false);
    }
    
    /**
     * create a log from the console app
     * */
    public static function logC( $config ){
    	$model = new Log($config);
    	$model->app = "Fitness rendszer";
    	$model->url = "console";
    	$model->id_user = 1;
    	$model->save(false);
    }

    public function getUser(){
        return $this->hasOne( User::className(), ["id" =>"id_user" ] );
    }

    public function getTicket(){
        return $this->hasOne( Ticket::className(), ["id_ticket" =>"id_ticket" ] );
    }

    public function getCustomer(){
        return $this->hasOne( Customer::className(), ["id_customer" =>"id_customer" ] );
    }

    public function getMoneyMovement(){
        return $this->hasOne( MoneyMovement::className(), ["id_money_movement" =>"id_money_movement" ] );
    }


    public function getUserName(){
        $user = $this->user;
        if ( isset($user)){
            return $user->username;
        }
        return null;
    }

    public function getCustomerName(){
        $customer = $this->customer;
        if ( isset($customer)){
            return $customer->name;
        }
        return null;
    }

    public function getTicketName(){
        $ticket = $this->ticket;
        if ( isset($ticket)){
            return $ticket->ticketTypeName;
        }
        return null;
    }


}
