    <?php
        require('top_inc.php');
        $categories_id='';
        $name='';
        $image='';
        $short_desc='';
        $description='';
        $meta_title='';
        $meta_desc='';
        $meta_keyword='';


        $msg='';
        $msg1='';
        $image_required='required';

        if (isset($_GET['id']) && $_GET['id']!='') {
            $image_required='';
            $id=get_safe_value($con,$_GET['id']);
            $res=mysqli_query($con, "select * from product where id='$id'");
            $check=mysqli_num_rows($res);
            if($check>0){
                $row=mysqli_fetch_assoc($res);
                $categories_id=$row['categories_id'];
                $name=$row['name'];
                $short_desc=$row['short_desc'];
                $description=$row['description'];
                $meta_title=$row['meta_title'];
                $meta_desc=$row['meta_desc'];
                $meta_keyword=$row['meta_keyword'];
            }
            else{
            }
        }

        if(isset($_POST['submit'])){
            $categories_id=get_safe_value($con,$_POST['categories_id']);
            $name=get_safe_value($con,$_POST['name']);
            $short_desc=get_safe_value($con,$_POST['short_desc']);
            $description=get_safe_value($con,$_POST['description']);
            $meta_title=get_safe_value($con,$_POST['meta_title']);
            $meta_desc=get_safe_value($con,$_POST['meta_desc']);
            $meta_keyword=get_safe_value($con,$_POST['meta_keyword']);

            $res=mysqli_query($con, "select * from product where name='$name'");
            $check=mysqli_num_rows($res);
            if($check>0){
                if (isset($_GET['id']) && $_GET['id']!='') {
                    $getData=mysqli_fetch_assoc($res);
                    if($id==$getData['id']){

                    }
                    else{
                        $msg="Products already exist";   
                    }
                }
                else{
                    $msg="Products already exist";
                }
            }
            if($_FILES['image']['type']!='image/png' && $_FILES['image']['type']!='image/jpg' && $_FILES['image']['type']!='image/jpeg'){
                $msg="Please select only png, jpg and jpeg image formate";
            }
            if($msg==''){
                if (isset($_GET['id']) && $_GET['id']!='') {
                    if ($_FILES['image']['name']!='') {
                        $image=rand(111111111,999999999).'_'.$_FILES['image']['name'];
                        move_uploaded_file($_FILES['image']['tmp_name'], PRODUCT_IMAGE_SERVER_PATH.$image);
                        $update_sql="update product set categories_id='$categories_id',name='$name',short_desc='$short_desc',description='$description',meta_title='$meta_title',meta_desc='$meta_desc',meta_keyword='$meta_keyword',image='$image' where id='$id'";
                    }
                    else{
                        $update_sql="update product set categories_id='$categories_id',name='$name',short_desc='$short_desc',description='$description',meta_title='$meta_title',meta_desc='$meta_desc',meta_keyword='$meta_keyword' where id='$id'";
                    }
                    mysqli_query($con,$update_sql);
                }
                else{
                    $image=rand(111111111,999999999).'_'.$_FILES['image']['name'];
                    move_uploaded_file($_FILES['image']['tmp_name'], PRODUCT_IMAGE_SERVER_PATH.$image);
                    mysqli_query($con,"insert into product(categories_id,name,short_desc,description,meta_title,meta_desc,meta_keyword,status,image) values('$categories_id','$name','$short_desc','$description','$meta_title','$meta_desc','$meta_keyword',1,'$image')");
                }
                $msg1="Categories successfull submit";
                
            }

        }
        
    ?>
        <!-- Page Area Start Here -->
        <div class="dashboard-page-one">
            <!-- Sidebar Area Start Here -->
            <?php
                require('side_menu.php');
            ?>
            <!-- Sidebar Area End Here -->
            <div class="dashboard-content-one">
                <!-- Breadcubs Area Start Here -->
                <div class="breadcrumbs-area">
                    <h3>Add Products</h3>
                    <ul>
                        <li>
                            <a href="index.php">Home</a>
                        </li>
                        <li>Add Products</li>
                    </ul>
                </div>
                <!-- Breadcubs Area End Here -->
                <!-- Student Table Area Start Here -->
                <div class="card height-auto">
                    <div class="card-body">
                        <div class="heading-layout1">
                            <div class="item-title">
                                <h3>Add Products</h3>
                            </div>
                        </div>
                        <form method="post" enctype="multipart/form-data" class="mg-b-20">
                            <div class="row gutters-8">
                                <div class="col-3-xxxl col-xl-6 col-lg-6 col-12 form-group">
                                    <select class="form-control" name="categories_id">
                                        <option>Select categories</option>
                                        <?php
                                        $res=mysqli_query($con,"select id,categories from categories order by categories asc");

                                        while ($row=mysqli_fetch_assoc($res)) {
                                            if ($row['id']==$categories_id) {
                                                echo "<option selected value=".$row['id'].">".$row['categories']."</option>";
                                            }
                                            else{
                                            echo "<option value=".$row['id'].">".$row['categories']."</option>";
                                            }   
                                        }
                                        ?>
                                    </select>
                                </div>
                                <div class="col-3-xxxl col-xl-6 col-lg-6 col-12 form-group">
                                    <input type="text" placeholder="Enter Product name ..." class="form-control" name="name" value="<?php echo $name; ?>">
                                </div>
                                <div class="col-3-xxxl col-xl-12 col-lg-12 col-12 form-group">
                                    <input type="file" class="form-control" name="image" <?php echo $image_required?>>
                                </div>
                                <div class="col-3-xxxl col-xl-12 col-lg-12 col-12 form-group">
                                    <textarea placeholder="Enter Product Short description ..." class="form-control" name="short_desc"><?php echo $short_desc; ?></textarea>
                                </div>
                                <div class="col-3-xxxl col-xl-12 col-lg-12 col-12 form-group">
                                    <textarea placeholder="Enter Product description ..." class="form-control" name="description"><?php echo $description; ?></textarea>
                                </div>
                                <div class="col-3-xxxl col-xl-12 col-lg-12 col-12 form-group">
                                    <textarea placeholder="Enter Product Meta Title ..." class="form-control" name="meta_title"><?php echo $meta_title; ?></textarea>
                                </div>
                                <div class="col-3-xxxl col-xl-12 col-lg-12 col-12 form-group">
                                    <textarea placeholder="Enter Product Meta Desc ..." class="form-control" name="meta_desc"><?php echo $meta_desc; ?></textarea>
                                </div>
                                <div class="col-3-xxxl col-xl-12 col-lg-12 col-12 form-group">
                                    <textarea placeholder="Enter Product Meta Desc ..." class="form-control" name="meta_desc"><?php echo $meta_desc; ?></textarea>
                                </div>
                                <div class="col-3-xxxl col-xl-12 col-lg-12 col-12 form-group">
                                    <textarea placeholder="Enter Product Meta Keyword ..." class="form-control" name="meta_keyword"><?php echo $meta_keyword; ?></textarea>
                                </div>

                                <div class="col-1-xxxl col-xl-12 col-lg-12 col-12 form-group">
                                    <button type="submit" name="submit" class="fw-btn-fill btn-gradient-yellow">SUBMIT</button>
                                </div>
                            </div>
                        </form>
                        <div class="field_error">
                            <?php echo $msg ?>
                        </div>
                        <div class="field_error">
                            <?php echo $msg1 ?>
                        </div>
                    </div>
                </div>
                <!-- Student Table Area End Here -->
                <?php
                    require('footer_inc.php');
                ?>