<?php

require_once '../library/config.php';
require_once '../library/functions.php';

checkUser();

$action = isset($_GET['action']) ? $_GET['action'] : '';

switch ($action) {
	
	case 'add' :
		addFile();
		break;
		
	case 'modify' :
		modifyFile();
		break;
		
	case 'delete' :
		deleteClient();
		break;
  case 'deleteimg' :
		deleteImage();
		break;
   case 'deleteimg1' :
		deleteImage1();
		break;		
	case 'deleteimg2' :
		deleteImage2();
		break;			
	default :
	    // if action is not defined or unknown
		// move to main user page
		header('Location: index.php');
}


function addFile()
{	
	@$title   = $_REQUEST["txttitle"];	
	@$thumb   = uploadProductImage('txtImage', SRV_ROOT . '../ImgProduct/');
	@$detail   = $_REQUEST["txtdetail"];
	 
    $sql   = "INSERT INTO why_solar(whytitle, whyimage, whydetail)
		      VALUES ('$title', '$thumb', '$detail')";
		
		//die();
		dbQuery($sql);
		header('Location: index.php');	
	}

/*
	Modify a user
*/
function modifyFile()
{	
    $fileId   = $_REQUEST['hidFileId'];
	@$title   = $_REQUEST["txttitle"];	
	@$thumb   = uploadProductImage('txtImage', SRV_ROOT . '../ImgProduct/');
	@$detail   = $_REQUEST["txtdetail"];

    $sql = "SELECT * from why_solar Where whyId = '$fileId'";
	$res = dbQuery($sql);
	$row = dbFetchAssoc($res);
	
   if ($thumb=='') {
    // leave the image as it was
		$thumb = $row["whyimage"];
	}
 		
	 $sql5 ="update why_solar set whytitle = '$title', whyimage = '$thumb', whydetail = '$detail' where whyId = $fileId";
		//die();
		 dbQuery($sql5);
		
		header('Location:index.php');	
}
/*
	Remove a user
*/
function deleteClient()
{
	if (isset($_GET['clientId']) && (int)$_GET['clientId'] > 0) {
		$clientId = (int)$_GET['clientId'];
	} else {
		header('Location: index.php');
	}
	
	
	$sql = "DELETE FROM why_solar 
	        WHERE whyId = $clientId";
	dbQuery($sql);
	
	header('Location: index.php');
}

/*********************************************************88
	Upload an file and return the uploaded file name 
*/

function uploadProductImage($inputName, $uploadDir)
{

	$image = $_FILES[$inputName];
	$imagePath = '';
	
		$imagePath =   $image['name']; 
		
		$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
	
	return $imagePath;
}
?>