<?php
  include "header.php";
?>
<body>
  <main id="main">
    <!-- ======= About Lists Section ======= -->
    <section id="situasi" class="about-lists">
      <div class="container">
        <?php include "kop.php";?>
        <div class="row contact">
          <!-- <video controls autoplay name="media"><source src="<?php //echo base_url()?>assets/1.mkv" type="application/ogg"></video> -->
          <div class="col-lg-12 col-md-12 content-item" data-aos="fade-up">
            <span class="alert alert-success text-center" style="color: black;">LIVE SCORE <nama id="nama_ujian"></nama></span>
            <div id="t4_live">
              <div class="col-lg-12" data-aos="fade-up" data-aos-delay="300">
                  <div class="row justify-content-center">
                    <div class="col-lg-6 form-group">
                      <input type="number" onkeydown="load_live()" class="form-control" name="pin" id="pin" placeholder="PIN Sesi" data-rule="minlen:4" data-msg="Please enter at least 4 chars" autocomplete="off" />
                      <div class="validate"></div>
                    </div>
                  </div>
                  <div class="mb-3">
                  </div>
                  <div class="text-center"><button type="button" onclick="load_live()" class="btn btn-flat btn-md btn-primary">Tampilkan</button></div>
              </div>
              <div class="col-lg-12 col-md-12 text-center" data-aos="fade-up" data-aos-delay="300">
                <br>
                <br>
                <h3>KETERANGAN :</h3>
                <p style="color: black;">
                  PIN Sesi adalah : PIN Ujian yang sedang berlangsung
                </p>
                
              </div>
            </div>
          </div>

      </div>

    </div>
      
  </section><!-- End About Lists Section -->

  </main><!-- End #main -->

  <?php include "footer.php";?>

  <script type="text/javascript">

    var jlh_peserta=0;
    var timeload=0;

    function load_live(){
      var pin = $("#pin").val();

      if(pin==""){
        return false;
      }
      else{
        $.ajax({
          url:'<?php echo base_url('cat/tampil_data_live?pin=')?>'+pin,
          type:'get',
          success:function(e){
            $("#t4_live").html(e);
            cari_jumlah(pin);
          },
          error:function(x){
            console.log(x);
          }
        })
      }

      return false;
    }

    function cari_jumlah(pin){
      $.ajax({
        url:'<?php echo base_url('cat/cari_jumlah?pin=')?>'+pin,
        type:'get',
        success:function(e){
          jlh_peserta = e;
        },
        error:function(x){
          console.log(x);
        }
      })
    }

    if(jlh_peserta <= 20){
      timeload = 300;
    }
    else if(jlh_peserta <=60){
      timeload = 600;
    }
    else if(jlh_peserta > 60){
      timeload = jlh_peserta * 1000;
    }

    $(function(){
      setInterval(function(){
        load_live();
        console.log(timeload);
      },3000);
    })
  </script>