<?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 add_devices extends database
{
   public function index($data)
    {
		return $this->insert('subcategory',$data);
	}
	public function getCat()
    {
		return $this->fetchAll('category');
	}
}
$dev = new add_devices();	
$msg="";
if(isset($_GET['message']))
{
	$msg=$_GET['message'];
}
if(isset($_POST['data']))
{
	$data=$_POST['data'];
	if(trim($data['cat_id']) == '')
	{
		$msg.= "Select any category<br/>";
	}
	if(trim($data['name']) == '')
	{
		$msg.= "Subcategory name is required<br/>";
	}
        if(trim($data['description']) == '')
	{
		$msg.= "Description is required<br/>";
	}
	if($msg=="")
	{
		 $res = $dev->index($data);
            if($res)
            {
                    header('Location:subcategory.php?message=Subcategory Added Successfully');
            }
            else {
                $msg = 'Something Went Wrong';
            }
	}
}
 $cat = $dev->getCat();
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">Add Subcategory</h2>
                                    
                                </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-10">
                                <div class="card">
                                    
                                    <div class="card-body card-block">
									<form method="post">
										<div class="form-group">
											<label>Category</label>
												<select name="data[cat_id]" class="form-control">
												<option value="">--- Select ---</option>
												<?php
													foreach($cat as $c)
													{
												?>
												<option value="<?php echo $c['id'];?>"><?php echo $c['name'];?></option>
												<?php
												}
												?>
												</select>
										</div>
                                        <div class="form-group">
                                            <label for="Name" class=" form-control-label">Subcategory Name</label>
                                            <input type="text" name="data[name]" id="Name" placeholder="Subcategory Name" class="form-control" value="<?php if(isset($data['name'])){echo $data['name'];}?>" />
                                        </div>
                                        <div class="form-group">
                                            <label for="Description" class=" form-control-label">Description</label>
                                            <input type="text" id="Description" name="data[description]" placeholder="Description" class="form-control" value="<?php if(isset($data['description'])){echo $data['description'];}?>" />
                                        </div>
                                        
										<div class="form-group">
										<button type="submit" class="track-btn pull-right">Add</button>
										
										</div>
                                        
                                        </form>
                                    </div>
                                </div>
                            </div>
                          
                        </div>
                        
                    <script>
							$(document).ready(function(){
								$('.active').removeClass('active');
								$('#subcat-menu').addClass('active');
							}); 
						</script>      
                       

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