<?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 edit_devices extends database
{
   public function index($data,$id)
    {
		$where['id']= $id;
		return $this->update('category',$data, $where);
	}
	public function Getdata($id)
    {
		$where['id']= $id;
		return $this->fetchRow('category', $where);
	}
}
$dev = new edit_devices();	
$msg="";
if(!isset($_GET['id']))
{
	  header('Location:category.php');
}
$id=$_GET['id'];
if(isset($_POST['data']))
{
	$data=$_POST['data'];
	if(trim($data['name']) == '')
	{
		$msg.= "Category name is required<br/>";
	}
        if(trim($data['description']) == '')
	{
		$msg.= "Description is required<br/>";
	}
	 if(isset($_FILES['image']['tmp_name']) && is_uploaded_file($_FILES['image']['tmp_name']))
    {
        
        $target_dir = "uploads/cenas-category/";
        $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.";
            }
        }
    }
	if($msg=="")
	{
		 $res = $dev->index($data,$id);
            if($res)
            {
                    header('Location:category.php?message=Category Updated Successfully');
            }
            else {
                $msg = 'Something Went Wrong';
            }
	}
}
 $res1 = $dev->Getdata($id);
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">Edit devices</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 for="Name" class=" form-control-label">Category Name</label>
                                            <input type="text" name="data[name]" id="Name" placeholder="Category Name" class="form-control" value="<?php echo htmlspecialchars($res1['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 echo htmlspecialchars($res1['description']);?>" />
                                        </div>
                                         <div class="form-group">
                                              <label>Icon Image</label>
												<input type="file" name="image" class=" form-control"/>
											Current Image : <img src="<?php echo htmlspecialchars($res1['image']) ?>" width="150"/>
                                        </div>
										<div class="form-group">
										<button type="submit" class="track-btn pull-right">Submit</button>
										
										</div>
                                        
                                        </form>
                                    </div>
                                </div>
                            </div>
                          
                        </div>
                        
                    <script>
							$(document).ready(function(){
								$('.active').removeClass('active');
								$('#cat-menu').addClass('active');
							}); 
						</script>      
                       

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