<?php

if(!isset($_SESSION['admin_email'])){

    echo "<script>window.open('login.php','_self')</script>";

} else {

?>
<!DOCTYPE html>
<html>
<head>
<title> Insert Products </title>
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>tinymce.init({ selector:'#product_desc,#product_features' });</script>
</head>
<body>

<div class="row">
<div class="col-lg-12">
    <ol class="breadcrumb">
        <li class="active">
            <i class="fa fa-dashboard"> </i> Dashboard / Insert Products
        </li>
    </ol>
</div>
</div>

<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
    <h3 class="panel-title">
        <i class="fa fa-money fa-fw"></i> Insert Products
    </h3>
</div>

<div class="panel-body">

<form class="form-horizontal" method="post" enctype="multipart/form-data">

<!-- Product Title -->
<div class="form-group">
    <label class="col-md-3 control-label"> Product Title </label>
    <div class="col-md-6">
        <input type="text" name="product_title" class="form-control" required>
    </div>
</div>

<!-- Product URL -->
<div class="form-group">
    <label class="col-md-3 control-label"> Product Url </label>
    <div class="col-md-6">
        <input type="text" name="product_url" class="form-control" required>
        <br>
        <p style="font-size:15px; font-weight:bold;">
            Example: navy-blue-t-shirt
        </p>
    </div>
</div>

<!-- Manufacturer -->
<div class="form-group">
    <label class="col-md-3 control-label"> Select A Manufacturer </label>
    <div class="col-md-6">
        <select class="form-control" name="manufacturer" required>
            <option value=""> Select A Manufacturer </option>
            <?php
            $get_manufacturer = "select * from manufacturers";
            $run_manufacturer = mysqli_query($con,$get_manufacturer);
            while($row_manufacturer = mysqli_fetch_array($run_manufacturer)){
                echo "<option value='".$row_manufacturer['manufacturer_id']."'>
                        ".$row_manufacturer['manufacturer_title']."
                      </option>";
            }
            ?>
        </select>
    </div>
</div>

<!-- Product Category -->
<div class="form-group">
    <label class="col-md-3 control-label"> Product Category </label>
    <div class="col-md-6">
        <select name="product_cat" class="form-control" required>
            <option value=""> Select a Product Category </option>
            <?php
            $get_p_cats = "
                SELECT pc.p_cat_id, pc.p_cat_title, c.cat_type
                FROM product_categories pc
                JOIN categories c ON pc.cat_id = c.cat_id
            ";
            $run_p_cats = mysqli_query($con,$get_p_cats);
            while($row_p_cats = mysqli_fetch_array($run_p_cats)){
                echo "<option value='".$row_p_cats['p_cat_id']."'>
                        ".$row_p_cats['cat_type']." → ".$row_p_cats['p_cat_title']."
                      </option>";
            }
            ?>
        </select>
    </div>
</div>

<!-- Images -->
<div class="form-group">
    <label class="col-md-3 control-label"> Product Image 1 </label>
    <div class="col-md-6"><input type="file" name="product_img1" class="form-control" required></div>
</div>
<div class="form-group">
    <label class="col-md-3 control-label"> Product Image 2 </label>
    <div class="col-md-6"><input type="file" name="product_img2" class="form-control"></div>
</div>
<div class="form-group">
    <label class="col-md-3 control-label"> Product Image 3 </label>
    <div class="col-md-6"><input type="file" name="product_img3" class="form-control"></div>
</div>

<!-- Prices -->
<div class="form-group">
    <label class="col-md-3 control-label"> Product Price </label>
    <div class="col-md-6"><input type="text" name="product_price" class="form-control" required></div>
</div>
<div class="form-group">
    <label class="col-md-3 control-label"> Product Sale Price </label>
    <div class="col-md-6"><input type="text" name="psp_price" class="form-control"></div>
</div>

<!-- Keywords -->
<div class="form-group">
    <label class="col-md-3 control-label"> Product Keywords </label>
    <div class="col-md-6"><input type="text" name="product_keywords" class="form-control" required></div>
</div>

<!-- Tabs -->
<div class="form-group">
    <label class="col-md-3 control-label"> Product Tabs </label>
    <div class="col-md-6">
        <ul class="nav nav-tabs">
            <li class="active"><a data-toggle="tab" href="#description"> Description </a></li>
            <li><a data-toggle="tab" href="#features"> Features </a></li>
            <li><a data-toggle="tab" href="#video"> Sounds & Videos </a></li>
        </ul>
        <div class="tab-content">
            <div id="description" class="tab-pane fade in active"><br>
                <textarea name="product_desc" class="form-control" rows="15" id="product_desc"></textarea>
            </div>
            <div id="features" class="tab-pane fade in"><br>
                <textarea name="product_features" class="form-control" rows="15" id="product_features"></textarea>
            </div>
            <div id="video" class="tab-pane fade in"><br>
                <textarea name="product_video" class="form-control" rows="15"></textarea>
            </div>
        </div>
    </div>
</div>

<!-- Label -->
<div class="form-group">
    <label class="col-md-3 control-label"> Product Label </label>
    <div class="col-md-6"><input type="text" name="product_label" class="form-control"></div>
</div>

<!-- Submit -->
<div class="form-group">
    <label class="col-md-3 control-label"></label>
    <div class="col-md-6"><input type="submit" name="submit" value="Insert Product" class="btn btn-primary form-control"></div>
</div>

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

</body>
</html>

<?php
if(isset($_POST['submit'])){

    $product_title = $_POST['product_title'];
    $p_cat_id = $_POST['product_cat']; // only product category
    $manufacturer_id = $_POST['manufacturer'];
    $product_price = $_POST['product_price'];
    $psp_price = $_POST['psp_price'];
    $product_desc = $_POST['product_desc'];
    $product_features = $_POST['product_features'];
    $product_video = $_POST['product_video'];
    $product_keywords = $_POST['product_keywords'];
    $product_label = $_POST['product_label'];
    $product_url = $_POST['product_url'];

    $status = "product";

    // Get cat_id automatically from product_categories
    $get_cat = "SELECT cat_id FROM product_categories WHERE p_cat_id='$p_cat_id' LIMIT 1";
    $run_cat = mysqli_query($con,$get_cat);
    $row_cat = mysqli_fetch_array($run_cat);
    $cat_id = $row_cat['cat_id'];

    // Images
    $product_img1 = $_FILES['product_img1']['name'];
    $product_img2 = $_FILES['product_img2']['name'];
    $product_img3 = $_FILES['product_img3']['name'];

    $temp_name1 = $_FILES['product_img1']['tmp_name'];
    $temp_name2 = $_FILES['product_img2']['tmp_name'];
    $temp_name3 = $_FILES['product_img3']['tmp_name'];

    move_uploaded_file($temp_name1,"product_images/$product_img1");
    move_uploaded_file($temp_name2,"product_images/$product_img2");
    move_uploaded_file($temp_name3,"product_images/$product_img3");

    $insert_product = "INSERT INTO products 
    (p_cat_id,cat_id,manufacturer_id,date,product_title,product_url,product_img1,product_img2,product_img3,product_price,product_psp_price,product_desc,product_features,product_video,product_keywords,product_label,status)
    VALUES 
    ('$p_cat_id','$cat_id','$manufacturer_id',NOW(),'$product_title','$product_url','$product_img1','$product_img2','$product_img3','$product_price','$psp_price','$product_desc','$product_features','$product_video','$product_keywords','$product_label','$status')";

    $run_product = mysqli_query($con,$insert_product);

    if($run_product){
        echo "<script>alert('Product has been inserted successfully')</script>";
        echo "<script>window.open('index.php?view_products','_self')</script>";
    }
}
?>

<?php } ?>
