<?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_cenas extends database
{
   public function index($data)
    {
		return $this->insert('cenas_main',$data);
	}
	public function getCat()
    {
		return $this->fetchAll('category');
	}
	public function getsubCat()
    {
		return $this->fetchAll('subcategory');
	}
}
$obj = new add_cenas();	
$msg="";
if(isset($_GET['message']))
{
	$msg=$_GET['message'];
}
if(isset($_POST['data']))
{
	$data=$_POST['data'];
	if(trim($data['name']) == '')
	{
		$msg.= "Name is required<br/>";
	}

    if(trim($data['cat_id']) == '')
    {
        $msg.= "Category is required<br/>";
    }

    if(!isset($data['subcat_id']) || trim($data['subcat_id']) == '')
    {
        $msg.= "Sub Category is required<br/>";
    }
      
    if(isset($_FILES['image']['tmp_name']) && is_uploaded_file($_FILES['image']['tmp_name']))
    {
        
        $target_dir = "uploads/";
        $randName = date('dmYhis') . rand(10000,99999) . basename($_FILES['image']["name"]);
        $target_file = $target_dir . $randName;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        // Check if image file is a actual image or fake image

        $check = getimagesize($_FILES['image']["tmp_name"]);
        if($check !== false) {

        } else {
            $msg.= "File is not an image.";
            $uploadOk = 0;
        }

        // Check if file already exists
        if (file_exists($target_file)) {
            $msg.=  "Sorry, file already exists.";
            $uploadOk = 0;
        }
        // Check file size
        if ($_FILES['image']["size"] > 500000000) {
            $msg.=  "Sorry, your file is too large.";
            $uploadOk = 0;
        }
        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        && $imageFileType != "gif" ) {
            $msg.=  "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $uploadOk = 0;
        }
        // Check if $uploadOk is set to 0 by an error
        if(trim($msg) == '')
        {
            if (move_uploaded_file($_FILES['image']["tmp_name"], $target_file)) {
                $uploadOk = 1;
                $data['image'] = $target_file;
            } else {
                $msg.=  "Sorry, there was an error uploading your file.";
            }
        }
    }
    else
    {
        $msg.=  "Image Not Uploaded";
    }


    if(trim($msg) == '')
    {
		$res = $obj->index($data);
        if($res)
        {
                header('Location:list_cenas_table_row.php?message=Cenas Added Successfully&id=' . $res);
                exit;
        }
        else {
            $msg = 'Something Went Wrong';
        }
    }
	
}
$cat = $obj->getCat();
$scat = $obj->getsubCat();
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 Cenas</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" enctype="multipart/form-data">
                                        <div class="form-group">
                                            <label> Name</label>
											<input type="text" name="data[name]" class=" form-control" placeholder="Name" value="<?php if(isset($data['name'])){echo $data['name'];} ?>"/>
                                        </div>
                                        <div class="form-group">
                                              <label> Image</label>
												<input type="file" name="image" class=" form-control"/>
                                        </div>
                                        <div class="form-group">
											<label>Category</label>
											<select name="data[cat_id]" id="category_id" class=" form-control">
												<option value="">--- Select Category---</option>
												<?php
												foreach($cat as $c)
												{
												?>
												   <option value="<?php echo $c['id'];?>" <?php if(isset($data['cat_id']) && $data['cat_id'] == $c['id']){echo 'selected';} ?>><?php echo $c['name'];?></option>
												<?php
												}
												?>
											</select>
										</div>
										<div class="form-group">
											<label>Subcategory</label>
											<select name="data[subcat_id]" id="sub_category_id" class=" form-control">
												
											</select>
										</div>
										
										<div class="form-group">
										<button type="submit" class="track-btn pull-right">Add</button>
										
										</div>
                                        
                                        </form>
                                    </div>
                                </div>
                            </div>
                          
                        </div>
                        
                    <script>
							function appendHtml(html)
							{
								$('#sub_category_id').html(html);
							}
							
							$(document).ready(function(){
								$('.active').removeClass('active');
								$('#cenasm-menu').addClass('active');
							
							
							$('#category_id').change(function(){
								var categoryID = $(this).val();
								$('#sub_category_id').html('');
								$.get('get_sub_category', {categoryID:categoryID}, function(res, value){
									console.log(res);
									var html = '<option value="">---Select Sub Category----</option>';;
									$(res).each(function(i, value){
										html+= '<option value="' + value.id + '">' + value.name + '</option>';
									});
									appendHtml(html);
								},'json');
							})
						}); 
						</script>      
                       

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