<?php
global $hooks;

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

$hooks->add_action('silex_action', 'admin_registration');

// Define Page Title & Heading Text
function dreg_title() {
    echo "Downline Registration";
}
 
// Define the CSS 
function dreg_css() {
    ?>
	<link href="/assets/global/plugins/bootstrap-daterangepicker/daterangepicker.min.css" rel="stylesheet" type="text/css" />
	<link href="/assets/global/plugins/bootstrap-datepicker/css/bootstrap-datepicker3.min.css" rel="stylesheet" type="text/css" />
    <link href="/assets/global/plugins/select2/css/select2.min.css" rel="stylesheet" type="text/css" />
	<link href="/assets/global/plugins/select2/css/select2-bootstrap.min.css" rel="stylesheet" type="text/css" />
    <?php
}

// Define The JS
function dreg_js() {
    ?>
	<script src="/assets/global/plugins/select2/js/select2.full.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/jquery-validation/js/jquery.validate.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/jquery-validation/js/additional-methods.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/bootstrap-wizard/jquery.bootstrap.wizard.min.js" type="text/javascript"></script>
	<script src="/assets/modul-js/registration/dreg.min.js" type="text/javascript"></script>
    <?php
}

// Declare & SET the URL handler
function admin_registration() {
    global $app;
    // Check Username Handler
    $app->post('/register-account/check', function(Request $request) {
        global $db;
        $username = $request->get('username');
        $db->bind('username', $username);
        $row = $db->query("SELECT user_id FROM users WHERE username = :username");
        if (count($row) > 0) {
            $status = 'false';
        } else {
            $status = 'true';
        }
        echo $status;
        return '';
    });
    // Handler New Registration 
    $app->post('/register-account/submit', function(Request $request) {
        global $db;
        global $global_env;
        // Catch the GET
		$created_on = date("Y-m-d H:i:s");
        $username = $request->get('username');
        $email = $request->get('email');
        $password = $request->get('password');
        $first_name = $request->get('first_name');
        $last_name = $request->get('last_name');     
		
        $group_id = $request->get('group_id');
		
        // END
        // Check if the request match the conditional
        if (!userExist($username) && groupExist($group_id) && is_numeric($group_id)) {
            // PREPARE
           
			
			
			$db->bind("username", $username);
            $db->bind("password", $password);
            $db->bind("group_id", $group_id);
            $db->bind("created_on", $created_on);
			
			
            // Record to users to get the user id
            $userReg = $db->query("INSERT INTO users(username,password,created_on,role,group_id) VALUES(:username,:password,:created_on,'1',:group_id)");
            $user_id = $db->lastInsertId();
            if ($userReg && $user_id) {
				$db->bind("user_id", $user_id);
                // Record User Detail
                // PREPARE
                $db->bind("user_id", $user_id);
                $db->bind("first_name", $first_name);
                $db->bind("last_name", $last_name);
                $db->bind("email", $email);
				
                // EXECUTE
                $userDetRec = $db->query("INSERT INTO user_detail(user_id,first_name,last_name,email) VALUES(:user_id,:first_name,:last_name,:email)");
                                
                if ($userDetRec) {
					action_log($_SESSION['user_id'], "User Registration " . $first_name . " " . $last_name ." (" .$username . ")", "SUCCESS");
                    return new Response("SUCCESS", 200);
                } else {
					action_log($_SESSION['user_id'], "User Registration " . $first_name . " " . $last_name ." (" .$username . ")", "FAILED");
                    return new Response('FAILED', 200);
                }
            }
        }
    });
	// End Handler
    $app->get('/register-account', function() {
        global $hooks;
        $hooks->add_action('global_css', "dreg_css");
        $hooks->add_action('global_js', "dreg_js");
        $hooks->add_action('the_title', "dreg_title");
        the_head();
        include 'dreg.tpl.php';
        the_footer();
        return "";
    });
}

include 'dreg.func.php';
