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

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

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

    $companyselectid = mysqli_real_escape_string($db,$_POST['companyselectid']);
    $payroll_startdate = mysqli_real_escape_string($db,$_POST['payroll_startdate']);
    $payroll_enddate = mysqli_real_escape_string($db,$_POST['payroll_enddate']);
    $payroll_workingdays = mysqli_real_escape_string($db,$_POST['payroll_workingdays']);
    $payroll_name = mysqli_real_escape_string($db,$_POST['payroll_name']);

    $sqlAddPayroll = "INSERT INTO periods (
      companyid,
      periodName,
      startDate,
      endDate,
      workingDays,
      status
      ) VALUES (
        '$companyselectid',
      '$payroll_name',
      '$payroll_startdate',
      '$payroll_enddate',
      '$payroll_workingdays',
      '1');";

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

      $payrollAdd = "User added payroll " . $payroll_startdate . " to " . $payroll_enddate;

      $last_id = mysqli_insert_id($db);

      logEvent($db,"Payroll Added",$payrollAdd,$last_id);
      
      header("location: payrolls.php");
      } else {
         echo "Error: " . mysqli_error($db);
    }
 
}

?>