<?php
include "db.php";

if(isset($_POST['submit'])){
    $product_name = $_POST['product_name'];
    $product_type = $_POST['product_type'];
    $color = $_POST['color'];
    $size_length = $_POST['size_length'];
    $size_width = $_POST['size_width'];
    $size_height = $_POST['size_height'];
    $weight = $_POST['weight'];
    $price_per_piece = $_POST['price_per_piece'];
    $stock_quantity = $_POST['stock_quantity'];
    $vehicle = $_POST['vehicle'];

    // Image upload
    $image_path = null;
    if(isset($_FILES['image_url']) && $_FILES['image_url']['error'] == 0){
        $target_dir = "uploads/";
        if(!is_dir($target_dir)) mkdir($target_dir, 0777, true);

        $image_name = time() . "_" . basename($_FILES["image_url"]["name"]);
        $target_file = $target_dir . $image_name;

        if(move_uploaded_file($_FILES["image_url"]["tmp_name"], $target_file)){
            $image_path = $target_file;
        } else {
            $error = "Image upload failed.";
        }
    }

    // Insert into DB
    $sql = "INSERT INTO products (product_name, product_type, color, size_length, size_width, size_height, weight, price_per_piece, stock_quantity, image_url, vehicle)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    $stmt = $conn->prepare($sql);
    // Fixed types mismatch profile definition mapping string alignment: sssddddidss
    $stmt->bind_param("sssddddidss", $product_name, $product_type, $color, $size_length, $size_width, $size_height, $weight, $price_per_piece, $stock_quantity, $image_path, $vehicle);

    if($stmt->execute()){
        $success = "Product added successfully!";
    } else {
        $error = "Error: " . $stmt->error;
    }
    $stmt->close();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Brick Product - Tameer Al Madina</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

<style>
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    background: #F4F6F9;
    font-family: 'Poppins', sans-serif;
    min-height: 100vh;
}

.main-layout {
    display: flex;
    width: 100%;
    min-height: 100vh;
    position: relative;
}

/* Master Sidebar Fixed Allocations */
aside, .sidebar, [class*="sidebar"] {
    width: 260px;
    min-width: 260px;
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    transition: all 0.3s ease;
}

.content-wrapper {
    flex: 1;
    margin-left: 260px;
    display: flex;
    flex-direction: column;
    background: #F4F6F9;
    min-width: 0;
    width: calc(100% - 260px);
    transition: all 0.3s ease;
}

/* Top Navbar Frame */
.top-navbar {
    height: 60px;
    background: #FFFFFF;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 30px;
    border-bottom: 1px solid #EAEAEA;
}

.navbar-left-flex {
    display: flex;
    align-items: center;
    gap: 15px;
}

.mobile-menu-toggle {
    display: none;
    font-size: 20px;
    color: #0A192F;
    cursor: pointer;
    background: #F4F6F9;
    width: 38px;
    height: 38px;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    border: 1px solid #E2E8F0;
}

.search-container {
    position: relative;
    width: 300px;
}

.search-container input {
    width: 100%;
    padding: 8px 15px 8px 40px;
    background: #F8F9FA;
    border: 1px solid #E2E8F0;
    border-radius: 8px;
    font-size: 13px;
    outline: none;
}

.search-container i {
    position: absolute;
    left: 15px;
    top: 50%;
    transform: translateY(-50%);
    color: #A0AEC0;
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 20px;
}

.profile-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

.profile-info i.profile-icon {
    font-size: 26px;
    color: #4A5568;
    background: #F4F6F9;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: 1px solid #E2E8F0;
}

.profile-text h4 {
    font-size: 13px;
    color: #1A202C;
    font-weight: 600;
}

.profile-text p {
    font-size: 11px;
    color: #718096;
}

/* Page Form Container */
.page-container {
    padding: 20px 30px;
    flex: 1;
    display: flex;
    flex-direction: column;
}

.form-card {
    background: #FFFFFF;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
    overflow: hidden;
    width: 100%;
    display: flex;
    flex-direction: column;
    flex: 1;
}

form {
    display: flex;
    flex-direction: column;
    flex: 1;
}

.form-banner {
    background: linear-gradient(135deg, #0A192F 0%, #172A45 100%);
    padding: 15px 30px;
    color: #FFFFFF;
    border-bottom: 4px solid #0d6efd;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.banner-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.banner-icon {
    background: rgba(255, 255, 255, 0.1);
    width: 38px;
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    font-size: 16px;
}

.banner-text h2 {
    font-size: 18px;
    font-weight: 600;
}

.banner-text p {
    font-size: 11px;
    color: #A0AEC0;
}

.banner-right-text {
    font-size: 18px;
    font-weight: 700;
    color: rgba(255, 255, 255, 0.15);
}

.section-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 18px 30px 10px 30px;
    font-size: 14px;
    font-weight: 600;
    color: #0A192F;
}

