<?php

namespace app\controllers;

use Yii;
use app\models\Posts;
use app\models\search\PostsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\Comments;
use app\models\search\CommentsSearch;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\data\ActiveDataProvider;
use yii\web\UploadedFile;


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

    /**
     * Lists all Posts models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new PostsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    /**
     * Displays a single Posts model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $searchModel = new CommentsSearch();
        $comments = Comments::findAll(['PostID' => $id]);

        $newComment = new Comments();

        $sql = "SELECT MAX(CommentID) AS CommentID FROM comments";
        $max_id = Comments::findBySql($sql)->one();
        $upload_id_comments = $max_id->CommentID;
        
        if ($newComment->load(Yii::$app->request->post())) {
            $newComment->TimeStamp = date("Y-m-d H:i:s");

            $newComment->Attachment = UploadedFile::getInstance($newComment, 'Attachment');   
            if ($newComment->Attachment && $newComment->validate()) {                
                $newComment->Attachment->saveAs('../uploads/comments/' . $newComment->Attachment->baseName .'_11111'.$upload_id_comments. '.' . $newComment->Attachment->extension);
                $newComment->Attachment = $newComment->Attachment->baseName .'_11111'.$upload_id_comments. '.' . $newComment->Attachment->extension;
            }
            
            if($newComment->save()) {
                return $this->redirect(['view', 'id' => $newComment->PostID]);    
            }
            
        } else {
            return $this->render('view', [
                'model' => $this->findModel($id),
                'newComment' => $newComment,
                'comments' => $comments,
            ]);
        }
    }

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

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

    /**
     * Updates an existing Posts 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->PostID]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

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