<?php

namespace app\controllers;

use app\models\Perfil;
use Yii;
use app\models\Usuario;
use app\models\UsuarioSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * UsuarioController implements the CRUD actions for Usuario model.
 */
class UsuarioController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

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

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

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

    /**
     * Creates a new Usuario model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Usuario();
        $list_perfil = Perfil::find()->all();
        if ($model->load(Yii::$app->request->post())) {
            $password_form = $_POST['Usuario']['usr_pwd'];
            $model->usr_pwd =md5($password_form);
            $perfil = Perfil::find()->where(['per_pk' => $model->per_pk])->one();
            $model->rol_id = $perfil->item_name;
            if ($model->save()) {
                return $this->redirect(['index']);
            }            
        }

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

    /**
     * Updates an existing Usuario model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $pwd_old = $model->usr_pwd;
        $list_perfil = Perfil::find()->all();
        if ($model->load(Yii::$app->request->post())) {
            $password_form = $_POST['Usuario']['usr_pwd'];

            if ($pwd_old == $password_form) {
                $passequal = 1;
            } else {
                $passequal = 0;
            }
            if ($passequal == 0) {
                $model->usr_pwd = md5($password_form);
            }
            $perfil = Perfil::find()->where(['per_pk' => $model->per_pk])->one();
            $model->rol_id = $perfil->item_name;
            $model->save();
            return $this->redirect(['index']);
        }

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

    /**
     * Deletes an existing Usuario model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete()
    {
        $id = $_POST['id'];
        $this->findModel($id)->delete();

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

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

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