<!DOCTYPE html>
<html lang="en">
<?php

require_once('../assets/constants/config.php');
require_once('constants/check-login.php');

    try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


	$stmt = $conn->prepare("SELECT * FROM tbl_exams LEFT JOIN tbl_subjects ON tbl_exams.subject = tbl_subjects.id LEFT JOIN tbl_classes ON tbl_exams.class = tbl_classes.id WHERE tbl_exams.exam_id = ?");
	$stmt->execute([$_GET['node']]);
	$result = $stmt->fetchAll();

    foreach($result as $row)
    {
        $exam_name = $row[1];
        $qust = $row[14];
        $ex_class = $row[21];
        $ex_subject = $row[16];
        $ex_minutes = $row[2];
        $ex_terms = $row[11];
        if ($ex_minutes == NULL) {
            $r_m = "UnTimed";
        }else{

            $d = floor ($ex_minutes / 1440);
            $h = floor (($ex_minutes - $d * 1440) / 60);
            $m = $ex_minutes - ($d * 1440) - ($h * 60);
            $r_m = ''.$h.'Hours '.$m.' Minutes';
        }
	}
	}catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4;collation=utf8_general_ci;prefix=''", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


        $stmt = $conn->prepare("SELECT * FROM tbl_school_info");
        $stmt->execute();
        $result = $stmt->fetchAll();

        foreach($result as $row) {

        $school_name = $row[0];
        $phone = $row[1];
        $email = $row[2];
        $address = $row[3];
        $logo = $row[4];
        }
    }catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }


?>
<head>
<meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>OES - Print Exam</title>
    <link rel="icon" href="../assets/images/favicon.ico">
    <link href="../assets/google-fonts/montserrat.css" rel="stylesheet">
</head>
<style>

</style>

<body style="background-color:lightgrey;  font-family: 'Montserrat', sans-serif;">
    <div class="app-admin-wrap" style="width:80%; background-color:white; margin:auto; padding-left:30px; padding-right:30px;">



        <div class="main-content-wrap sidenav-open d-flex flex-column">
<br>
        <button class="print-link no-print" onclick="jQuery('#ele1').print()">
                Print Exam
                </button>  &nbsp;<button onclick="goBack()">Go Back</button>


                    <div class="row">            <div  id="ele1"  class="col-md-12 mb-3">
                    <div class="card text-left">

                        <div class="card-body"><center>
                            <div  class="text-center">
                                <img width="140" src="../assets/uploads/logo/<?php echo $logo; ?>">
                            <h3 class="card-title mb-2 mt-2"><?php echo $school_name; ?><br><?php echo $exam_name; ?><br><?php echo $ex_subject; ?>, <?php echo $ex_class; ?><br></h3>
                            <p><?php echo $email; ?>, <?php echo $phone; ?><br> <?php echo $address; ?></p></center>
                    </div>

                            <div class="table-responsive">
                            <table class="table table-borderless">
                                    <thead>
                                        <tr>
                                            <td scope="col"><b>Instructions / Terms : </b><br><?php echo $ex_terms; ?></td>
                                        </tr>
                                    </thead>
                                </table>
<hr>
                            <table  width="100%">
                                    <thead>
                                        <tr>
                                            <th style=" text-align: left; ">Name :</th>
                                            <th width="300" scope="col">Time : <?php echo $r_m; ?></th>
                                        </tr>
                                    </thead>
                                </table>
<hr>
                                <table  width="100%">

                                    <tbody>
                                        <?php
                                        try {
                                            $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
                                            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


                                            $stmt = $conn->prepare("SELECT * FROM tbl_questions WHERE exam = ? ORDER BY rand() LIMIT $qust");
                                            $stmt->execute([$_GET['node']]);
                                            $result = $stmt->fetchAll();

                                            foreach($result as $row)
                                            {
                                                $arr_m = [];
                                                $type = $row['type'];
                                                $op1 = $row['op1'];
                                                $op2 = $row['op2'];
                                                $op3 = $row['op3'];
                                                $op4 = $row['op4'];
                                                array_push($arr_m, $op1, $op2, $op3, $op4);
                                                shuffle($arr_m);


                                                if ($row[2] == "1") {

                                                    ?>
                                                      <tr>


                                                            <td style=" padding-top: 13px;"><?php echo $row[1];?></td>


                                                        </tr>
                                                        <tr>
                                                            <td>A) <?php print_r($arr_m[0]); ?></td>
                                                        </tr>
                                                        <tr>
                                                            <td>B) <?php print_r($arr_m[1]); ?></td>
                                                            <td rowspan="2">(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)</td>
                                                        </tr>
                                                        <tr>
                                                            <td>C) <?php print_r($arr_m[2]); ?></td>

                                                        </tr>
                                                        <tr style="border-bottom:1pt solid black">
                                                            <td style="padding-bottom: 13px;
                                                                ">D) <?php print_r($arr_m[3]); ?></td>

                                                        </tr>

                                                        <?php

                                                }else{

                                                    ?>

                                                    <tr>

                                                        <td  style=" padding-top: 13px;" colspan="2"><?php echo $row[1];?></td>

                                                    </tr>

                                                    <tr style=" line-height:30px; border-bottom:1pt solid black">
                                                        <td  style="padding-bottom: 13px;"  colspan="2">Answer : </td>

                                                    </tr>

                                                    <?php


                                                }

                                            }
                                            }catch(PDOException $e)
                                            {
                                            echo "Connection failed: " . $e->getMessage();
                                            }

                                        ?>


                                    </tbody>
                                </table>

                            </div>


                        </div>
                    </div>
                </div></div>

            <div class="flex-grow-1"></div>

        </div>

    </div>


    <script src="../assets/js/vendor/jquery-3.3.1.min.js"></script>


        <script>
        window.jQuery || document.write('<script src="../bower_components/jquery/dist/jquery.min.js"><\/script>')
        </script>
        <script src="../assets/print/jQuery.print.js"></script>
        <script type='text/javascript'>


        function goBack() {
            window.location.href = "./exams";
}
        </script>

</body>


</html>
