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

   $companyID = $_SESSION['companyselect'];

    $sqlGetEmployeesByCompany = "SELECT * from employees WHERE companyid = $companyID";

    $resultEmployeesByCompany = mysqli_query($db,$sqlGetEmployeesByCompany);

    $countEmployeesByCompany = mysqli_num_rows($resultEmployeesByCompany);

    if ($countEmployeesByCompany !== 0) {

      $statusA = "NOTOK";

      while ($rowEmployeeInCompany = mysqli_fetch_array($resultEmployeesByCompany)) { 

        $employeeid = $rowEmployeeInCompany['employeeid'];

        // delete payroll details
        $sqlDeletePayrollDetails = "DELETE FROM `payrollbasicdetails` WHERE employeeid = $employeeid";
        

        // delete bonus payouts
        $sqlDeleteBonusPayouts = "DELETE FROM `bonuspayouts` WHERE employeeid = $employeeid";
        

        // delete salaries
        $sqlDeleteSalaries = "DELETE FROM `salaries` WHERE employeeid = $employeeid";

        // delete employee deductions
        $sqlDeleteEmployeeDeductions = "DELETE FROM `employee_deductions` WHERE employeeid = $employeeid";
        mysqli_query($db, $sqlDeleteEmployeeDeductions);

        // delete employee benefits
        $sqlDeleteEmployeeBenefits = "DELETE FROM `employee_benefits` WHERE employeeid = $employeeid";
        mysqli_query($db, $sqlDeleteEmployeeBenefits);
        

        if (mysqli_query($db, $sqlDeletePayrollDetails) && mysqli_query($db, $sqlDeleteBonusPayouts) && mysqli_query($db, $sqlDeleteSalaries) ) {
          $statusA = "OK1";
        }


      }

      // delete periods in company
      $sqlDeletePeriods = "DELETE FROM `periods` WHERE companyid = $companyID";

      // delete employees in company
      $sqlDeleteEmployees = "DELETE FROM `employees` WHERE companyid = $companyID";

      if (mysqli_query($db, $sqlDeletePeriods) && mysqli_query($db, $sqlDeleteEmployees)){
        header("location: employees.php");
      }

    } else {
      header("location: dashboard1.php");
    }

/*
	if(mysqli_query($db, $insertdata)) {
        $truncatefull = "User WIPED DATABASE ";

        logEvent($db,"PURGE DATA",$truncatefull,$last_id);

      	header("location: index.php");
      } else {
         echo "Error: " . mysqli_error($db);
    }
*/
    

?>