<?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;
$curDate = date('Y-m-d H:i:s');

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

if(isset($_REQUEST['project_id']) && $_REQUEST['project_id']!=''){	
	$projectId = mysqli_real_escape_string($con, trim($_REQUEST['project_id']));		
} else {
	$error = 'Unable to fetch project id';
	header('Location:index.php?page=manage_projects&p='.$p.'&error='.$error);
}


if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){
	
	$isEdit = TRUE;
	$editId = mysqli_real_escape_string($con, trim($_REQUEST['id']));	
	$editRs = mysqli_query($con, "SELECT * FROM tbl_sub_task WHERE id = '".$editId."'");
	if(mysqli_num_rows($editRs)>0){
		$resEdit = mysqli_fetch_array($editRs);
	} else {
		$error = 'Project does not exist!';
	}
	
	if(isset($_REQUEST['action']) && $_REQUEST['action']=='mark_complete'){
		$update = mysqli_query($con, "UPDATE tbl_sub_task SET status = 'completed' WHERE id = '".$editId."' AND project_id = '".$projectId."'");
		if($update){
			$success = 'Marked the task as completed';
			header('Location:index.php?page=add_sub_task&project_id='.$projectId.'&success='.$success);
			exit();
		}
	}

} 

if(isset($_POST['add'])) {
		
	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(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(empty($error)) {
						
		/* Adding New staff Details */		
		$insertQry = "INSERT INTO tbl_sub_task(project_id, name, description, total_amount, date, status, added_by) VALUES (
		'".mysqli_real_escape_string($con, $projectId)."',
		 '".mysqli_real_escape_string($con, $name)."',
		 '".mysqli_real_escape_string($con, $description)."',		 
		 '".mysqli_real_escape_string($con, $total_amount)."',
		 '".mysqli_real_escape_string($con, $curDate)."',	 
		 'Active',
		 '".$_SESSION['adminId']."')";
		
		$execInsert = mysqli_query($con,$insertQry);
		$insert_id = mysqli_insert_id($con);		
		
		if($execInsert) {
					
			$success = 'Project Added Successfully';
			header('Location:index.php?page=add_sub_task&project_id='.$projectId.'&success='.$success);
			exit();			
		}				
	} 
}

if(isset($_POST['update'])) {
	
	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(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(empty($error)) {
		
		$curDate = date('Y-m-d H:i:s');					
		$updateQry = "UPDATE tbl_sub_task SET name = '".$name."', description = '".$description."', total_amount = '".$total_amount."', 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=add_sub_task&project_id='.$projectId.'&success='.$success);
			exit();			
		}				
	} 
}

$tasksRs = mysqli_query($con, "SELECT * FROM tbl_sub_task WHERE project_id = '".$projectId."' ORDER BY id DESC");

?>