<?php

namespace modules\custom\controllers;

use Yii;
use modules\custom\models\Contacts;
use modules\custom\models\ContactsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\helpers\Json;
use frontend\models\Users;
/**
 * ContactsController implements the CRUD actions for Contacts model.
 */
class ContactsController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
            'access' => [
    			'class' => AccessControl::class,
    			'rules' => [
       						 [
         					   'actions' => ['login', 'error'],
            					'allow' => true,
        						],
       						[
            'actions' => ['logout', 'index', 'create','pdf','view', 'update', 'getdetails', 'getuserdetails', 'createajax', 'delete' ], // add all actions to take guest to login page
            'allow' => true,
            'roles' => ['@'],
       						],
   							],
						],
        ];
    }

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

        return $this->render('@modules/custom/views/contacts/index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Contacts model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('@modules/custom/views/contacts/view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Contacts model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Contacts();
        if ($model->load(Yii::$app->request->post())
         ) {
         $model->save();
         $model = \modules\custom\models\Contacts::find()->Where(['created_at' => $model->created_at, 'created_by' => $model->created_by])->one();
		    return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('@modules/custom/views/contacts/create', [
                'model' => $model,
            ]);
        }
    }
          public function actionCreateajax()
{
	        $modal = new Contacts();
        if ($modal->load(Yii::$app->request->post()) && $modal->save()) {
        Yii::$app->response->format = 'json';
            return [
                'message' => 'Success!!! Press Back',
            ];
            }
        return $this->renderAjax('@modules/custom/views/contacts/create', [
                'model' => $modal,
            ]);
    }

    /**
     * Updates an existing Contacts model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } elseif ($model->load(Yii::$app->request->post()) && !$model->save()) {
			return print_r($model->errors);
		
        } else {
            return $this->render('@modules/custom/views/contacts/update', [
                'model' => $model,
            ]);
        }
    }
    
    public function actionGetdetails($id)
    {
	$details = contacts::find()->where(['=','_id', $id])->one();
    return Json::encode($details);
	}
    public function actionGetuserdetails($id)
    {
	$details = users::findOne($id);
    echo Json::encode($details);
	}
    /**
     * Deletes an existing Contacts 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->deleted_at = strtotime('now');
        $model->deleted = TRUE;
        $model->save();
        return $this->redirect(['index']);
    }
    public function actionUndelete($id)
    {
        $model = $this->findModel($id);
        $model->deleted_at = null;
        $model->deleted = null;
        $model->save();
        return $this->redirect(['index']);
    }

	
    public function actionDeleted() {
        $searchModel = new ContactsSearch();
        $dataProvider = $searchModel->deleted(Yii::$app->request->queryParams);

        return $this->render('@modules/custom/views/contacts/index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Finds the Contacts model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Contacts the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Contacts::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
