<?php

function fetchSection($conn_rjllearning, $institution_id,$branch_id,$search_string,$limit,$offset) {
    
    $sql = "SELECT  count(*) as total_rows FROM    section_info where institution_id =? AND (branch_id LIKE ? OR branch_id = 'all') ";
    $stmt = $conn_rjllearning->prepare($sql);
    $stmt->execute(array($institution_id,'%' . $branch_id . '%'));
    $result_total = $stmt->fetchAll();
    $totalrows = $result_total[0]['total_rows'];
    
    if($limit == 'all'){
        $sql_section = "SELECT * FROM  section_info where institution_id =? AND (branch_id LIKE ? OR branch_id = 'all')";
        $array_params = [$institution_id, '%' . $branch_id . '%'];
        
        if ($search_string != null) {
            $sql_section .= " AND section_name LIKE ? ";
            $array_params[] = '%' . $search_string . '%'; // Append to $array_params
        }
    
    }else{
        $sql_section = "SELECT * FROM  section_info where institution_id =?  AND (branch_id LIKE ? OR branch_id = 'all')";
        $array_params = [$institution_id,'%' . $branch_id . '%'];
        
        if ($search_string != null) {
            $sql_section .= " AND section_name LIKE ? ";
            $array_params[] = '%' . $search_string . '%'; // Append to $array_params
        }
        $sql_section .="LIMIT " . $offset . " ," . $limit . "";
    }
    $stmt = $conn_rjllearning->prepare($sql_section);
    $stmt->execute($array_params);
    $users_rows = $stmt->fetchAll();
    
    $array_return = [];

   foreach ($users_rows as $rows) {
       
       $branch_id = $rows['branch_id'];
    
        $branches = []; // Initialize branches array
        
        if ($branch_id == 'all') {
        
                $branches[] = array('branch_id' => $branch_id,'name' =>ucwords($branch_id));

        } else {
            // $imploded_branchids = implode(',', $branch_id); // Convert array to comma-separated string
            // print_R($imploded_branchids);
            $sql = "SELECT * FROM branch WHERE institution_id = ? AND id IN ($branch_id) and status = ?";
            $array_params = [$institution_id,'1']; // Add institution_id to parameters
            $stmt = $conn_rjllearning->prepare($sql);
            $stmt->execute($array_params);
            $branch_rows = $stmt->fetchAll();
        
            foreach ($branch_rows as $branch) {
                $branches[] = array('branch_id' => $branch['id'], 'name' => $branch['name']);
            }
        }
        
      
        $array_return[] =  array('section_id' => $rows['id'],'branch_id' => $branches,'section_name' => $rows['section_name']);
    }
    
     if (count($users_rows) < 50) {

        return array('total_rows' => $totalrows, 'section_info' => $array_return, 'maxreach' => 1);
    } else {
        return array('total_rows' => $totalrows, 'section_info' => $array_return, 'maxreach' => 0);
    }

}


function fetchSpecificSection($conn_rjllearning, $institution_id,$branch_id,$section_id) {
    // reeturns all data from the specific row of the profile
    $sql = $conn_rjllearning->prepare("SELECT * FROM   section_info  where id = ? AND institution_id =? AND  branch_id LIKE ?");
    $sql->execute(array($section_id,$institution_id,'%' . $branch_id . '%' ));
    $rows = $sql->fetchAll();
    
    if (count($rows) == 1) {
         $branch_id = $rows[0]['branch_id'];
    
        $branches = []; // Initialize branches array
        
        if ($branch_id == 'all') {
          $branches[] = array('branch_id' => $branch_id,'name' =>ucwords($branch_id));
        } else {
            // $imploded_branchids = implode(',', $branch_id); // Convert array to comma-separated string
            // print_R($imploded_branchids);
            $sql = "SELECT * FROM branch WHERE institution_id = ? AND id IN ($branch_id) and status = ?";
            $array_params = [$institution_id,'1']; // Add institution_id to parameters
            $stmt = $conn_rjllearning->prepare($sql);
            $stmt->execute($array_params);
            $branch_rows = $stmt->fetchAll();
        
            foreach ($branch_rows as $branch) {
                $branches[] = array('branch_id' => $branch['id'], 'name' => $branch['name']);
            }
        }
         return array('section_id' => $rows[0]['id'],'branch_id' => $branches,'section_name' => $rows[0]['section_name']);

    }else{
          errorreturn("Enter Invalid Section Id.");
    }

}

