<?php
global $hooks;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$hooks->add_action('admin_action', 'the_usrmgmt');
$hooks->add_action('silex_action', 'godmode');

function usrmgmt_title() {
    echo "User Management";
}

function usrmgmt_css() {
    ?>
    
	<link href="/assets/global/plugins/bootstrap-datepicker/css/bootstrap-datepicker3.min.css" rel="stylesheet" type="text/css" />
<?php }

function usrmgmt_js() {
    ?>
    <script src="/assets/global/scripts/datatable.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/datatables/datatables.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/datatables/plugins/bootstrap/datatables.bootstrap.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js" type="text/javascript"></script>
	<script type="text/javascript" src="/assets/modul-js/admin/usrmgmt.min.js"></script>
<?php
}
function godmode(){
    global $app;
    $app->get('/godmode-off', function(Request $request) {
        global $db;
        global $app;
        if ($_SESSION["godmode"]["status"] == "1") {
            // SET SESSION
            $_SESSION["username"] = $_SESSION["godmode"]["username"];
            $_SESSION["user_id"] = $_SESSION["godmode"]["user_id"];
            $_SESSION["role"] = $_SESSION["godmode"]["role"];
            $_SESSION["group_id"] = $_SESSION["godmode"]["group_id"];
            $_SESSION["company_id"] = 0;
            $_SESSION["branch_id"] = 0;
			
            unset($_SESSION["godmode"]);
            // RETURN
        }
        return $app->redirect('/user-management');
    });
}

function the_usrmgmt() {
    global $app;
    
    $app->post('/user-management/list', function(Request $request)  {
        $curpage = $request->get('page');
        include 'usr.list.php';
        return '';
    });
    $app->post('/user-management/block', function(Request $request) {
        global $db;
        $user_id = $request->get('id');
        $db->bind("user_id", $user_id);
        $block = $db->query("UPDATE users SET status = '0' WHERE user_id = :user_id");
        if ($block) {
            return new Response('Success', 200);
        } else {
            return new Response('FAIL', 201);
        }
    });
	$app->post('/user-management/unblock', function(Request $request)  {
        global $db;
        $user_id = $request->get('id');
        $db->bind("user_id", $user_id);
        $block = $db->query("UPDATE users SET status = '1' WHERE user_id = :user_id");
        if ($block) {
            return new Response('Success', 200);
        } else {
            return new Response('FAIL', 201);
        }
    });
    
    $app->post('/user-management/godmode', function(Request $request)  {
        global $db;
        $user_id = $request->get('id');
        $db->bind("user_id", $user_id);
        $usr = $db->query("SELECT * FROM users WHERE user_id = :user_id");
        if ($usr && $_SESSION["role"] == "0") {
            // SET SESSION
            $_SESSION["godmode"]["status"] = "1";
            $_SESSION["godmode"]["username"] = $_SESSION["username"];
            $_SESSION["godmode"]["user_id"] = $_SESSION["user_id"];
            $_SESSION["godmode"]["role"] = $_SESSION["role"];
            $_SESSION["godmode"]["group_id"] = $_SESSION["group_id"];
            $_SESSION["godmode"]["company_id"] = 0;
            $_SESSION["godmode"]["branch_id"] = 0;
            $_SESSION["username"] = $usr[0]["username"];
            $_SESSION["user_id"] = $usr[0]["user_id"];
            $_SESSION["role"] = $usr[0]["role"];
            $_SESSION["group_id"] = $usr[0]["group_id"];
			if($usr[0]["role"] > 0){
				$_SESSION["company_id"] = getCompany($usr[0]["user_id"]);
				$_SESSION["branch_id"] = getBranch($usr[0]["user_id"]);				
			}else{
				$_SESSION["company_id"] = 0;
				$_SESSION["branch_id"] = 0;
			}
            // RETURN
            return new Response('Success', 200);
        } else {
            return new Response('FAIL', 201);
        }
    });
    $app->get('/user-management', function() {
        global $hooks;
        $hooks->add_action('global_css', "usrmgmt_css");
        $hooks->add_action('global_js', "usrmgmt_js");
        $hooks->add_action('the_title', "usrmgmt_title");
        the_head();
        include 'usr.tpl.php';
        the_footer();
        return "";
    });
}
