<?php
namespace myresto\controllers;

use Yii;
use mdm\admin\controllers\UserController as BaseUserController;
use mdm\admin\components\UserStatus;
use common\models\User;

class UserController extends BaseUserController {
    public function actionActivate($id) {
        $user = $this->findModel($id);
        if ($user->status == UserStatus::INACTIVE) {
            $user->status = UserStatus::ACTIVE;
            if ($user->save()) {
                Yii::$app->session->setFlash('success', "The \"$user->username\" user has been successfully marked as active.");
                return $this->redirect(['/admin/user']);
            } else {
                $errors = $user->firstErrors;
                throw new UserException(reset($errors));
            }
        }
        return $this->redirect(['/admin/user']);
    }

    public function actionDeactivate($id) {
        $user = $this->findModel($id);
        if ($user->status == UserStatus::ACTIVE) {
            $user->status = UserStatus::INACTIVE;
            if ($user->save()) {
                Yii::$app->session->setFlash('success', "The \"$user->username\" user has been successfully marked as inactive.");
                return $this->redirect(['/admin/user']);
            } else {
                $errors = $user->firstErrors;
                throw new UserException(reset($errors));
            }
        }
        return $this->redirect(['/admin/user']);
    }

    public function actionCreate() {
        $post = Yii::$app->request->post();
        $user = new User();
        $user->username = $post['username'];
        $user->email = $post['email'];
        $user->setPassword($post['password']);
        $user->generateAuthKey();
        if ($user->save()) {
            $manager = Yii::$app->authManager;
            $manager->revokeAll($user->id);
            $role = $manager->getRole($post['role']);
            $manager->assign($role, $user->id);
            Yii::$app->session->setFlash('success', "User \"$user->username\" with email \"$user->email\" successfully created");
            return $this->redirect(['/admin/user']);
        }
        return 'fail';
    }

    public function actionEdit() {
        $post = Yii::$app->request->post();
        $user = User::findIdentity($post['id']);
        $user->username = $post['username'];
        $user->email = $post['email'];
        if (!empty($post['password']))
            $user->setPassword($post['password']);
        if ($user->save()) {
            $manager = Yii::$app->authManager;
            $manager->revokeAll($user->id);
            $role = $manager->getRole($post['role']);
            $manager->assign($role, $user->id);
            Yii::$app->session->setFlash('success', "User \"$user->username\" with email \"$user->email\" successfully updated");
            return $this->redirect(['/admin/user']);
        }
        return 'fail';
        // if (!empty($post['password']))
        //     return 'not empty';
        // else
        //     return 'empty';
    }

    public function actionGetData() {
        $post = Yii::$app->request->post();
        $user = User::findIdentity($post['id']);
        $data = [
            'username' => $user->username,
            'email' => $user->email,
            'password' => '',
            'role' => $user->getUserRole(),
        ];
        return json_encode($data);
    }
}