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

$disp = '';
$pagetitle = 'Edit Project Information';

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

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


if(empty($id) or empty($project_name) or empty($start_date) or empty($type))
{
$sm = '<span class="error">Please fill all required details</span>';
echo '<meta http-equiv="refresh" content="0;URL=editproject.php?sm='.$sm.'&id='.$id.'">';
exit;
}

$q = "update project set project_name='$project_name', project_address='$project_address', project_details='$project_details', no_floors='$no_floors', no_flats='$no_flats', flats='$flats', start_date='$start_date', type='$type' where id='$id'";

//mysqli_autocommit($dbcon, false);

$res = mysqli_query($dbcon,$q);

if(!$res)
{
$disp .= '<span class="error">Changes could not be saved: '.mysqli_error($dbcon).'</span>';
include('../template.php');
exit;
}
$sm = "Project details edited";

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/".$id.".".$photoext;
$temp = $_FILES['photo']['tmp_name'];


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



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