<?php 
    include("../db.php");
    
    //insert
   
    
    if($_FILES){
        if(isset($_FILES["image"]["name"]) && !empty($_FILES["image"]["name"])){
    		$lfilename = basename($_FILES['image']['name']);
    		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
    		$lnewfile=md5(microtime()).".".$lext;
    		echo "$lnewfile";
    		if(move_uploaded_file($_FILES['image']['tmp_name'],"../uploads/menu/".$lnewfile)){
    		    $_POST['image'] = $lnewfile;
    		}
    	}
        if($_POST['image']!=""){
          
    
            $_POST['menu_id']=$_GET['id'];
            $db->insertAry("extra_menu_data", $_POST);
            
        }

       header('Location: ' . $_SERVER['HTTP_REFERER']);
    }
    //end
if($_GET['action']=="delete" && isset($_GET['id']) && trim($_GET['id'])!='' && isset($_GET['name']) && trim($_GET['name'])!=''){
    
    $img = $_GET["name"];
    unlink("../uploads/menu/$img");
 	$res=$db->delete("extra_menu_data","where id='".$_GET['id']."' ");
     header('Location: ' . $_SERVER['HTTP_REFERER']);
}
    
    
   
?>

<!DOCTYPE html>
<html>
<head>
      <title>Admin Dashboard</title>
    <?php include("meta.php"); ?>

</head>
<body> 
 
<?php include("header.php"); ?>

<?php include("sidebar.php"); 
$id=$_GET["id"];
    $data = $db->getRows("select * from extra_menu_data where menu_id=$id ");

?>

<div class="main">
    
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="content-div">
                    <h2>SUBMENU</h2>
                    <div>
                    <button  style="background: #007bff;color: white;font-size: 20px;margin-bottom:4px; " data-toggle="modal" data-target="#exampleModal">+Add Data</button>
                    </div>
                    <table class="table table-hover">
  <thead class="bg-black text-white font-weight-bold ml-3" style="font-size: 15px;">
    <tr>
            <th scope="col">S.No</th>
 <th scope="col">Title</th>
      <th scope="col">Description</th>
      <th scope="col">Image</th>
      <th scope="col">Action</th>
    </tr>
  </thead>
  <tbody>
      <?php 
      $i=0;
      foreach($data as $res){
          $i++;
          
          ?>
           <tr >
               <th  style="font-weight: 600; color: green;padding: 0.75rem;" ><?php echo $i?></th>
      <th  style="font-weight: 600; color: green;padding: 0.75rem;" ><?php echo $res['title'];?></th>
      <th style="padding: 0.75rem;" ><?php echo $res['description'];?></th>
      <td style="padding: 0.75rem;">
          <img src="../uploads/menu/<?php echo $res["image"]; ?>" alt="Service image" style="height: 100px;">
          </td>
     <td><a class="btn btn-primary edit_data" href="edit_sub_menu?action=edit&id=<?php echo $res['id']; ?>&menu_id=<?php echo $id;?>&name=<?php echo $res['image']; ?>" ><i class="fa fa-edit"></i></a><a href="sub_menu?action=delete&id=<?php echo $res['id']; ?>&name=<?php echo $res['image']; ?>" class="btn btn-danger"><i class="fa fa-trash" aria-hidden="true"></i></a></td>
    </tr>
    <?php  }?>
   
  
  </tbody>
</table>
<!-- Button trigger modal -->


 <!--Modal add -->
<div class="modal fade " id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Add New Sub Menu</h5>
        
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form action="" method="post" enctype="multipart/form-data">
    <div class="form-group">
    <label for="exampleInputEmail1">Title</label>
    <input type="text" class="form-control" name="title" required  placeholder="Enter title">
    
  </div>
  <div class="form-group mt-3">
    <label for="exampleInputPassword1">Description</label>
    <input type="text" class="form-control" name="description" required placeholder="description">
  </div>
 <div class="form-group mt-3">
    <label for="exampleInputPassword1">Image</label>
    <input type="file" class="form-control" required name="image" >
  </div>
  <button type="submit"  class="btn btn-success mt-4">Save</button>
</form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
       
      </div>
    </div>
  </div>
</div>
 
                    
                </div>

            </div>   
        </div>
    </div>
    
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
     <script>  
 
// function edit(id){
	 
// 	$.post( "ajax.php", { type:"edit", id: id } , function( data ) {
// 	    let value=$.parseJSON(data);
// 	   // alert(value.id);
// $('#id').val(value.id);  
//                      $('#service').val(value.service);  
//                      $('#desc').val(value.description); 
//                      var imgHtml = "<img src='../uploads/"+ value.image+"' alt='something'id='ig' value='"+value.image+"' style='width: 100px;'>";
                    
//                       $("#img").html(imgHtml);
//                      $('#editmodal').modal('show');  
// 	});
// }
// function update(){
// 	 var str = $("#id").val();
// 	  var service = $("#service").val();
// 	  var desc = $("#desc").val();
	  
// 	  var pic = $("#pic").val();
	 
// 	$.post( "ajax.php", { type:"update", id: str, service:service,desc:desc,pic:pic} , function( data ) {
	    
// 	});
// }
 </script>
</body>
</html> 
