<?php
/*
* date last modified 28-11-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - add_user.html.php
* Mysql Tables - tbl_designation, tbl_staff
*/
include('includes/imgFunctions.php');
include('includes/functions.php'); # general functions

$isEdit = FALSE;
$debug = true;
$curDate = date('Y-m-d H:i:s');

if(isset($_REQUEST['p'])){
	$p = trim($_REQUEST['p']);
} else {
	$p = 1;
}

$typeRs = mysqli_query($con, "SELECT id, name FROM tbl_task_types ORDER BY name");


if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){
	
	$isEdit = TRUE;
	$editId = mysqli_real_escape_string($con, trim($_REQUEST['id']));	
	$editRs = mysqli_query($con, "SELECT * FROM tbl_project WHERE id = '".$editId."'");
	if(mysqli_num_rows($editRs)>0){
		$resEdit = mysqli_fetch_array($editRs);
	} else {
		$error = 'Project does not exist!';
	}

} 

if(isset($_POST['add'])) {
	
	if(isset($_POST['end_date']) && strlen($_POST['end_date'])>0) {
		$end_date = trim($_POST['end_date']);
	} else {
		$error = 'Please select end date';
	}
	
	if(isset($_POST['start_date']) && strlen($_POST['start_date'])>0) {
		$start_date = trim($_POST['start_date']);
	} else {
		$error = 'Please select start date';
	}
		
	if(strlen(trim($_POST['total_amount'])) > 0) {
		$total_amount = trim($_POST['total_amount']);
		if(is_numeric($total_amount) && $total_amount>0){
			//ok
		} else {
			$error = 'Amount should be a numeric value';
		}
	} else {
		$total_amount = 0;
	}
	
	if(strlen(trim($_POST['client_details'])) > 0) {
		$client_details = trim($_POST['client_details']);
	} else {
		$client_details = '';
	}	
		
	if(isset($_POST['description']) && strlen($_POST['description'])>0) {
		$description = trim($_POST['description']);
	} else {
		$description = '';
	}
	
	if(isset($_POST['name']) && strlen($_POST['name'])>0) {
		$name = trim($_POST['name']);
	} else {
		$error = 'Please enter Name of Staff';
	}
	
	if(isset($_POST['type']) && strlen($_POST['type'])>0) {
		$type = trim($_POST['type']);
	} else {
		$error = 'Please select project type';
	}	
	
	
	
	if(empty($error)) {
						
		/* Adding New staff Details */		
		$insertQry = "INSERT INTO tbl_project(type, name, description, client_details, total_amount, date, start_date, end_date, status, added_by) VALUES (
		'".mysqli_real_escape_string($con, $type)."',
		 '".mysqli_real_escape_string($con, $name)."',
		 '".mysqli_real_escape_string($con, $description)."',
		 '".mysqli_real_escape_string($con, $client_details)."',
		 '".mysqli_real_escape_string($con, $total_amount)."',
		 '".mysqli_real_escape_string($con, $curDate)."',
		 '".mysqli_real_escape_string($con, $start_date)."',
		 '".mysqli_real_escape_string($con, $end_date)."',	 
		 'new',
		 '".$_SESSION['adminId']."')";
		
		$execInsert = mysqli_query($con,$insertQry);
		$insert_id = mysqli_insert_id($con);		
		
		if($execInsert) {
					
			$success = 'Project Added Successfully';
			header('Location:index.php?page=manage_projects&p='.$p.'&success='.$success);
			exit();			
		}				
	} 
}

if(isset($_POST['update'])) {
	
	if(isset($_POST['status']) && strlen($_POST['status'])>0) {
		$status = trim($_POST['status']);
	} else {
		$error = 'Please select project status';
	}
	
	if(isset($_POST['end_date']) && strlen($_POST['end_date'])>0) {
		$end_date = trim($_POST['end_date']);
		if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$end_date)) {
		   
		} else {
		    $endArr = explode('-', $end_date);
			$end_date = $endArr[2].'-'.$endArr[1].'-'.$endArr[0];
		}		
	} else {
		$error = 'Please select end date';
	}
	
	if(isset($_POST['start_date']) && strlen($_POST['start_date'])>0) {
		$start_date = trim($_POST['start_date']);	
		if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$start_date)) {
		   
		} else {
		    $startArr = explode('-', $start_date);
			$start_date = $startArr[2].'-'.$startArr[1].'-'.$startArr[0];
		}	
	} else {
		$error = 'Please select start date';
	}
	
	if(strlen(trim($_POST['total_amount'])) > 0) {
		$total_amount = trim($_POST['total_amount']);
		if(is_numeric($total_amount) && $total_amount>0){
			//ok
		} else {
			$error = 'Amount should be a numeric value';
		}
	} else {
		$total_amount = 0;
	}
	
	if(strlen(trim($_POST['client_details'])) > 0) {
		$client_details = trim($_POST['client_details']);
	} else {
		$client_details = '';
	}	
		
	if(isset($_POST['description']) && strlen($_POST['description'])>0) {
		$description = trim($_POST['description']);
	} else {
		$description = '';
	}
	
	if(isset($_POST['name']) && strlen($_POST['name'])>0) {
		$name = trim($_POST['name']);
	} else {
		$error = 'Please enter Name of Staff';
	}
	
	if(isset($_POST['type']) && strlen($_POST['type'])>0) {
		$type = trim($_POST['type']);
	} else {
		$error = 'Please select project type';
	}		
	
	if(empty($error)) {
		
		$curDate = date('Y-m-d H:i:s');					
			
		$updateQry = "UPDATE tbl_project SET type = '".$type."', name = '".$name."', description = '".$description."', client_details = '".$client_details."', 
		total_amount = '".$total_amount."', start_date = '".$start_date."', end_date = '".$end_date."', status = '".$status."', updated_by = '".$_SESSION['adminId']."', updated_on = '".$curDate."'  WHERE id = '".$editId."'";
		
		$execUpdate = mysqli_query($con,$updateQry);			
		
		if($execUpdate) {
			$success = 'Project Updated Successfully';
			header('Location:index.php?page=manage_projects&p='.$p.'&success='.$success);
			exit();			
		}				
	} 
}

?>