<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include("includes/function.php");

$zdb = $_SESSION["zdbyear_$dbname"];
$ztit = $_SESSION["tit"];
$usertype = $_SESSION["usertype"];
$user = $_SESSION['username_' . $dbname];
$keyfld = "vouchno";

mysqli_select_db($conn, $zdb);

if (isset($_REQUEST["exam"])) {

    $exam = $_REQUEST["exam"];
    $subj = $_REQUEST["subj"];
    $subj1 = $_REQUEST["subj1"];
    $subj2 = $_REQUEST["subj2"];

    if (!empty($subj)) {
        $getmsub = mysqli_query($conn, "SELECT * FROM `paprate` WHERE `examcode` = '$exam' and `section` = 'P' and `subjname`='$subj' ")or die(mysqli_error($conn));
        $pnum = mysqli_num_rows($getmsub);
        if ($pnum > 0) {
            $msub = mysqli_fetch_array($getmsub);
        } else {
            $prate = mysqli_query($conn, "SELECT * FROM `paprate` WHERE `section`='P' AND `examcode`='$exam'") or die(mysqli_error($conn));
            $msub = mysqli_fetch_array($prate);
        }
        $rt1 = $msub['rt1'];
        $rt2 = $msub['rt2'];
        $rt3 = $msub['rt3'];
        $rt4 = $msub['rt4'];
        $getmsub1 = mysqli_query($conn, "SELECT ppackrate FROM `paprate` WHERE `examcode` = '$exam' and `section` = 'P' and `ppackrate` > 0 ")or die(mysqli_error($conn));
        $msub1 = mysqli_fetch_array($getmsub1);
        $ppackrate = $msub1['ppackrate'];
        $sec = 'P';
        $row = ['rt1' => $rt1, 'rt2' => $rt2, 'rt3' => $rt3, 'rt4' => $rt4, 'ppackrate' => $ppackrate, 'sec' => $sec];
    } else if (!empty($subj1)) {
        $getmsub = mysqli_query($conn, "SELECT * FROM `paprate` WHERE `examcode` = '$exam' and `section` = 'S' and `subjname`='$subj1' ")or die(mysqli_error($conn));
        $pnum = mysqli_num_rows($getmsub);
        if ($pnum > 0) {
            $msub = mysqli_fetch_array($getmsub);
        } else {
            $prate = mysqli_query($conn, "SELECT * FROM `paprate` WHERE `section`='S' AND `examcode`='$exam'") or die(mysqli_error($conn));
            $msub = mysqli_fetch_array($prate);
        }
        $rt5 = $msub['rt5'];
        $rt6 = $msub['rt6'];
        $rt7 = $msub['rt7'];
        $rt8 = $msub['rt8'];
        $rt9 = $msub['rt9'];
        $rt10 = $msub['rt10'];
        $getmsub1 = mysqli_query($conn, "SELECT spackrate FROM `paprate` WHERE `examcode` = '$exam' and `section` = 'S' and `spackrate` > 0 ")or die(mysqli_error($conn));
        $msub1 = mysqli_fetch_array($getmsub1);
        $spackrate = $msub1['spackrate'];
        $sec = 'S';
        $row = ['rt5' => $rt5, 'rt6' => $rt6, 'rt7' => $rt7, 'rt8' => $rt8, 'rt9' => $rt9, 'rt10' => $rt10, 'spackrate' => $spackrate, 'sec' => $sec];
    } else {
        $getmsub = mysqli_query($conn, "SELECT * FROM `paprate` WHERE `examcode` = '$exam' and `section` = 'M' and `subjexamname`='$subj2' ")or die(mysqli_error($conn));
        $pnum = mysqli_num_rows($getmsub);
        if ($pnum > 0) {
            $msub = mysqli_fetch_array($getmsub);
        } else {
            $prate = mysqli_query($conn, "SELECT * FROM `paprate` WHERE `section`='M' AND `examcode`='$exam'") or die(mysqli_error($conn));
            $msub = mysqli_fetch_array($prate);
        }
        $rtprelm = $msub['rtprelm'];

        $getmsub1 = mysqli_query($conn, "SELECT mpackrate FROM `paprate` WHERE `examcode` = '$exam' and `section` = 'M' and `mpackrate` > 0 ")or die(mysqli_error($conn));
        $msub1 = mysqli_fetch_array($getmsub1);
        $mpackrate = $msub1['mpackrate'];
        $sec = 'M';
        $row = ['rtprelm' => $rtprelm, 'mpackrate' => $mpackrate, 'sec' => $sec];
    }
    header("Content-type: application\json");
    echo json_encode($row);
//    $rows = array('success' => true,
//        'rate' => "$r[rate]",
//        'unit' => "$r[unit]",
//        'mrp' => "$r[mrp]",
//        'msp' => "$r[msp]");
}