<?php

include '../admin/config.php';
$conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);


	if(isset($_GET['next'])){ 
	    return ;
	  }


////////////////////
// جلب سجل المستخدم
////////////////////

	    $email = $_GET["email"];
	    $pass = $_GET["pass"];
	
	    $query = "SELECT point, energe FROM USERS WHERE email='$email' ";
        $result = mysqli_query($conn,$query);
           if($row = mysqli_fetch_array($result)) {
		        $point = $row['point'] ;
				 $energe = $row['energe'] ;
             }else{
				echo "خطأ...خطأ...خطأ...خطأ...خطأ..." ;
				return ;
			 }
			 

	

	if(isset($_GET['answeruser'])){ 
	
	    $email = $_GET['email'] ;
		 $pass = $_GET['pass'] ;
		  $id_q = $_GET['id_q'] ;
		   $rihan = $_GET['rihan'] ;
		    $answeruser = $_GET['answeruser'] ;
		   	 $answer = $_GET['answer'] ;


   if( $rihan > $point || $energe <= 0 ){
	   echo "النقاط أو الطاقة غير كافية..." ;
	   exit ;
      }	   
	

	switch ($answer){
		case "1" :
		    $a = "a_1" ;
			break ;
			
		case "2" :
		    $a = "a_2" ;
			break ;
			
		case "3" :
		    $a = "a_3" ;
			break ;
			
		case "4" :
		    $a = "a_4" ;
            break ;
	   }
	   
	      $query = "SELECT $a FROM QUESTION WHERE id='$id_q' ";
           if($row = mysqli_fetch_array(mysqli_query($conn,$query) )) {
			  

			  
			  if( strlen(utf8_decode($answeruser)) < 4  && strlen(utf8_decode($row[$a])) > 4 ) {

				  echo "<center> <img src=\"https://cdn.iconscout.com/icon/free/png-256/false-delete-remove-cross-wrong-32-28635.png\" alt=\"Girl in a jacket\" > <br><br>
                                    <form action=\"\" method=\"get\"> <input type=\"hidden\" id=\"next\" name=\"next\" value=\"next_q\" /><input type=\"submit\" value=\"التالي\" style=\"font-weight: bold; margin-right:8px; padding-right: 20px; padding-left: 20px;\" />  </form>" ;									
						 echo "<br>الجواب: <br> $row[$a] </center>" ;	
				   
				    $sql = "UPDATE USERS SET point=point-$rihan  , log=CONCAT(log, 'chanllonge add $rihan - id_q:$id_q \n'), energe=energe-1 WHERE email='$email' ";
                           $conn->query($sql) ;
				  return ;
			     }


			  if( strstr($row[$a], $answeruser) ){
                         echo "<center><img src=\"https://www.iconninja.com/files/881/289/163/accept-icon.png\" alt=\"Girl in a jacket\" > <br><br>
                                   <form action=\"\" method=\"get\"> <input type=\"hidden\" id=\"next\" name=\"next\" value=\"next_q\" /><input type=\"submit\" value=\"التالي\" style=\"font-weight: bold; margin-right:8px; padding-right: 20px; padding-left: 20px;\" />  </form>" ;									
						 echo "<br>الجواب: <br> $row[$a] </center>" ;	
						 
						 $sql = "UPDATE USERS SET point=point+$rihan  , log=CONCAT(log, 'chanllonge add $rihan - id_q:$id_q \n') WHERE email='$email' ";
                           $conn->query($sql) ;
                            return ;
		   
                        }else{
							echo "<center><img src=\"https://cdn.iconscout.com/icon/free/png-256/false-delete-remove-cross-wrong-32-28635.png\" alt=\"Girl in a jacket\" > <br><br>
							                <form action=\"\" method=\"get\"> <input type=\"hidden\" id=\"next\" name=\"next\" value=\"next_q\" /><input type=\"submit\" value=\"التالي\" style=\"font-weight: bold; margin-right:8px; padding-right: 20px; padding-left: 20px;\" />  </form>" ;									
						 echo "<br>الجواب: <br> $row[$a] </center>" ;	
						 
							 $sql = "UPDATE USERS SET point=point-$rihan  , log=CONCAT(log, 'chanllonge add $rihan - id_q:$id_q \n'), energe=energe-1 WHERE email='$email' ";
                           $conn->query($sql) ;
                            return ;
						}

             }
			 
	
	}
	

/////////////
// جلب الأسئلة
/////////////

$sql = "SELECT id, type, question , answer FROM QUESTION ORDER BY RAND() " ; // جلب الاسئلة عشوائيا
$result = $conn->query($sql);

    while($row = $result->fetch_assoc()) {
         
           if ( $row["type"] == "txt"  ) {
			    $id_q =  $row["id"] ;
                $question  = $row["question"] ;
				$answer  = $row["answer"] ;
				break ;
           }
	  
    }
	
$conn->close();

?>


<!DOCTYPE html>
<html dir="rtl" lang="ar">
<head>
<title>قدرات مبدع</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<meta charset="utf-8">

<style>
html, body {
  min-height:100%;height:100%;

}
.red-background-not-on-body-or-html {
  background-color: #EF4247;    
  min-height:100%;
}
</style>

</head>
<body>
<div class="w3-container">
<br>

<div class="w3-bar w3-blue">

   <a class="w3-bar-item w3-button w3-right"style="font-weight: bold;" > <center> نقطة عبور </center> </a>

</div>


<p style="font-weight: bold;" > نقاطك : <?php echo $point ; ?> </p>


<form action="" method="get" >
 <input type="hidden" id="email" name="email" value="<?php echo $email ; ?>">
 <input type="hidden" id="pass" name="pass" value="<?php echo $pass ; ?>">
 <input type="hidden" id="id_q" name="id_q" value="<?php echo $id_q ; ?>">
 
<p> <input type="hidden" id="rihan" name="rihan" value="1" style="font-weight: bold; margin-right:8px;" > </p>


<div class="w3-bar w3-blue">
   <a href="#" class="w3-bar-item w3-button w3-right"style="font-weight: bold;" >الســؤال:</a>
</div>
<br>
<li style="font-weight: bold; " > <?php echo $question ; ?> </li>
<br>
 <input type="hidden" id="answer" name="answer" value="<?php echo $answer ; ?>">
 
  <label for="lname" style="font-weight: bold; margin-right:8px;" >الجواب:</label><br>
  <input type="text" id="answeruser" name="answeruser" style="font-weight: bold; margin-right:8px;"><br><br>
  
    <input type="submit" value="تأكيد" style="font-weight: bold; margin-right:8px; padding-right: 20px; padding-left: 20px;">
	<br>
<br>


</</form>




</body>
</html>
