<?php
   include('session.php');
   include('connect.php');
   include('logging.php');

// start role specific 
$user_role = $_SESSION['role'];

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

  if (isset($_POST['existboolean'])) {

    $existboolean = mysqli_real_escape_string($db,$_POST['existboolean']);
    $currentcompanyid = mysqli_real_escape_string($db,$_POST['currentcompanyid']);
    $companyName = mysqli_real_escape_string($db,$_POST['companyName']);
    $companyBRN = mysqli_real_escape_string($db,$_POST['companyBRN']);
    $companyERN = mysqli_real_escape_string($db,$_POST['companyERN']);
    $companyAddress = mysqli_real_escape_string($db,$_POST['companyAddress']);
    $accountantName = mysqli_real_escape_string($db,$_POST['accountantName']);
    $accountantPNumber = mysqli_real_escape_string($db,$_POST['accountantPNumber']);
    $accountantMNumber = mysqli_real_escape_string($db,$_POST['accountantMNumber']);
    $accountantEmail = mysqli_real_escape_string($db,$_POST['accountantEmail']);


    if ($existboolean == "new") {
      $sqlUpdateCompanyFile = "INSERT INTO companyfile (
      companyName,
      companyBRN,
      companyERN,
      companyAddress,
      accountantName,
      accountantPNumber,
      accountantMNumber,
      accountantEmail
      ) VALUES (
      '$companyName',
      '$companyBRN',
      '$companyERN',
      '$companyAddress',
      '$accountantName',
      '$accountantPNumber',
      '$accountantMNumber',
      '$accountantEmail');";

    } else {
      $sqlUpdateCompanyFile = "UPDATE companyfile
      SET companyName = '$companyName',
      companyBRN = '$companyBRN',
      companyERN = '$companyERN',
      companyAddress = '$companyAddress',
      accountantName = '$accountantName',
      accountantPNumber = '$accountantPNumber',
      accountantMNumber = '$accountantMNumber',
      accountantEmail = '$accountantEmail'
      WHERE companyid = '$currentcompanyid';";
    }

      if(mysqli_query($db, $sqlUpdateCompanyFile)) {

      $employeeUpdate = "User updated company file. ";

      logEvent($db,"Company File updated",$employeeUpdate,$last_id);
      
      header("location: companysettings.php?updated=1");

       } else {
         echo "Error: " . mysqli_error($db);
    }

    } else {
    echo "Error 101111!!!";
   }
 
}

?>