<?php
ini_set('display_errors', 1);
// this file is used to create all other users except superadmin
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';
//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';

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

validateCurlSignature($postdata['api_signature']);

$data = $postdata['institution_settings'];
// echo json_encode($data);die();
$workspace_id = $data['workspace_id'];
$branch_id = $data['branch_id'];
$role = $data['role'];
$userid = $data['userid'];

// echo json_encode($branch_id);die();

$sql = "Select db_name from workspace_database_info where workspace_id = ? and  status = ?";
$fetch_workspace = $conn_rjllearning->prepare($sql);
$fetch_workspace->execute(array($workspace_id, 1));
$result_workspace = $fetch_workspace->fetchAll();
// echo json_encode($result_workspace);die();
if (count($result_workspace) < 1) {
    errorreturn('Fatal error (1). Access denied.');
}
// user level restriction
$specific_db_name = $result_workspace[0]['db_name'];

$conn_rjllearning_specific = dynamicDbConnection($specific_db_name);

$sqlbranch = $conn_rjllearning_specific->prepare("Select * from  branch WHERE id = ? and status =? ");
$sqlbranch->execute(array($branch_id, '1'));
$result_branch = $sqlbranch->fetchAll();
//  echo json_encode($result_branch);die();
if (count($result_branch) < 1) {
    echo json_encode([
        'error' => true,
        'message' => 'Branch Not Found.'
    ]);
    die();
}

$institution_id =  $result_branch[0]['institution_id'];

$sql = "SELECT id,fee_concession_application,send_sms_on_fee_receipt FROM institution_global_settings WHERE institution_id = ? AND branch_id = ?";
$array_params = [$institution_id, $branch_id];
$stmt = $conn_rjllearning_specific->prepare($sql);
$stmt->execute($array_params);
$Result = $stmt->fetch(PDO::FETCH_ASSOC); // Fetch a single row as an associative array

// Check if a result is found
if ($Result) {
    $data = [
        'setting_id' => $Result['id'],
        'fee_concession_application' => $Result['fee_concession_application'],
        'send_sms_on_fee_receipt' => $Result['send_sms_on_fee_receipt'],
    ];

    echo json_encode($data);
} else {
    // Handle case where no results are found
    echo json_encode(["error" => "No data found for the provided institution and branch."]);
}
die();

