<?php
include("../../db.php");
header("Access-Control-Allow-Origin: * ");
header('Content-Type: application/json');
date_default_timezone_set("Asia/Kolkata");

/*
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
*/


$class = $_REQUEST['class'];
$section = trim($_REQUEST['section']);
$uid = $_REQUEST['uid'];
$session = $_REQUEST['session'];
//$exam = $_REQUEST['exam'];
$term = $_REQUEST['term'];

if ($term == 'Final'){
    $term = "Term2";
}

$monthsGeneric = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June',
    7 => 'July ', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December');

$due_mon_raw = $db->getVal("select report_due_month from tbl_info where id = '1'");

$conv = array(1=>10,2=>11,3=>12,4=>1,5=>2,6=>3,7=>4,8=>5,9=>6,10=>7,11=>8,12=>9);

$_REQUEST['due_month'] = $conv[$due_mon_raw];

$due_amount = include('fee_calculate.php');

$status = 0;
$msg = '';

if ($due_amount > 0) {
    $status = 1;
    $msg = "Please clear your Dues till " . $monthsGeneric[$due_mon_raw] . " to download result.";
} else {
    $msg = "You can download your result.";
}

$resultPublishCheck = $db->getVal("SELECT result_publish FROM tbl_info WHERE id = '1'");

$showRes = 0;

if ($resultPublishCheck == 1) {

    $whereSec = "";
    if ($section != ''){
     //   $whereSec = " AND section = '$section' ";
    }

    $responseQry = $db->getRow("SELECT * FROM termwise_timing WHERE term = '$term' AND class = '$class' AND session = '$session' AND start_date != '0000-00-00 00:00:00' AND end_date != '0000-00-00 00:00:00'".$whereSec);
    //print_r($responseQry);
    if ($responseQry && count($responseQry) > 0) {

        $currentDate = strtotime(date("Y-m-d H:i:s"));
        $publishDateBegin = strtotime($responseQry['start_date']);
        $publishDateEnd = strtotime($responseQry['end_date']);

        if ($currentDate > $publishDateBegin && $currentDate < $publishDateEnd) {
            //echo "Result Published";
            $showRes = 1;
        } else {
            //echo "Result Awaited..!";
            $showRes = 0;
        }

    }else{
        $showRes = 1;
    }

} else {
    $showRes = 1;
}

$resultFeeCheck = $db->getVal("SELECT report_resultview FROM tbl_info WHERE id = '1'");

$student_flag = $db->getVal("SELECT report_view_result FROM student WHERE idno = '$uid'");

if ($resultFeeCheck == 1) {
    $status = 0;
    $msg = "You can download your result.";
    $total_comp = 0;
} elseif ($resultFeeCheck == 3) {
    if ($student_flag == 1) {
        $status = 0;
        $msg = "You can download your result.";
        $total_comp = 0;
    }
} elseif ($resultFeeCheck == 4) {
    if ($student_flag == 1) {
        $status = 0;
        $msg = "You can download your result.";
        $total_comp = 0;
    } else {
        $status = 1;
        $msg = "Please clear your Dues till " . $monthsGeneric[$due_mon_raw] . " to download result.";
        $total_comp = 0;
    }
} elseif ($resultFeeCheck == 5) {
    $status = 0;
    $showRes = 0;
}


$array['status'] = $status;
$array['due'] = $due_amount;
$array['msg'] = $msg;
$array['show'] = $showRes;
$array['resss'] = $responseQry;

echo json_encode($array);