<?php
session_start();
include("conexion.php");

error_reporting(E_ALL ^ E_NOTICE); 
$sql2 = mysqli_query($con, "SELECT * FROM preguntas WHERE id='8'");
$row2 = mysqli_fetch_assoc($sql2);

$sql3 = mysqli_query($con, "SELECT * FROM reactivos WHERE idpregunta='8'");


while ($row333 = $sql3->fetch_assoc()) {
    $row3[] = $row333;
}
$idparticipante= $_SESSION['idparticipante'];
$sql5 = mysqli_query($con, "SELECT * FROM cuestionario WHERE idparticipante=$idparticipante");

if($idparticipante == null){
    header("Location:index");	
}

$sql55 = mysqli_query($con, "SELECT * FROM cuestionario WHERE idparticipante=$idparticipante");
//$row55 = mysqli_fetch_all($sql55,MYSQLI_ASSOC);
 while ($row5555 = $sql55->fetch_assoc()) {
    $row55[] = $row5555;
}
$idcuestionario55 = $row55[0]['id'];
$sql77 = mysqli_query($con, "SELECT * FROM respuestas WHERE idpregunta='7' and idcuestionario=$idcuestionario55 and idparticipante=$idparticipante and paso=7");

  if(mysqli_num_rows($sql77) == 0){
      
    header("Location:paso7");
}



if(mysqli_num_rows($sql5) > 0){
  //$row5 = mysqli_fetch_all($sql5,MYSQLI_ASSOC);
  while ($row555 = $sql5->fetch_assoc()) {
    $row5[] = $row555;
}
  $idcuestionario2 = $row5[0]['id'];
  $sql6 = mysqli_query($con, "SELECT * FROM respuestas WHERE idpregunta='8' and idcuestionario=$idcuestionario2 and idparticipante=$idparticipante and paso=8");
  
  while ($row666 = $sql6->fetch_assoc()) {
    $row6[] = $row666;
}
  //$row6 = mysqli_fetch_all($sql6,MYSQLI_ASSOC);
  
  $sql7 = mysqli_query($con, "SELECT * FROM respuestas WHERE idpregunta='8' and idcuestionario=$idcuestionario2 and idparticipante=$idparticipante and paso=8");

  if(mysqli_num_rows($sql7) > 0){
      
    $actualizarBandera = 1;
}else{
   
    $actualizarBandera = 0;
}    
  
  if($row6[0]['idreactivo']==$row3[0]['id']){
      
     $checked1='checked';
     $actualizarBandera = 1;
  }
  
  if($row6[0]['idreactivo']==$row3[1]['id']){
      
     $checked2='checked'; 
     $actualizarBandera = 1;
  }
  
 
}


