<?php
ini_set('display_errors', 1);

require_once '../../../../resources/base_path_include.php';
require_once $base_path_to_learning_resources  . "includes/connect.php"; // use require for mandatory files
require_once $base_path_to_learning_resources . 'lib/function.php';
require_once $base_path_to_learning_resources . 'lib/validate_api_signature.php';

//valdation file
require_once $base_path_to_learning_resources . 'lib/validate_function.php';
require_once $base_path_to_learning . 'user_management/authenticate.php';
//db access function
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';
require_once $base_path_to_learning . 'functions/common_functions.php';

$postdata = json_decode(file_get_contents("php://input"), true);
// print_r($postdata);
checkIfNoPhpContentsReceive();


if ( !isset($postdata['staff_id']) or !checkIfIssetAndNotEmpty($postdata['staff_id']) or
  !isset($postdata['institution_id']) or !checkIfIssetAndNotEmpty($postdata['institution_id']) or
    !isset($postdata['api_signature']) or !checkIfIssetAndNotEmpty($postdata['api_signature']) or
    !isset($postdata['function_type']) or !checkIfIssetAndNotEmpty($postdata['function_type'])  or 
    !isset($postdata['jwt_token']) or  !checkIfIssetAndNotEmpty($postdata['jwt_token'])
) {
    errorreturn("All * fields are required.");
}
validateApiSignature($postdata['api_signature']);

$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);
//then workspace_id is iin decodede data of jwt use from there
$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$username = $jwt_validate->data->username;
$role = $jwt_validate->data->role;
$branch_id = $jwt_validate->data->branch_id;

$specific_db_name = validateUserAccess($conn_rjllearning, $userid, $role, $workspace_id, ["teacher"], null);
if (!$specific_db_name) {
    errorreturn('Access denied.');
} else {
    $conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
}

$institution_id = trim($postdata['institution_id']);    

require_once $base_path_to_learning . 'user_management/user_versioning.php';

$version = isset($_POST['version']) ? $_POST['version'] : (isset($postdata['version']) ? $postdata['version'] : null);
$updated_time = isset($_POST['updated_time']) ? $_POST['updated_time'] : (isset($postdata['updated_time']) ? $postdata['updated_time'] : null);

$response = user_data_refresh($conn_rjllearning, $conn_rjllearning_specific,$version, $updated_time, $userid, $institution_id);


function getSpecificStaff($conn_rjllearning_specific,$institution_id,$branch_id,$staff_id) {
    
    $sql = $conn_rjllearning_specific->prepare("SELECT * FROM  staff_details where id = ? and institution_id =? AND branch_id = ?");
    $sql->execute(array($staff_id,$institution_id,$branch_id));
    $rows = $sql->fetchAll();
    if (count($rows) == 1) {
      
         $branch_info =  getBranchInfo($conn_rjllearning_specific, $institution_id, $rows[0]['branch_id']);

        if($rows[0]['print_bank_details'] == 1 and $rows[0]['print_bank_details'] !=  null){
            
            $account_holder_name = $rows[0]['account_holder_name'];
            $account_name = $rows[0]['account_name'];
            $account_no = $rows[0]['account_no'];
            $ifsc_code = $rows[0]['ifsc_code'];
            $branch_address = $rows[0]['branch_address'];
            $account_holder_name = $rows[0]['account_holder_name'];
            $account_type = $rows[0]['account_type'];
            
        }
        return array('staff_id'=> $rows[0]['id'],'branch_id'=>$branch_info ,'name'=> $rows[0]['name'],
        'email'=> $rows[0]['email'],
        'phone'=> $rows[0]['phone'],
        'address'=> $rows[0]['address'],
        'group_id'=> $rows[0]['group_id'],
        'date_of_joining'=> $rows[0]['date_of_joining'],
        'print_bank_details'=> $rows[0]['print_bank_details'],
        'account_holder_name'=> $account_holder_name,
        'account_name'=> $account_name,
        'ifsc_code'=> $ifsc_code,
        'account_no'=> $account_no,
        'branch_address'=> $branch_address,
        'account_type'=> $account_type,
        );
      
    }else{
          errorreturn("Staff not found.");
    }

}

if($postdata['function_type']  == "fetch_specific_staff") {  
    
    $staff_id = trim($postdata['staff_id']);
    
    $success['data'] = getSpecificStaff($conn_rjllearning_specific,$institution_id,$branch_id,$staff_id);
    
    successreturn($success);
      
}

