<?php
include('../headertop.php');
include('../db.php');
//include('../datepicker.php');

$disp = '';
$pagetitle = 'Add New Project';

if(!isset($_POST['addprojectsubmit']))
{
echo '<meta http-equiv="refresh" content="0;URL=addproject.php">';
exit;
}

$project_name = $_POST['project_name'];
$no_floors = $_POST['no_floors'];
$project_details = $_POST['project_details'];
$start_date = $_POST['start_date'];
$project_address = $_POST['project_address'];
$type = $_POST['type'];
$no_flats = $_POST['no_flats'];
$flats = $_POST['flats'];


if(empty($project_name) or empty($start_date) or empty($type))
{
$disp .= '<span class="errordiv">Please go back and fill all required fields</span>';
include('../template.php');
exit;
}

/*
if(!is_numeric($no_floors))
{
$disp .= '<span class="errordiv">Number of floors should be numeric value</span>';
include('../template.php');
exit;
}
*/

$q = "insert into project (project_name,project_address, project_details, start_date, no_floors, no_flats,flats,type) values('$project_name','$project_address','$project_details','$start_date','$no_floors','$no_flats','$flats','$type')";
mysqli_query($dbcon,$q) or die(mysqli_error($dbcon));
$pid = mysqli_insert_id($dbcon);

if(!is_dir("../files/expenses/".$pid))
{
mkdir("../files/expenses/".$pid);
}

if(!is_dir("../files/payments/".$pid))
{
mkdir("../files/payments/".$pid);
}

$sm = "Project information saved ";

if(!empty($_FILES['photo']['type']))
{
if($_FILES['photo']['type'] != 'image/jpeg' and $_FILES['photo']['type'] != 'image/png')
{
$disp .= '<span class="error">Please upload only image files</span>';
include('../template.php');
exit;
}


$photoname = explode(".",$_FILES['photo']['name']);
foreach($photoname as $v)
$photoext = $v;

$photolocation = "../files/projectphotos/".$pid.".".$photoext;
$temp = $_FILES['photo']['tmp_name'];


if(!move_uploaded_file($temp,$photolocation))
$sm .= ' but picture could not be uploaded';
else
{
$sm .= ' and picture uploaded';
$qphoto = "update project set photo='$photolocation' where id='$pid'";
mysqli_query($dbcon,$qphoto);
}

}//end of if to check if project photo exists or not


//$eid = mysqli_insert_id($dbcon);

echo '<meta http-equiv="refresh" content="0;URL=addproject.php?sm='.$sm.'">';


