<?php
session_start();
$linkPage = base64_encode(
    "https://app2.drsiamakheydari.ir" . $_SERVER["REQUEST_URI"]
);
if (!isset($_SESSION["user_id"])) {
    header("Location: ChatLogin.php?lnk=$linkPage");
    exit();
}
include "ChatConfig.php";
/////////////////////////////////////////////////////
$query = $connection->prepare("SELECT * FROM users WHERE id=:id");
$query->bindParam("id", $_SESSION["user_id"], PDO::PARAM_STR);
$query->execute();
$result_user = $query->fetch(PDO::FETCH_ASSOC);
/////////////////////////////////////////////////////
$query3 = $connection->prepare("SELECT * FROM answer WHERE examUID=:examUID");
$query3->bindParam("examUID", $_GET["id"], PDO::PARAM_STR);
$query3->execute();
$result_answer = $query3->fetch(PDO::FETCH_ASSOC);
/////////////////////////////////////////////////////
if (!isset($_GET["ans"])) {
    if ($result_answer["username"] == $result_user["username"]) {
        $existStatus = true;
    }
}
/////////////////////////////////////////////////////
$query2 = $connection->prepare("SELECT * FROM exam WHERE uniqid=:uniqid");
$query2->bindParam("uniqid", $_GET["id"], PDO::PARAM_STR);
$query2->execute();
$result_exam = $query2->fetch(PDO::FETCH_ASSOC);
/////////////////////////////////////////////////////
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>سامانه آزمون گیر آنلاین</title>
    <meta name="author" content="NovaGolden" />
    <meta name="theme-color" content="#dde9f3"/>
    <!-- Windows Phone -->
    <meta name="msapplication-navbutton-color" content="#dde9f3">
    <!-- iOS Safari -->
    <meta name="apple-mobile-web-app-status-bar-style" content="#dde9f3">
    <link rel="icon" href="https://app2.drsiamakheydari.ir/chit_chat/rtl/images/favicon/favicon.png" type="image/x-icon" />
    <link rel="shortcut icon" href="https://app2.drsiamakheydari.ir/chit_chat/rtl/images/favicon/favicon.png" type="image/x-icon" />
    <!-- bootstrap css -->
    <link rel="stylesheet" type="text/css" href="assets/css/bootstrap.min.css">
    <!-- Font awesome 6 -->
    <link rel="stylesheet" type="text/css" href="https://use.fontawesome.com/releases/v6.1.1/css/all.css">
    <!-- custom styles -->
    <link rel="stylesheet" type="text/css" href="assets/css/style.css?v=<?php echo time() ?>">
    <link rel="stylesheet" type="text/css" href="assets/css/responsive.css">
    <link rel="stylesheet" type="text/css" href="assets/css/animation.css">
    <link rel="stylesheet" href="assets/fonts/vazir/Farsi-Digits/Vazirmatn-FD-font-face.css" />
