<?php

namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\Category;
use app\models\Job;
use yii\data\Pagination;
class JobsController extends \yii\web\Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['create','edit','delete'],
                'rules' => [
                    [
                        'actions' => ['create','edit','delete'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ]
        ];
    }
    public function actionIndex()
    {
        Yii::$app->formatter->locale = 'fr-FR';
        //get all jobs from database
        $query = Job::find();
        //use pagination
        $pagination = new Pagination([
            'defaultPageSize' => 10,
            'totalCount' => $query->count(),
        ]);
        $jobs = $query->orderBy('created_at')
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        return $this->render('index',[
            'jobs' => $jobs,
            'pagination'=>$pagination
        ]);
    }
    public function actionView($id){
        Yii::$app->formatter->locale = 'fr-FR';
        //get all jobs from database
        $job = Job::find()->where(['id'=>$id])->one();
        return $this->render('view',[
            'job' => $job
        ]);
    }
    public function actionCategory($id){
        Yii::$app->formatter->locale = 'fr-FR';
        //get all jobs from database
        $jobs = Job::find()->where(['category_id'=>$id])->orderBy('created_at')->all();
        return $this->render('jobs_by_category',[
            'jobs' => $jobs
        ]);
    }
    public function actionCreate()
    {
        $job = new Job();
        if ($job->load(Yii::$app->request->post())) {
            if ($job->validate()) {
               $job->save();
               \Yii::$app->getSession()->setFlash('success', 'Offre ajoutée.');
               return $this->redirect('index.php?r=jobs');
            }
        }
        return $this->render('create', [
            'job' => $job,
        ]);
    }

    public function actionDelete($id)
    {
        $job = Job::find()->where(['id'=>$id])->one();
        if(isset(Yii::$app->user->identity->id) && (Yii::$app->user->identity->id != $job->user_id)){
            return $this->redirect('index.php?r=jobs');
        }
        $job->delete();
        \Yii::$app->getSession()->setFlash('success', 'Offre supprimée.');
        return $this->redirect('index.php?r=jobs');
    }

    public function actionEdit($id)
    {
        $job = Job::find()->where(['id'=>$id])->one();
        if(isset(Yii::$app->user->identity->id) && (Yii::$app->user->identity->id != $job->user_id)){
            return $this->redirect('index.php?r=jobs');
        }
        if ($job->load(Yii::$app->request->post())) {
            if ($job->validate()) {
               $job->save();
               \Yii::$app->getSession()->setFlash('success', 'Offre modifiée.');
               return $this->redirect('index.php?r=jobs');
            }
        }
        return $this->render('create', [
            'job' => $job,
        ]);
    }
}
