<?php
  $conn = @mysqli_connect('localhost', 'root', '');
  if (!$conn)
  {
    die("Database Connection Failed" . mysqli_error());
  }

  $select_db = mysqli_select_db($conn,'p2');
  if (!$select_db)
  {
    die("Database Selection Failed" . mysqli_error());
  }

  $connection = mysqli_connect('localhost', 'root', '');
  $select_db = mysqli_select_db($connection, 'p2');
  if (!$select_db)
  {
    echo "failed db select";
    die("Database Selection Failed" . mysqli_error());
  }
  
  //computations of deductions
  $sql = mysqli_query($connection,"SELECT * from deductions WHERE deduction_id='1'");
  while($row = mysqli_fetch_array($sql))
  {
    $phil = $row['philhealth'];
    $bir = $row['bir'];
    $gsis = $row['gsis'];
    $love = $row['pag_ibig'];
    $loans = $row['loans'];

    $total = $phil + $bir + $gsis + $love + $loans;
  }
  

  if(isset($_POST['submit'])!="")
  {
     $emp_id     = mysqli_real_escape_string($conn,$_POST['emp_id']);
  $lname      = mysqli_real_escape_string($conn,$_POST['lname']);
  $fname      = mysqli_real_escape_string($conn,$_POST['fname']);
  $gender     = mysqli_real_escape_string($conn,$_POST['gender']);
	$contact    = mysqli_real_escape_string($conn,$_POST['contact']);
	$email      = mysqli_real_escape_string($conn,$_POST['email']);
	$dob        = mysqli_real_escape_string($conn,$_POST['dob']);
	$jdate      = mysqli_real_escape_string($conn,$_POST['jdate']);
	$address    = isset($_POST['address']) ? $_POST['address'] : '';
  $city       = mysqli_real_escape_string($conn,$_POST['city']);
	$zip        = mysqli_real_escape_string($conn,$_POST['zip']);
	$country    = mysqli_real_escape_string($conn,$_POST['country']);
	$type       = mysqli_real_escape_string($conn,$_POST['emp_type']);
  $division   = mysqli_real_escape_string($conn,$_POST['division']);
	$password   = mysqli_real_escape_string($conn,$_POST['password']);
	$username   = mysqli_real_escape_string($conn,$_POST['username']);
	$basic_pay  = mysqli_real_escape_string($conn,$_POST['basic_pay']);
 
    $query = "INSERT into records(lname, fname, gender,contact,email,dob,jdate,address,city,zip,country, emp_type, division, deduction, overtime, bonus, basic_pay)
	VALUES('$lname','$fname','$gender', '$contact','$email', '$dob', '$jdate', '$address', '$city', '$zip', '$country', '$type', '$division','$total','0','0','$basic_pay')";

   $query1 = "INSERT into user_access(emp_id, user_role)
  VALUES('$emp_id','user')";  



	 $sql = mysqli_query($conn,$query);
    $sql1 = mysqli_query($conn,$query1);

    if($sql)
    {
      ?>
        <script>
            alert('Employee had been successfully added.');
        </script>
      <?php 
    }

    else
    {
      ?>
        <script>
            alert('Invalid.');  
        </script>
      <?php 
    }
  }
?>