</head>
<body class="show-section">

    <section class="steps" id="steps">
        <div class="container">
            <div class="mx-auto col-md-12 col-lg-7">

                <div class="show-section">
                    <!-- step-2 -->
                    <?php
                    if(!$existStatus === true){
    $array = json_decode($result_exam["body"]);
    foreach ($array as $value) {
?>
                    <section class="steps-inner" id="step-<?php echo $value->Question->ID ?>">
                        <div class="wrapper">
                            <div class="step-heading">
                                <h2><?php echo $Exam['name'] ?></h2>
                            </div>
                            <div class="step-bar">
                                <span class="step-counter">
									سوال <?php echo $value->Question->ID ?> / <?php echo count($array); ?>
								</span>
								<?php 
								    $CountJson = count($array);
								    $Tghsim = 100 / $CountJson;
								    $Zarb = $Tghsim * $value->Question->ID;
								?>
                                <div class="step-bar-inner">
                                    <div class="step-bar-move step-move m50"  style="width: <?php echo $Zarb.'%!important' ?>;"></div>
                                </div>
                            </div>
                            <form action="" method="post">
                                <div class="form-heading">
                                    <?php
                                    if(filter_var($value->Question->FACE, FILTER_VALIDATE_URL)){
                                       echo'<img class="bg-img" src="'.$value->Question->FACE.'" alt="Avatar" style="width: 100%;height: 100%;">';
                                    }else{
                                       echo $value->Question->FACE;
                                    }
                                    ?>
                                </div>
                                <?php if(isset($_GET['ans'])) {
                                        $arr = json_decode($_GET['ans'],true);
                                         foreach ($arr as $key => $values) {
                                             if($values['Answer']['ID'] == $value->Question->ID)
                                         {
                                                 $Replay = $values['Answer']['REPLAY'];
                                           }
                                 }
                                 if(filter_var($Replay, FILTER_VALIDATE_URL)){ ?>
                                 <label class="form-input active-input" for="work" style="height: auto;line-break: anywhere;margin-top: 20px;">
                                 <img class="bg-img" src="<?php echo $Replay ?>" alt="Avatar" style="width: 100%;height: 100%;">
                                 </label>
                                 <?php
                                    }else{
                                 ?>
                                <label class="form-input active-input" for="work" style="height: auto;line-break: anywhere;margin-top: 20px;"><?php echo $Replay ?></label>
                                <?php }
                                    } else { 
                                ?>
                                <?php if($value->Question->TYPE == 'True&False'){ ?>
                                <div class="form-inner pop-slide">
                                    <label class="form-input" for="question-<?php echo $value->Question->ID ?>">
										<input type="radio" name="question-<?php echo $value->Question->ID ?>" onclick="handleClick(this, '<?php echo $value->Question->ID ?>');" value="صحیح">
										صحیح
									</label>
                                    <label class="form-input" for="question-<?php echo $value->Question->ID ?>">
										<input type="radio" name="question-<?php echo $value->Question->ID ?>" onclick="handleClick(this, '<?php echo $value->Question->ID ?>');" value="غلط">
										غلط
									</label>
                                </div>
                                <?php } else if($value->Question->TYPE == 'MultipleChoice'){ ?>
                                <div class="form-inner pop-slide">
                                    <label class="form-input" for="work">
										<input type="radio" name="question-<?php echo $value->Question->ID ?>" onclick="handleClick(this, '<?php echo $value->Question->ID ?>');" value="<?php echo $value->Question->Options->A ?>">
										<?php echo $value->Question->Options->A ?>
									</label>
                                    <label class="form-input" for="work">
										<input type="radio" name="question-<?php echo $value->Question->ID ?>" onclick="handleClick(this, '<?php echo $value->Question->ID ?>');" value="<?php echo $value->Question->Options->B ?>">
										<?php echo $value->Question->Options->B ?>
									</label>
                                    <label class="form-input" for="work">
										<input type="radio" name="question-<?php echo $value->Question->ID ?>" onclick="handleClick(this, '<?php echo $value->Question->ID ?>');" value="<?php echo $value->Question->Options->C ?>">
										<?php echo $value->Question->Options->C ?>
									</label>
                                    <label class="form-input" for="work">
										<input type="radio" name="question-<?php echo $value->Question->ID ?>" onclick="handleClick(this, '<?php echo $value->Question->ID ?>');" value="<?php echo $value->Question->Options->D ?>">
										<?php echo $value->Question->Options->D ?>
									</label>
                                </div>
                                <?php } else if($value->Question->FIELD == 'TexBox'){ ?>
                                     <textarea class="form-input" onchange="handleInput(this.value, '<?php echo $value->Question->ID ?>')" rows="4" cols="50" style=" height: 100%; border-radius: 5px; margin-top: 20px;"></textarea>
                                <?php } else if($value->Question->FIELD == 'File'){ ?>
                                    <label class="form-input active-input" id="FileTxT-<?php echo $value->Question->ID ?>" for="File-Upload-<?php echo $value->Question->ID ?>" style="margin-top: 20px;">برای بارگذاری فایل، کلیک کنید.</label>
                                    <input id="File-Upload-<?php echo $value->Question->ID ?>" type="file" accept="image/*,.pdf" style="display: none;" onchange="FileUpload('<?php echo $value->Question->ID ?>')"/>
                                <?php }
                                }
                                ?>
                                <!-- next-prev-btn -->
                                <div class="form-buttons" style="direction: ltr;">
                                    <?php if($value->Question->ID == 1){ ?>
                                        <button type="button" class="next">سوال بعدی<i class="fa-solid fa-arrow-right"></i></button>
                                    <?php } else if($value->Question->ID == count($array)){ ?>
                                        <button type="button" class="prev"><i class="fa-solid fa-arrow-left"></i>سوال قبلی</button>
                                        <?php if(!isset($_GET['ans'])) { ?>
                                        <button type="button" class="next" id="finish-exam">پایان آزمون<i class="phone-ring fa-solid fa-thumbs-up"></i></button>
                                        <?php } ?>
                                    <?php } else { ?>
                                        <button type="button" class="prev"><i class="fa-solid fa-arrow-left"></i>سوال قبلی</button>
                                        <button type="button" class="next">سوال بعدی<i class="fa-solid fa-arrow-right"></i></button>
                                    <?php } ?>
                                </div>
                            </form>
                        </div>
                    </section>
                    <?php
                        }
                     }
                    ?>
                    <script>
                        function handleClick(myRadio, myAnswer) {
                             document.getElementById(myAnswer).value = myRadio.value;
                        }
                        function handleInput(myInput, myAnswer) {
                             document.getElementById(myAnswer).value = myInput;
                        }
                        function FileUpload(myId) {
                        var files = document.getElementById('File-Upload-'+myId).files;
                        var formData = new FormData();
                        formData.append("file", files[0]);
                        var xhttp = new XMLHttpRequest();
                        xhttp.open("POST", "https://app2.drsiamakheydari.ir/ChatFileUpload.php", true);
                        xhttp.upload.onprogress = function(evt) {
                        if (evt.lengthComputable) {
                        var percentComplete = parseInt((evt.loaded / evt.total) * 100);
                        document.getElementById('FileTxT-'+myId).innerHTML = 'فایل در حال بارگذاری...' + '(' + percentComplete + ' %' + ')';
                        }
                        };
                        // call on request changes state
                        xhttp.onreadystatechange = function() {
                        if (this.readyState == 4 && this.status == 200) {
                        var response = this.responseText;
                        var jsonis = $.parseJSON(response);
                        if (jsonis.status == 1) {
                        document.getElementById('FileTxT-'+myId).innerHTML = 'فایل انتخاب شده است! ' + '(' + files[0].name + ')';
                        document.getElementById(myId).value = 'https://app2.drsiamakheydari.ir/' + jsonis.link;
                        }
                        }
                        };
                        // Send request with data
                        xhttp.send(formData);
                        }
                    </script>
                    <?php
                        $array2 = json_decode($result_exam["body"]);
                        ?>
                        <input id="exam-uniqid" value="<?php echo $_GET["id"] ?>" type="hidden"/>
                        <input id="exam-number" value="<?php echo $_GET["numb"] ?>" type="hidden"/>
                        <input id="exam-username" value="<?php echo $result_user['username'] ?>" type="hidden"/>
                        <?php
                        foreach ($array2 as $value2) {
                    ?>
                            <input name='EXAM[]' id="<?php echo $value2->Question->ID ?>" value="" type="hidden"/>
                    <?php } ?>
                    <section class="thankyou-page thankyou-circle steps-inner" id="thankyou">
                        <div class="row">
                            <div class="col-md-6 mx-auto">
                                <div class="thankyou-page-inner">
                                    <div class="wrapper">
                                        <!-- tick  -->
                                        <div class="tick">
                                            <div class="done-tick"></div>
                                            <i class="fa-solid fa-check"></i>
                                        </div>
                                        <!-- thankyou page heading -->
                                        <h2>خسته نباشید !</h2>
                                        <!-- thankyou page text -->
                                        <p>
                                            جواب آزمون شما با موفقیت در سامانه ثبت شد. تشکر از اینکه در همکاری کردید!
                                        </p>
                                        <!-- thankyou paeg button -->
                                        <div class="next-prev-btn">
                                            <button class="next back" onclick="location.href = 'https://app2.drsiamakheydari.ir/ChatMessage#';">خروج از آزمون</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                </div>
            </div>
        </div>
    </section>
    <?php if(!isset($_GET['ans'])) { ?>
    <!-- timer -->
    <div class="sm-sticky">
        <div class="timer" style="background-color: #dde9f3!important;">
            <div class="countdown">
                <?php list($min, $sec) = explode(':', $result_exam['timer']);
                ?>
                    <span id="countdown-timer" class="time"><?php echo $result_exam['timer'] ?></span>
                    <input id="min" value="<?php echo $min ?>" type="hidden"/>
                    <input id="sec" value="<?php echo $sec ?>" type="hidden"/>
            </div>
            <div class="timer-heading">
                زمان باقی‌مانده
            </div>
        </div>
    </div>
    <?php } ?>
    <!-- bootstrap JS -->
    <script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
    <!-- Jquery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- custom JS -->
    <script type="text/javascript" src="assets/js/custom.js?v=<?php echo time() ?>"></script>
</body>
</html>