<?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_item', $data);
	}
	 public function changes($data,$id)
    {
        $where['id'] = $id;
		return $this->update('menu_item', $data, $where);
	}
	public function GetAll()
    {
        
        return $this->fetchAll('menu_item');
    }
    public function GetAllCategory()
    {
        
        return $this->fetchAll('menu_category');
    }
     public function Currentitem($id)
    {
        $where['id']=$id;
        return $this->fetchRow('menu_item', $where);
    }
     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-menu-item.php?message=Item Added Successfully');
                    exit;
            }
            else {
                $msg = 'Something Went Wrong';
            }
	}
	elseif($msg=="" && $id!="")
	{
	     $res = $cat->changes($data,$id);
            if($res)
            {
                    header('Location:add-menu-item.php?message=Item 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 Item</h4>
                

              <!-- Basic Layout -->
              <div class="row">
						<?php if($msg !=''){echo '<span class="alert alert-danger col-12">'.$msg.'</span>';}?>
						<?php if(isset($successMsg) && $successMsg !=''){echo '<span class="alert alert-success col-12">'.$successMsg.'</span>';}?>
						
                   
                    <div class="col-sm-12">
                    	<div class="order-contact">
                        	<div class="row edit-user-box">
                                <div class="col-sm-12">
                                    <div class="row">
									<?php
									$catid="";
									$name="";
									$price="";
									$btn="ADD";
									if($id !="")
									{
									        $item = $cat->Currentitem($id);
									    	$name= $item['name'];
									    	$price= $item['price'];
									    	$btn="UPDATE";
									    	$catid=$item['cat_id'];
									}
									?>
									<form method="post">
									    <div class="mb-3">
                                            <label class="form-label">Item Name</label>
                                            <select class="form-control" type="text" name="data[cat_id]">
                                                <option value="">----Select----</option>
                                                <?php
                                                $category = $cat->GetAllCategory();
                                                foreach($category as $c)
                                                {
                                                    
                                                
                                                ?>
                                                <option value="<?php echo $c['id'];?>" <?php if(isset($catid) && ($catid == $c['id'])){echo 'selected';} ?>><?php echo $c['name'];?></option>
                                                <?php
                                                }
                                                ?>
                                            </select>
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Item Name</label>
                                            <input class="form-control" type="text" name="data[name]" placeholder="Item Name" value="<?php echo $name;?>" />
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Price</label>
                                            <input class="form-control" type="text" name="data[price]" placeholder="Item Price" value="<?php echo $price;?>" />
                                        </div>
                                        <div class="col-md-12 botm-cont">
                                            <button type="submit" class="track-btn btn btn-primary"><?php echo $btn;?></button>
                                        </div>
									</form>
                                    </div>
                            	</div>
                        	</div>                            
                        </div>
                    </div>
                </div>
                <div class="order-wraper mt-5">
                	<div class="col-md-12">
                    	<h1>Menu 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>Price</th>
												<th>Category</th>
												<th>Actions</th>
											</tr>
											<?php
											 $items = $cat->GetAll();
											 foreach($items as $u)
											 {
											  $ca= 	$cat->CurrentCat($u['cat_id']);
											?>
											<tr>
												<td><?php echo htmlspecialchars($u['name']);?></td>
												<td><?php echo htmlspecialchars($u['price']);?></td>
												<td><?php echo htmlspecialchars($ca['name']);?></td>
												<td><button class="btn  btn-danger btn-remove" data-href="item-remove?id=<?php  echo htmlspecialchars($u['id']); ?>" class="track-btn btn btn-danger">REMOVE</button>
												<a id="edit" href="add-menu-item.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>

<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 item?");
            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');
        $('#item-menu').addClass('active');
    }); 
</script>
<?php include 'footer.php';?>