<?php

namespace manager\controllers;

use manager\models\Comments;
use app\models\CommentsConf;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * CommentsController implements the CRUD actions for Comments model.
 */
class CommentsController extends BaseController
{
    /**
     * @inheritDoc
     */
    public function behaviors()
    {
        return array_merge(
            parent::behaviors(),
            [
                'verbs' => [
                    'class' => VerbFilter::className(),
                    'actions' => [
                        'delete' => ['POST'],
                    ],
                ],
            ]
        );
    }

    /**
     * Lists all Comments models.
     *
     * @return string
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Comments::find(),

            'pagination' => [
                'pageSize' => 50
            ],
            'sort' => [
                'defaultOrder' => [
                    'visible' => SORT_DESC,
					'created_at' => SORT_DESC,
                ]
            ],
        ]);

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

    /**
     * Displays a single Comments model.
     * @param int $id
     * @return string
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Comments model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return string|\yii\web\Response
     */
    public function actionCreate()
    {
        $model = new Comments();

        if ($this->request->isPost) {
            if ($model->load($this->request->post()) && $model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        } else {
            $model->loadDefaultValues();
        }

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

    /**
     * Updates an existing Comments model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param int $id
     * @return string|\yii\web\Response
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

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

    /**
     * Deletes an existing Comments model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param int $id
     * @return \yii\web\Response
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

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

    /**
     * @param $id
     * @return string
     * @throws NotFoundHttpException
     */
    public function actionToggle($id,$value)
    {
        $model = $this->findModel($id);
        $model->updateAttributes(['visible'=>$value]);
        return $this->actionIndex();
    }

    public function actionAjaxtoggle()
    {
		$ret = ['stat'=>'err'];
		if( $this->request->isPost && $this->request->post('id') && $this->request->post('value') ){
			$id = $this->request->post('id');
			$value = $this->request->post('value');
			$model = $this->findModel($id);
			if( $model->updateAttributes(['visible'=>$value]) ){
				$ret = ['stat'=>'ok', 'id'=>$id, 'value'=>$value];
			}
		}
        return \yii\helpers\Json::encode($ret);;
    }

    /**
     * Finds the Comments model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param int $id
     * @return Comments the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Comments::findOne(['id' => $id])) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

	public function actionConf()
    {
		$model = $this->confModel();
		if ($this->request->isPost && $post = $this->request->post()){
			$post['Conf'] = $this->formNormalizer($post['Conf']);
			if( $model->load($post,'Conf') && $model->save()) {
				\Yii::$app->cache->delete(CommentsConf::$keysCache);
				return $this->redirect('index');
			}
		}
        return $this->render('conf', [
            'model' => $model,
        ]);
    }
    public function formNormalizer($post)
    {
		$post['AllStop'] = $this->CheckYN(@$post['AllStop']);
		$post['reg'] = $this->CheckYN(@$post['reg']);
		$post['d0'] = $this->CheckYN(@$post['d0']);
		$post['d1'] = $this->CheckYN(@$post['d1']);
		$post['d2'] = $this->CheckYN(@$post['d2']);
		$post['d3'] = $this->CheckYN(@$post['d3']);
		$post['d4'] = $this->CheckYN(@$post['d4']);
		$post['d5'] = $this->CheckYN(@$post['d5']);
		$post['d6'] = $this->CheckYN(@$post['d6']);
		return $post;
	}
	    public function CheckYN($attr)
    {
		return (isset($attr) && $attr == 'Y')?'Y':'N';
	}

    protected function confModel()
    {
        if (($model = CommentsConf::findOne(0)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

}
