<?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';
require_once $base_path_to_learning . 'functions/common_functions.php';
//db access function
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';
require_once $base_path_to_learning . 'admin_api/modules/students/student_id_cards/id_cards_template/function_students_id_cards_generate.php';

require_once $base_path_to_learning . "vendor/autoload.php";
require_once $base_path_to_learning_resources . 'lib/bucket_functions/bucket_functions.php';
require_once $base_path_to_learning_resources . 'includes/aws_bucket_config.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;


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

if (!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);
$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$username = $jwt_validate->data->username;
$role = $jwt_validate->data->role;

$specific_db_name = validateUserAccess($conn_rjllearning, $userid, $role, $workspace_id, 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);

if ($postdata['function_type'] == "fetch_student_template_cards") {
    
    $branch_id = trim($postdata['branch_id']);
    // $id_card_template_selected = trim($postdata['id_card_template_selected']);
    
    if (isset($postdata['class_id']) and !empty($postdata['class_id'])) {
        $class_id = trim($postdata['class_id']);
    } else {
        $class_id = null;
    }
    if (isset($postdata['section_id']) and !empty($postdata['section_id'])) {
        $section_id = trim($postdata['section_id']);
    } else {
        $section_id = null;
    }
    //search student all or multiple
    if (isset($postdata['student_id']) and !empty($postdata['student_id'])) {
        $student_id = trim($postdata['student_id']);
    } else {
        $student_id = null;
    }
    
     if (isset($postdata['display_filter']) and !empty($postdata['display_filter'])) {
        $display_filter = trim($postdata['display_filter']);
    } else {
        $display_filter = null;
    }
    

    $check_academic_confirmation = CheckAcademicSession($conn_rjllearning_specific, $institution_id);
    if ($check_academic_confirmation != true) {
        errorreturn("Access denied. Academic session not active.");
    }
    $export_type = (isset($postdata['export_type']) and !empty($postdata['export_type'])) ? trim($postdata['export_type']) : null;
    
    $SqlTemplate = "SELECT * FROM   print_settings WHERE institution_id = ? AND status = ?";
    $StmtTemplate = $conn_rjllearning_specific->prepare($SqlTemplate);
    $StmtTemplate->execute(array($institution_id,1));
    $result = $StmtTemplate->fetchAll();
    // print_r($result);
    if(count($result) < 1){
       errorreturn("Template not found. contact admin");
    } 
    
    if (count($result) >= 1) {
        $primary_color =  $result[0]['primary_color'];
        $secondary_color =  $result[0]['secondary_color'];
        $id_card_template_selected =  $result[0]['id_card_template_selected'];
    }    
    
    if (isset($export_type)) {
    if ($export_type == 'print') {
    if($id_card_template_selected === '1'){
        require_once $base_path_to_learning . 'admin_api/modules/students/student_id_cards/id_cards_template/template1.php';
    }elseif($id_card_template_selected === '2'){
       require_once $base_path_to_learning . 'admin_api/modules/students/student_id_cards/id_cards_template/template2.php';
    }elseif($id_card_template_selected === '3'){
        require_once $base_path_to_learning . 'admin_api/modules/students/student_id_cards/id_cards_template/template3.php';
    }
    }
    }else{
        
        $success['data'] = geStudenIDCardTemplate($conn_rjllearning,$conn_rjllearning_specific, $institution_id,$branch_id,$class_id,$section_id,$student_id,$display_filter);

         successreturn($success);
        
    }
    
}




