<?php

if($_SESSION["type"] != 1){
	echo "No permission to access"; die();
	}
$cat_id = $_GET['cat_id'];
?>
<div class="row">
    <div class="col-lg-12" style="margin-top:-41px;margin-bottom:-13px;">
        <h1 class="page-header" style="font-size:19px; color:#09F;"><i class="fa fa-user fa-fw"></i>&nbsp;AddNew Category</h1>
    </div>
    <!-- /.col-lg-12 -->
</div>
<!-- Row -->
<?php       
    if (isset($_POST['btupdate'])) {
        $main_menu = $_POST['main_menu'];
        $name = $_POST['cat_title'];
		$order = $_POST['order'];
        mysqli_query($conn,"SET NAMES utf8");
        mysqli_query($conn,"update tbl_category set pr_id = $main_menu,cat_title='$name',cat_order=$order where cat_id = $cat_id") or die(mysql_error());
        
        ?>
        	<script type="text/javascript">
              window.location.href="?page=category";
            </script>
       <?php 
    }   
?>
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default" style="width:100%;">
            <div class="panel-body">
                <div class="row">
                    <form method="post" enctype="multipart/form-data">
                    	<?php
							mysqli_query($conn,'SET NAMES utf8');
							$select2=mysqli_query($conn,"SELECT * FROM tbl_category where cat_id = $cat_id"); 
							while($row_cat=mysqli_fetch_array($select2)){
						?>
                        <div class="col-lg-6">
                            <div class="form-group">
                                <label style="color:#3a3a3a;">Main Menu</label>
                                <select class="form-control" name="main_menu" required>
                                	<option>Main Menu</option>
                                	<?php
										mysqli_query($conn,'SET NAMES utf8');
										$select2=mysqli_query($conn,"SELECT * FROM tbl_category where pr_id = 0 AND cat_id !=0"); 
										while($row_p=mysqli_fetch_array($select2)){
											$p_id = $row_p['cat_id'];
											if($row_p['cat_id']== $row_cat['pr_id'])
														$select="selected";
													else
														$select="";	
									?>
                                    <option value="<?php echo $row_p['cat_id'];?>" <?php echo $select;?> style="color:#F00; border-bottom:1px dotted #333;"><?php echo $row_p['cat_title'];?></option>
                                    
                                    	<?php
											mysqli_query($conn,'SET NAMES utf8');
											$select1=mysqli_query($conn,"SELECT * FROM tbl_category where pr_id = $p_id order by cat_id DESC"); 
											while($row_c=mysqli_fetch_array($select1)){				
										?>
                                        
                                    <option value="" style="padding-left:20px;"><?php echo $row_c['cat_title'];?></option>
                                       	<?php } ?>
                                    </optgroup>
                                    <?php } ?>
                                </select>
                            </div>
                            <div class="form-group">
                                <label style="color:#3a3a3a;"v>Category Name</label>
                                <input class="form-control" placeholder="Category Name" name="cat_title" value="<?=$row_cat['cat_title'];?>"/>
                            </div>
                            <div class="form-group">
                                <label>Order Number</label>
                                <input class="form-control" placeholder="Order Number" name="order" value="<?=$row_cat['cat_order'];?>" />
                            </div>
                            
                            <div class="form-group" style="margin-top:-25px;">
                                <label></label><br/>
                                <input type="submit" name="btupdate" class="btn btn-success" value="&nbsp;Update&nbsp;" title="Update"/>
                                 <a href="?page=category">
                            <input type="button" name="addnew" class="btn btn-primary" value="&nbsp;Back&nbsp;" title="Back"/>
                            </a>
                            </div> 
                        </div>
                        <?php } ?> 
                    </form>
                </div>
            </div>
       </div>
    </div>
</div>