if($_POST['init'] && $_POST['actualizar']==0){
   
    $reactivo=$_POST['pregunta2'];
    $idparticipante= $_SESSION['idparticipante'];
     $sql33 = mysqli_query($con, "SELECT * FROM reactivos WHERE idpregunta='8' and id=$reactivo");
    //$row33 = mysqli_fetch_all($sql33, MYSQLI_ASSOC);
    
    while ($row3333 = $sql33->fetch_assoc()) {
    $row33[] = $row3333;
}
    
    //$video=$row33[0]['video'];
    $sql4 = mysqli_query($con, "SELECT * FROM cuestionario WHERE idparticipante=$idparticipante");
    //$row4 = mysqli_fetch_all($sql4,MYSQLI_ASSOC);
    while ($row4444 = $sql4->fetch_assoc()) {
    $row4[] = $row4444;
}
    $idcuestionario = $row4[0]['id'];
    $insert = mysqli_query($con, "INSERT INTO respuestas(idpregunta, idreactivo, paso, idparticipante, idcuestionario)
															VALUES('8','$reactivo','8','$idparticipante', '$idcuestionario')") or die(mysqli_error());
															
    
															
	header("Location:paso9");														
}else{
    if($_POST['init'] && $_POST['actualizar']==1){
    $reactivo=$_POST['pregunta2'];    
    $sql6 = mysqli_query($con, "SELECT * FROM respuestas WHERE idpregunta='8' and idcuestionario=$idcuestionario2 and idparticipante=$idparticipante and paso=8");
    //$row6 = mysqli_fetch_all($sql6,MYSQLI_ASSOC); 
    while ($row666 = $sql6->fetch_assoc()) {
    $row6[] = $row666;
}
    $idreactivo=$row6[0]['idreactivo'];
    $idparticipante= $_SESSION['idparticipante'];
     $sql33 = mysqli_query($con, "SELECT * FROM reactivos WHERE idpregunta='8' and id=$reactivo");
    //$row33 = mysqli_fetch_all($sql33, MYSQLI_ASSOC);
     while ($row3333 = $sql33->fetch_assoc()) {
    $row33[] = $row3333;
}
    //$video=$row33[0]['video'];
    $sql4 = mysqli_query($con, "SELECT * FROM cuestionario WHERE idparticipante=$idparticipante");
    //$row4 = mysqli_fetch_all($sql4,MYSQLI_ASSOC);
    while ($row4444 = $sql4->fetch_assoc()) {
    $row4[] = $row4444;
}
    $idcuestionario = $row4[0]['id'];
    mysqli_query($con,"DELETE FROM respuestas WHERE idpregunta =8 and idreactivo=$idreactivo and idparticipante = $idparticipante and idcuestionario = $idcuestionario and paso=8");
    $insert = mysqli_query($con, "INSERT INTO respuestas(idpregunta, idreactivo, paso, idparticipante, idcuestionario)
															VALUES('8','$reactivo','8','$idparticipante', '$idcuestionario')") or die(mysqli_error());
															
    
															
	header("Location:paso9"); 
    }
    	
}

       
?>
<?php include("header.php"); ?>
    
   <link rel="stylesheet" href="css/style.css" >
    
   
    <link href="https://www.jqueryscript.net/css/jquerysctipttop.css" rel="stylesheet" type="text/css">
    

    

    
  </head>

  <body class="d-flex flex-column text-center escritorio"  >
      <?php include("body.php"); ?>
    
   <header>
      <nav  class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">

      <div style="float:left;" class="collapse navbar-collapse" id="navbarsExampleDefault">
        
       
          <img style="position:relative;left: 80%;" width="10%" src="logo.png">
         
      </div>
    </nav> 
    <img class="mobile" src="logo.png">
    
    <div style="position:relative;top: 50px;">
         
    <img class="mobile2" style="width: 70%;" src="images/numeros8.png"> 
    
    </div>
   </header>
    

    <main role="main"  class="container mobileMargen" style="position:relative;top:10%;">
        
        <section style="text-align:center;">
           <div class="col-12">
        <h1 style="color: #5f5093;" ><?php echo $row2['pregunta'] ?></h1>
      <br>
      
     
     </div> 
     
            
        </section>
        
      <br>
      <br>
           <br>
            <br>
        <form method="post" class="topform" action="" id="paso1" > 
        <input  type="hidden" name="init" value="1">
        <input type="hidden" name="actualizar" value="<?php echo $actualizarBandera ?>"> 
         
         <div style="text-align: justify;
    position: relative;
    left: 25%;" class="col col-12 row questionMobile">
        <div class="col-6 form-group">
            
            <div class="form-check">
  <input class="form-check-input" type="radio" id="pregunta1" required name="pregunta2" <?php echo $checked1; ?>  value="<?php echo $row3[0]['id']; ?>">
  <label style="color: #5f5093;"  class="form-check-label" for="pregunta1">
      
    <?php echo $row3[0]['reactivo']; ?>
  </label>
</div>
        </div> 
        
        <br>
        
        
          <div class="col-6 form-group">
            
            <div class="form-check">
  <input class="form-check-input" type="radio" id="pregunta2" name="pregunta2" <?php echo $checked2; ?>  value="<?php echo $row3[1]['id']; ?>" required>
  <label style="color: #5f5093;" class="form-check-label" for="pregunta2">
    <?php echo $row3[1]['reactivo']; ?>
  </label>
</div>
</div>
        
           
           
       </div>
      
          
            <br>
        <section style="position:relative;top:60%;">
            <button class="btn" type="button"><a style="color:white;text-decoration-line: none;" href="https://testansiedad.psicocentrofc.com/paso7">&laquo; Anterior </a></button>
<button class="btn" type="submit">Siguiente &raquo;</button>


        </section>
        </form> 
  

    </main><!-- /.container -->
    
    <footer class="mastfoot mt-auto">
    <div class="inner">
      <p class="copyright">© 2020 PsicoCentro® Todos los derechos reservados.</p>
    </div>
  </footer>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>



<script src="./javascript/html-input-range.js"></script>

    <script>
        $(document).ready(function () {
  // initiating html input range plugin
  // htmlInputrange.default();
  // if you want to customize html input range plugin
  htmlInputRange.options({
    tooltip: true,
    max: 90,
    labels: true,
    labelsData: {
      one: 'one',
      two: 'two'
    }
  });
});
    </script>

  </body>
</html>
