<?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();

if(!isset($_SESSION['id']) || $_SESSION['id']=='')
    {
            header('Location: index.php');
    }
    if(!isset($_SESSION['user_type']) || ($_SESSION['user_type'] != 'admin'))
    {
           // header('Location:index.php');
    }*/


session_start();
require_once "check_admin.php";
require_once 'database.php';

class submissions extends database
	{
		public function index($page)
		{
			$start = $page - 1;
			return $this->fetchAll('new_submit', null, 'created_at', 'DESC', ($start * 20), 20);
		}
		
	}
if(isset($_GET['message']))
{
	$msg=$_GET['message'];
}

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

$sub = new submissions();
$res = $sub->index($page);
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">Reservations</h4>

              <!-- Basic Layout -->
              <div class="row">
					<?php
					foreach($res as $od)
					{
					?>
                    <div class="col-sm-6 submission-container">
                    	<div class="order-contact card mb-4">
                    	    <div class="card-header d-flex justify-content-between align-items-center">
                      <h5 class="mb-0">ID: #<?php echo $od['id'];?></h5>
                      
                    </div>
                     <div class="card-body">
                        
                            
                            	<div class="mb-3">
                                	<label class="form-label">Vārds</label>
                                    <input type="text" class="form-control" placeholder="" value="<?php echo htmlspecialchars($od['name']); ?>" readonly />
                                </div>
                            	 <div class="mb-3">
                                	<label class="form-label">Kontakttālrunis</label>
                                    <input type="text" class="form-control" value="<?php echo htmlspecialchars($od['phone']); ?>" readonly />
                                </div>
								 <div class="mb-3">
                                	<label class="form-label">Vēlamais norises datums</label>
                                    <input type="text" class="form-control" value="<?php echo htmlspecialchars(date("d M Y", strtotime($od['preferred_date']))) . ' ' . htmlspecialchars($od['preffered_time']); ?>" readonly />
                                </div>
                            
                                <div class="mb-3">
                                	<label class="form-label">Cilvēku skaits</label>
                                    <input type="text" class="form-control" placeholder="Phone Number"  value="<?php echo htmlspecialchars($od['no_of_persons']); ?>" readonly />
                                </div>
                               
                            	<div class="col-md-12 botm-cont">
                                    <button type="button" class="btn btn-danger pull-left btn-delete" data-id="<?php echo htmlspecialchars($od['id'])?>">Delete</button>
                                </div>
                            	
                              <!--  <div class="col-md-12 botm-cont">
                                	<button type="submit" class="track-btn">Approve</button>
                                </div>-->
                                
                            </div>
                        </div>
                    </div>
					<?php
					}
					?>
                    <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) == 20)
                        {
                        ?>
                            <a href="?page=<?php echo htmlspecialchars($page) + 1 ?>" class="track-btn">
                                Next
                            </a>
                        <?php
                        }
                        ?>
                    </div>
                </div>
                
            </div>
        </div>
	
<script>
    $(document).ready(function(){
        $('.menu-item').removeClass('active');
        $('#reservation-menu').addClass('active');
    }); 

    $(document).on('click', '.btn-delete', function(res){
         var conf = confirm("Are you sure you want to delete this reservation?");
            if(!conf)
            {
                return;
            }
        $(this).attr('disabled', 'disabled').text('Deleting......');

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

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