<?php
 namespace Module\AdminLoginDefault;
 use TCMS\AuthExtension;
 use TCMS\Controller;
 use TCMS\TException;
 use Request;
 class LoginController extends Controller {
 public function index() {
 if(ENVIRONMENT == 'dev' || ENVIRONMENT == 'test') {
 return view([ 'username' => 'vanthoa', 'password' => 'vanthoa' ]);
 }
 return view();
 }
 public function login() {
 if(empty(Request::get('username')) || empty(Request::get('password'))) {
 view()->addErrorMessage('message', 'Vui lòng nhập đây đủ thông tin');
 if(empty(Request::get('username'))) view()->addErrorMessage('username', 'error');
 if(empty(Request::get('password'))) view()->addErrorMessage('password', 'error');
 view()->addStyle(asset('/style/bootstrap.min.css'));
 view()->addStyle(asset('/style/admin-login-default/login.css'));
 return view('index');
 }
 $username = trim(Request::get('username'));
 $password = trim(Request::get('password'));
 $rememberMe = Request::get('rememberMe', 0);
 $authExt = load_auth();
 if(!$authExt) {
 throw new TException('Không tìm thấy auth extension cho area ' . area_name());
 }
 if($authExt->login($username, $password, ['isRememberMe' => $rememberMe == 1])) {
 if(Request::has('redirect_url')) return redirect(Request::get('redirect_url'));
 return redirect(url());
 }
 view()->username = $username;
 view()->remember = $rememberMe;
 view()->addErrorMessage('message', 'Tài khoản hoặc mật khẩu không đúng');
 view()->addStyle(asset('/style/bootstrap.min.css'));
 view()->addStyle(asset('/style/admin-login-default/login.css'));
 return view('index');
 }
 }
