<?php
use yii\widgets\ListView;
use nirvana\infinitescroll\InfiniteScrollPager;
use kop\y2sp\ScrollPager;
use yii\web\Session;
use yii\helpers\Html;
use common\widgets\Alert;
use kartik\popover\PopoverX;
use yii\helpers\Url;
if($alert==TRUE){
  foreach (Yii::$app->session->getAllFlashes() as $message):; 

    echo \kartik\widgets\Growl::widget([
        'type' => (!empty($message['type'])) ? $message['type'] : 'danger',
        'title' => (!empty($message['title'])) ? Html::encode($message['title']) : 'Title Not Set!',
        'icon' => (!empty($message['icon'])) ? $message['icon'] : 'fa fa-info',
        'body' => (!empty($message['message'])) ? Html::encode($message['message']) : 'Message Not Set!',
        'showSeparator' => true,
        'delay' => 1, //This delay is how long before the message shows
        'pluginOptions' => [
            'delay' => (!empty($message['duration'])) ? $message['duration'] : 3000, //This delay is how long the message shows for
            'placement' => [
                'from' => (!empty($message['positonY'])) ? $message['positonY'] : 'top',
                'align' => (!empty($message['positonX'])) ? $message['positonX'] : 'right',
            ]
        ]
    ]);
                  
  endforeach; 
}
###untuk paging#####
###setting page,limit dan offset####
$page= ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
$limit= ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 10;
$offset = ceil($page / $limit); 
$base=Yii::$app->homeUrl;
$this->registerJS("
$(document).ready(function(){
    $('[data-toggle=\"popover\"]').popover(); 
});


  ");
$this->registerJS("
$('#popover').popover({ 
                      html : true,
                      title: function() {
                        return $(\"#popover-head\").html();
                      },
                      content: function() {
                        return $(\"#popover-content\").html();
                      },
                      footer: function() {
                        return $(\"#popover-head\").html();
                      }
                  });

  "); 
$url = Url::to(['keranjang/cetak']);
/*$this->registerJs("
  $('#PrintButton').click(function(){
    $.get('". $url ."', {id: ".$model->ID."},function(data, status){
      if(status == 'success'){
        try {
          var oIframe = document.getElementById('Iframe1Slip');
          var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
          if (oDoc.document) oDoc = oDoc.document;
          oDoc.write('<html><head>');
          oDoc.write('</head><body onload=\"this.focus(); this.print(true);\" style=\"text-align: left; font-size: 8pt; width: 95%; height:90%\">');
          oDoc.write(data + '</body></html>');
          oDoc.close();
        } catch (e) {
          alert(e.message);
          self.print();
        }
      }
    });
  });
");*/
 ?>    
<!--untuk menampilkan collection dan konten digital  -->
        <script type="text/javascript">
            function favorite(id) {        
       
              $.ajax({
                type     :"POST",
                cache    : false,
                url  : "?action=favourite&catID="+id,
                success  : function(response) {
                      $("#favourite"+id).html(response);
                  }
              });


            }
            function KirimEmail() {       
              var id = $("#catalogID").val();
              //var email = document.getElementById('emailnya');
              //var email = $("#emailnya").val();
              var email = $("#emailnya").attr('value')
                 $.ajax({
                    type     :"POST",
                    cache    : false,
                    url  : "?action=email&emailID="+email,
                    success  : function(response) {
                        $("#collectionShow"+id).html(response);
                    }
                });
            }
            function collection(id) {       
                //var id = $("#catalogID").val();
                 $.ajax({
                    type     :"POST",
                    cache    : false,
                    url  : "?action=showCollection&catID="+id,
                    success  : function(response) {
                        $("#collectionShow"+id).html(response);
                    }
                });
            }
              function search(id) {   
                //var id = $("#catalogID").val();
                $.ajax({
                  type     :"POST",
                  cache    : false,
                  url  : "?action=search&catID="+id,
                  success  : function(response) {
                    $("#search"+id).html(response);
                  }
                });


              }

            function kontenDigital(id) {        
                //var id = $("#catalogID").val();
                 $.ajax({
                    type     :"POST",
                    cache    : false,
                    url  : "?action=showKontenDigital&catID="+id,
                    success  : function(response) {
                        $("#kontenDigitalShow"+id).html(response);
                    }
                });
            }
            function hapusKeranjang(id) {        
                //var id = $("#catalogID").val();
                 $.ajax({
                    type     :"POST",
                    cache    : false,
                    url  : "?action=showKontenDigital&catID="+id,
                    success  : function(response) {
                        $("#kontenDigitalShow"+id).html(response);
                    }
                });
            }
            function popover() {        
                //var id = $("#catalogID").val();
                  
            }



        </script>
        <script language="JavaScript">
        function toggle(source) {
        checkboxes = document.getElementsByName('catID[]');
        for(var i=0, n=checkboxes.length;i<n;i++) {
            checkboxes[i].checked = source.checked;
        }
        }   
        </script>
          <section class="content">
            <div class="box box-default">
              <div class="box-body" style="padding:20px 0">
                  <div class="breadcrumb">
                  <ol class="breadcrumb">
                            <li><a href="<?=$base ?>">Home</a></li>                   
                            <li class="active">Keranjang</li>
                          </ol>
                  </div>
                    <div class="row">
                      <div class="col-md-12">
                        <div class="row">
 <iframe id='Iframe1Slip' src='#' class='clsifrm' style="width: 0pt; height: 0pt; border: none;" ></iframe>
<div id="divPrint" style="display:inline;"></div>

  <form method="POST" action="<?php Yii::$app->homeUrl?>" class="form-inline">  
   
<div id="popover-head" class="hide"> <i class="glyphicon glyphicon-lock"></i>  Enter Email adresses</div>
<div id="popover-content" class="hide">

  <div class="form-group">
    <input type="email" class="form-control"  name="email"  >
    <button type="submit" class="btn btn-default  " onClick="KirimEmail()">Send</button>
    <input type="hidden" name="action" value="email">
   <input type="hidden" name="<?= Yii::$app->request->csrfParam; ?>" value="<?= Yii::$app->request->csrfToken; ?>" />
  </div>
 

</div>
  
    <?php
    $awal=($page==1) ? $page : (($page-1)*$limit)+1;
    $akhir=$page*$limit;
    if($akhir>$countKeranjang){$akhir=$countKeranjang;}
    if($countKeranjang!=0) echo"Menampilkan <b>".$awal." - ".$akhir."</b> dari <b>".$countKeranjang."</b> hasil <br> <br>";
    ?>    
<?php
    if(isset($_SESSION['catIDmerge'])){
?>
                        <div class="col-md-12">
                                
                                <input type="checkbox" onClick="toggle(this)"> Pilih semua &nbsp; &nbsp; &nbsp; 
                                <input type="button" id="popover" name="action" class="btn btn-default btn-xs navbar-btn" value="Email">  
                               <!-- <input type="button" name="action" class="btn btn-default btn-xs navbar-btn" id="PrintButton" value="PDF"> 
                                <input type="submit" name="action" class="btn btn-default btn-xs navbar-btn" value="Unduh Katalog"> -->
                                <input type="submit" name="action" class="btn btn-default btn-xs navbar-btn" value="Hapus"> 
                                <input type="submit" name="action"class="btn btn-default btn-xs navbar-btn" value="Kosongkankeranjang">
                                <?= Html::csrfMetaTags() ?>
                                <input type="hidden" name="<?= Yii::$app->request->csrfParam; ?>" value="<?= Yii::$app->request->csrfToken; ?>" />
                        </div>
                         </div>
    

    
<?php   
        ####untuk menghitung jumlah tmpilan di keranjang####
        ####default 10 item perpage##################
        $count=($countKeranjang<$offset*10) ? $countKeranjang : $offset*10;
        $start=$count-9;
        $start=($start<=0) ? 1 : $start=$count-9;

        for($start;$start<=$count;$start++){
        $pengarang=explode(";", $dataKeranjang[$start]['author'])
        //echo"<br>isi dari start adalah ".$start."dan isi dari page adalah".$page."dan isi dari count adalah".$count."<br>";
    ?>

    
   
        <div class="row">
            <table class="table table-striped">
            <tr><td>
                <div id="search<?= $dataKeranjang[$start]['id'];?>">
            <div class="col-sm-1"><?= $count*($page-1)+$start;?> &nbsp;
                 <input type="checkbox" name="catID[]" value="<?php echo $dataKeranjang[$start]['id'];?>"> &nbsp; 
                <div id="favourite<?=$dataKeranjang[$start]['id']?>">
                   <?php echo"<a onclick=\"favorite(".$dataKeranjang[$start]['id'].")\" href=\"javascript:void(0)\"  title=\"Tambah ke Favorite\"><span class=\"glyphicon glyphicon-star\"></span></a>";  ?>
                </div>
            </div>
            <div class="col-md-2"><img src="<?php echo $this->context->ConvertRealPath($dataKeranjang[$start]['CoverURL'],$dataKeranjang[$start]['worksheet']); ?>"></div>    
            <div class="col-md-9">
             <table class="table" style="background:transparent">
                <tr>
                  <th colspan="2">
                  <a href="" class="topnav-content"><?= $dataKeranjang[$start]['kalimat2'];?></a></th>
               </tr>
              <tr>
                  <td width="22%">Jenis Bahan</td>
                  <td width="78%"><?= $dataKeranjang[$start]['worksheet'];?></td>
              </tr>
              <?php
                      for($x=0;$x<sizeof($pengarang);$x++){
                      if($x==0){
                        echo"
                        <tr>
                        <td>Pengarang</td>
                        <td><a href=\"?action=pencarianSederhana&ruas=Pengarang&bahan=".$dataKeranjang[$start]['Worksheet_id']."&katakunci=".$dataKeranjang[$start]['author']."\"> ".$pengarang[$x]." </a></td>
                        </tr>
                        ";
                      } else
                      {

                       echo"
                        <tr>
                        <td></td>
                        <td><a href=\"?action=pencarianSederhana&ruas=Pengarang&bahan=".$dataKeranjang[$start]['Worksheet_id']."&katakunci=".$dataKeranjang[$start]['author']."\"> ".$pengarang[$x]." </a></td>
                        </tr>
                        ";
                      }

                      }
                      ?>              
                <tr>
                  <td>Penerbit</td>
                  <td>: <?= $dataKeranjang[$start]['PublishLocation']." : ".   $dataKeranjang[$start]['publisher']." ".$dataKeranjang[$start]['PublishYear'];?></td>
              </tr>
                <tr>
                  <td>Konten Digital</td>
                   <td>: <?php  if($dataKeranjang[$start]['KONTEN_DIGITAL']==NULL){$dataKeranjang[$start]['KONTEN_DIGITAL']="Tidak Ada Data";} else {echo"<a data-toggle='collapse' data-target='#collapseKontenDigital".$dataKeranjang[$i]['id']."'  class='show_hide' id='showmenu".$dataKeranjang[$start]['id']."' onclick='kontenDigital(".$dataKeranjang[$start]['id'].")' href='javascript:void(0)' >"; } echo $dataKeranjang[$start]['KONTEN_DIGITAL'];?> </td>
              </tr>
               <tr>
                  <td>Ketersediaan</td>
                  <td>: <?php if($dataKeranjang[$start]['ALL_BUKU']!=0){echo"<a data-toggle='collapse' data-target='#collapsecollection".$dataKeranjang[$start]['id']."'  id='showmenu".$dataKeranjang[$start]['id']."' onclick='collection(".$dataKeranjang[$start]['id'].")' href='javascript:void(0)' >"; } echo $dataKeranjang[$start]['JML_BUKU']." dari ".$dataKeranjang[$start]['ALL_BUKU']." ekslempar"; ?> </a></td>
              </tr>
              </table>

        </div>
        </div>

          </td> </tr> 
         </table>
                <div class="collapse" id="collapseKontenDigital<?= $dataKeranjang[$start]['id'];?>">
                <div id="kontenDigitalShow<?= $dataKeranjang[$start]['id'];?>">

                </div>
            </div>
            <br>
            <div class="collapse" id="collapsecollection<?= $dataKeranjang[$start]['id'];?>">
                <div id="collectionShow<?= $dataKeranjang[$start]['id'];?>">
                </div>
            </div>
        
               

        <?php
        }
        }

        ?>
  </form>
        <center>
        <nav>
                  <?php


          

        $total_records=$countKeranjang;
        $total_pages=ceil($total_records / $limit); 


        $perpage=10*$offset;
        $perpage=($perpage>$total_pages) ?  $total_pages : 10*$offset ;
        $startpage=$perpage-9;
        $startpage=($startpage<=0) ? 1 : $startpage=$perpage-9;

        echo"<ul class='pagination pagination-lg' >";
          
          if($startpage<=10) {
            echo"<li class=\"disable\"> </li>";
          } else 
          {
             echo"<li> <a href='?page=".($perpage-10)."&limit=".$limit."    '> &laquo;</a></li>" ;           
          }

        for ($startpage; $startpage<=$perpage; $startpage++) {                   
                    echo"<li";
                if ($page==$startpage){                    
                    echo" class='active'";                    
                }
                echo"> <a href='?page=".$startpage."&limit=".$limit."    '>".$startpage."</a></li>"; 
            } 
            if($perpage>= $total_pages){
                echo"<li class=\"disable\"> </li>";            } 
            else {            
                echo"<li> <a href='?page=".($perpage+1)."&limit=".$limit."  '> &raquo;</a></li>" ;          
                 }


        ?>
        </ul>
         </nav>
        </center>
        </div>
                                          
                   </div>
                        
                 </div>         
            </div>
          </div>
                <div class="row">&nbsp;</div>                    
      </section><!-- /.content -->





