<?php
$is_done = false;
//// insert / update into database table
if(isset($_POST['edit_id'])){
	$creation_date = date("Y-m-d");
	$spec_preparedby = ($_SESSION['user_id']==0) ? $_SESSION['id'] : $_SESSION['user_id'];
	if($_POST['edit_id'] == ""){
	////// insert records
		$sql = "INSERT INTO `specs_scope` 
			   (`title`, `spec_project`, `spec_client`, `WBSID`, `spec_consultant`, `companyID`, `dateCreated`, `spec_preparedby`, `spec_status`) 
			   VALUES ('".$_POST['briefname']."', '".$_POST['projname']."', '".$_POST['clientname']."',  '".$_POST['wbs']."',  '".$_POST['consname']."', '".$companyID."', '".$creation_date."', '".$spec_preparedby."', '1')";
		mysql_query($sql) or die("Failed to Add Data : ".mysql_error());
		$msg = "Data has been added successfully";		
	}else{
	////// update records
		$sql = "UPDATE `specs_scope` SET 
				`title` = '".$_POST['briefname']."',
				`spec_project` = '".$_POST['projname']."',
				`spec_client` = '".$_POST['clientname']."',
				`spec_consultant` = '".$_POST['consname']."'				
				WHERE `SPECID` = ".$_POST['edit_id']."";
		mysql_query($sql) or die("Failed to Update : ".mysql_error());
		$msg = "Data has been updated successfully";	
	}
	$is_done = true;
}
//// end insert / update into database table
//// select record for edit 
if(isset($_REQUEST['update']) && $_REQUEST['update']){
	$result = mysql_query("SELECT * FROM specs_scope WHERE `SPECID` = '".$_REQUEST['update']."'");
	if(mysql_num_rows($result)){
		$specs_scope = mysql_fetch_array($result);
	}
}
//// end select record for edit
//// start delete record
if(isset($_REQUEST['del']) && $_REQUEST['del'] != ''){
	mysql_query("UPDATE `specs_scope` SET `spec_status` = '2' WHERE `SPECID` = ".$_REQUEST['del']."");
	$msg = "Data has been deleted successfully";
	$is_done = true;
}
//// end delete record
//// change status to 0 or 1
if(isset($_REQUEST['statusId']) && $_REQUEST['statusId'] != '' && isset($_REQUEST['status']) && $_REQUEST['status'] != ""){	
	$sql = "UPDATE `specs_scope` SET `spec_status` = '".$_REQUEST['status']."' WHERE `SPECID` = ".$_REQUEST['statusId']."";
	mysql_query($sql) or die("Failed to Update Status : ".mysql_error());
	$msg = "Status has been updated successfully";
	$is_done = true;
}
//// end change status to 0 or 1
if($is_done){
	echo '<script>window.location="03-05Scope-01Specifications01.php";</script>';
}
?>