<?php

namespace app\base\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Url;

class AdminLangController extends Controller
{
    public $modelClass;
    public $modelClassSearch;
    public $modelName;
    public $modelNameLang;
            
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
    
    public function actionIndex()
    {
        $searchModel = new $this->modelClassSearch;
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    public function actionCreate()
    {
        $model = new $this->modelClass;

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

            foreach (Yii::$app->request->post($this->modelNameLang, []) as $language => $data) {
                foreach ($data as $attribute => $translation) {
                    $model->translate($language)->$attribute = $translation;
                }
            }
            // Save translations
            $model->save();
            Yii::$app->session->setFlash('success', Yii::t('app', 'Создание прошло успешно'));
            return $this->redirect(['update', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    public function actionUpdate($id)
    {
        $model = $this->modelClass::find()->with('translations')->where(['id' => $id])->one();
        if ($model === null) {
            throw new NotFoundHttpException(Yii::t('app', 'Страница не найдена'));
        }
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            foreach (Yii::$app->request->post($this->modelNameLang, []) as $language => $data) {
                foreach ($data as $attribute => $translation) {
                    $model->translate($language)->$attribute = $translation;
                }
            }
            // Save translations
            $model->save();
            Yii::$app->session->setFlash('success', Yii::t('app', 'Обновление произведено успешно'));
            if (Yii::$app->request->post('save') !== null) {
                return $this->redirect(['index']);
            }
            return $this->redirect(['update', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        if ($model->delete()) {
            Yii::$app->session->setFlash('success', Yii::t('app', 'Удаление произведено успешно'));
        }
        return $this->redirect(['index']);
    }

    protected function findModel($id)
    {
        if (($model = $this->modelClass::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException(Yii::t('app', 'Страница не найдена'));
        }
    }
}
