<?php
//base path learning to resources

require_once '../base_path_include.php';
require_once $base_path_to_learning_resources . "/includes/connect.php";;
require_once $base_path_to_learning_resources . 'lib/validate_api_signature.php';

$data = json_decode(file_get_contents('php://input'), true);

validateCurlSignature($data['api_signature']);

$rows = $data['user'];
$userid = $rows['userid'];
$name = $rows['name'];
$dob = $rows['dob'];
$gender = $rows['gender'];
$email = $rows['email'];
$username = $rows['username']; // the username of super admin is his / her email id only.
$mobile = $rows['mobile'];
$workspace_id = $rows['workspace_id'];
$password = $rows['password'];
$designation = $rows['designation'];
// $role = $rows['role'];
$function_type = $rows['function_type'];

if($rows['role'] == null){
   $role = 'subadmin'; 
}else{
   $role = $rows['role'];
}

if ($function_type == 'add_user') {

    $excute_sql = $conn_rjllearning->prepare("SELECT can_activate FROM users WHERE  workspace_id = ? and rejoice_solution_id = ? ");
    $excute_sql->execute(array($workspace_id, $userid));
    $result_users = $excute_sql->fetchAll();
    if (count($result_users) == 1) {
        if ($result_users[0]['can_activate'] == 0) {
            $sql = "UPDATE users SET product_access_grant_time = ?, status = ?, can_activate = ? WHERE rejoice_solution_id = ?";
            $conn_rjllearning->prepare($sql)->execute(array(time(), 1, 1, $userid));
        }
    } else {
        $sql_users = "INSERT INTO users(name,dob,gender,username,email,mobile,workspace_id,role,password,date_time_created,designation,product_access_grant_time,rejoice_solution_id,status,can_activate)
                                       VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        $stmt_users =  $conn_rjllearning->prepare($sql_users);
        $stmt_users->execute(array($name, $dob, $gender, $username, $email, $mobile, $workspace_id, $role, $password, time(), $designation, time(),$userid, '1', '1'));
    }
}

return true;
