<?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 subcategory extends database
{
   public function index($page)
    {
		$start = $page - 1;
		return $this->fetchAll('subcategory', null, 'cl_created_at', 'DESC', ($start * 10), 10);
	}
	 public function GetData($id)
    {
		$where['id']=$id;
		return $this->fetchRow('category',$where);
	}
}
$msg='';
if(isset($_GET['message']))
{
	$msg=$_GET['message'];
}
$page = 1;
if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 1)
{
    $page = htmlspecialchars($_GET['page']);
}
$dev = new subcategory();	
$res=$dev->index($page);
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">All Subcategories</h2>
                                    <a class="btn btn-success pull-right" href="add-subcategory.php">Add Subcategory</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" id="myTable">
                                        <thead>
                                            <tr>
                                                <th>Name</th>
												<th>Category</th>
												<th>Description</th>
												<th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
										<?php
                                         foreach($res as $d)
											{
												$c=$dev->GetData($d['cat_id']);
											?>
											<tr>
											<td><?php echo htmlspecialchars($d['name']);?></td>
												<td><?php echo htmlspecialchars($c['name']);?></td>
												<td><?php echo htmlspecialchars($d['description']);?></td>
												<td><a id="edit" href="edit-subcategory?id=<?php  echo htmlspecialchars($d['id']); ?>" class="btn btn-primary"><i class="fa fa-edit"></i></a> <a data-href="subcat-remove?id=<?php  echo htmlspecialchars($d['id']); ?>" class="remove btn btn-warning">Remove</a></td>
											</tr>
											<?php
											}
											?>
                                           
                                        </tbody>
                                    </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(){
        $('.remove').click(function(){
            var parent = $(this).parents('tr').first();
            var conf = confirm("Are you sure you want to delete this category?");
            if(conf)
            {
                var href = $(this).attr('data-href');
                $.post(href, function(res){
                    alert("Deletion Successful");
                    $(parent).remove();
                },'json').fail(function(xhr){
                   alert(xhr.responseJSON);
                },'json');
            }
        });
    });
	
    </script>
						
						
                       <script>
						$(document).ready(function(){
							$('.active').removeClass('active');
							$('#subcat-menu').addClass('active');
						}); 
						</script>              

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