<?php

namespace backend\controllers;

use Yii;
use backend\models\Language;
use backend\models\LanguageSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use frontend\models\Option;
use frontend\models\OptionSearch;
use frontend\models\OptionLang;

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

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

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

    /**
     * Creates a new Language model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        if(!empty(Yii::$app->request->get('id'))) {
            $model = $this->findModel(Yii::$app->request->get('id'));
            $model->status = 1;
            $model->save();
            return $this->redirect(['index']);
        } else {
            $searchModel = new LanguageSearch();
            $dataProvider = $searchModel->searchNew(Yii::$app->request->queryParams);
            
            return $this->render('create', [
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
            ]);
        }
    }

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

        return $this->redirect(['index']);
    }
    
    public function actionDefault($id)
    {
        $model = $this->findModel($id);
        $models = Language::find()->where(['is_default' => 1])->all();
        foreach($models as $onemodel) {
            $onemodel->is_default = 0;
            $onemodel->save();
        }
        $model->is_default = 1;
        $model->save();
        return $this->redirect(['index']);
    }
    /**
     * Finds the Language model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Language the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Language::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function actionCreateAdress()
    {
        $form[0] = Option::create(\Yii::$app->request->post(), 'Main', 1, [['name' => 'adres', 'template' => 'text', 'translate' => true], ['name' => 'x', 'template' => 'number', 'translate' => false], ['name' => 'y', 'template' => 'number', 'translate' => false], ['name' => 'phone', 'template' => 'text', 'translate' => false], ['name' => 'name', 'template' => 'text', 'translate' => true]], true);
        if($form[0]['success'] == false) {
            return $this->render('create_adress', ['forms' => $form]);
        } else {
            return $this->redirect(['view-adress']);
        }
    }
    
    public function actionUpdateAdress($id)
    {
        $form[0] = Option::change($id, \Yii::$app->request->post(), 'Main', 1);
        if($form[0]['success'] == false) {
            return $this->render('update_adress', ['forms' => $form]);
        } else {
            return $this->redirect(['view-adress']);
        }
    }
    
    public function actionViewAdress()
    {
        $searchModel = new OptionSearch();
        
        $dataProvider = $searchModel->search(array_merge(Yii::$app->request->queryParams, ['OptionSearch' => ['model' => 'Main', 'name' => 'adres']]));
        
        return $this->render('view_adress', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    
    /**
     * Deletes an existing Option model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDeleteAdress($id)
    {
        $model = $this->findModelAdress($id);
        $children = $model->hasMany(Option::className(), ['option_pid' => 'option_id'])->all();
        $langs = array();
        if(!empty($children)) {
            foreach($children as $child) {
                $langs = OptionLang::findAll(['option_language_id' => $child->option_id]);
                foreach($langs as $lang) {
                    $lang->delete();
                }
                $child->delete();
            }
        }
        $langs = OptionLang::findAll(['option_language_id' => $id]);
        foreach($langs as $lang) {
            $lang->delete();
        }
        $model->delete();
    
        return $this->redirect(['view-adress']);
    }
    
    /**
     * Finds the Option model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Option the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModelAdress($id)
    {
        if (($model = Option::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
