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

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

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

$id = $_POST['id'];
$name = $_POST['name'];
$fname = $_POST['fname'];
$qualification = $_POST['qualification'];
$mobile = $_POST['mobile'];
$address = $_POST['address'];
$doj = $_POST['doj'];
$salary = $_POST['salary'];


if(empty($name) or empty($fname) or empty($qualification) or empty($mobile) or empty($address) or empty($doj) or empty($salary))
{
$sm = '<span class="error">Please fill all required details</span>';
echo '<meta http-equiv="refresh" content="0;URL=editemployee.php?sm='.$sm.'&id='.$id.'">';
exit;
}

if(!is_numeric($mobile))
{
$sm = '<span class="error">Mobile number invalid</span>';
echo '<meta http-equiv="refresh" content="0;URL=editemployee.php?sm='.$sm.'&id='.$id.'">';
exit;
}

$q = "update employee set name='$name', fname='$fname', qualification='$qualification', mobile='$mobile', address='$address', doj='$doj', salary='$salary' 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 = "Employee details edited";

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


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

$photolocation = "../files/empphotos/".$id.".".$photoext;
$temp = $_FILES['empphoto']['tmp_name'];


if(!move_uploaded_file($temp,$photolocation))
$sm .= ' but picture could not be uploaded';
else
{
$sm .= ' and picture uploaded';

$qupload = "update employee set photopath='$photolocation' where id='$id'";
mysqli_query($dbcon,$qupload);
mysqli_commit($dbcon);
}




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



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