<?php require_once 'protect.php'; ?>
<?php if ($activenr==0) { ?>
<?php echo $textcartempty ?>
<?php } else {?>

<div class="divcarts bordercolor1">
<table class="table">
<tr class="tcolor">
<td>&nbsp;</td> 
<td><strong><?php echo $textitem ?></strong></td>
<td>&nbsp;</td>
<td><strong><?php echo $textquantity ?></strong></td>
<?php if ($inquiry==0) { ?>
<td><strong><?php echo $textprice ?> &#8364;</strong></td>
<td><strong><?php echo $texttotal ?> <?php echo $textprice ?> &#8364;</strong></td>
<td><strong><?php echo $textweight ?> <?php echo $unitweight ?></strong></td>
<?php } ?> 
</tr>
<?php
$result1 = $mysqli->query("SELECT * FROM cart WHERE cookie_id='$cookieid' AND item_id=ANY (SELECT insert_id FROM items WHERE publish='yes') GROUP BY active_id ORDER BY item_id, category_id, extra_id");
while($row = $result1->fetch_array()) {
$active_id=$row['active_id'];
$item_id=$row['item_id'];
$price=$row['price'];
$quantity=$row['quantity'];

$result2 = $mysqli->query("SELECT * FROM items WHERE insert_id=$item_id AND lang_id=$lang AND publish='yes'");
$row = $result2->fetch_array();
$id=$row['insert_id'];
$pageid=$row['page_id'];
$pageitem=$row['page'];
if ($itempage==1) { $siteitem=$pageitem; }
$pagesiteitem=$siteitem.".".$pageid;
$name=$row['name'];
$code=$row['code'];
$weight=$row['weight'];
$unit=$row['unit'];
$image=$row['image'];
if ($weight<1) { 
$weightcart = $weight*1000;
} else {
$weightcart = $weight;
}
?>
<form name="formcart" method="post">
<input type="hidden" name="activeid" value="<?php echo $active_id ?>">
<tr>
<td>
<?php if ($image==1) { ?>
<a href="<?php echo $pagesiteitem ?>"><img src="<?php echo $sitepath ?>includes/image.php?image=../images/items/<?php echo $id ?>.jpg&amp;nwidth=60" alt="<?php echo $name ?>" title="<?php echo $name ?>"></a>
<?php } ?>
</td> 
<td> 
<a href="<?php echo $pagesiteitem ?>"><?php echo $name ?></a> <br>
<span class="tstyle"> <?php echo $code ?> </span> 
</td>
<td> 
<!--specs -->
<?php
$result3 = $mysqli->query("SELECT * FROM cart WHERE active_id='$active_id' ORDER BY category_id");
while($row = $result3->fetch_array()) {	
$aid=$row['id'];
$scatid=$row['category_id'];
$extraid=$row['extra_id'];

$result4 = $mysqli->query("SELECT * FROM categories WHERE type=3 AND insert_id='$scatid' AND lang_id=$lang AND publish='yes' AND priority=1");
$row = $result4->fetch_array();
$scategory=$row['name'];

if (isset($_POST['update'.$active_id])) {
$activeid = $mysqli->real_escape_string(stripslashes($_REQUEST['activeid']));
$extraid = $mysqli->real_escape_string(stripslashes($_REQUEST["extraid$aid"]));
$quantity = $mysqli->real_escape_string(stripslashes($_REQUEST['quantity']));

if ($extraid) {
$result4b = $mysqli->query("SELECT * FROM extras WHERE type=1 AND insert_id=$extraid AND lang_id=$lang AND publish='yes'");
$row = $result4b->fetch_array();
$price=$row['price'];
}

if ($quantity > 0) {
$mysqli->query("UPDATE cart SET extra_id=$extraid WHERE id=$aid");
$mysqli->query("UPDATE cart SET price=$price WHERE id=$aid");
$mysqli->query("UPDATE cart SET quantity=$quantity WHERE active_id=$activeid");
}

}
?>

<?php if ($extraid>0) {?>
<select name="extraid<?php echo $aid ?>" class="formscart">
<option value="<?php echo $extraid ?>" class="color1"><?php echo $scategory ?></option>
<?php
$result5 = $mysqli->query("SELECT * FROM extras WHERE type=1 AND item_id=$item_id AND category_id='$scatid' AND lang_id=$lang ORDER BY priority");
while($row = $result5->fetch_array()) {	
$extraid1=$row['insert_id'];
$extra1=$row['name'];
$color=$row['color'];
?>
<option value="<?php echo $extraid1 ?>"<?php if ($extraid1==$extraid) { echo " selected"; }?>> 
<?php echo $extra1 ?>
</option>
<?php } ?>
</select>
<?php } ?>

<?php } ?>
<!--end specs -->

<?php 
$pprice = $quantity * $price;
$totalquantity += $quantity;
$totalprice += $pprice;
$tweight = $quantity * $weight;
$tweightcart = $quantity * $weightcart;
$totalweight += $tweight;
$totalweightcart += $tweightcart;
?>
</td>
<td> 
<input type="text" name="quantity" value="<?php echo $quantity ?>" size="4" maxlength="11" class="formscart inputssmall">
<?php if ($unit=='') { echo $siteunit; } else { echo $unit; } ?>
<button type="submit" name="update<?php echo $active_id ?>" class="buttonicon" title="<?php echo $buttonupdate ?>"><span class="icon fa-refresh"></span></button>

<button type="submit" name="delete<?php echo $active_id ?>" class="buttonicon colorgrey" title="<?php echo $buttoncancel ?>"><span class="icon fa-trash-o"></span></button>
</td>
<?php if ($inquiry==0) { ?>
<td> 
<?php echo $price ?>
</td>
<td><?php if ($decimalprice==1) { ?><?php printf ("%01.2f", $pprice); ?><?php }else{?><?php echo $pprice ?><?php } ?></td>
<td> 
<?php if ($tweightcart>0) { ?><?php echo $tweightcart ?> <?php } ?>
</td>
<?php } ?>
</tr>
</form>
<?php } ?> 
<tr class="tcolor">
<td>&nbsp;</td> 
<td> <strong><?php echo $texttotal ?></strong> </td>
<td>&nbsp;</td>
<td> &nbsp; <strong><?php echo $totalquantity ?></strong></td>
<?php if ($inquiry==0) { ?>
<td>&nbsp;</td>
<td><span class="price"><?php if ($decimalprice==1) { ?><?php printf ("%01.2f", $totalprice); ?><?php }else{?><?php echo $totalprice ?><?php } ?></span></td>
<td><?php if ($totalweightcart>0) { ?><span class="price"><?php echo $totalweightcart ?></span> <?php } ?> </td>
<?php } ?>
</tr>
</table>

<hr>
<div class="divcartcancel"> 
<form name="formcancel" method="post">
<button type="submit" name="deleteactive" class="buttonicon colorgrey" title="<?php echo $buttoncancel ?>"><span class="icon fa-trash-o"></span></button>
</form> 
</div>
<!--transport -->
<?php 
$result7 = $mysqli->query("SELECT id FROM transport WHERE startprice!='0.00'");
$nrrowstr = mysqli_num_rows($result7);
?>
<?php if($inquiry==0 AND $nrrowstr>0) { ?>
<div class="divtransport"> 
<?php echo $texttransportcost ?> 
<select name="tcost">
<?php
$result8 = $mysqli->query("SELECT * FROM transport WHERE publish='yes' ORDER BY priority, name");
while($row = $result8->fetch_array()) {	
$destination=$row['name'];
$startcost=$row['startprice'];
$kgcost=$row['kgprice'];

$totalkgcost = $kgcost * $totalweight;
if ($totalweight<$siteweight) { $totalkgcost=0; }
$totaltcost = $startcost + $totalkgcost;
$totaltcost = number_format($totaltcost,2);
?>
<option value="<?php echo $totaltcost ?>">
<?php echo $destination ?> &raquo; <?php echo $totaltcost ?> &#8364; 
</option>
<?php } ?>
</select>
<?php echo $texttransportcost2 ?>
</div>
<?php } ?>
<!--end transport -->
<div class="cleared"></div>
</div>

<div class="divcartbutton">
<div class="trigger"><div class="button buttonsubmit buttonorder"><span class="icon fa-check"></span> <?php echo $buttonorder ?></div></div>
<div class="toggle-container margtop20"><?php include 'cartlogin.php';?></div>
</div>

<hr>
<?php echo $textcart ?>

<?php } ?>