<?php

session_start();
require_once "../Config/session_check.php";

$_SESSION["last_Active"] = time();

$ID = "";
if (isset($_GET["ID"])) {
    $ID = $_GET["ID"];

} else {
    header("location: ../");
}


include "../Config/config.php";
$Table = "";


$SQL = "SELECT invoces.Invoice_ID, suppliers.NAME, invoces.Total_Quantity, invoces.Billed_Amount, invoces.Date_Ordered, invoces.Date_Recived
FROM invoces 
	LEFT JOIN suppliers ON invoces.Supplier_ID = suppliers.Supplier_ID
WHERE invoces.Invoice_ID = ?;";


if ($stmt = mysqli_prepare($link, $SQL)) {


    mysqli_stmt_bind_param($stmt, "s", $ID);

    if (mysqli_stmt_execute($stmt)) {

        // Store result

        mysqli_stmt_bind_result($stmt, $Invoice_ID, $NAME, $Total_Quantity, $Billed_Amount, $Date_Ordered, $Date_Recived);
        if (mysqli_stmt_fetch($stmt)) {
            $Table = '
                        <tr>
                        </tr>
                        <tr>
                            <th> Invoice ID </th>
                            <td  colspan="4">' . $Invoice_ID . '</td>
                        </tr>
                        <tr>
                        
                            <th>Business Name </th>
                            <td  colspan="4">' . $NAME . '</td>
                        </tr>
                        <tr>
                            <th> Total Items </th>
                            <td colspan="4">' . $Total_Quantity . '</td>
                        </tr>
                        <tr>
                            <th> Total billed </th>
                            <td  colspan="4">' . $Billed_Amount . '</td>
                        </tr>
                        <tr>
                            <th> Date Ordered </th>
                            <td  colspan="4">' . $Date_Ordered . '</td>                       
                        </tr>
                        <tr>
                            <th> Date Recived </th>
                            <td colspan="4">' . $Date_Recived . '</td>
                        </tr>
                        <tr>
                         <th colspan="5" class="text-center"> Items on Invoice</th>
                        </tr>
                        <tr>
                        <th>Product ID</th>
                        <th>Product Name</th>
                        <th>Quantity</th>
                        <th>Price</th>
                        <th>Edit</th>
                        </tr>
                ';
            mysqli_close($link);

            include "../Config/config.php";
            $SQL = "SELECT recived_stock.Recived_Stock_ID, recived_stock.Invoice_ID, recived_stock.Product_ID, products.Product_Name, recived_stock.Quantity, recived_stock.Price
FROM recived_stock 
	LEFT JOIN products ON recived_stock.Product_ID = products.Product_ID
WHERE recived_stock.Invoice_ID = '" . $ID . "';";

            //mysqli_stmt_bind_param($stmt, "s", $ID);
            $stmt = mysqli_query($link, $SQL);


            while ($row = mysqli_fetch_array($stmt)) {
                $Table = $Table . '<tr>
                        <td>' . $row["Product_ID"] . '</td>
                        <td>' . $row["Product_Name"] . '</td>
                        <td>' . $row["Quantity"] . '</td>
                        <td>' . $row["Price"] . '</td>
                        <td style="padding: 0;margin: 0;"><a class="btn btn-danger btn-block" style=" padding-bottom: 14px"  href="DeleteItem.php?ID=' . $ID . '&AddedItemID=' . $row["Recived_Stock_ID"] . '" >Delete</a></td>
                        </tr>';


            }

        }
    } else {
        echo "something went wrong, try again later.";
    }
}
$Table = $Table . "</tbody></table>";


?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include "../Templates/Header.php"; ?>
    <title>Dashboard</title>
</head>
<body>
<script>
    function chkclicked() {
        var state = document.getElementById("chkbox");
        if (state.checked) {
            document.getElementById("delBtn").disabled = false;
        } else {
            document.getElementById("delBtn").disabled = true;
        }
    }
</script>

<div id="page-container">

    <div id="content-wrap">
        <?php include "../Templates/navBar.php"; ?>
        <div class="container-fluid">
            <div class="row">
                <?php include "../Templates/SideBar.php"; ?>

                <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
                    <h1>Information:</h1>
                    <table class="table table-bordered">
                        <?php echo $Table; ?>
                    </table>

                </main>
            </div>
        </div>


    </div>
    <?php include "../Templates/Footer.php"; ?>

</div>

</body>
</html>
