<?php
include("../../db.php");
header('Content-Type: application/json');

$status = 0;
$msg = "Unable to fetch data..! Please retry later";
$data = array();

$type = "";
$folder_path = 'https://'.$_SERVER['HTTP_HOST'].'/media/photo/';

if (isset($_GET['session']) && isset($_GET['type'])){
    $session = $_GET['session'];
    $type    = $_GET['type'];

    if ($type == 'hosteler'){

        $hosteler_list = get_hosteler($db, $session, $folder_path);
        if ($hosteler_list){
            $status = 1;
            $data = $hosteler_list;
            $msg = "Record found";
        }else{
            $msg = "Record not found";
        }

    }elseif ($type == 'day_scholar'){

        $day_scholar_list = get_day_scholar($db, $session, $folder_path);
        if ($day_scholar_list){
            $status = 1;
            $data = $day_scholar_list;
            $msg = "Record found";
        }else{
            $msg = "Record not found";
        }

    }elseif ($type == 'transport'){

        $transport_list = get_transport($db, $session, $folder_path);
        if ($transport_list){
            $status = 1;
            $data = $transport_list;
            $msg = "Record found";
        }else{
            $msg = "Record not found";
        }

    }elseif ($type == 'non_transport'){

        $non_transport_list = get_non_transport($db, $session, $folder_path);
        if ($non_transport_list){
            $status = 1;
            $data = $non_transport_list;
            $msg = "Record found";
        }else{
            $msg = "Record not found";
        }

    }

}

function get_hosteler($db, $session, $folder_path){

    $whereclass = get_where_class();
    $wheresection = get_where_section();

    $where = " s.session='".$session."' and s.status=1 and i.idno=s.idno and i.total>0 and i.session='".$session."' ";
    $table = " student as s, hostelinstallment as i";
    return $db->getRows("SELECT s.idno,s.name,CONCAT('Adm No. - ', s.admissionno) AS admissionno,CONCAT('$folder_path', s.photo) AS photo FROM ".$table." where $whereclass $wheresection s.school_id='1' and s.issue_TC=0 and ".$where." order by s.name");
}

function get_day_scholar($db, $session, $folder_path){
    
    //$whereclass = get_where_class();
    //$wheresection = get_where_section();

    return $db->getRows("SELECT idno,name,CONCAT('Adm No. - ', admissionno) AS admissionno,CONCAT('$folder_path', photo) AS photo FROM student WHERE session='$session' and status='1' and issue_tc=0 and school_id='1' and idno NOT IN (SELECT idno from hostelinstallment WHERE session='$session') ORDER by name");

}

function get_transport($db, $session, $folder_path){

    return $db->getRows("select idno,name,CONCAT('Adm No. - ', admissionno) AS admissionno,CONCAT('$folder_path', photo) AS photo from student where school_id='1' and session='".$session."' and bus != '' and issue_TC=0 and status = 1 order by name");

}

function get_non_transport($db, $session, $folder_path){

    return $db->getRows("select idno,name,CONCAT('Adm No. - ', admissionno) AS admissionno,CONCAT('$folder_path', photo) AS photo from student where school_id='1' and session='".$session."' and (bus IS NULL OR bus = '') and issue_TC=0 and status = 1 order by name");

}

function get_where_class(){
    $whereclass = "";
    if($_GET['class']!=""){
        $whereclass .= " s.class='".$_GET["class"]."' and ";
    }
    return $whereclass;
}

function get_where_section(){
    $wheresection = "";
    if($_GET['section']!=""){
        $wheresection .= " s.section='".$_GET["section"]."' and ";
    }
    return $wheresection;
}



//////////////////// Result ////////////////////



echo json_encode(array("status" => $status, "data" => $data, "msg" => $msg, 'type' => $type));

