<?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 */

?>
<main id="main">
        <section class="gradient-bar-thin" style="height: 1px;"></section>
        <div class="container pb-5">
            <div class="row">
                <div class="col">
                <h1>Your basket</h1>
<?php 
 if(!empty($cartContent)){
 	
 	?>
 	You will receive a confirmation email including a csv metadat file for requested material.
 	<table class="table table-striped table-bordered table-hover table-sm">	
					<thead>
             			   <tr>
                             <th>Scientific name</th>
                              <th>Country</th>
                     		  <th>IPEN</th>
                     		  <th>Accession Number</th>
								<th>Garden</th>
								<th>Material</th>
								<th>Action</th>
								<th>Supp. info</th>

                        </tr>
                        </thead>
                        <tbody>

<?php 
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>'.$item['unitaccessionnumber'].'</td>';
	echo '<td>'.$item['Institution'].'</td>';
	echo '<td>'.$item['Type'].'</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-warning','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-warning', 'name' => 'delete-button', 'data-confirm'=>'Are you sure you want to delete all items from your basket?'])?>
<?php if ( $user->cites_code  =="(NULL)") {?>
                   <?= Html::button('Checkout', ['value' => Url::to(['cart/checkout']), 'title'=>'Cites code',
                   		 'name' => 'checkout-button', 'class'=>'showModalButton  btn  btn-warning', '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-warning', '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>
        </div>
    </main>
                
<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>