<?php
session_start();
require 'db-connection.php';

if(isset($_POST['delete_emp']))
{
    $user_id = mysqli_real_escape_string($conn, $_POST['delete_emp']);

    $query = "DELETE FROM employee_accounts WHERE id='$user_id' ";
    $query_run = mysqli_query($conn, $query);

    if($query_run)
    {
        $_SESSION['message'] = "Employee Account Deleted Successfully";
        header("Location: ../delete-acc.php");
        exit(0);
    }
    else
    {
        $_SESSION['message'] = "Employee Account Not Deleted";
        header("Location: ../delete-acc.php");
        exit(0);
    }
}

if(isset($_POST['update_emp']))
{
    $id = mysqli_real_escape_string($conn, $_POST['id']);

    $fname = mysqli_real_escape_string($conn, $_POST['fname']);
    $lname = mysqli_real_escape_string($conn, $_POST['lname']);
    $uname = mysqli_real_escape_string($conn, $_POST['uname']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);

    $query = "UPDATE employee_accounts SET first_name='$fname', last_name='$lname', username='$uname', email='$email', password='$password' WHERE id='$id' ";
    $query_run = mysqli_query($conn, $query);

    if($query_run)
    {
        $_SESSION['message'] = "Account Updated Successfully";
        header("Location: ../manage-acc.php");
        exit(0);
    }
    else
    {
        $_SESSION['message'] = "Account Not Updated";
        header("Location: ../manage-acc.php");
        exit(0);
    }

}


if(isset($_POST['save_emp']))
{
    $fname = mysqli_real_escape_string($conn, $_POST['fname']);
    $lname = mysqli_real_escape_string($conn, $_POST['lname']);
    $uname = mysqli_real_escape_string($conn, $_POST['uname']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);

    $query = "INSERT INTO employee_accounts (id, first_name, last_name, username, email, password) VALUES (NULL, '$fname', '$lname', '$uname', '$email', '$password')";

    $query_run = mysqli_query($conn, $query);
    if($query_run)
    {
        $_SESSION['message'] = "Employee Account Created Successfully";
        header("Location: ../create-acc.php");
        exit(0);
    }
    else
    {
        $_SESSION['message'] = "Employee Account Was not Created";
        header("Location: ../create-acc.php");
        exit(0);
    }
}

?>