<?php

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

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

} else {

?>

<div class="row"><!-- 1 row Starts -->
<div class="col-lg-12">
    <ol class="breadcrumb">
        <li>
            <i class="fa fa-dashboard"></i> Dashboard / Insert Product Category
        </li>
    </ol>
</div>
</div><!-- 1 row Ends -->

<div class="row"><!-- 2 row Starts -->
<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 Product Category
            </h3>
        </div>

        <div class="panel-body">
            <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
                
                <!-- Product Category Title -->
                <div class="form-group">
                    <label class="col-md-3 control-label">Product Category Title</label>
                    <div class="col-md-6">
                        <input type="text" name="p_cat_title" class="form-control" required>
                    </div>
                </div>

                <!-- Hardware / Software Dropdown -->
                <div class="form-group">
                    <label class="col-md-3 control-label">Category Type</label>
                    <div class="col-md-6">
                        <select name="cat_id" class="form-control" required>
                            <option value="">Select Category</option>
                            <?php
                            $get_cats = "SELECT * FROM categories";
                            $run_cats = mysqli_query($con,$get_cats);
                            while($row_cats = mysqli_fetch_array($run_cats)){
                                $cat_id = $row_cats['cat_id'];
                                $cat_type = $row_cats['cat_type'];
                                echo "<option value='$cat_id'>$cat_type</option>";
                            }
                            ?>
                        </select>
                    </div>
                </div>

                <!-- Show as Top -->
                <div class="form-group">
                    <label class="col-md-3 control-label">Show as Top Product Category</label>
                    <div class="col-md-6">
                        <input type="radio" name="p_cat_top" value="yes"> <label>Yes</label>
                        <input type="radio" name="p_cat_top" value="no" checked> <label>No</label>
                    </div>
                </div>

                <!-- Upload Image -->
                <div class="form-group">
                    <label class="col-md-3 control-label">Select Product Category Image</label>
                    <div class="col-md-6">
                        <input type="file" name="p_cat_image" 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="Submit Now" class="btn btn-primary form-control">
                    </div>
                </div>

            </form>
        </div>
    </div>
</div>
</div><!-- 2 row Ends -->

<?php

if(isset($_POST['submit'])){

    $p_cat_title = $_POST['p_cat_title'];
    $cat_id = $_POST['cat_id']; // ✅ Hardware or Software
    $p_cat_top = $_POST['p_cat_top'];

    $p_cat_image = $_FILES['p_cat_image']['name'];
    $temp_name = $_FILES['p_cat_image']['tmp_name'];

    if(!empty($p_cat_image)){
        move_uploaded_file($temp_name,"other_images/$p_cat_image");
    } else {
        $p_cat_image = "";
    }

    $insert_p_cat = "INSERT INTO product_categories (cat_id, p_cat_title, p_cat_top, p_cat_image) 
                     VALUES ('$cat_id', '$p_cat_title', '$p_cat_top', '$p_cat_image')";

    $run_p_cat = mysqli_query($con,$insert_p_cat);

    if($run_p_cat){
        echo "<script>alert('New Product Category Has been Inserted')</script>";
        echo "<script>window.open('index.php?view_p_cats','_self')</script>";
    }

}

?>

<?php } ?>
