<?php
session_start();

if (!isset($_SESSION['user_id'])) {
    header("Location: /beauty_pos/login.php");
    exit;
}

include_once "../config/database.php";

$error = "";

$employee_name = "";
$phone = "";
$email = "";
$role = "";
$salary = "";
$commission = "";
$joining_date = "";
$status = "Active";

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

    $employee_name = trim(mysqli_real_escape_string($conn,$_POST['employee_name']));
    $phone         = trim(mysqli_real_escape_string($conn,$_POST['phone']));
    $email         = trim(mysqli_real_escape_string($conn,$_POST['email']));
    $role          = trim(mysqli_real_escape_string($conn,$_POST['role']));
    $salary        = trim(mysqli_real_escape_string($conn,$_POST['salary']));
    $commission    = trim(mysqli_real_escape_string($conn,$_POST['commission']));
    $joining_date  = trim(mysqli_real_escape_string($conn,$_POST['joining_date']));
    $status        = trim(mysqli_real_escape_string($conn,$_POST['status']));

    if(
        $employee_name=="" ||
        $phone=="" ||
        $email=="" ||
        $role=="" ||
        $salary=="" ||
        $joining_date==""
    ){

        $error="Please fill all required fields.";

    }else{

        $check=mysqli_query($conn,"
            SELECT id
            FROM employees
            WHERE email='$email'
            OR phone='$phone'
        ");

        if(mysqli_num_rows($check)>0){

            $error="Email or Phone already exists.";

        }else{

            $insert=mysqli_query($conn,"
                INSERT INTO employees
                (
                    employee_name,
                    phone,
                    email,
                    role,
                    salary,
                    commission,
                    joining_date,
                    status
                )
                VALUES
                (
                    '$employee_name',
                    '$phone',
                    '$email',
                    '$role',
                    '$salary',
                    '$commission',
                    '$joining_date',
                    '$status'
                )
            ");

            if($insert){

                header("Location: index.php?msg=added");
                exit;

            }else{

                $error="Failed to add employee.";

            }

        }

    }

}
?>

<?php include_once "../includes/header.php"; ?>
<?php include_once "../includes/sidebar.php"; ?>

<div class="overlay" id="overlay"></div>

<div class="main-content">

<?php include_once "../includes/topbar.php"; ?>

<div class="container-fluid py-4">

<div class="d-flex justify-content-between align-items-center mb-4">

<h3 class="fw-bold">

<i class="fas fa-user-plus me-2 text-primary"></i>

Add Employee

</h3>

<a href="/beauty_pos/employees/index.php"
class="btn btn-secondary">

<i class="fas fa-arrow-left me-2"></i>

Back

</a>

</div>

<?php if($error!=""){ ?>

<div class="alert alert-danger">

<?php echo $error; ?>

</div>

<?php } ?>

<div class="card rounded-4 shadow border-0">

<div class="card-body">

<form method="POST">

<div class="row g-3">

<div class="col-xl-6 col-lg-6 col-md-6 col-12">

<label class="form-label">

Employee Name <span class="text-danger">*</span>

</label>

<input
type="text"
name="employee_name"
class="form-control"
required
value="<?php echo htmlspecialchars($employee_name); ?>">

</div>

<div class="col-xl-6 col-lg-6 col-md-6 col-12">

<label class="form-label">

Phone <span class="text-danger">*</span>

</label>

<input
type="text"
name="phone"
class="form-control"
required
value="<?php echo htmlspecialchars($phone); ?>">

</div>

<div class="col-xl-6 col-lg-6 col-md-6 col-12">

<label class="form-label">

Email <span class="text-danger">*</span>

</label>

<input
type="email"
name="email"
class="form-control"
required
value="<?php echo htmlspecialchars($email); ?>">

</div>

<div class="col-xl-6 col-lg-6 col-md-6 col-12">

<label class="form-label">

Role <span class="text-danger">*</span>

</label>

<input
type="text"
name="role"
class="form-control"
required
value="<?php echo htmlspecialchars($role); ?>">

</div>
<div class="col-xl-6 col-lg-6 col-md-6 col-12">

<label class="form-label">

Salary <span class="text-danger">*</span>

</label>

<input
type="number"
step="0.01"
min="0"
name="salary"
class="form-control"
required
value="<?php echo htmlspecialchars($salary); ?>">

</div>

<div class="col-xl-6 col-lg-6 col-md-6 col-12">

<label class="form-label">

Commission

</label>

<input
type="number"
step="0.01"
min="0"
name="commission"
class="form-control"
value="<?php echo htmlspecialchars($commission); ?>">

</div>

<div class="col-xl-6 col-lg-6 col-md-6 col-12">

<label class="form-label">

Joining Date <span class="text-danger">*</span>

</label>

<input
type="date"
name="joining_date"
class="form-control"
required
value="<?php echo htmlspecialchars($joining_date); ?>">

</div>

<div class="col-xl-6 col-lg-6 col-md-6 col-12">

<label class="form-label">

Status

</label>

<select
name="status"
class="form-select">

<option
value="Active"
<?php if($status=="Active"){ echo "selected"; } ?>>

Active

</option>

<option
value="Inactive"
<?php if($status=="Inactive"){ echo "selected"; } ?>>

Inactive

</option>

</select>

</div>

<div class="col-12">

<hr>

</div>

<div class="col-xl-6 col-lg-6 col-md-6 col-12">

<button
type="submit"
name="save_employee"
class="btn btn-primary">

<i class="fas fa-save me-2"></i>

Save Employee

</button>

<a
href="/beauty_pos/employees/index.php"
class="btn btn-secondary ms-2">

Cancel

</a>

</div>

</div>

</form>

</div>

</div>
</div>

<?php include_once "../includes/footer.php"; ?>

</div>