<?php

// Set up error reporting
ini_set('display_errors', 1);

// Include necessary files
require_once '../../../../resources/base_path_include.php';
require_once $base_path_to_learning_resources . "includes/connect.php";
require_once $base_path_to_learning_resources . 'lib/function.php';
require_once $base_path_to_learning_resources . 'lib/validate_api_signature.php';
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_resources . 'includes/specific_db_config.php';
require_once $base_path_to_learning_resources . 'template/marksheet/header/header1.php';
require_once $base_path_to_learning_resources . 'template/marksheet/header/header2.php';
require_once $base_path_to_learning_resources . 'template/marksheet/header/header3.php';

// Check if JSON is valid
$postdata = json_decode(file_get_contents("php://input"), true);
checkIfNoPhpContentsReceive();

$required_fields = ['api_signature', 'function_type', 'jwt_token'];
foreach ($required_fields as $field) {
    if (!isset($postdata[$field]) || !checkIfIssetAndNotEmpty($postdata[$field])) {
        errorreturn("Missing required field: $field");
    }
}
validateApiSignature($postdata['api_signature']);
// Authenticate user and get user details
$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;
$institution_id = $jwt_validate->data->institution_id;
$branch_id = $jwt_validate->data->branch_id;

// Validate user access to specific database
$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);
}
// Include necessary function files
require_once $base_path_to_learning . 'functions/common_functions.php';
require_once $base_path_to_learning . 'functions/get_branch_functions.php';
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 MarksheetTemplateGenerate($conn_rjllearning_specific, $institution_id, $branch_id)
{
    $additional_data = ['branch_code','pincode','address','contact_number','email'];
  
    $sql = $conn_rjllearning_specific->prepare("SELECT * FROM  marksheet_template where institution_id =? AND branch_id = ?");
    $sql->execute(array($institution_id, $branch_id));
    $result = $sql->fetchAll();
    // print_r($result);die();
    if (count($result) >= 1) {
        // $data  = array();
        foreach ($result as $rows) {
            $header =  $rows['header'];
            $body =  $rows['body'];
            $footer =  $rows['footer'];
               if($header == 'header1'){
                    $header = getHeader1($GLOBALS['conn_rjllearning'],$conn_rjllearning_specific,$institution_id,$branch_id,$additional_data);
                    
                  }else if($header == 'header2'){
                    $header = getHeader2($GLOBALS['conn_rjllearning'],$conn_rjllearning_specific,$institution_id,$branch_id,$additional_data);
                  }else if($header == 'header3'){
                     $header = getHeader3($GLOBALS['conn_rjllearning'],$conn_rjllearning_specific,$institution_id,$branch_id,$additional_data);
                }
            }

            // echo $header;
        $data = $header . $body . $footer;
        echo $data;
    }
}

if ($postdata['function_type'] == 'marksheet_template_generate') {

    // $institution_id = trim($postdata['institution_id']);
    // $branch_id = trim($postdata['branch_id']);
    // $marksheet_id = trim($postdata['marksheet_id']);
    $type = trim($postdata['type']);
    $academic_id = CheckAcademicSession($conn_rjllearning_specific, $institution_id);
    if ($academic_id == false) {
        errorreturn("Access denied. Academic session not active.");
    }

    $success['data'] = MarksheetTemplateGenerate($conn_rjllearning_specific, $institution_id, $branch_id,$type);
    // successreturn($success);
}
