<?php 

	$xuid	= $_GET['xuid'];
	$lang 	= $_GET['lang'];	
	$xfdc 	= $_GET['xfdc'];
	
	// CHECK FOR FORMS NOT HAVING xfdc on url
	$nfid = 1;

		include('config.php');
		include('field_names.php'); 
		include('users.php');	


include_once('nf_database.php');
include_once('nf_paginator.class.php');
define('SS_DB_NAME', $data_db);
define('SS_DB_USER', $data_user);
define('SS_DB_PASSWORD', $data_pswd);
define('SS_DB_HOST', $data_server);

$dsn	= 	"mysql:dbname=".SS_DB_NAME.";host=".SS_DB_HOST."";
$pdo	=	"";
try {
	$pdo = new PDO($dsn, SS_DB_USER, SS_DB_PASSWORD);
}catch (PDOException $e) {
	echo "Connection failed: " . $e->getMessage();
}
$db 	=	new Database($pdo);
$pages 	=   new Paginator();

// Change character set to utf8
$pdo->query("SET NAMES 'utf8'");

$gcrud = 'CRUD';

?>

<?php 						
	// GET MAX PROJECT ID
include('r_dbc.php');
 		$sql = "select max(pid) as pidmax
		from ccprojects";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
      while($row = mysqli_fetch_assoc($result)) {
		$pidmax 	= $row["pidmax"]+1;		
		}
	}
mysqli_close($conn);		
		
?>

<?php 
if(isset($_REQUEST['submit']) and $_REQUEST['submit']!=""){
	extract($_REQUEST);
	if($desc_project==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue');
		exit;
	}
	else{
		

			$pid = $pidmax;
			$logopic = "project.png";
			$geochart_region = 0;
			$septh = ".";
			$sepdc = ",";
			$totdc = 2;
			$totdcqty = 2;
			$umqtyuc = 1;
			$stockrep = 0;
			$invoiceon = 0;
			
			$data	=	array(
							'pid'=>$pid,
							'desc_project'=>$desc_project,
							'logopic'=>$logopic,
							'geochart_region'=>$geochart_region,						
							'septh'=>$septh,							
							'sepdc'=>$sepdc,
							'totdc'=>$totdc,
							'totdcqty'=>$totdcqty,
							'umqtyuc'=>$umqtyuc,
							'stockrep'=>$stockrep,
							'invoiceon'=>$invoiceon,
							'project_short'=>$desc_project
						);
			$user_role = "admins";			

			$data1	=	array(
							'username'=>$xuid,
							'pids'=>$pid,
							'user_role'=>$user_role
						);
						
						
			$mid 			= 1;
			$desc_manager	= "Budget";
			$budget			= 1;
			$expenses		= 0;
			$rep_column		= "A";
			$desc_rep		= "A.Budget";
			$desc_rep_stock	= "Budget";
			$delta_col		= 0;
			$delta_perc		= 0;
			$menu_order		= 1;
			
			$datam1	=	array(
							'pid'=>$pid,
							'mid'=>$mid,
							'desc_manager'=>$desc_manager,
							'budget'=>$budget,
							'expenses'=>$expenses,
							'rep_column'=>$rep_column,
							'desc_rep'=>$desc_rep,
							'desc_rep_stock'=>$desc_rep_stock,
							'delta_col'=>$delta_col,
							'delta_pm'=>$delta_pm,
							'delta_calc'=>$delta_calc,
							'delta_perc'=>$delta_perc,
							'menu_order'=>$menu_order		
					);						

			$mid 			= 2;
			$desc_manager	= "Expenses";
			$budget			= 0;
			$expenses		= 1;
			$rep_column		= "B";
			$desc_rep		= "B.Expenses";
			$desc_rep_stock	= "Expenses";
			$delta_col		= 0;
			$delta_perc		= 0;
			$menu_order		= 2;
			
			$datam2	=	array(
							'pid'=>$pid,
							'mid'=>$mid,
							'desc_manager'=>$desc_manager,
							'budget'=>$budget,
							'expenses'=>$expenses,
							'rep_column'=>$rep_column,
							'desc_rep'=>$desc_rep,
							'desc_rep_stock'=>$desc_rep_stock,
							'delta_col'=>$delta_col,
							'delta_pm'=>$delta_pm,
							'delta_calc'=>$delta_calc,
							'delta_perc'=>$delta_perc,
							'menu_order'=>$menu_order		
					);						

			$mid 			= 3;
			$desc_manager	= "A-B";
			$budget			= 0;
			$expenses		= 0;
			$rep_column		= "C";
			$desc_rep		= "C=A-B";
			$desc_rep_stock	= "A-B";
			$delta_col		= 1;
			$delta_pm		= "M";
			$delta_calc		= "1,2";
			$delta_perc		= 1;
			$menu_order		= 0;
			
			$datam3	=	array(
							'pid'=>$pid,
							'mid'=>$mid,
							'desc_manager'=>$desc_manager,
							'budget'=>$budget,
							'expenses'=>$expenses,
							'rep_column'=>$rep_column,
							'desc_rep'=>$desc_rep,
							'desc_rep_stock'=>$desc_rep_stock,
							'delta_col'=>$delta_col,
							'delta_pm'=>$delta_pm,
							'delta_calc'=>$delta_calc,
							'delta_perc'=>$delta_perc,
							'menu_order'=>$menu_order		
					);						

			$insert	=	$db->insert('ccprojects',$data);
			$insert	=	$db->insert('members_projects',$data1);	
			$insert	=	$db->insert('ccmanager_id',$datam1);	
			$insert	=	$db->insert('ccmanager_id',$datam2);	
			$insert	=	$db->insert('ccmanager_id',$datam3);	

			
			if($insert){
				header('location:projects.php?msg=ras&xuid='.$xuid.'&xfdc='.$xfdc.'&lang='.$lang);
				exit;
			}else{
				header('location:projects.php?msg=rna&xuid='.$xuid.'&xfdc='.$xfdc.'&lang='.$lang);
				exit;
			}
		
	}
}
?>

