<?php
session_start();
error_reporting(1);
ini_set('display_errors', 1);
require_once "config.php";
include_once "../ichat/admin/route.php";
if ($session->is_logged_in() != true ) {
        redirect_to(WEB_LINK.'/login');
} else {
    $current_user = User::get_specific_id($session->admin_id);
    $current_user->set_online();
    $group = $current_user->prvlg_group;
}

require_once("../ichat/admin/assets/includes/Library/SessManager.php");
require_once("../ichat/admin/assets/includes/Library/User.php");
$session = new  SessManager();

//require_once "check_admin.php";
require_once 'database.php';
class ListCenas extends database
	{
		public function index($page, $cenasID)
		{
            $where['main_id'] = $cenasID;
			$start = $page - 1;
			return $this->fetchAll('cenas', $where, 'cl_created_at', 'ASC', ($start * 10), 10);
		}

        public function checkMainCenas($cenasID)
        {
            $where['id'] = $cenasID;
            return $this->fetchRow('cenas_main', $where);
        }
	}

if(!isset($_GET['id']))
{
    header('Location: list_cenas.php');
    exit;
}

$id = $_GET['id'];
if(isset($_GET['message']))
{
	$msg=$_GET['message'];
}

$page = 1;
if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 1)
{
    $page = htmlspecialchars($_GET['page']);
}



$order = new ListCenas();
$mainCenas = $order->checkMainCenas($id);
if(!$mainCenas)
{
    header('Location: list_cenas.php');
    exit;
}
$res = $order->index($page, $id);
include 'header.php';
?>
					 
            <!-- MAIN CONTENT-->
            <div class="main-content">
                <div class="section__content section__content--p30">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="overview-wrap">
                                    <h2 class="title-1">Cenas - <?php echo $mainCenas['name'] ?> Tables</h2>
                                    <a class="btn btn-success pull-right" href="add-cenas-table-row?id=<?php echo htmlspecialchars($id) ?>">Add Cenas Table</a>
                                </div>
								<?php if($msg !=''){echo '<span class="col-md-6 alert alert-danger pull-left">'.$msg.'</span>';}?>
                            </div>
                        </div>
                        
                        <div class="row m-t-25 m-row">
							<div class="col-lg-12">
                                <div class="table-responsive table--no-card m-b-30">
                                    <table class="table table-borderless table-data3">
                                        <thead>
                                          
                                        </thead>
                                        <?php
										foreach($res as $od)
										{
										?>
										<tbody>
											<tr>
												<td>
													<?php echo htmlspecialchars($od['name']); ?>
												</td>
												<td>
													 <a href="edit_cenas_table_row.php?id=<?php echo $od['id'];?>&main_id=<?php echo $_GET['id'] ?>" class="btn btn-success pull-left " style="margin-right:5px">Edit</a>&nbsp;
													 <button type="button" class="btn btn-danger pull-left btn-delete" data-id="<?php echo htmlspecialchars($od['id'])?>">Delete</button>
													
													   
												</td>
											</tr>
										</tbody>
									   
										<?php
										}
										?>
                                    </table>
									<div class="pull-right col-xs-12 top-margin">
										 <?php
										if($page > 1)
										{
										?>
										<a href="?page=<?php echo htmlspecialchars($page) - 1 ?>" class="track-btn">
											Previous
										</a>
										<?php
										}
										if(count($res) == 10)
										{
										?>
											<a href="?page=<?php echo htmlspecialchars($page) + 1 ?>" class="track-btn">
												Next
											</a>
										<?php
										}
										?>
									</div>
                                </div>
                            </div>
                            
                          
                        </div>
                     
						
						
                       <script>
						$(document).ready(function(){
							$('.active').removeClass('active');
							$('#cenasm-menu').addClass('active');
						}); 
						
						 $(document).on('click', '.btn-delete', function(res){
							 var conf = confirm("Are you sure you want to delete this table?");
								if(!conf)
								{
									return;
								}
							$(this).attr('disabled', 'disabled').text('Deleting......');

							var current = $(this);
							var id = $(this).attr('data-id');
							var parent = $(this).parents('tr').first();
							$.post('cenas_delete_table_row', {id:id}, function(){
								alert('Cenas Table Deleted Successfully');
								$(parent).remove();
							},'json').fail(function(xhr){
							   alert(xhr.responseJSON);
							   $(current).removeAttr('disabled').text('Delete');
							},'json');
						});
						</script>              

  <?php
  include 'footer.php';
  ?>