<?php
session_start();
require_once('../includes/admin-security.php');
include_once('includes/config.php');

// Handle product submission
if (isset($_POST['submit'])) {
    $category = $_POST['category'];
    $subcat = $_POST['subcategory'];
    $productname = $_POST['productName'];
    $productcompany = $_POST['productCompany'];
    $productprice = $_POST['productprice'];
    $productpricebd = $_POST['productpricebd'];
    $productdescription = $_POST['productDescription'];
    $productscharge = $_POST['productShippingcharge'];
    $productavailability = $_POST['productAvailability'];

    // Handle file uploads
    $productimage1 = $_FILES["productimage1"]["name"];
    $productimage2 = $_FILES["productimage2"]["name"];
    $productimage3 = $_FILES["productimage3"]["name"];

    $extension1 = substr($productimage1, strlen($productimage1) - 4, strlen($productimage1));
    $extension2 = substr($productimage2, strlen($productimage2) - 4, strlen($productimage2));
    $extension3 = substr($productimage3, strlen($productimage3) - 4, strlen($productimage3));

    // Rename image files
    $imgnewfile1 = md5($productimage1 . time()) . $extension1;
    $imgnewfile2 = md5($productimage2 . time()) . $extension2;
    $imgnewfile3 = md5($productimage3 . time()) . $extension3;

    $addedby = $_SESSION['aid'];

    // Move uploaded files
    move_uploaded_file($_FILES["productimage1"]["tmp_name"], "productimages/" . $imgnewfile1);
    move_uploaded_file($_FILES["productimage2"]["tmp_name"], "productimages/" . $imgnewfile2);
    move_uploaded_file($_FILES["productimage3"]["tmp_name"], "productimages/" . $imgnewfile3);

    // Insert into database
    $sql = mysqli_query($con, "INSERT INTO products(category, subCategory, productName, productCompany, productPrice, productDescription, shippingCharge, productAvailability, productImage1, productImage2, productImage3, productPriceBeforeDiscount, addedBy) VALUES('$category', '$subcat', '$productname', '$productcompany', '$productprice', '$productdescription', '$productscharge', '$productavailability', '$imgnewfile1', '$imgnewfile2', '$imgnewfile3', '$productpricebd', '$addedby')");

    $_SESSION['msg'] = "Product added successfully!";
    header('location: manage-products.php');
    exit();
}
?>

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

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>Add Product - Admin</title>
    <link href="css/styles.css" rel="stylesheet" />
    <link href="css/admin-professional.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js"
        crossorigin="anonymous"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>



    <script>
        function getSubcat(val) {
            $.ajax({
                type: "POST",
                url: "get_subcat.php",
                data: 'cat_id=' + val,
                success: function (data) {
                    $("#subcategory").html(data);
                }
            });
        }
    </script>
</head>