.section-header i {
    color: #0d6efd;
    font-size: 16px;
}

/* Master Form Blueprint Grid */
.form-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr); 
    gap: 15px 25px;
    padding: 0 30px;
}

.full-width {
    grid-column: 1 / -1;
}

.form-group {
    display: flex;
    flex-direction: column;
}

.form-group label {
    font-size: 12px;
    font-weight: 500;
    color: #2D3748;
    margin-bottom: 6px;
}

.input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.input-wrapper i {
    position: absolute;
    left: 15px;
    color: #A0AEC0;
    font-size: 13px;
    z-index: 2;
}

.input-wrapper input,
.input-wrapper select {
    width: 100%;
    height: 38px;
    border: 1px solid #E2E8F0;
    border-radius: 6px;
    padding: 5px 15px 5px 40px;
    font-size: 13px;
    color: #4A5568;
    background: #FFFFFF;
    outline: none;
}

.input-wrapper input:focus,
.input-wrapper select:focus {
    border-color: #0d6efd;
    box-shadow: 0 0 0 2px rgba(13, 110, 253, 0.1);
}

/* Alert Notification Framework */
.alert-container {
    padding: 15px 30px 0 30px;
}

.alert {
    padding: 10px 15px;
    border-radius: 6px;
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.alert-success { background: #E6FFFA; border: 1px solid #B2F5EA; color: #00875A; }
.alert-error { background: #FFF5F5; border: 1px solid #FED7D7; color: #E53E3E; }

/* System Action Triggers Control */
.form-actions {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 12px;
    padding: 20px 30px 25px 30px;
    margin-top: auto; 
    border-top: 1px solid #F0F2F5;
}

.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 22px;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s ease;
}

.btn-cancel { background: #EDF2F7; color: #4A5568; }
.btn-cancel:hover { background: #E2E8F0; }
.btn-submit { background: #0d6efd; color: #FFFFFF; }
.btn-submit:hover { background: #0a58ca; }

.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.4);
    z-index: 998;
}

@media (min-width: 992px) {
    body { overflow: hidden; }
    .page-container { height: calc(100vh - 60px); }
}

@media (max-width: 1024px) and (min-width: 769px) {
    aside, .sidebar, [class*="sidebar"] { width: 240px; min-width: 240px; }
    .content-wrapper { margin-left: 240px; width: calc(100% - 240px); }
}

@media (max-width: 768px) {
    body { overflow-y: auto !important; }
    .mobile-menu-toggle { display: inline-flex; }
    aside, .sidebar, [class*="sidebar"] { left: -260px !important; }
    aside.active, .sidebar.active, [class*="sidebar"].active { left: 0 !important; }
    .content-wrapper { margin-left: 0 !important; width: 100% !important; }
    .top-navbar { padding: 0 15px; }
    .search-container { width: 140px; }
    .page-container { padding: 10px; }
    .form-banner { padding: 12px 15px; }
    .banner-right-text { display: none; }
    .section-header { padding: 15px 15px 8px 15px; }
    .form-grid { grid-template-columns: 1fr !important; padding: 0 15px; gap: 12px; }
    .form-actions { padding: 15px; flex-direction: column-reverse; }
    .btn { width: 100%; justify-content: center; }
}
</style>
</head>
<body>

<div class="sidebar-overlay" id="sidebarOverlay" onclick="toggleMobileSidebar()"></div>

<div class="main-layout">
    
    <?php 
    $sidebar_path = "/home/tameeralmadina/public_html/TAM/sidebar";
    if (file_exists($sidebar_path)) {
        include $sidebar_path;
    } elseif (file_exists($sidebar_path . ".php")) {
        include $sidebar_path . ".php";
    }
    ?>

    <div class="content-wrapper">
        
        <div class="top-navbar">
            <div class="navbar-left-flex">
                <div class="mobile-menu-toggle" onclick="toggleMobileSidebar()">
                    <i class="fa-solid fa-bars"></i>
                </div>
                <div class="search-container">
                    <i class="fa-solid fa-magnifying-glass"></i>
                    <input type="text" placeholder="Search here...">
                </div>
            </div>
            
            <div class="user-profile">
                <div class="profile-info">
                    <i class="fa-solid fa-circle-user profile-icon"></i>
                    <div class="profile-text">
                        <h4>Rubab Admin</h4>
                        <p>Administrator</p>
                    </div>
                </div>
            </div>
        </div>

        <div class="page-container">
            <div class="form-card">
                
                <div class="form-banner">
                    <div class="banner-left">
                        <div class="banner-icon">
                            <i class="fa-solid fa-cubes"></i>
                        </div>
                        <div class="banner-text">
                            <h2>Add New Product Entry</h2>
                            <p>Register new inventory variants, structural weight allocations and metrics logs</p>
                        </div>
                    </div>
                    <div class="banner-right-text">Tameer Al Madina</div>
                </div>

                <form method="POST" enctype="multipart/form-data">
                    
                    <div style="overflow-y: auto; flex: 1; padding-bottom: 15px;">
                        
                        <?php if(isset($success)) { ?>
                            <div class="alert-container">
                                <div class="alert alert-success">
                                    <i class="fa-regular fa-circle-check"></i> <?= $success ?>
                                </div>
                            </div>
                        <?php } ?>

                        <?php if(isset($error)) { ?>
                            <div class="alert-container">
                                <div class="alert alert-error">
                                    <i class="fa-solid fa-triangle-exclamation"></i> <?= $error ?>
                                </div>
                            </div>
                        <?php } ?>

                        <div class="section-header">
                            <i class="fa-solid fa-circle-info"></i> Basic Structural Identifiers
                        </div>

                        <div class="form-grid">
                            <div class="form-group full-width">
                                <label>Product Identity Label / Name</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-tag"></i>
                                    <input type="text" name="product_name" placeholder="e.g. Premium Clay Brick A-Grade">
                                </div>
                            </div>

                            <div class="form-group">
                                <label>Product Type Category Classification</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-layer-group"></i>
                                    <select name="product_type">
                                        <option value="Clay">Clay</option>
                                        <option value="Fly Ash">Fly Ash</option>
                                        <option value="Concrete">Concrete</option>
                                    </select>
                                </div>
                            </div>

                            <div class="form-group">
                                <label>Color Appearance Tint</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-palette"></i>
                                    <input type="text" name="color" placeholder="e.g. Classic Terracotta Red">
                                </div>
                            </div>

                            <div class="form-group">
                                <label>Vehicle</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-truck"></i>
                                    <input type="text" name="vehicle" placeholder="e.g. Truck / Loader / Rickshaw">
                                </div>
                            </div>
                        </div>

                        <div class="section-header">
                            <i class="fa-solid fa-ruler-combined"></i> Dimensional Scale Metrics & Weights
                        </div>

                        <div class="form-grid">
                            <div class="form-group">
                                <label>Length Metric Scale (cm)</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-arrows-left-right"></i>
                                    <input type="number" step="0.01" name="size_length" placeholder="0.00">
                                </div>
                            </div>

                            <div class="form-group">
                                <label>Width Metric Scale (cm)</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-arrows-up-down"></i>
                                    <input type="number" step="0.01" name="size_width" placeholder="0.00">
                                </div>
                            </div>

                            <div class="form-group">
                                <label>Height Metric Scale (cm)</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-arrows-expand"></i>
                                    <input type="number" step="0.01" name="size_height" placeholder="0.00">
                                </div>
                            </div>

                            <div class="form-group">
                                <label>Net Unit Weight Profile (kg)</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-weight-hanging"></i>
                                    <input type="number" step="0.01" name="weight" placeholder="0.00">
                                </div>
                            </div>
                        </div>

                        <div class="section-header">
                            <i class="fa-solid fa-calculator"></i> Valuations & Initial Logistics Stock
                        </div>

                        <div class="form-grid">
                            <div class="form-group">
                                <label>Price per Single Unit Piece (PKR)</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-money-bill-wave"></i>
                                    <input type="number" step="0.01" name="price_per_piece" placeholder="0.00">
                                </div>
                            </div>

                            <div class="form-group">
                                <label>Initial Stock Opening Quantity</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-boxes-stacked"></i>
                                    <input type="number" name="stock_quantity" placeholder="0">
                                </div>
                            </div>

                            <div class="form-group full-width">
                                <label>Product Graphic Identity Reference / Image</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-image"></i>
                                    <input type="file" name="image_url" accept="image/*" style="padding-top:7px;">
                                </div>
                            </div>
                        </div>

                    </div>

                    <div class="form-actions">
                        <button type="button" onclick="window.location='products_list.php';" class="btn btn-cancel">
                            <i class="fa-solid fa-arrow-left"></i> Back to Inventory
                        </button>
                        <button type="submit" name="submit" class="btn btn-submit">
                            <i class="fa-regular fa-circle-check"></i> Save Product
                        </button>
                    </div>

                </form>

            </div>
        </div>
    </div>
</div>

<script>
/* Mobile Interface Dynamic Toggle Operations */
function toggleMobileSidebar() {
    const sidebarElements = document.querySelectorAll('aside, .sidebar, [class*="sidebar"]');
    const overlay = document.getElementById('sidebarOverlay');
    
    sidebarElements.forEach(sidebar => {
        sidebar.classList.toggle('active');
    });
    
    if(sidebarElements[0] && sidebarElements[0].classList.contains('active')) {
        overlay.style.display = 'block';
    } else {
        overlay.style.display = 'none';
    }
}
</script>
</body>
</html>