<?php
   include('session.php');
   include('header1.php');

?>

<?php 

$uploadOk = 5;

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  $target_file = basename($_FILES["fileToUpload"]["name"]);
  $uploadOk = 1;
  $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

  // Check if image file is a actual image or fake image
  if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
      echo "File is an image - " . $check["mime"] . ".";
      $uploadOk = 1;
    } else {
      echo "File is not an image.";
      $uploadOk = 0;
    }
  }


  // Check file size
  if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
  }

  // Allow certain file formats
  if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
  }

  $ext = pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION);

  $fileNameToAdd = "logo" . ".";

  $finalFileName = $fileNameToAdd . $ext;


  // Check if $uploadOk is set to 0 by an error
  if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
  // if everything is ok, try to upload file
  } else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $finalFileName)) {
      echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
    } else {
      echo "Sorry, there was an error uploading your file.";
    }
  }


 }






// start role specific 

$user_role = $_SESSION['role'];

$updated = 0;



  if (file_exists("logo.png")) {
        $logoPresent = 1;
      } else {
        $logoPresent = 0;
      }


?>


        <main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
          <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
            <h1 class="h2">Company Logo Settings</h1>
            <div class="btn-toolbar mb-2 mb-md-0">
              <div class="btn-group mr-2">
              </div>
            </div>
          </div>

<?php 

switch ($uploadOk) {

  case 1: 
    echo "<div class=\"alert alert-primary\" role=\"alert\">
      Logo File Uploaded!
    </div>";
  break;

    case 0: 
    echo "<div class=\"alert alert-danger\" role=\"alert\">
      Logo File Not Uploaded!
    </div>";
  break;


  default:

}



if ($user_role == 1) {


echo "<form id=\"settingsform\" name=\"form\" method = \"POST\" action = \"uploadlogo.php\" enctype=\"multipart/form-data\">
          <table class=\"table\">
              ";

      echo "<tbody>";

      if ($logoPresent == 0) {
      echo "<tr><td><i>Logo file present. Uploaded logo will update current logo.</i></td>";
    }

      echo "<tr><td>Select image to upload:</td>";

      echo "<td><input type=\"file\" name=\"fileToUpload\" id=\"fileToUpload\" accept=\".png\"></td></tr>";

      echo "<tr><td><input type=\"submit\" value=\"Upload Image\" name=\"submit\"></td></tr>";
        echo "
        </tbody></table></form>
              ";

      echo "<br>Logo:<br>";
      echo "<img src = \"logo.png\" style=\"width:128px;height:128px;\">";



} else {
  echo "You have no permission to edit company settings. Please contact Administrator";
}
     ?>


        </main>
      </div>
    </div>

<?php
   include('footer1.php');
?>