<?php
$configdir = "../../../../../../".$_POST['SubDir'];
require_once("../../../../general/config.php");

function Error($title,$text = ""){
    echo '<div class="defaultTabText">';
    echo '<div style="color:red"';echo '<i class="fa fa-exclamation-triangle fa-3x"></i>';echo '</div>';
    echo '<div>';echo $title;echo '</div>';  
    echo '<div>';echo $text;echo '</div>';  
    echo '</div>';

  }

  function ErrorBox($title,$text = ""){
    GroupBox("title=Result Ledger,id=studRstLedgerGB,size=auto,logo=id-badge");
    echo '<div class="defaultTabText">';
    echo '<div style="color:red"';echo '<i class="fa fa-exclamation-triangle fa-3x"></i>';echo '</div>';
    echo '<div>';echo $title;echo '</div>';  
    echo '<div>';echo $text;echo '</div>';  
    echo '</div>';
    _GroupBox();
    exit;
  }
  //check priv
  AllowUser("studledger");
LoadFile("basic","../../../../");

extract($_POST);
if(!isset($RegNo)){
    ErrorBox("Invalid Student","No Student Selected. Search and Select a Student to Generate Result Ledger");
    exit;
}

//get the student details
$studDet = GetBasicInfo($RegNo);
if(!is_array($studDet)){
    ErrorBox("Invalid Student", "Reading Student Record Failed");
}
GroupBox("title=RESULT LEDGER: {$RegNo} | {$studDet['Name']} | {$studDet['Gen']} ,id=studRstLedgerGB,size=auto,logo=id-badge");
	 Box("style=width:100%;font-size:0.95em;margin-top:5px;overflow:auto,id=studRstLedgerBx");
    //get all student year of study
    $YOS = (int)$studDet['YearOfStudy'];
    $StartSes = $studDet['StartSes'];
    Box();
    Table("style=width:calc(100% - 0px);font-size:0.8em;margin:10px auto;text-align:left,id=totalrpt,multiselect=false,data-type=table");
    THeader(["REGNO", "NAME", "GENDER", "PROGRAMME/SCHOOL"],"style=text-align:left");
    TRecord([$RegNo, $studDet['Name'],$studDet['Gen'], $studDet['Prog'] ],"data-id=studrstledgerhead,style=font-weight:bolder;text-transform:uppercase");
    _Table();
    _Box();
    echo "<div style=\"width:100%; height:1px;margin:10px 0px\" class=\"altBgColor2\" > </div>";
    
    //get all student result'
    $AllRst = $dbo->Select("result_tb r, session_tb s","r.*,s.SesName","r.RegNo='".$dbo->SqlSafe($RegNo)."' AND r.SesID = s.SesID ORDER BY r.SesID, r.Lvl, r.Sem, r.GroupID DESC");
    if(!is_array($AllRst)){
        Error('Operation Failed',"Fetching Student Result Failed");
    }else{
        if($AllRst[1] < 1){
            Error('No Result Found',"Student have No Result Yet");
        }else{
            $rstArray = [];
            while($indrst = $AllRst[0]->fetch_assoc()){
                $lookupkey = $indrst['SesID'].$indrst['Sem'];
                if(isset($rstArray[$lookupkey]))continue;
                $LvlName = LevelName($indrst['Lvl'], $studDet['StudyID']);
                $SemName = SemesterName($indrst['Sem']);
                Table("style=width:calc(100% - 0px);margin:20px auto;text-align:left,id=totalrpt,multiselect=false,data-type=table");
                THeader(["{$LvlName} ({$SemName} SEMESTER {$indrst['SesName']})"],"style=font-weight:bolder;text-align:center; font-size:1.2em; text-transform:uppercase,cspan=d1:4");
                THeader(["COURSE CODE", "CH", "SCORE/GRADE", "GP"],"style=text-align:left");
                
                $studrst =  $indrst['Rst'];
                $RstInfo =  json_decode($indrst['RstInfo'],true);
                $ResultScores= [];
               if(trim($studrst) != ""){
                   $studrstarr = explode("&",$studrst);
                   if(count($studrstarr) > 0){
                       //initialize display rst variable 
                       
                       foreach($studrstarr as $studindrst){
                           $studindrstArr = explode("=",$studindrst);
                           if(count($studindrstArr) > 1){
                               $courseID = $studindrstArr[0];
                               $indstudrst = $studindrstArr[1];
                               //get ca and exam
                               $indrstArr = explode("|",$indstudrst);
                               if(strpos($indrstArr[0],",") === false){ //if old
                                 $indrstArr[0] = $indrstArr[0].",".$indrstArr[1];
                               }
                               // $tot = (int)$indrstArr[0] + (int)$indrstArr[1];
                                //check if course det exist
                                if(isset($RstInfo[$courseID])){
                                    $courseDet = $RstInfo[$courseID];
                                }else{
                                    $courseDet = isset($allcourses[$courseID])?$allcourses[$courseID]:CourseDetails($courseID);
                                }
                               $tot = array_sum(explode(",",$indrstArr[0]));
                               if(count($indrstArr) > 2){
                                 $pass = (int)$indrstArr[5];
                                 $grdval = $indrstArr[3];
                                 $grdlvl = (int)$indrstArr[2];
                                 $grdPoint = (int)$indrstArr[7];
                               }else{
                                   
                                  $grds = GetGrade($tot,$grdstr,$schgrdstr);
                                  $pass = (int)$grds["PASS"];
                                  $grdval = $grds["Grade"];
                                  $grdlvl = (int)$grds["Level"];
                                  $grdPoint = $grdlvl * $courseDet['CH'];
                               }
       
                              
                               

                               $ResultScores[$courseID] = ["Total"=>$tot, "Pass"=>$pass, "Grade"=>$grdval, "GradeLevel"=>$grdlvl, "CourseDetails"=>$courseDet, "GradePoint"=>$grdPoint ];
                            //   THeader(["COURSE CODE", "CH", "SCORE/GRADE", "GP"],"style=text-align:left");
                               TRecord([$courseDet['CourseCode'], $courseDet['CH'],$tot." ".$grdval, $grdPoint ],"data-id=studrstledger,style=text-transform:uppercase");
                             // $TCH += (int)$courseDet['CH'];
                              //$TGP += $grdlvl * (int)$courseDet['CH'];
                              
                           }
                       }
                   }
               }
               $GPA = number_format($indrst['GPA'],2);
               $CGPA = number_format($indrst['CGPA'],2);
               TRecord(["TCH", $indrst['TCH'],"TGP", $indrst['TGP']],"style=text-transform:uppercase;font-weight:bolder");
            //    TRecord([],"style=text-transform:uppercase;font-weight:bolder,cspan=d2:3");
               TRecord(["GPA", $GPA, "CGPA", $CGPA],"style=text-transform:uppercase;font-weight:bolder");
               unset($indrst['Rst']);
               unset($indrst['RstInfo']);
               $indrst['Result'] = $ResultScores;
                $rstArray[$lookupkey] = $indrst;
                _Table();
            }
            //  var_dump($rstArray);
            echo '<div id="loadedRegNo" style="display:none">'.$RegNo.'</div>';
        }
    }

   _Box();
   _GroupBox();


?>