<?php
include 'include/chks.php';
if ($_SESSION['ulevel'] == 1) {
    include 'include/db1.php';
} elseif ($_SESSION['ulevel'] == 2) {
    include 'include/db2.php';
} elseif ($_SESSION['ulevel'] == 3) {
    include 'include/db3.php';
} elseif ($_SESSION['ulevel'] == 4) {
    include 'include/db4.php';
} elseif ($_SESSION['ulevel'] == 5) {
    include 'include/db5.php';
} else {
    include 'include/db6.php';
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $item_c = trim($_POST["item_c"]);
    $qty = trim($_POST["qty"]);
    $price = trim($_POST["price"]);
    $desc = trim($_POST["description"]);


    // check email exist or not
    $query = "SELECT item_c FROM Items WHERE item_c='$item_c'";
    $result = mysqli_query($conn, $query) or die(mysqli_error($conn));
    $count = mysqli_num_rows($result);
    if ($count == 0) {
        $query = "INSERT INTO Items(item_c,price,qty,description) VALUES ('$item_c','$price','$qty','$desc')";
        if (mysqli_query($conn, $query)) {
            echo "<p>" . $query . " <br/></p>";
            echo "Succsess <br/>";
            echo "<a href='itemadd.php'>Back</a>";
        } else {
            echo "<p>Error: " . $query . "<br>" . mysqli_error($conn) . "</p>";
        }
    } else {
        echo "<p>" . $query . " <br/></p>";
        echo "Failed: Duplicate item code <br/>";
        echo "<a href='itemadd.php'>Back</a>";
    }


} else {
    ?>
    <!DOCTYPE HTML>

    <html>
    <head>
        <title>Moratuwa Funiturers</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />

        <link rel="stylesheet" href="assets/css/main1.css" />

    </head>
    <body class="landing">

    <!-- Header -->
    <header id="header" class="alt">
        <nav id="nav">
            <ul>
                <li><a href="index.html">Home</a></li>
                <li><a href="cusadd.php">New Customer</a></li>
                <li><a href="orderadd.php" >Add Order</a></li>
                <li><a href="orderup.php">Update Order</a></li>
                <li class="dropdown active">
                    <a href="javascript:void(0)" class="dropbtn">Stock</a>
                    <div class="dropdown-content">
                        <a href="itemup.php">New stock</a>
                        <a href="itemadd.php">New Item</a>
                    </div>
                <li><a href="orderview.php">View Orders</a></li>
                <li><a href="itemview.php">View Items</a></li>
                <li class="dropdown">
                    <a href="javascript:void(0)" class="dropbtn">Admin</a>
                    <div class="dropdown-content">
                        <a href="register.php">Add user</a>
                        <a href="showroomadd.php">Add show room </a>
                        <a href="cusdel.php">Remove Customer</a>
                        <a href="cusview.php">View Customers </a>
                        <a href="cusup.php">Update customer info</a>
                        <a href="orderdel.php">Delete order</a>
                        <a href="orderview.php">View Orders</a>
                        <a href="itemdel.php">Delete Item</a>
                        <a href="showroomview.php">View Showrooms</a>
                        <a href="reports.php">Reports</a>


                    </div>
                <li style="float:right"><a href="logout.php?logout"><b>Log out! </b><?php echo $_SESSION['name']?></a></li>
            </ul>

        </nav>
    </header>

    <a href="#menu" class="navPanelToggle"><span class="fa fa-bars"></span></a>

    <!-- Banner -->
    <section id="banner">

    </section>

    <section id="four" class="wrapper style3 special">

    <div class="form">
        <link rel="stylesheet" href="css/forms.css">
        <form action="itemadd.php" method="post">
            New Item code
            <input type="text" name="item_c" placeholder="Item code..." required>
            QTY
            <input class="mini" type="number" name="qty" placeholder="Enter new QTY..." required><br>
            Price
            <input class="mini" type="number" name="price" placeholder="Price..." required><br>
            Description
            <input type="text" name="description" placeholder="Description" required>

            <input type="submit" value="Submit">
        </form>
    </div>

    </section>

    <!-- Scripts -->
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/skel.min.js"></script>
    <script src="assets/js/util.js"></script>
    <script src="assets/js/main.js"></script>
    </body>
    </html>
<?php }