<!doctype html>

<html lang="en-US" class="no-js">

<head>

	<meta charset="UTF-8">

	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

	<title><?php echo $nadd_new." ".$nproject ?></title>

	

    <link rel="stylesheet" href="./bootstrap/css/bootstrap.css" >
    <link rel="stylesheet" href="./fontawesome/css/all.css" >
	

</head>



<body>

	

  	<div class="container">


		<?php

		if(isset($_REQUEST['msg']) and $_REQUEST['msg']=="un"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> $field_mand </div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="ue"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> $field_mand </div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="up"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> $field_mand </div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="ras"){

			echo	'<div class="alert alert-success"><i class="fa fa-thumbs-up"></i> $recadded </div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="rna"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> $recaddedno <strong> $tryagain </strong></div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="dsd"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> Please delete a user and then try again <strong>We set limit for security reasons!</strong></div>';

		}

		?>

		<div class="card">

			<div class="card-header"><i class="fa fa-fw fa-plus-circle"></i> <strong><?php echo $nadd_new." ".$nproject  ?></strong> </div>

			<div class="card-body">

				

				<div class="col-sm-6">

					<h6 class="card-title"><?php echo $nfields_with ?> <span class="text-danger">*</span> <?php echo $nmandatory ?></h6>

					<form method="post">
					
						<div class="mb-3">

							<label><?php echo $nproject ?><span class="text-danger">*</span></label>

							<input type="text" name="desc_project" id="desc_project" class="form-control" placeholder="<?php echo $nadd." ".$nproject." ".$ndesc ?>" required>

						</div>						
					

							<input type="hidden" id="xuid" name="xuid" value="<?php echo $xuid ?>" >
							<input type="hidden" id="lang" name="lang" value="<?php echo $lang ?>" >						

						<div>
							<button type="submit" name="submit" value="submit" id="submit" class="btn btn-primary"><i class="fa fa-fw fa-plus-circle"></i><?php echo $nadd." ".$nproject ?></button>

						</div>

					</form>

				</div>

			</div>

		</div>

	</div>



    <script src="./jquery-3.4.1.js"></script>
    <script src="popper.min.js" ></script>
    <script src="./bootstrap/js/bootstrap.min.js" ></script>    


    

</body>

</html>