<?php 

// Include main file
include_once('../libs/main.php');

// Check admin Login
include_once('checkLogin.php');

$successMsg = '';

if(isset($_REQUEST['successMsg'])){
	$successMsg = $_REQUEST['successMsg'];
}

if(isset($_REQUEST['failedMsg'])){
	$failedMsg = $_REQUEST['failedMsg'];
}

// Logical section of package
$packagesObj =  new  packages();

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['package_id'])){
	$packagesDetail = $packagesObj->fromId($_REQUEST['package_id']);
}


if(isset($_POST['hiddenSubmit'])){
	
	if(isset($_FILES['package_image']) && $_FILES['package_image']['name']!=''){
		$image=$_FILES['package_image']['name'];
		$image=time().'_'.$image;
		$extension = strtolower(getExtension($image));
		
		if($extension!='jpg' and $extension!='gif' and $extension!='jpeg' and $extension!='png'){
			header('location:addpackage.php?$failedMsg=Invalid image extension.');
			exit;
		}
		
		move_uploaded_file($_FILES['package_image']['tmp_name'], DIR_UPLOAD_BANNER.$image);		
		$packagesData->package_image	= $image;	
	}else{
		$packagesData->package_image	= $_POST['uploaded_file'];	
	}
	
	$packagesData->package_title 		= $_POST['package_title'];	
	$packagesData->package_slug 		= $_POST['package_slug'];	
	$packagesData->package_desc 		= addslashes($_POST['package_desc']);
	$packagesData->package_short_desc 		= $_POST['package_short_desc'];
	$packagesData->package_meta_title 	= $_POST['package_meta_title'];	
	$packagesData->package_meta_keywords 	= $_POST['package_meta_keywords'];	
	$packagesData->package_meta_desc 		= $_POST['package_meta_desc'];
	$packagesData->package_status 		= $_POST['package_status'];	
	//print_r($packagesData); exit;
	// edit packages details 
	if(isset($_POST['package_id'])){

		$packagesData->package_id = $_POST['package_id'];
		
		$packageD = $packagesObj->fromId($packagesData->package_id);
		
		if(isset($_FILES['package_image']) && $_FILES['package_image']['name']!=''){
			if($packageD->package_image!='' && file_exists(DIR_UPLOAD_BANNER.$packageD->package_image)){
				unlink(DIR_UPLOAD_BANNER.$packageD->package_image);
			}
		}	
		
		$rs = $packagesObj->updateRecords($packagesData);
		if($rs){
			$successMsg ="package Details Updated successfully";			
			header('Location:packagelist.php?&successMsg='.$successMsg);
			exit;
		}
		
	// add packages details 	
	}else{
		
		$lastOrder = $packagesObj->getLastOrder();
		$packagesData->package_order = $lastOrder+1;
		
		$rs = $packagesObj->addRecords($packagesData);
		if($rs){
			$successMsg ="package Details Added successfully";			
			header('Location:packagelist.php?successMsg='.$successMsg);
			exit;
		}
	}
}

//$allpackages = $packagesObj->getAllRecords();

// Include Template 
include_once('../libs/routs.php')

?>