<?php
namespace manager\controllers;
use  Yii;
use  \app\models\Tagsfilter;
use  \app\models\base\Tags;

class TagsfilterController extends BaseController
{
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    public function actionIndex()
    {
        return $this->render("/Tagsfilter/index",[]);
    }


	public function actionDel($id)
    {
		$model = $this->findModel($id);
		if($model) $model->del($id);
		Yii::$app->cache->delete('tagsmenu');
        return $this->render("/Tagsfilter/index", []);
    }

    public function actionSort()
    {
		if (isset($_POST['item']) && is_array($_POST['item'])) {
			$i = 0;
			$a = array();
			// beginTransaction(); было бы неплохо обернуть в транзакцию но хз как это сделать
			foreach ($_POST['item'] as $item) {
				$model = $this->findModel($item);
				$model->sort = $i;
				$a[$model->id] = $i;
				$model->save(true, ['sort']);
//				print_a($model->errors);
				$i++;
			}
//			Yii::$app->cache->delete("archive_rubrics-");
			Yii::$app->cache->delete('tagsmenu');
			return json_encode(['status'=>'ok','data'=>$a]);
		}
		return json_encode(['status'=>'err']);
    }

    public function actionSearch()
    {
		if (Yii::$app->request->isGet && $get = Yii::$app->request->get('q')){
			$model = new Tags();
			$get = mb_strlen( $get ) < 6?$get.'*':$get;
			$items = $model->search($get, 10);
			$res = array();
			if( $items && is_array( $items ) ){
				foreach( $items as $item ){
					$res[] = array( 'id'=>$item['id']*1, 'label'=>$item['title'] );
				}
				return json_encode(  ['status'=>'ok', 'item'=>$res] );
			}
		}
        return json_encode( [] );
    }

    public function actionCreate()
    {
		$model = new Tagsfilter();
        if (Yii::$app->request->isPost && Yii::$app->request->post()){
			if($model->load($_POST) && $model->save()){
				Yii::$app->cache->delete('tagsmenu');
			}else{
				print_a($model->errors);
				return $this->render('/Tagsfilter/Create', ['model'=>$model]);
			}
            return $this->redirect('index');
        }
        return $this->render('/Tagsfilter/Create', ['model'=>$model]);
    }

	public function actionUpdate($id)
    {
		$model = $this->findModel($id);
        if (Yii::$app->request->isPost && Yii::$app->request->post()){
		if($model->load($_POST) && $model->save()){
				Yii::$app->cache->delete('tagsmenu');
			}else{
				print_a($model->errors);
				return $this->render('/Tagsfilter/Update', ['model'=>$model]);
			}
            return $this->redirect('index');
        }
        return $this->render('/Tagsfilter/Update', ['model'=>$model]);
    }


   /**
     * Найти Tagsfilter.
     *
     * @param $id
     * @return Story|null
     * @throws NotFoundHttpException
     */
    protected function findModel($id)
    {
        if (($model = Tagsfilter::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('Ой! не найдена.');
        return false;
    }
}
