<?php
session_start();
require_once('../connection.php');


if (!isset($_SESSION['userID'])) {
    header('Location: ../login/login-form.php');
    exit();
}

$first_name = $_SESSION['first_name'];
$email = $_SESSION['email'];

?>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>GLH | Producer Dashboard</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
    
    <style>
        
    .sidebar { 
        background-color: #3b3b3b;
        height: 100%;
        width: 260px;
        position: fixed;
        z-index: 1;
        top: 0;
        left: 0;
        overflow-x: hidden;
        padding-top: 20px;
    }


    .dash {
        padding: 20px;
        transition: margin-left .3s;
    }


    @media screen and (min-width: 768px) {
    .sidebar {
        display: block !important;
    }
    
    .dash {
        margin-left: 260px;
        padding-right: 20px; 
    }
    
    .sidebar-open, .sidebar-hidden {
        display: none !important;
    }
    }
        
    
    .logo {
        margin-left: 15px;
        margin-top: 10px; 
        margin-bottom: 30px; 
        display: block;    
    }
    
    hr.solid-top {
        border-top: 1px solid #bbb;
        border-radius: 5px;
        width: 90%;
        margin-left: 13px;
    }
    
    .sidebar-links {
        padding: 6px 8px 6px 16px;
        text-decoration: none;
        font-size: 14px; 
        display: block;
        border-color: transparent;
    }
    
    .sidebar-links a:hover {
        background-color: #90d5ff;
    }
    
    .sidebar-links button {
        background-color: transparent;
        color: white;
        border-color: transparent;
        border-radius: 3px;
    }
    
    .sidebar-links button:hover {
        background-color: #90d5ff;
        color: black;
    }
    
    hr.solid-bottom {
        border-top: 1px solid #bbb;
        border-radius: 5px;
        width: 90%;
        margin-left: 13px;
    }

    .user-profile {
        margin-top: 64.7vh;
    }
    
    .dash {
        padding-right: 260px;
        padding: 20px;
        transition: margin-left .3s;
    }

    .sidebar-open {
        background: transparent;
        border: none;
        color: black;
        font-size: 30px;
        transition: margin-left .3s;
    }

    .sidebar-hidden {
        background: transparent;
        border: none;
        color: white;
        font-size: 30px;
        float: right;
        margin-right: 8px;
        margin-top: -115px;
        transition: margin-left .3s;
    }
    
    
    

    </style>
</head>
<body>
    
    <header class="sidebar" id="sidebar">
        <div class="logo">
            <a href="../index.php">
                <img src="../glh.png" class="logo" alt="logo" width="150" height="60">
            </a>
        </div>
        
        <button class="sidebar-hidden" onclick="hideNav()"><i class="bi bi-x"></i></button>
        
        <hr class="solid-top">
        
        <div class="sidebar-links">
            <div class="home-page">
                <a href="../index.php">
                    <button><i class="bi bi-house"></i> Home</button>
                </a>
            </div>
            <div class="orders">
                <a href="orders.php"><button><i class="bi bi-receipt-cutoff"></i> Order History</button></a>
            </div>
            <div class="dashboard">
                <a href="dashboard.php"><button><i class="bi bi-laptop"></i> Dashboard</button></a>
            </div>
        </div>
            
            <hr class="solid-bottom">
            
            <div class="sidebar-links">
                <div class="user-profile">
                    <a href="account.php">
                        <button><i class="bi bi-person-exclamation"></i> Account Details</button>
                    </a>
                </div>
                <div class="logout">
                    <a href="logout.php">
                        <button><i class="bi bi-box-arrow-left"></i> Logout</button>
                    </a>
                </div>
            </div>
    </header>
    
    <script>
        function showNav() {
        document.getElementById("sidebar").style.display = "block";
        }

        function hideNav() {
        document.getElementById("sidebar").style.display = "none";
        }
    </script>
    
    <div class="dash">
        <button class="sidebar-open" onclick="showNav()"><i class="bi bi-list"></i></button>
        
    <div class="product-entry">
        <div class="form-card">
            <form method="POST" action="<?=$_SERVER['PHP_SELF']?>" enctype="multipart/form-data">
                
                <h3>Add a product to the market</h3><br>
                
                <input type="text" name="product_name" placeholder="Product Name" required><br><br>
                <input type="text" name="producer" placeholder="Producer Name" required><br><br>
                <input type="float" name="product_price" placeholder="Product Price" required><br><br>
                <input type="number" name="stock" placeholder="Stock" required><br><br>
                <input type="file" name="image_upload"><br><br>
                
                <input type="submit" class="submit-button" name="submit" value="Add Product"><br><br>
                
                
                <?php
            
                    if (isset($_POST['submit'])) {
                        $product_name = $_POST['product_name'];
                        $producer = $_POST['producer'];
                        $product_price = $_POST['product_price'];
                        $stock = $_POST['stock'];
                        
                        
                        $target_dir = "../product_images/";
                        $file_name = time() . "_" . basename($_FILES["image_upload"]["name"]); 
                        $target_file = $target_dir . $file_name;

                        $product_image = "/glh/product_images/$file_name";
        
                        $uploadOk = 1;
                        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));


                        if(isset($_POST["submit"])) {
                            $check = getimagesize($_FILES["image_upload"]["tmp_name"]);
                            if($check !== false) {
                                echo "File is an image - " . $check["mime"] . ".";
                                $uploadOk = 1;
                            } else {
                                echo "<p style='color: red; font-weight: bold;'>File is not an image.</p>";
                                $uploadOk = 0;
                                exit;
                            }
                        }


                        if (file_exists($target_file)) {
                            echo "<p style='color: red; font-weight: bold;'>Sorry, file already exists.</p>";
                            $uploadOk = 0;
                            exit;
                        }


                        if ($_FILES["image_upload"]["size"] > 500000) {
                            echo "<p style='color: red; font-weight: bold;'>Sorry, your file is too large.</p>";
                            $uploadOk = 0;
                            exit;
                        }


                        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
                        && $imageFileType != "gif" ) {
                            echo "<p style='color: red; font-weight: bold;'>Sorry, only JPG, JPEG, PNG & GIF files are allowed</p>";
                            $uploadOk = 0;
                            exit;
                        }


                        if ($uploadOk == 0) {
                            echo "<p style='color: red; font-weight: bold;'>Sorry, your file was not uploaded.</p>";
                            } else {
                                if (move_uploaded_file($_FILES["image_upload"]["tmp_name"], $target_file)) {
                                echo "The file ". htmlspecialchars( basename( $_FILES["image_upload"]["name"])). " has been uploaded.";
                            } else {
                                echo "<p style='color: red; font-weight: bold;'>Sorry, there was an error uploading your file.</p>";
                            }
                        }


                        try {
        
                            $stmt = $conn->prepare("INSERT INTO products (product_name, producer, product_price, stock, product_image) VALUES (?, ?, ?, ?, ?)");
                            $stmt->bind_param("ssdis", $product_name, $producer, $product_price, $stock, $product_image);
                    

                            if ($stmt->execute()) {
                                echo "<script>
                                    alert('You have successfully added your product to the market.');
                                    window.location.href = 'producer-dashboard.php';
                                    </script>";
                                exit();
                        
                            } else {
                                echo "<script>
                                    alert('There was an issue adding your product to the market.');
                                    window.location.href = 'producer-dashboard.php';
                                </script>";
                                exit();
                            }
                            } catch (Exception $e) {
                        } 
                    }
                
                
                
                ?>
                
                
                
                
                
                
                
                
                
                
            </form>
        </div>
    </div>
        
        
        
        
    </div>
    
    
    
</body>
</html>
