<?php

//include sql connection
include ('' . $config['LocalPath'] . '/includes/.sql_con.php');

//check if form has been submited. 
	if(isset($_POST['task_id'])){
	        $insert_task_id=$_POST['task_id'];
	        $insert_job_id=$_POST['job_id'];
	        $insert_due_date=$_POST['due_date'];
	        $insert_date_completed=$_POST['date_completed'];
	        $insert_job_status_id=$_POST['job_status_id'];
	        $insert_note=$_POST['note'];
	        $insert_user=$_POST['user'];
		$insert_task_name=$_POST['task_name'];
		$insert_device_name=$_POST['device_name'];
		$insert_in_progress=$_POST['inp'];

if($insert_in_progress=='0'){
	                $update_task_status_in_progress="UPDATE `TASK` SET JOB_STATUS_ID = '8' WHERE ID=$insert_task_id";
                                        mysql_query($update_task_status_in_progress)or die ('Failed to update</br>'.mysql_error());
}

?>
<div class="row">
	<div class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
		<div class="card card-mini col-md-offset-7 col-lg-offset-7 col-sm-offset-7 col-xs-offset-7">
			<div class="card-header">
				<div class="card-title">
					<b>Complete The Task "<?php echo $insert_task_name;?>"</br> For "<?php echo $insert_device_name;?>"</b>
				</div>
			</div>
			<div class="card-body">
				<h6>If you have issues completing a task please use the note field below to describe the issue. If the task has been completed with no issue Please leave the Note field below blank.</h6><br/>
				<form class="form form-horizontal" method="post" id="form">
					<div class="form-group">
						<div class="col-md-3">
						<label class="col-md-5 control-label"><b>Add Note</b></label>
						</div>
							<div class="col-md-5">
								<input type="textbox" name="insert_note" value="Note:" class="form-control">
								<input type="hidden" name="insert_task_id" value="<?php echo $insert_task_id;?>">
								<input type="hidden" name="insert_job_id" value="<?php echo $insert_job_id;?>">
								<input type="hidden" name="insert_due_date" value="<?php echo $insert_due_date;?>">
								<input type="hidden" name="insert_date_completed" value="<?php echo $insert_date_completed;?>">
								<input type="hidden" name="insert_job_status_id" value="<?php echo $insert_job_status_id;?>">
								<input type="hidden" name="insert_user" value="<?php echo $insert_user;?>">
							</div>
					</div>
					<div class="form-footer">
						<div class="col-md6 col-md-offset-4">
								<button type="submit" class="btn btn-primary">Complete Task</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>
			
<?php
}

if(isset($_POST['insert_task_id'])){

	if ($_POST['insert_note'] != "Note:"){
	//if(isset($_POST['insert_note'])){
			
	        $complete_task_id=$_POST['insert_task_id'];
                $complete_job_id=$_POST['insert_job_id'];
                $complete_due_date=$_POST['insert_due_date'];
                $complete_date_completed=$_POST['insert_date_completed'];
                $complete_job_status_id="3";
                $complete_note=$_POST['insert_note'];
                $complete_user=$_POST['insert_user'];
                $complete_task_name=$_POST['insert_task_name'];
                $complete_device_name=$_POST['insert_device_name'];

		$complete_task_with_note="INSERT INTO `JOB_LOGGING` (TASK_ID, JOB_ID, DUE_DATE, DATE_COMPLETED, STATUS_ID, NOTE, USER) VALUES ('$complete_task_id' , '$complete_job_id', '$complete_due_date', '$complete_date_completed', '$complete_job_status_id', '$complete_note', '$complete_user')";
			mysql_query($complete_task_with_note)or die ('Failed to insert</br>'.mysql_error());

		$update_task_status_complete_with_note="UPDATE `TASK` SET JOB_STATUS_ID = '3' , LAST_COMPLETED = NOW() WHERE ID=$complete_task_id";
                                        mysql_query($update_task_status_complete_with_note)or die ('Failed to update</br>'.mysql_error());

$URL="". $config['URL'] ."/tasks.php";
echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";
echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';

}
 if ($_POST['insert_note'] == "Note:"){
                $complete_task_id=$_POST['insert_task_id'];
                $complete_job_id=$_POST['insert_job_id'];
                $complete_due_date=$_POST['insert_due_date'];
                $complete_date_completed=$_POST['insert_date_completed'];
                $complete_job_status_id="1";
                $complete_note="";
                $complete_user=$_POST['insert_user'];
                $complete_task_name=$_POST['insert_task_name'];
                $complete_device_name=$_POST['insert_device_name'];

                $complete_task_without_note="INSERT INTO `JOB_LOGGING` (TASK_ID, JOB_ID, DUE_DATE, DATE_COMPLETED, STATUS_ID, NOTE, USER) VALUES ('$complete_task_id' , '$complete_job_id', '$complete_due_date', '$complete_date_completed', '$complete_job_status_id', '$complete_note', '$complete_user')";
					mysql_query($complete_task_without_note)or die('Failed to insert</br>'.mysql_error());

                $update_task_status_complete_without_note="UPDATE `TASK` SET JOB_STATUS_ID = '1', LAST_COMPLETED = NOW() WHERE ID=$complete_task_id";
                                        mysql_query($update_task_status_complete_without_note)or die ('Failed to update</br>'.mysql_error());
$URL="". $config['URL'] ."/tasks.php";
echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";
echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';

}
}
