<?php

namespace modules\custom\controllers;

use Yii;
use modules\custom\models\Accounts;
use modules\custom\models\AccountsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use modules\custom\models\ContactsSearch;


/**
 * AccountsController implements the CRUD actions for Accounts model.
 */
class AccountsController extends Controller
{
     
      						
	public function beforeAction($action) {
   		\frontend\models\Audit::qcreate(['id' => yii::$app->request->get('id', null), 'controller' =>  Yii::$app->controller->id,  'action' => Yii::$app->controller->action->id,  'attributes' => null]);
        return parent::beforeAction($action);
	} 						
    /**
     * Lists all Accounts models.
     * @return mixed
     */
    public function actionIndex() {
 		
        $searchModel = new AccountsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

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

    /**
     * Creates a new Accounts model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreateajax() { 
        $modal = new accounts;
        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/accounts/createajax', [
                'modal' => $modal,
            ]);
    }
    
    public function actionCreate() {
    	$model = new Accounts();
	    if ($model->load(Yii::$app->request->post()) && $model->save()) {
	    	$model = Accounts::find()->where(['=','created_at', $model->created_at])->andWhere(['=','created_by', $model->created_by])->One();
	      return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('@modules/custom/views/accounts/create', [
                'model' => $model,
	        ]);
        }
    }

    /**
     * Updates an existing Accounts 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->errors) {
        	return print_r($model->errors);
       	} else {
            return $this->render('@modules/custom/views/accounts/update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Accounts 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 AccountsSearch();
        $dataProvider = $searchModel->deleted(Yii::$app->request->queryParams);

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


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