<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\bootstrap\Button;
use yii\widgets\LinkPager;
use yii\helpers\Url;
use yii\web\View;
use yii\bootstrap\Modal;
use yii\widgets\Pjax;
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $cartContent: An array containing the shopping cart content */

?>
<div class='site-cart'>
<div class="jumbotron"><h2>Basket</h2></div><div class="container">
<?php 
 if(!empty($cartContent)){

echo "<table id='myTable3' class='tablesorter'>";
echo "<thead>";
echo '<tr>
		
		<th>Scientific Name</th>
		<th>Country</th>
		<th>IPEN No.</th>
		<th>Action</th>
		<th>Supp. info</th>
</tr></thead>';


echo "<tbody>";
function createLink($unitID, $collectionCode, $institutionCode){
	$ar = ['unitID'=>$unitID,'collectioncode'=> $collectionCode, 'institutioncode'=>$institutionCode];
	$nurl = Url::to ( array_merge ( [
			'search/record'
	], $ar ) );
	
	return $nurl;
}

foreach ( $cartContent as $key =>$item ) {
	
	$link = createLink($key,$item['collectioncode'],$item['institutioncode']);
	echo "<tr>";
	
	echo '<td >'.$item['ScientificName'].'</td>';
	echo '<td>'.$item['Country'].'</td>';
	echo '<td><a href="'.$link.'">'.$item['IPEN'].'</td>';
	echo '<td>';
	Pjax::begin([ 'enablePushState'=>false]);
	$unitid=$key;
	$collection_code=$item['collectioncode'];
	$institution_code=$item['institutioncode'];
	?>
	<?= Html::a("Delete", ['cart/deleteone?unit_id='.$unitid.'&collection_code='.$collection_code.'&institution_code='.$institution_code], ['class' => 'btn btn-primary','data-pjax'=>'w0','timeout'=>'100000']) ?>
	<?php Pjax::end();
	echo "</td>";
	if($item['cites']!=false){
	echo '<td><strong><font color="red">Cites protected!</font></strong></td>';
	}
	else{
		echo "<td> </td>";}
		
	echo "</tr>";
}
echo"</tbody></table>";

$form = ActiveForm::begin( [
		'options' => [
				'id' => 'edit-form'
		]
		]);?>
<div class="form-group" align="right">
<?php $user = Yii::$app->user->identity;?>
<?= Html::a('Empty basket', ['cart/delete'],
                   		 ['class' => 'btn  btn-primary', 'name' => 'delete-button', 'data-confirm'=>'Are you sure you want to delete all items from your cart?'])?>
<?php if ( $user->cites_code  =="(NULL)") {?>
                   <?= Html::button('Checkout', ['value' => Url::to(['cart/checkout']), 'title'=>'Cites code',
                   		 'name' => 'checkout-button', 'class'=>'showModalButton  btn  btn-success', 'align'=>'right'])?>
                   		<?php }else{?>
                   			<?= Html::a('Checkout',['cart/overview'],['class'=>' btn  btn-success', 'align'=>'right'])?>
                   		<?php }?>
                   
                </div>
                <?php 

           Modal::begin ( [
                		'headerOptions' => [
                				'id' => 'modalHeader'
                		],
                		'id' => 'modal',
                		'size' => 'modal-lg',
           		'footer'=> Html::submitButton('Cancel', ['class' => ' btn btn-primary', 'data-dismiss'=>'modal']),
	   						
                		// keeps from closing modal with esc key or by clicking out of the modal.
                		// user must click cancel or X to close
                		'clientOptions' => [
                				'backdrop' => 'static',
                				'keyboard' => FALSE
                		]
                ] );
                ?>
                      <div class="modalContent" id="modalContent"></div>
                	
                     
                <?php
                Modal::end ();
ActiveForm::end();

 }
 else{
 	echo "<h2>Your basket is empty!</h2>";
 }

 ?>
 </div>
 </div>
<script type="text/javascript">
$(document).on('click', '.showModalButton', function(){
        if ($('#modal').data('bs.modal').isShown) {
            $('#modal').find('#modalContent')
                    .load($(this).attr('value'));
            //dynamiclly set the header for the modal via title tag
            document.getElementById('modalHeader').innerHTML = '<h4>' + $(this).attr('title') + '</h4>';
        } else {
            //if modal isn't open; open it and load content
            $('#modal').modal('show')
                    .find('#modalContent')
                    .load($(this).attr('value'));
             //dynamiclly set the header for the modal via title tag
            document.getElementById('modalHeader').innerHTML = '<h4>' + $(this).attr('title') + '</h4>';
        }
        $('#modal').on('hidden.bs.modal', function () {
       	 location.reload();
       	})
    });  
$(document).ready(function(){
var oTable =$('#myTable3').DataTable( {
    "oLanguage": {
        "sSearch": "Filter basket:",
      },
      "paging":false,
      
    } );
});  
</script>