<body class="sb-nav-fixed">
    <?php include_once('includes/header.php'); ?>

    <div id="layoutSidenav">
        <?php include_once('includes/sidebar.php'); ?>

        <div id="layoutSidenav_content">
            <main>
                <div class="container-fluid px-4">
                    <!-- Page Header -->
                    <div class="page-header mt-4">
                        <div>
                            <h1><i class="fas fa-plus-circle me-2"></i> Add New Product</h1>
                            <p>Fill in the details to add a new product to your store</p>
                        </div>
                    </div>

                    <!-- Form Card -->
                    <div class="form-card">
                        <form method="post" enctype="multipart/form-data">

                            <!-- Basic Information -->
                            <div class="form-section">
                                <div class="form-section-title">
                                    <i class="fas fa-info-circle"></i> Basic Information
                                </div>

                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label class="form-label">Category *</label>
                                        <select name="category" id="category" class="form-select"
                                            onChange="getSubcat(this.value);" required>
                                            <option value="">Select Category</option>
                                            <?php
                                            $query = mysqli_query($con, "SELECT * FROM category");
                                            while ($row = mysqli_fetch_array($query)) {
                                                ?>
                                                <option value="<?php echo $row['id']; ?>">
                                                    <?php echo $row['categoryName']; ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>

                                    <div class="col-md-6 mb-3">
                                        <label class="form-label">Subcategory *</label>
                                        <select name="subcategory" id="subcategory" class="form-select" required>
                                            <option value="">Select Subcategory</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label class="form-label">Product Name *</label>
                                        <input type="text" name="productName" placeholder="Enter product name"
                                            class="form-control" required>
                                    </div>

                                    <div class="col-md-6 mb-3">
                                        <label class="form-label">Company/Brand *</label>
                                        <input type="text" name="productCompany" placeholder="Enter brand name"
                                            class="form-control" required>
                                    </div>
                                </div>
                            </div>

                            <!-- Pricing & Availability -->
                            <div class="form-section">
                                <div class="form-section-title">
                                    <i class="fas fa-dollar-sign"></i> Pricing & Availability
                                </div>

                                <div class="row">
                                    <div class="col-md-4 mb-3">
                                        <label class="form-label">Price Before Discount *</label>
                                        <input type="number" step="0.01" name="productpricebd" placeholder="0.00"
                                            class="form-control" required>
                                    </div>

                                    <div class="col-md-4 mb-3">
                                        <label class="form-label">Selling Price *</label>
                                        <input type="number" step="0.01" name="productprice" placeholder="0.00"
                                            class="form-control" required>
                                    </div>

                                    <div class="col-md-4 mb-3">
                                        <label class="form-label">Shipping Charge *</label>
                                        <input type="number" step="0.01" name="productShippingcharge" placeholder="0.00"
                                            class="form-control" required>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-md-12 mb-3">
                                        <label class="form-label">Availability *</label>
                                        <select name="productAvailability" class="form-select" required>
                                            <option value="">Select Availability</option>
                                            <option value="In Stock">In Stock</option>
                                            <option value="Out of Stock">Out of Stock</option>
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <!-- Description -->
                            <div class="form-section">
                                <div class="form-section-title">
                                    <i class="fas fa-align-left"></i> Product Description
                                </div>

                                <div class="mb-3">
                                    <label class="form-label">Description</label>
                                    <textarea name="productDescription" placeholder="Enter detailed product description"
                                        class="form-control"></textarea>
                                </div>
                            </div>

                            <!-- Product Images -->
                            <div class="form-section">
                                <div class="form-section-title">
                                    <i class="fas fa-images"></i> Product Images
                                </div>

                                <div class="row">
                                    <div class="col-md-4 mb-3">
                                        <label class="form-label">Featured Image *</label>
                                        <input type="file" name="productimage1" class="form-control" accept="image/*"
                                            required>
                                    </div>

                                    <div class="col-md-4 mb-3">
                                        <label class="form-label">Image 2 *</label>
                                        <input type="file" name="productimage2" class="form-control" accept="image/*"
                                            required>
                                    </div>

                                    <div class="col-md-4 mb-3">
                                        <label class="form-label">Image 3 *</label>
                                        <input type="file" name="productimage3" class="form-control" accept="image/*"
                                            required>
                                    </div>
                                </div>
                            </div>

                            <!-- Submit Buttons -->
                            <div class="mt-4 d-grid gap-2 d-md-flex justify-content-md-end">
                                <button type="submit" name="submit" class="btn btn-primary px-4">
                                    <i class="fas fa-check me-2"></i> Add Product
                                </button>
                                <a href="manage-products.php" class="btn btn-light px-4">
                                    <i class="fas fa-times me-2"></i> Cancel
                                </a>
                            </div>
                        </form>
                    </div>
                </div>
            </main>
            <?php include_once('includes/footer.php'); ?>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
        crossorigin="anonymous"></script>
    <script src="js/scripts.js"></script>
</body>

</html>
