<?php

namespace backend\controllers;
use developeruz\db_rbac\behaviors\AccessBehavior;
use common\models\Slider;
use Yii;
use common\models\SliderImage;
use common\models\SliderImageSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
/**
 * SliderImageController implements the CRUD actions for SliderImage model.
 */
class SliderImageController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access'=>[
                'class' => AccessBehavior::className(),
                'rules' =>
                    ['site' =>
                        [
                            [
                                'actions' => ['login', 'error'],
                                'allow' => true,
                            ]
                        ]
                    ]
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all SliderImage models.
     * @param $slider_id Slider id
     * @return mixed
     */
    public function actionIndex($slider_id)
    {
        $searchModel = new SliderImageSearch();
        $dataProvider = $searchModel->search($slider_id, Yii::$app->request->queryParams);

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

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

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

        if ($model->load(Yii::$app->request->post())) {
            $model->slider_id = $slider_id;
            if ( ($image = UploadedFile::getInstance($model, 'image')) ) {
                $model->image = $image->name;
            }
            if ($model->save() && $image) {

                $imgDir = Yii::getAlias('@storage/slider/');

                if(!is_dir($imgDir)) {
                    mkdir($imgDir, 0755, true);
                }

                $image->saveAs(Yii::getAlias('@storage/slider/' . $image->name));
            }
            return $this->redirect(['view', 'slider_id'=>$slider_id, 'id' => $model->slider_image_id]);
        } else {

            $slider = Slider::findOne($slider_id);

            return $this->render('create', [
                'slider_id' => $slider_id,
                'model' => $model,
                'slider' => $slider,
            ]);
        }
    }

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

        if ($model->load(Yii::$app->request->post())) {



            if ( ($image = UploadedFile::getInstance($model, 'image')) ) {
                $model->image = $image->name;
            }

            if(!$model->image){
                $model->image = $model->getOldAttribute('image');
            }

            if ($model->save() && $image) {
                $imgDir = Yii::getAlias('@storage/slider/');

                if(!is_dir($imgDir)) {
                    mkdir($imgDir, 0755, true);
                }

                $image->saveAs(Yii::getAlias('@storage/slider/' . $image->name));
            }
            return $this->redirect(['view', 'slider_id'=>$slider_id, 'id' => $model->slider_image_id]);
        } else {

            $slider = Slider::findOne($slider_id);
            return $this->render('update', [
                'model' => $model,
                'slider_id' => $slider_id,
                'slider' => $slider,
            ]);
        }
    }

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

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

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