<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
session_start();
require_once "check_admin.php";
require_once 'database.php';
class new_category extends database
{
   public function index($data)
    {
		return $this->insert('menu_category', $data);
	}
	 public function changes($data,$id)
    {
        $where['id'] = $id;
		return $this->update('menu_category', $data, $where);
	}
	 public function GetAll()
    {
        
        return $this->fetchAll('menu_category');
    }
     public function CurrentCat($id)
    {
        $where['id']=$id;
        return $this->fetchRow('menu_category', $where);
    }
}
$cat = new new_category();
$msg="";
if(isset($_GET['message']))
{
	$successMsg=$_GET['message'];
}
$id="";
if(isset($_GET['id']))
{
 $id= $_GET['id'];
}
if(isset($_POST['data']))
{
	$data=$_POST['data'];
	if(trim($data['name']) == '')
	{
		$msg.= "Category name is required<br/>";
	}
   
      
	if($msg=="" && $id=="")
	{
		 $res = $cat->index($data);
            if($res)
            {
                    header('Location:add-category.php?message=Category Added Successfully');
            }
            else {
                $msg = 'Something Went Wrong';
            }
	}
	elseif($msg=="" && $id!="")
	{
	     $res = $cat->changes($data,$id);
            if($res)
            {
                    header('Location:add-category.php?message=Category Updated Successfully');
            }
            else {
                $msg = 'Something Went Wrong';
            }
	}
}
 include 'header.php';
 ?>
<div class="ordes-content">
    <div class="container midle-order">
        <div class="row">
        	<?php include 'menu.php';?>
            <div class="col-md-9">
                <div class="order-wraper">
                	<div class="col-md-12">
                    	<h1>Add Menu Category</h1>
						<?php if($msg !=''){echo '<span class="alert alert-danger pull-left">'.$msg.'</span>';}?>
                        <?php if(isset($successMsg) && $successMsg !=''){echo '<span class="alert alert-success pull-left">'.$successMsg.'</span>';}?>						
						
                    </div>
                    <div class="clearfix"></div>
                    <div class="col-sm-12">
                    	<div class="order-contact">
                        	<div class="row edit-user-box">
                                <div class="col-sm-12">
                                    <div class="row">
									<?php
									$name="";
									$des="";
									$btn="ADD";
									if($id !="")
									{
									    $cate = $cat->CurrentCat($id);
									    	$name= $cate['name'];
									    	$des= $cate['description'];
									    	$btn="UPDATE";
									}
									?>
									<form method="post">
                                        <div class="col-md-12">
                                            <label>Category Name</label>
                                            <input type="text" name="data[name]" placeholder="Category Name" value="<?php echo $name;?>" />
                                        </div>
                                        <div class="col-md-12">
                                            <label>Description</label>
                                            <textarea name="data[description]" placeholder="Description" ><?php echo $des;?></textarea>
                                        </div>
                                        <div class="col-md-12 botm-cont">
                                            <button type="submit" class="track-btn"><?php echo $btn;?></button>
                                        </div>
									</form>
                                    </div>
                            	</div>
                        	</div>                            
                        </div>
                    </div>
                </div>
                <div class="order-wraper">
                	<div class="col-md-12">
                    	<h1>Category List</h1>
                    </div>
                    <div class="clearfix"></div>
                    <div class="col-sm-12">
                    	<div class="curent-user">
                        	<div class="row">
								
                                <div class="col-sm-12">
                                    <div class="curent-user-nav">
									<table class="table table-responsive"> 
											<tr>
												<th>Name</th>
												<th>Description</th>
												<th>Actions</th>
											</tr>
											<?php
											 $category = $cat->GetAll();
											 foreach($category as $u)
											 {
												
											?>
											<tr>
												<td><?php echo htmlspecialchars($u['name']);?></td>
												<td><?php echo htmlspecialchars($u['description']);?></td>
												<td><button data-href="cat-remove?id=<?php  echo htmlspecialchars($u['id']); ?>" class="remove track-btn">REMOVE</button>
												<a id="edit" href="add-category.php?id=<?php  echo htmlspecialchars($u['id']); ?>" class="track-btn bg-primary">EDIT</a>
												</td>
											</tr>
											<?php
											
											 }
											?>
                                </table>
                                    	
                                    </div>
                            	</div>
                                
                        	</div>                            
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
	</div>
</div>
<script>
    $(document).ready(function(){
        $('.remove').click(function(){
            var parent = $(this).parents('tr').first();
            var conf = confirm("Are you sure you want to delete this category?");
            if(conf)
            {
                var href = $(this).attr('data-href');
                $.post(href, function(res){
                    alert("Deletion Successful");
                    $(parent).remove();
                },'json').fail(function(xhr){
                   alert(xhr.responseJSON);
                },'json');
            }
        });
    });
	
    </script>

    <script>
    $(document).ready(function(){
        $('.activ-cat').removeClass('activ-cat');
        $('#add-category-menu').addClass('activ-cat');
    }); 
</script>
<?php include 'footer.php';?>