<?php
session_start();
include 'db_connection.php';
//$user_id = $_SESSION["user_id"];
$receiptno = $_GET['receiptno'];
$orderinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileorders WHERE order_id='$receiptno'"));
$userid = $orderinfo['user_id'];
$customer = $orderinfo['customer'];
$pay_option = $orderinfo['pay_option'];

$userinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileusers WHERE user_id='$userid'"));
$sql = "SELECT * FROM mobileorders WHERE order_id = '$receiptno'";

$result = $conn->query($sql);
$discount = 0;
$dis_type = 0;
$paid_amount = 0;
if ($result->num_rows > 0) {

    while ($row = $result->fetch_assoc()) {
        $paid_amount = $row["paid_amount"];
        $orid = $row["orid"];
        $create_at = $row["create_at"];
        $pay_option = $row["pay_option"];
        $amount = $row["amount"];
        $discount = $row["discount"];
        $dis_type = $row["dis_type"];
    }
}

$sql = "SELECT * FROM mobiletemporarycart WHERE orderid = '$receiptno'";
$result = $conn->query($sql);

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Invoice</title>
    <style>
        @page {
            size: 10.4cm 10cm landscape;
            font-size: 15px;
        }

        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }

        .container {
            width: 528px;
            height: 529.13;
            margin: 0 auto;
            padding: 10px;
            box-sizing: border-box;
            padding-left: 57px;
            padding-right: 57px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 4px;
        }

        tbody {
            td {
                font-size: 15px;
            }
        }

        tfoot {
            td {
                font-size: 15px;
            }
        }

        th,
        td {
            padding: 5px;
            text-align: left;
            font-size: 10px;

        }

        img {
            width: 90px;
            height: auto;
            margin-right: 10px;
        }

        p {
            font-size: 12px;
            margin: 0;
        }

        .center {
            text-align: center;
        }
        .header{
            display: flex;
            justify-content: space-between;
            height: 94.5px;
        }
        .headerbox{
           
            display: block;
            width: 99%;
            height: 37.79px;
            
           padding: 2px;
            margin-bottom: -1px;
            
        }
        .headerbox p{
            height: 100%;
           
            line-height: 36px;
            padding-left: 12px;
            font-weight: 500;
        }
        
        thead{
            
            height: 37.79px;
        }
          
            tbody{
                height: 207.87px;
            }
            tfoot{
                height: 75.59px;
                width: 408.64px;
                display: block;
            }
            tfoot tr{
                height: 24.94px;
            }
            tfoot td{
               
            }
            thead, tbody {
               
                display: block;
            }
    </style>
</head>

<body>
    <div class="container">
        <div style="width:100%;">
            
            <div class="header">
                
            </div>
        </div>
       
        <div class="headerbox"><p style="font-size:16px; padding-left:75.59px;"><?php echo $orid; ?></p></div>
        <div class="headerbox"><p style="font-size:16px; padding-left:113.38px;"><?php echo $create_at; ?></p></div>
        <div class="headerbox"><p style="font-size:16px; padding-left:113.38px;"></p></div>
       
        <table>
            <thead>
               
              
            </thead>
            <tbody>
                <?php
                // $total_amount = 0;
                // $dis=0;
                $total_amount = 0;
                $discounted_amount = 0;
                $rowCount = 0;
                if ($result->num_rows > 0) {
                    $distot = 0;
                    while ($row = $result->fetch_assoc()) {
                        $rowCount = $rowCount + 1;
                        $sql_products = "SELECT * FROM mobileproducts WHERE prid = " . $row['proid'];
                        $result_products = $conn->query($sql_products);
                        if ($result_products->num_rows > 0) {
                            $product = $result_products->fetch_assoc();
                            if ($row['pro_name'] == null) {
                                $proname = $product["proname"];
                            } else {
                                $proname = $row['pro_name'];
                            }
                        }
                        if ($row['discount_type'] == 1) {
                            $dismark = "%";
                        } else {
                            $dismark = "";
                        }
                        // $pro_price = number_format((float)$row["pro_price"], 2);
                        // $pro_qty = $row["pro_qty"];
                        // $pro_discount=number_format((float)$row["pro_discount"], 2);
                        // $product_price = number_format((float)$pro_price * $pro_qty, 2);
                        // $total_amount += $product_price;
                        //$pro_price = number_format((float)$row["pro_price"], 2);
                        $pro_price = $row["pro_price"];
                        $pro_qty = $row["pro_qty"];
                        $pro_discount = $row["pro_discount"];

                        if ($row['discount_type'] == 1) {
                            //  $pdis =  ($pro_price * $pro_qty)/100*$pro_discount;

                            $pdis =  ($pro_price / 100 * $pro_discount) * $pro_qty;
                            // $product_price = number_format((float)($pro_price * $pro_qty)-$pdis, 2);
                            $product_price = ($pro_price * $pro_qty) - $pdis;
                            $distot +=  $pdis;
                        } else {

                            // $product_price = number_format((float)($pro_price * $pro_qty)-$pro_discount,2);

                            $product_price = ($pro_price * $pro_qty) - ($pro_discount * $pro_qty);
                            $distot += $pro_discount * $pro_qty;
                        }

                        $total_amount += $product_price;
                        $pro_price = number_format((float)$row["pro_price"], 2);
                        $product_price = number_format((float)$product_price, 2);
                      
                        echo "<tr>";
                        echo "<td style='font-size:16px;width:207.87px;'><strong>$proname</strong></td>";
                        echo "<td style='font-size:16px; text-align:center; width:75.59px;'>$pro_price</td>";
                        echo "<td style='font-size:16px; text-align:center; width:45.35px;'>$pro_qty</td>";
                       
                        echo "<td style='text-align: center; font-size:16px; text-align:right; width:90.70px;'>$product_price</td>";
                        echo "</tr>";
                        $total_discount_amount += ($pro_discount * $pro_qty);
                    }
                }
                $discounted_amount = $total_discount_amount;

                if ($dis_type == 1) {
                    $discounted_amount = ($paid_amount / 100) * $discount;
                } else {
                    $discounted_amount = $discount;
                }

                $bill_amount = $total_amount - $discounted_amount;
                $balance_amount = $paid_amount - $bill_amount;
                $discount = ($discounted_amount * 100) / $total_amount;
                $t =  $total_amount + $discounted_amount;

                ?>
            </tbody>
            <tfoot>
                
               
                <tr>
                    <td style="width:207.87px;"></td>
                    <td style=""></td>
                    <td style="width: 215px;"></td>
                    <td style="font-size: 20px; width: 113.38px; text-align:right;"><strong>Rs.<?php echo number_format((float)$t, 2); ?></strong></td>
                </tr>
                <tr>
                    <td style="width:207.87px;"></td>
                    <td style=""></td>
                    <td style="width: 215px;"></td>
                    <td style='font-size:16px; width: 113.38px;'></td>
                </tr>
                
                <tr>
                <td style="width:207.87px;"></td>
                <td style=""></td>
                    <td style="width: 215px;"></td>
                    <td style='font-size:16px; width: 113.38px; text-align:right;'>Rs.<?php echo number_format((float)$bill_amount, 2); ?></td>
                </tr>
                
                
                
            </tfoot>
        </table>

       
    </div>
    <!-- JavaScript to trigger print functionality -->
    <script>
        window.onload = function() {
            window.print();
        };
    </script>
</body>

</html>

<?php
$conn->close();
?>