<?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('submission', 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';
?>
 <!-- Content wrapper -->
          <div class="content-wrapper">
            <!-- Content -->

            <div class="container-xxl flex-grow-1 container-p-y">
              <h4 class="fw-bold py-3 mb-4">Submissions</h4>

              <!-- Basic Layout -->
              <div class="row">
			  <?php
					foreach($res as $od)
					{
					?>
                <div class="col-md-4 submission-container">
                  <div class="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" for="basic-default-fullname">Vārds</label>
                          <input type="text" class="form-control" id="basic-default-fullname" placeholder="" value="<?php echo htmlspecialchars($od['name']); ?>" readonly  />
                        </div>
                        <div class="mb-3">
                          <label class="form-label" for="basic-default-company">Uzvārds</label>
                          <input type="text" class="form-control" id="basic-default-company" placeholder="" value="<?php echo htmlspecialchars($od['surname']); ?>" readonly />
                        </div>
						 <div class="mb-3">
                          <label class="form-label" for="basic-default-noru">Vēlamais norises datums</label>
                          <input type="text" class="form-control" id="basic-default-noru" placeholder="" value="<?php echo htmlspecialchars($od['preferred_date']); ?>" readonly  />
                        </div>
                        <div class="mb-3">
                          <label class="form-label" for="basic-default-email">E-pasts</label>
                          <div class="input-group input-group-merge">
                            <input type="text" id="basic-default-email" class="form-control" value="<?php echo htmlspecialchars($od['email']); ?>" readonly />
                           
                          </div>
                        
                        </div>
                        <div class="mb-3">
                          <label class="form-label" for="basic-default-phone">Cilvēku skaits</label>
                          <input
                            type="text"
                            id="basic-default-phone"
                            class="form-control"
                            value="<?php echo htmlspecialchars($od['no_of_persons']); ?>" readonly 
                          />
                        </div>
                        <div class="mb-3">
                          <label class="form-label" for="basic-default-message">Jūsu piezīmes</label>
                          <textarea
                            id="basic-default-message"
                            class="form-control"
                           class="form-control" readonly style="background-color:white"><?php echo htmlspecialchars($od['message']); ?></textarea>
                        </div>
                         <button type="button" class="btn btn-danger pull-left btn-delete" data-id="<?php echo htmlspecialchars($od['id'])?>">Delete</button>
                      
                    </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>
            <!-- / Content -->



<script>
    $(document).ready(function(){
        $('.menu-item').removeClass('active');
        $('#submission-menu').addClass('active');
    }); 

    $(document).on('click', '.btn-delete', function(res){
         var conf = confirm("Are you sure you want to delete this submission?");
            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('submission_delete.php', {id:id}, function(){
            alert('Submission Deleted Successfully');
            $(parent).remove();
        },'json').fail(function(xhr){
           alert(xhr.responseJSON);
           $(current).removeAttr('disabled').text('Delete');
        },'json');
    });
</script>

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