<?php

namespace app\controllers;

use app\models\Trade;
use Yii;
use app\models\Eventlog;
use app\models\EventlogSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * EventlogController implements the CRUD actions for Eventlog model.
 */
class EventlogController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Eventlog models.
     * @return mixed
     */
    public function init()
    {

        if(Yii::$app->user->isGuest)
        {
            return $this->redirect('/user/login');
        }
        if(!Yii::$app->user->can("admin"))
        {
            return $this->redirect('/site/index');
        }
    }

    public function actionIndex()
    {
        $searchModel = new EventlogSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        //print_r($dataProvider); die();

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Eventlog model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Eventlog model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Eventlog();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Eventlog model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }
    public function actionTradelog($id,$user_id)
    {
        $query = Yii::$app->db->createCommand('SELECT * FROM trade_logs WHERE auk_id=:id AND user_id=:user_id');
        $query->bindValues([':id' => $id, ':user_id'    =>  $user_id]);
        $result = $query->queryAll();
        $filename = "../uploads/temp/tradelog-".$id.$user_id.date('Ymdhis').".doc";
        $file = fopen($filename,'w+');
        foreach ($result as $item => $value)
        {
            fputs($file,$value['date']." ".$value['comment']."\n");
        }
        fclose($file);
        Yii::$app->response->xSendFile($filename);
    }

    /**
     * Deletes an existing Eventlog model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Eventlog model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Eventlog the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Eventlog::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
