<?php
/*include_once "../ichat/admin/route.php";
require_once 'database.php';
if(isset($_SESSION[$elhash]) && $_SESSION[$elhash] != "") { 
    $random_hash = $_SESSION[$elhash];
} else {
    $random_hash = uniqid();
    $_SESSION[$elhash] = $random_hash;
}*/

session_start();
require_once "check_admin.php";
require_once 'database.php';
class ListCenas extends database
	{
		public function index($page)
		{
			$start = $page - 1;
			return $this->fetchAll('cenas', null, 'created_at', 'ASC', ($start * 10), 10);
		}

	}
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();
$res = $order->index($page);
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>Cenas</h1>
							<?php if($msg !=''){echo '<span class="alert alert-danger pull-left">'.$msg.'</span>';}?>
                    </div>
                    <div class="clearfix"></div>
                    <table class="table table-striped table-condensed">
                        <thead>
                            <tr>
                                <th colspan="2">
                                    <a href="add-cenas" class="btn btn-success pull-right">Add Cenas Table</a>
                                </th>
                            </tr>
                        </thead>
					<?php
					foreach($res as $od)
					{
					?>
                    <tbody>
                        <tr>
                            <td>
                                <?php echo htmlspecialchars($od['name']); ?>
                            </td>
                            <td>
                                 <a href="edit_cenas.php?id=<?php echo $od['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>
	</div>
</div>

<script>
    $(document).ready(function(){
        $('.activ-cat').removeClass('activ-cat');
        $('#cenas-menu').addClass('activ-cat');
    }); 

    $(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', {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';
?>