<?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('tb_subscriptions', null, 'created_at', 'DESC', ($start * 100), 100);
		}
		
	}
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();
$subscriptions = $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">Subscriptions</h4>

              <!-- Basic Layout -->
              <div class="row">
                  <div class="card-body">
                    
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>
                                    Phone
                                </th>
                                <th>
                                    
                                </th>
                            </tr>
                        </thead>
                        <?php
                        if(!empty($subscriptions))
                        {
                            foreach($subscriptions AS $subscription)
                            {
                                ?>
                                <tr>
                                    
                                    <td>
                                        <span class="phone" contenteditable="true" style="padding: 10px;" class="subscribe_phone">
                                        <?php echo htmlspecialchars($subscription['cl_phone']) ?>
                                        </span>
                                    </td>
                                    <td>
                                        <button type="button" class="btn btn-primary btn-update" cluster-id="<?php echo htmlspecialchars($subscription['cl_id']) ?>">Update</button>
                                        <button type="button" class="btn btn-danger btn-delete" cluster-id="<?php echo htmlspecialchars($subscription['cl_id']) ?>">Delete</button>
                                        <div class="error-input"></div>
                                    </td>
                                </tr>
                                <?php
                            }
                        }
                        ?>
                        <tfoot>
                            <tr>
                                <td colspan="2">
                                    <a href="?page=<?php echo htmlspecialchars($page - 1) ?>">Prev</a>
                            <span style="border: 1px solid gray;padding: 10px;background-color: white;margin-left: 5px;margin-right: 5px;border-radius: 25px">Page : <?php echo htmlspecialchars($page) ?> </span>
                            <a href="?page=<?php echo htmlspecialchars($page + 1) ?>">Next</a>
                                </td>
                            </tr>
                            
                        </tfoot>
                    </table>
                    </div>
                    </div>
            </div>
        </div>
  
    
    <script>
    $(document).ready(function(){
        $('.menu-item').removeClass('active');
        $('#subscription-menu').addClass('active');
        
       $('.btn-update').click(function(){
          var id = $(this).attr('cluster-id');
          var parent = $(this).parents('tr').first();
          var phone = $(parent).find('.phone').text().trim();
          var current = $(this);
          $(current).attr('disabled', 'disabled');
          
          $(parent).find('.error-input').html('<div class="alert alert-warning">Please wait while phone is updated.</div>');
          $.post('ajax/subscribe/edit_ajax?id=' + id, {phone:phone}, function(){
              $(current).removeAttr('disabled');
              $(parent).find('.error-input').html('<div class="alert alert-success">Phone Updated Successfully.</div>');
          }, 'json')
          .fail(function(xhr) {

            $(current).removeAttr('disabled');
            $(parent).find('.error-input').html('<div class="alert alert-danger">Following errors are listed. Please check errors and resubmit the form.</div>').show();
            	$('.error-input').html('');
              var html = '';
              var res = xhr.responseJSON;
              if (typeof res === 'undefined') {
              	$(parent).find('.error-input').html('<div class="alert alert-danger">Something went wrong</div>').show();
              }
             
              	if(typeof res.error_text === 'undefined')
              	{
                	$(current).find('.error-input').html('<div class="alert alert-danger">Something went wrong</div>').show();
                	window.location.href = '#body_part';
                	return;
                }
                else
                {
                	$(current).find('.error-input').html('<div class="alert alert-danger">' + res.error_text + '</div>').show();
                	window.location.href = '#body_part';
                	return;
                }
            
          },'json');
       });
       
       
       
       $('.btn-delete').click(function(){
          var id = $(this).attr('cluster-id');
          var parent = $(this).parents('tr').first();
          var current = $(this);
          $(current).attr('disabled', 'disabled');
          
          $(parent).find('.error-input').html('<div class="alert alert-warning">Please wait while phone is deleted.</div>');
          $.get('ajax/subscribe/delete_ajax?id=' + id, function(){
              $(current).removeAttr('disabled');
              $(parent).find('.error-input').html('<div class="alert alert-success">Phone Deleted Successfully.</div>');
              
              $(parent).remove();
          }, 'json')
          .fail(function(xhr) {

            $(current).removeAttr('disabled');
            $(parent).find('.error-input').html('<div class="alert alert-danger">Following errors are listed. Please check errors and resubmit the form.</div>').show();
            	$('.error-input').html('');
              var html = '';
              var res = xhr.responseJSON;
              if (typeof res === 'undefined') {
              	$(parent).find('.error-input').html('<div class="alert alert-danger">Something went wrong</div>').show();
              }
             
              	if(typeof res.error_text === 'undefined')
              	{
                	$(current).find('.error-input').html('<div class="alert alert-danger">Something went wrong</div>').show();
                	window.location.href = '#body_part';
                	return;
                }
                else
                {
                	$(current).find('.error-input').html('<div class="alert alert-danger">' + res.error_text + '</div>').show();
                	window.location.href = '#body_part';
                	return;
                }
            
          },'json');
       });
    }); 
    
    </script>