<?php

session_start();

if (isset($_SESSION["security_Level"]) && $_SESSION["security_Level"] < 2) {
    require_once "../Config/session_check.php";
} else {
    header("location: ../index.php");
}
$_SESSION["last_Active"] = time();
require_once "../Config/config.php";
$supplier_IDs = '';
$ProductIDs = '';
$SQL = mysqli_query($link, "SELECT supplier_ID FROM suppliers");

while ($row = mysqli_fetch_array($SQL)) {
    $supplier_IDs = $supplier_IDs . "<option value='" . $row[0] . "'>" . $row[0] . "</option>";

}

$SQL = mysqli_query($link, "SELECT Product_ID FROM products");

while ($row = mysqli_fetch_array($SQL)) {
    $ProductIDs = $ProductIDs . '<option value="' . $row[0] . '">' . $row[0] . '</option>';

}


$SupplierID_err = $qty_err = $billed_err = $Ordered_err = $Received_err = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {


    if (empty(($_POST["supplier"]))) {
        $SupplierID_err = "Select a Username for the account.";
    } else {
        $supplierID = trim($_POST['supplier']);
    }


    $qty = trim($_POST['qty']);
    $billed = trim($_POST['billed']);
    $Ordered = trim($_POST['Ordered']);
    $Received = trim($_POST['Received']);

    if (empty($qty) || strlen($qty) > 10) {
        $qty_err = 'Please enter a valid name.';
    }

    if (empty($billed) || strlen($billed) > 17) {
        $billed_err = 'Please enter a valid phone number';
    }

    if (empty($Ordered)) {
        $Ordered_err = 'please enter a date of birth';
    }

    if (empty($Received)) {
        $Received_err = 'please enter a valid house number.';
    }
    echo $SupplierID_err;
    if (empty($SupplierID_err) && empty($qty_err) && empty($billed_err) && empty($Ordered_err) && empty($Received_err)) {

        $sql = "INSERT INTO invoces (Invoice_ID, Supplier_ID, Total_Quantity, Billed_Amount, Date_Ordered, Date_Recived) VALUES (?,?,?,?,?,?)";
        if ($stmt = mysqli_prepare($link, $sql)) {
            mysqli_stmt_bind_param($stmt, "ssssss", $invoiceID, $supplierID, $qty, $billed, $par_Ordered, $par_Received);

            $time = strtotime($Ordered);
            $nwFormat = date('Y-m-d', $time);
            $par_Ordered = $nwFormat;

            $time = strtotime($Received);
            $nwFormat = date('Y-m-d', $time);
            $par_Received = $nwFormat;


            $invoiceID = rand(1000000000, 9999999999);


            if (mysqli_stmt_execute($stmt)) {

                $checkitems = true;
                $checkitemsint = 0;
                while ($checkitems) {
                    if (isset($_POST[$checkitemsint])) {
                        $checkitemsint = $checkitemsint + 1;
                    } else {
                        $checkitems = false;
                    }


                }
                $sql = "INSERT INTO recived_stock (Invoice_ID, Product_ID, quantity, price) VALUES (?,?,?,?)";

                for ($i = 0; $i < $checkitemsint; $i++) {
                    if ($stmtW = mysqli_prepare($link, $sql)) {
                        $product_ID = trim($_POST['ProductID' . $i]);
                        $subQty = trim($_POST['subqty' . $i]);
                        $totalamount = trim($_POST['price' . $i]);

                        mysqli_stmt_bind_param($stmtW, "ssii", $invoiceID, $product_ID, $subQty, $totalamount);
                        if (mysqli_stmt_execute($stmtW)) {

                        } else {
                            echo 'fail';
                        }
                    }

                }
                header('location: ../invoices');


            } else {
                echo "something went wrong, please try again later";
            }
        }


    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include "../Templates/Header.php"; ?>
    <title>Dashboard</title>
</head>
<body>
<script>
    var itemtrack = 1;

    function loadnew(trigger) {

        $(trigger).hide();
        var clickfunction = "loadnew('#btn" + itemtrack + "')";

        $("#Items").append('       <div class="form-row">\n' +
            '                            <div hidden class="col">' +
            '                            <input id="' + itemtrack + '" name="' + itemtrack + '" value="' + itemtrack + '">' +
            '                            </div>\n' +
            '                            <div class="col">\n' +
            '                                <label for="ProductID' + itemtrack + '">Product ID</label>\n' +
            '                                <select class="form-control" id="ProductID' + itemtrack + '" name="ProductID' + itemtrack + '">\n' +
            '                                    <option value="" disabled selected>Select:</option>\n' +
            '                                     <?php echo $ProductIDs?>' +

            '                                </select>\n' +
            '                            </div>\n' +
            '                            <div class="col">\n' +
            '                                <label for="subqty' + itemtrack + '">quantity</label>\n' +
            '                                <input id="subqty' + itemtrack + '" name="subqty' + itemtrack + '" type="text"\n' +
            '                                       class="form-control"\n' +
            '\n' +
            '                                       placeholder="Enter quantity">\n' +
            '                            </div>\n' +
            '                            <div class="col">\n' +
            '                                <label for="price' + itemtrack + '">Total amount</label>\n' +
            '                                <input id="price' + itemtrack + '" name="price' + itemtrack + '" type="text"\n' +
            '                                       class="form-control"\n' +
            '\n' +
            '                                       placeholder="Enter Total amount">\n' +
            '                            </div>\n' +
            '                            <div class="col">\n' +
            '                                <label for="price" style="color: #ffffff">T</label>\n' +
            '                                <input ID="btn' + itemtrack + '" type="button" class="btn btn-block btn-success" value="add another item" onclick="' + clickfunction + '";">\n' +
            '                            </div>\n' +
            '                        </div>');

        itemtrack++;

    }

    function wow() {
        alert('well i can go here.')
    }


</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>Add new employee</h1>
                    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                        <div class="form-group">
                            <label for="supplier">supplier ID</label>
                            <select class="form-control" id="supplier" name="supplier">
                                <option value="" disabled selected>Select:</option>
                                <?php
                                echo $supplier_IDs;

                                ?>
                            </select>
                        </div>
                        <div class="form-row">
                            <div class="col">
                                <label for="qty">Total Quantity</label>
                                <input id="qty" name="qty" type="email"
                                       class="form-control <?php echo (!empty($qty_err)) ? 'is-invalid' : ''; ?>"
                                       placeholder="Enter quantity">
                                <span><?php echo "<p class='error'>" . $qty_err . "</p>"; ?></span>
                            </div>
                            <div class="col">
                                <label for="billed">Amount Billed</label>
                                <input id="billed" name="billed" type="text"
                                       class="form-control <?php echo (!empty($billed_err)) ? 'is-invalid' : ''; ?>"

                                       placeholder="Enter Amount Billed">
                                <span><?php echo "<p class='error'>" . $billed_err . "</p>"; ?></span>
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="col">
                                <label for="Ordered">Date Ordered</label>
                                <input id="Ordered" name="Ordered"
                                       class="form-control <?php echo (!empty($Ordered_err)) ? 'is-invalid' : ''; ?>>"
                                       type="date" value=""
                                <span><?php echo "<p class='error'>" . $Ordered_err . "</p>"; ?></span>
                            </div>
                            <div class="col">
                                <label for="Received">Date Received</label>
                                <input id="Received" name="Received"
                                       class="form-control <?php echo (!empty($Received_err)) ? 'is-invalid' : ''; ?>>"
                                       type="date" value=""
                                <span><?php echo "<p class='error'>" . $Received_err . "</p>"; ?></span>
                            </div>
                        </div>
                        <h4>Items delivered:</h4>
                        <div class="form-row">
                            <div class="col" hidden>
                                <input id="0" name="0" value="0">
                            </div>
                            <div class="col">
                                <label for="ProductID0">Product ID</label>
                                <select class="form-control" id="ProductID0" name="ProductID0">
                                    <option value="" disabled selected>Select:</option>
                                    <?php
                                    echo $ProductIDs;

                                    ?>
                                </select>
                            </div>
                            <div class="col">
                                <label for="subqty0">quantity</label>
                                <input id="subqty0" name="subqty0" type="text"
                                       class="form-control"

                                       placeholder="Enter quantity">
                            </div>
                            <div class="col">
                                <label for="price0">Total amount</label>
                                <input id="price0" name="price0" type="text"
                                       class="form-control"
                                       placeholder="Enter Total amount">
                            </div>
                            <div class="col">
                                <label for="price" style="color: #ffffff">T</label>
                                <input id="btn0" type="button" class="btn btn-block btn-success"
                                       value="add another item"
                                       onclick="loadnew('#btn0');">
                            </div>
                        </div>
                        <span ID="Items">

                        </span>
                        <div class="form-group">
                            <input type="submit" class="btn btn-danger btn-block" value="Create Invoice">
                        </div>
                    </form>

                </main>
            </div>
        </div>


    </div>
    <?php include "../Templates/Footer.php"; ?>

</div>

</body>
</html>
