<?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="content-wrapper">
            <!-- Content -->

            <div class="container-xxl flex-grow-1 container-p-y">
              <h4 class="fw-bold py-3 mb-4">Add Menu Category</h4>
                

              <!-- Basic Layout -->
              <div class="row">
							<?php if($msg !=''){echo '<span class="alert alert-danger pull-left col-12">'.$msg.'</span>';}?>
                        <?php if(isset($successMsg) && $successMsg !=''){echo '<span class="alert alert-success pull-left col-12">'.$successMsg.'</span>';}?>					
					
                                <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="mb-3">
                                            <label class="form-label">Category Name</label>
                                            <input type="text" class="form-control" name="data[name]" placeholder="Category Name" value="<?php echo $name;?>" />
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Description</label>
                                            <textarea name="data[description]" class="form-control" placeholder="Description" ><?php echo $des;?></textarea>
                                        </div>
                                        <div class="mb-3">
                                            <button type="submit" class="track-btn btn btn-primary"><?php echo $btn;?></button>
                                        </div>
									</form>
                                    </div>
                            	</div>
                        
                <div class=" col-sm-12 order-wraper">
                    <div class="row">
                    	<div class="col-md-12">
                        	<h4 class="fw-bold py-3 mb-4">Category List</h4>
                        </div>
                  
                        <div class="col-md-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="track-btn btn btn-danger btn-remove">REMOVE</button>
    												<a id="edit" href="add-category.php?id=<?php  echo htmlspecialchars($u['id']); ?>" class="track-btn btn btn-primary">EDIT</a>
    												</td>
    											</tr>
    											<?php
    											
    											 }
    											?>
                                    </table>
                                        	
                                        </div>
                                	</div>
                                    
                            	</div>                            
                            </div>
                        </div>
                        </div>
                </div>
                
            </div>
        </div>
	</div>

<script>
    $(document).ready(function(){
        $('.btn-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(){
        $('.menu-item').removeClass('active');
        $('#add-category-menu').addClass('active');
    }); 
</script>
<?php include 'footer.php';?>