<?php
    
    namespace backend\controllers;
    
    use artbox\core\admin\actions\Delete;
    use artbox\core\admin\actions\Index;
    use artbox\core\admin\actions\View;
    use artbox\core\admin\interfaces\ControllerInterface;
    use artbox\core\admin\widgets\Form;
    use common\models\Feedback;
    use yii\filters\AccessControl;
    use yii\filters\VerbFilter;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    
    /**
     * FeedbackController implements the CRUD actions for Feedback model.
     */
    class FeedbackController extends Controller implements ControllerInterface
    {
        public function behaviors()
        {
            return [
                'verbs'  => [
                    'class'   => VerbFilter::className(),
                    'actions' => [
                        'delete' => [ 'POST' ],
                    ],
                ],
                'access' => [
                    'class' => AccessControl::className(),
                    'rules' => [
                        [
                            'allow' => true,
                            'roles' => [ '@' ],
                        ],
                    ],
                ],
            ];
        }
        public function actions()
        {
            return [
                'index'  => [
                    'class'            => Index::className(),
                    'columns'          => [
                        'name'      => [
                            'type' => Index::ACTION_COL,
                        ],
                        'email' => [
                            'type' => Index::STRING_COL
                        ],
                        'status'    => [
                            'type' => Index::STATUS_COL,
                        ],
                        'created_at' => [
                            'type' => Index::DATETIME_COL
                        ]
                    ],
                    'model'            => Feedback::className(),
                    'hasLanguage'      => false,
                    'enableMassDelete' => true,
                    'modelPrimaryKey'  => 'id',
                    'defaultSort'      =>  [
                        'created_at' => 'DESC'
                    ]
                ],
                'view'   => [
                    'class'          => View::className(),
                    'model'          => Feedback::className(),
                    'hasAlias'       => false,
                    'hasGallery'     => false,
                    'languageFields' => [
                    ],
                    'fields'         => [
                        [
                            'name' => 'name',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'email',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'phone',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'message',
                            'type' => Form::TEXTAREA,
                        ],
                        [
                            'name' => 'ip',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'url',
                            'type' => Form::STRING,
                        ],
                        [
                            'name' => 'created_at',
                            'type' => Form::STRING,
                        ],
                        
                    ],
                ],
                'delete' => [
                    'class' => Delete::className(),
                ],
            ];
        }
        public function actionUpdate($id)
        {
            $model = $this->findModel($id);
        
            $model->status = true;
            $model->save(false, [ 'status' ]);
        
            if ($model->load(\Yii::$app->request->post()) && $model->save()) {
                return $this->redirect('index');
            } else {
                return $this->render(
                    'update',
                    [
                        'model' => $model,
                    ]
                );
            }
        }
        public function findModel($id)
        {
            $model = Feedback::find()
                             ->where([ 'id' => $id ])
                             ->one();
            if ($model !== null) {
                return $model;
            } else {
                throw new NotFoundHttpException('The requested page does not exist.');
            }
        }
    
        public function newModel()
        {
            return new Feedback();
        }
    
        public function deleteModel($id)
        {
            $category = Feedback::find()
                                ->where(
                                    [
                                        'id' => $id,
                                    ]
                                )
                                ->one();
        
            return $category->delete();
        }
    
        protected static function fieldsConfig()
        {
            return [
                'model'          => Feedback::className(),
                'hasAlias'       => false,
                'hasGallery'     => false,
                'languageFields' => [
                ],
                [
                    'name' => 'name',
                    'type' => Form::STRING,
                ],
                [
                    'name' => 'email',
                    'type' => Form::STRING,
                ],
                [
                    'name' => 'phone',
                    'type' => Form::STRING,
                ],
                [
                    'name' => 'message',
                    'type' => Form::TEXTAREA,
                ],
                [
                    'name' => 'ip',
                    'type' => Form::STRING,
                ],
                [
                    'name' => 'url',
                    'type' => Form::STRING,
                ],
                [
                    'name' => 'created_at',
                    'type' => Form::STRING,
                ],
                [
                    'name' => 'updated_at',
                    'type' => Form::STRING,
                ],
            ];
        }
    
    
    }
