<?php 
session_start();
if(!isset($_SESSION['user_id'])){
    echo "<script> window.location.href = '/'; </script>";
}


?>
<div class="table-responsive">  
<table class="table table-borderless mb-0">
                    <thead style="background-color:#083fdb;">
                        <tr>
                        <th scope="col" style="font-weight:bold;font-size:12px;color:#ffffff;">Name / Price</th>
                        <th scope="col" style="font-weight:bold;font-size:12px;color:#ffffff;">Qty</th>
                        
                        <th scope="col" style="font-weight:bold;font-size:12px;color:#ffffff;">Dis</th>
                        <!--<th scope="col" style="font-weight:bold;font-size:16px;color:#ffffff;">Tax</th>-->
                        <th scope="col" style="font-weight:bold;font-size:12px;color:#ffffff;">Total</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    //session_start();
                    require_once 'db_connection.php';
                    $user_id = $_SESSION['user_id'];
                    $branch_id = $_SESSION['branch_id'];
                    $total_amount = 0;
                    $total_dis = 0;
                    if ($_SESSION['order_id'] != 0) {
                        $orderid = $_SESSION['order_id'];

                        ///	$sql = "SELECT SUM(pro_qty) AS pro_qty , tempid,  pro_dis,pro_price,proid,proname,cover  FROM mobiletemporarycart LEFT JOIN mobileproducts ON mobileproducts.prid=mobiletemporarycart.proid
                        //WHERE mobiletemporarycart.user_id='$user_id' AND mobiletemporarycart.hold=0 AND mobiletemporarycart.orderid=0 GROUP BY  mobiletemporarycart.proid,mobiletemporarycart.pro_dis   ORDER BY mobiletemporarycart.tempid  DESC";
                        $sql = "SELECT
    SUM(mobiletemporarycart.pro_qty) AS pro_qty,
    mobiletemporarycart.tempid,
    mobiletemporarycart.pro_dis,
    mobiletemporarycart.pro_price,
    mobiletemporarycart.proid,
	mobiletemporarycart.pro_name,
    mobileproducts.proname,
	mobileproducts.price,
	mobileproducts.prid,
    mobileproducts.discount1,
    mobileproducts.discount_type1,
    mobileproducts.tax1,
    mobileproducts.tax_type1,
	mobiletemporarycart.pro_discount,
    mobiletemporarycart.tax,
    mobiletemporarycart.tax_type,
    mobileproducts.cover,
	mobiletemporarycart.pro_dis_price,
    mobiletemporarycart.discount_type
FROM
    mobiletemporarycart
LEFT JOIN
    mobileproducts ON mobileproducts.prid = mobiletemporarycart.proid
WHERE
   mobiletemporarycart.user_id='$user_id' AND mobiletemporarycart.branch_id='$branch_id' AND mobiletemporarycart.hold = 0 AND mobiletemporarycart.orderid = '$orderid' OR mobiletemporarycart.orderid = '0'
GROUP BY
    mobiletemporarycart.tempid, mobiletemporarycart.pro_dis, mobiletemporarycart.pro_price, mobiletemporarycart.proid, mobileproducts.proname, mobileproducts.cover, mobileproducts.prid
ORDER BY
    mobiletemporarycart.tempid DESC";
                    } else {

                        $sql = "SELECT
    SUM(mobiletemporarycart.pro_qty) AS pro_qty,
    mobiletemporarycart.tempid,
    mobiletemporarycart.pro_dis,
    mobiletemporarycart.pro_price,
    mobiletemporarycart.proid,
	mobiletemporarycart.pro_name,
    mobileproducts.proname,
	mobileproducts.price,
	mobileproducts.prid,
    mobileproducts.discount1,
    mobileproducts.discount_type1,
    mobileproducts.tax1,
    mobileproducts.tax_type1,
	mobiletemporarycart.pro_discount,
    mobiletemporarycart.tax,
    mobiletemporarycart.tax_type,
    mobileproducts.cover,
	mobiletemporarycart.pro_dis_price,
    mobiletemporarycart.discount_type
FROM
    mobiletemporarycart
LEFT JOIN
    mobileproducts ON mobileproducts.prid = mobiletemporarycart.proid
WHERE
   mobiletemporarycart.user_id='$user_id' AND mobiletemporarycart.branch_id='$branch_id' AND mobiletemporarycart.hold = 0 AND mobiletemporarycart.orderid = 0
GROUP BY
    mobiletemporarycart.tempid, mobiletemporarycart.pro_dis, mobiletemporarycart.pro_price, mobiletemporarycart.proid, mobileproducts.proname, mobileproducts.cover, mobileproducts.prid
ORDER BY
    mobiletemporarycart.tempid DESC";
                    }

                    $result = $conn->query($sql);
                    $td = 0;
                    $total_row = 0;
                    $row = 0;
                    if ($result->num_rows > 0) {
                        $c = 1;
                        $qtyprice = 0;
                        $pro_discount = 0;
                        $pro_qty = 0;
                        $td = 0;
                        $total_dis = 0;
                        $pdistot = 0;
                        $pricetotal = 0;
                        $taxx = 0;
						$taxvaltot = 0;
                        while ($row = $result->fetch_assoc()) {
                            
                            $td = 0;
                            $pro_qty = 0;
                            $tempid = $row["tempid"];
                            $prid = $row["prid"];
                            $proid = $row["proid"];
                            $price = $row["price"];
                            $pro_name = $row["pro_name"];
                            $proname = $row["proname"];
                            $cover = $row["cover"];
                            $pro_dis = $row["pro_dis"];

                            $pro_dis_price = $row['pro_dis_price'];
                           
                           
                            if($row["pro_discount"]==0){
                                $discount_type = $row["discount_type1"];
                                $pro_discount = $row["discount1"];
                            }else{
                                if($row["pro_discount"]==-1){
                                    $discount_type = 0;
                                    $pro_discount = 0;
                                }else{
                                    $discount_type = $row["discount_type"];
                                    $pro_discount = $row["pro_discount"];
                                }
                                
                            }
                            if($row["tax"]==0){
                                $tax_type = $row["tax_type1"];
                                $tax = $row["tax1"];
                            }else{
                                if($row["tax"]==-1){
                                    $tax_type = 0;
                                    $tax = 0;
                                }else{
                                    $tax_type = $row["tax_type"];
                                    $tax = $row["tax"];
                                }
                                
                            }
                           

                            $pro_price = $row["pro_price"];
                            $pro_price1 = $row["pro_price"];
                            $pro_qty = $row["pro_qty"];
                            $total_row = $total_row + 1;



                            $productinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileproducts WHERE prid ='$prid'"));
                            //$discounted_price = ($pro_price / 100) * $pro_dis;
                            // $discounted_price = ($pro_price / 100) * $pro_discount;
                            //var_dump($pro_discount);

                            //$product_price = ($pro_price - $discounted_price) * $pro_qty;

                            $qtyprice = $pro_price * $pro_qty;
                            $pd = 0;
                            $pdis = 0;
                            $dis = 0;
                            $td = 0;
                            $sellprice = $productinfo['sellprice'];

                            if ($discount_type == 1) {


                                if ($sellprice != $pro_price) {
                                    if($proid==148){
                                        $pdis =  ($pro_price / 100) * $pro_discount;
                                       // $pd = $pdis * $pro_qty;
                                      //  $p = ($qtyprice / 100) * $pro_discount; //

                                      $pdis = $pdis *$pro_qty;
                                       // $product_price = $qtyprice - $p;
                                        //$product_price = $pro_price - $pd;

                                        $product_price = ($pro_price *$pro_qty) - $pdis;
                                       
                                    }else{
                                        $pdis =  ($pro_price / 100) * $pro_discount;
                                        $pd = $pdis;
                                        $p = ($qtyprice / 100) * $pro_discount; //


                                        $product_price = $qtyprice - $p;
                                        $product_price = $pro_price - $pd;

                                        $product_price = ($pro_price * $pro_qty)-($pdis*$pro_qty);
                                    }
                                    
                             } else {
                                    
                                    $pdis =  ($pro_price / 100) * $pro_discount;
                                    $pd = $pdis * $pro_qty;
                                    $p = ($qtyprice / 100) * $pro_discount;


                                    $product_price = $qtyprice - $p;
                                   //  $product_price = $pro_price-$pdis;
                                }
                            } else {


                                $products = mysqli_query($conn, "SELECT * FROM mobileproducts WHERE prid ='$prid'");
                                $prinfo = mysqli_fetch_array($products);
                                $productprice = $prinfo['sellprice'];
                                if ($sellprice != $pro_price) { ////
                                    if($proid==148){
                                        $pd = $pro_discount * $pro_qty;
                                        $product_price =  ($pro_price * $pro_qty) - $pd;
                                    }else{
                                       
                                        $pd = $pro_discount * $pro_qty;
                                        //$pd = $pro_discount;
                                        $product_price = ($pro_price * $pro_qty) - $pd;
                                    }
                                   
                                } else {
                                    $pd = $pro_discount * $pro_qty;
                                    $product_price =  ($pro_price * $pro_qty) - $pd;
                                }

                                // $product_price = $pro_price-$pro_discount;
                            }

                            //if($price != $pro_price){


                            // $product_price = $product_price;
                            // if($row["pro_dis_price"]==0){
                            //$product_price = $product_price;
                            //}else{
                            //	$product_price = $row["pro_dis_price"];
                            //}
                         //   $td = 0;
                        //    $totcoldis = 0;
                            $total_amount = $total_amount + $product_price;
                            if ($discount_type == 1) {
                               if ($sellprice != $pro_price) {
                            
                                if($proid==148){
                                     $total_dis = $total_dis + $pdis;
                                    $td =  $total_dis;

                                    
                                    }else{
                                        $total_dis = $total_dis + $pdis;
                                        $td =  $total_dis;
                                    }
                                } else {
                                    $total_dis = $total_dis + ($pdis * $pro_qty);
                                    $td =  $total_dis;
                                }
                            } else {
                                if ($sellprice != $pro_price) {
                                    $total_dis = $total_dis + ($pro_discount * $pro_qty);
                                    $td =  $total_dis;
                                } else {
                                    $total_dis = $total_dis + ($pro_discount * $pro_qty);
                                    $td =  $total_dis;
                                }
                            }
                            
                            if ($tax_type == 1) {
                                $taxval1 = ($pro_price/100) * $tax;
                                $taxval1 = $taxval1 * $pro_qty;
                            }else{
                                $taxval1 = $tax * $pro_qty;
                            }
                            $taxvaltot = $taxvaltot + $taxval1;
                            $product_price = $product_price + $taxval1;
                            //  $total_dis= $total_dis + ($pro_price-$row["pro_dis_price"]);
                            if ($pro_name == null) {
                                $pname = $proname;
                            } else {
                                $pname = $pro_name;
                            }


                            if ($pro_discount == 0) {
                                $dis = $pro_dis;
                            } else {
                                $dis = $pro_discount;
                            }

                           
                            if ($discount_type == 1) {
                                $dtype = "%";
                            } else {
                                $dtype = "";
                            }
                           
                           if($tax_type == 1){
                                $taxx = $taxval1." (".$tax."%".")";
                               
                            }else{
                                $taxx = $tax;
                            }
                           

                            $stockinfo = mysqli_query($conn, "SELECT * FROM tbl_stock WHERE prid ='$prid'");
                            $stock = mysqli_fetch_array($stockinfo);

                           $sqty = $stock['qty'];
                           $pdistot = $pdistot  + $pd;
                           $pricetotal = $pricetotal + $pro_price;
                    ?>
                                <tr style="height: 10px;font-weight: 700;" data-price="<?php echo $pro_price1; ?>" data-id="<?php echo $tempid; ?>" data-qty="<?php echo $sqty; ?>" talindex="0" onclick="tbrowclick(event);">
                                    <td>
                                    <?php  echo $pname;
                                        echo "<br>";
                                        echo "Rs.".$pro_price;
                                        ?>
                                        
                                    </td>
                                    <td>
                                    
                                    
                                        <input type="number" class="quantity" min="1" data-qty-id="<?php echo $tempid; ?>" value="<?php  echo $pro_qty; ?>" style="width:50px;" onchange="onChangeQuantity(this.value,<?php echo $tempid; ?>)">
                                    </td>
                                   <!-- <td>
                                    Rs.<?php  //echo $pro_price; 
                                        ?>
                                        
                                    </td>-->
                                   
                                    <td>
                                    <?php 
                                    if($discount_type == 1){
                                        if($proid==148){
                                           
                                            echo $pdis." (".$dis . $dtype.")";
                                        }else{
                                            echo ($pdis*$pro_qty)." (".$dis . $dtype.")";
                                        }
                                       
                                    }else{
                                        if($proid==148){
                                            echo ($dis*$pro_qty) . $dtype;
                                        }else{
                                            echo ($dis*$pro_qty) . $dtype;
                                        }
                                       
                                    }
                                    
                                        ?>
                                        
                                    </td>
                                 <!--   <td>
                                    Rs.<?php //echo $taxx; 
                                   
                                   ?>
                                    </td>-->
                                    <td>
                                    Rs.<?php 
                                    
                                        //if($prid==148){
                                         //   $product_price =  $pro_price * $pro_qty;
                                       // }else{
                                            $product_price =  $product_price;
                                      //  }
                                         
                                    
                                    echo number_format((float) $product_price, 2); ?>
                                        
                                    </td>
                                </tr>
                        <?php
                            $c++;
                        }
                    } else {
                        //echo "Not found items..";
                    }
                    ?>
                    </tbody>
                    <tfoot>
                        <tr>
                        </tr>
                       <!-- <tr>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td>Sub Total</td>
                            <td style=" ">$ <?php 
                                

                                 $alldis = 0;
                                 $t2 = $total_amount;
                                 $dis = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM all_discount_tbl WHERE all_discount_tbl_id ='1' AND status='1'")); 
                                if($dis['discount_type']==0){ 
                                     $alldis =  $t2-$dis['discount_val'];
                                     $dis2 =  $dis['discount_val'];
                                 }else{
                                     $disval = $dis['discount_val'];
                                     
                                     $dis2 =  ($t2/100)*$disval;
                                     
                                     $alldis = $t2-$dis2;
                                 }
                            
                            
                               //  echo number_format((float) $total_amount, 2);
                             ?></td>
                        </tr>-->
                        <tr>
                        
                            <td style="background-color: #08a306;font-weight: bold; font-size:14px;">Discount</td>
                            <td style="background-color: #08a306;font-weight: bold; font-size:14px;">Rs.<?php 
                               
                            
                           // $nt = $td* $pro_qty;
                            echo number_format((float) $td+$dis2, 2); ?>
							<input type="hidden" id="tddiscount" name="tddiscount" value="<?php echo number_format((float) $td+$dis2, 2); ?>">
							</td>
						
                         <!--<tr>
                       <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td style="background-color: #0910e7;font-weight: bold;">Tax</td>
                            <td style="background-color: #0910e7;font-weight: bold;">Rs.<?php 
                               
                             //  $tax = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM tax WHERE tax_id ='1' AND status='1'")); 
                             //  $taxval = 0;
							//   $taxt = 0;
							   $taxvaltot = 0;
                              $tott = $total_amount;
                             //    if(empty($tax)){
                              //      echo "0";
                              //   }else{
                              //      if($tax['tax_type']==0){
                               //         $taxt = $tax['tax'];
                               //         $tott = $tott + $tax['tax'];
                               //    }else{
                                      //  echo $tax['tax']."%";
                                //        $tval= ($tott /100) * $tax['tax'];
                                  //      $taxt = $tval;
                                  //      $tott = $tott + $tval;
                                  // }
                                // }
                                // echo number_format((float) $taxt+$taxvaltot, 2); 
                           
                            ?></td>
                        </tr>-->
                        
                            <td class="heighlight" style="background-color: #f50404;font-weight: bold;font-size:14px;">Sub Total</td>
                            <td class="heighlight" style="background-color: #f50404;font-weight: bold;font-size:14px;">Rs.<span id="total_amount_view"><?php echo number_format((float) $tott - $dis2 + $taxvaltot, 2); ?></span>
                            <input type="hidden" id="cashat" name="cashat" value="<?php echo number_format((float) $tott - $dis2 + $taxvaltot, 2); ?>">
                        </td>
                        </tr>
                    </tfoot>
                    </table>

</div>