<?php
session_start();

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

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

/* ==========================
   DELETE SUCCESS MESSAGE
========================== */
$success = "";

if (isset($_GET['msg'])) {

    if ($_GET['msg'] == "added") {
        $success = "Employee added successfully.";
    } elseif ($_GET['msg'] == "updated") {
        $success = "Employee updated successfully.";
    } elseif ($_GET['msg'] == "deleted") {
        $success = "Employee deleted successfully.";
    }

}
$error = "";

if (isset($_GET['error'])) {

    if ($_GET['error'] == "appointment_exists") {

        $error = "Employee cannot be deleted because this employee is assigned to one or more appointments. Please delete or reassign those appointments first.";

    }

    if ($_GET['error'] == "delete_failed") {

        $error = "Unable to delete employee.";

    }

}

/* ==========================
   SEARCH
========================== */
$search = "";

if (isset($_GET['search'])) {
    $search = trim(mysqli_real_escape_string($conn, $_GET['search']));
}

/* ==========================
   PAGINATION
========================== */

$limit = 10;

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

if ($page <= 0) {
    $page = 1;
}

$offset = ($page - 1) * $limit;

/* ==========================
   TOTAL RECORDS
========================== */

$countQuery = "SELECT COUNT(*) as total
FROM employees
WHERE employee_name LIKE '%$search%'
OR phone LIKE '%$search%'
OR email LIKE '%$search%'
OR role LIKE '%$search%'";

$countResult = mysqli_query($conn, $countQuery);

$countRow = mysqli_fetch_assoc($countResult);

$totalRecords = $countRow['total'];

$totalPages = ceil($totalRecords / $limit);

/* ==========================
   FETCH EMPLOYEES
========================== */

$query = "SELECT *
FROM employees
WHERE employee_name LIKE '%$search%'
OR phone LIKE '%$search%'
OR email LIKE '%$search%'
OR role LIKE '%$search%'
ORDER BY id DESC
LIMIT $offset,$limit";

$result = mysqli_query($conn, $query);

?>

<?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 flex-wrap mb-4">

<div>
<h3 class="fw-bold mb-1">
<i class="fas fa-users me-2 text-primary"></i>
Employees
</h3>

<p class="text-muted mb-0">
Manage employee records
</p>
</div>

<div>

<a href="/beauty_pos/employees/add.php"
class="btn btn-primary rounded-3">

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

Add Employee

</a>

</div>

</div>

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

<div class="alert alert-danger alert-dismissible fade show">

<?php echo $error; ?>

<button
type="button"
class="btn-close"
data-bs-dismiss="alert">
</button>

</div>

<?php } ?>

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

<div class="alert alert-success alert-dismissible fade show">

<?php echo $success; ?>

<button
type="button"
class="btn-close"
data-bs-dismiss="alert">
</button>

</div>

<?php } ?>

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

<div class="card-body">

<form method="GET">

<div class="row g-3 align-items-end">

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

<label class="form-label">

Search Employee

</label>

<input
type="text"
name="search"
class="form-control"
placeholder="Name / Phone / Email / Role"
value="<?php echo htmlspecialchars($search); ?>">

</div>

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

<button
type="submit"
class="btn btn-primary w-100">

<i class="fas fa-search me-1"></i>

Search

</button>

</div>

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

<a
href="index.php"
class="btn btn-secondary w-100">

Reset

</a>

</div>

<div class="col-xl-3 text-end">

<span class="badge bg-dark fs-6">

Total Employees :
<?php echo $totalRecords; ?>

</span>

</div>

</div>

</form>

<hr>

<div class="table-responsive">

<table class="table table-hover align-middle">

<thead class="table-light">

<tr>

<th width="70">
#
</th>

<th>
Employee
</th>

<th>
Phone
</th>

<th>
Email
</th>

<th>
Role
</th>

<th>
Salary
</th>

<th>
Commission
</th>

<th>
Joining
</th>

<th>
Status
</th>

<th width="180">
Action
</th>

</tr>

</thead>

<tbody>

<?php

if(mysqli_num_rows($result)>0){

$sn=$offset+1;

while($row=mysqli_fetch_assoc($result)){

?>

<tr>

<td>

<?php echo $sn++; ?>

</td>

<td>

<strong>

<?php echo htmlspecialchars($row['employee_name']); ?>

</strong>

</td>

<td>

<?php echo htmlspecialchars($row['phone']); ?>

</td>

<td>

<?php echo htmlspecialchars($row['email']); ?>

</td>

<td>

<?php echo htmlspecialchars($row['role']); ?>

</td>

<td>

Rs.
<?php echo number_format($row['salary'],2); ?>

</td>

<td>

Rs.
<?php echo number_format($row['commission'],2); ?>

</td>

<td>

<?php
echo date("d M Y",strtotime($row['joining_date']));
?>

</td>

<td>

<?php

if($row['status']=="Active"){

?>

<span class="badge bg-success">

Active

</span>

<?php

}else{

?>

<span class="badge bg-danger">

Inactive

</span>

<?php

}

?>

</td>

<td>
    <a
href="edit.php?id=<?php echo $row['id']; ?>"
class="btn btn-sm btn-warning">

<i class="fas fa-edit"></i>

</a>

<button
type="button"
class="btn btn-sm btn-info text-white"
data-bs-toggle="modal"
data-bs-target="#viewEmployee<?php echo $row['id']; ?>">

<i class="fas fa-eye"></i>

</button>

<a
href="delete.php?id=<?php echo $row['id']; ?>"
class="btn btn-sm btn-danger"
onclick="return confirm('Are you sure you want to delete this employee?');">

<i class="fas fa-trash"></i>

</a>

</td>

</tr>

<!-- View Employee Modal -->

<div
class="modal fade"
id="viewEmployee<?php echo $row['id']; ?>"
tabindex="-1">

<div class="modal-dialog modal-lg">

<div class="modal-content rounded-4 border-0">

<div class="modal-header">

<h5 class="modal-title">

Employee Details

</h5>

<button
type="button"
class="btn-close"
data-bs-dismiss="modal">

</button>

</div>

<div class="modal-body">

<div class="row g-3">

<div class="col-md-6">

<label class="fw-semibold">

Employee Name

</label>

<p>

<?php echo htmlspecialchars($row['employee_name']); ?>

</p>

</div>

<div class="col-md-6">

<label class="fw-semibold">

Phone

</label>

<p>

<?php echo htmlspecialchars($row['phone']); ?>

</p>

</div>

<div class="col-md-6">

<label class="fw-semibold">

Email

</label>

<p>

<?php echo htmlspecialchars($row['email']); ?>

</p>

</div>

<div class="col-md-6">

<label class="fw-semibold">

Role

</label>

<p>

<?php echo htmlspecialchars($row['role']); ?>

</p>

</div>

<div class="col-md-6">

<label class="fw-semibold">

Salary

</label>

<p>

Rs. <?php echo number_format($row['salary'],2); ?>

</p>

</div>

<div class="col-md-6">

<label class="fw-semibold">

Commission

</label>

<p>

Rs. <?php echo number_format($row['commission'],2); ?>

</p>

</div>

<div class="col-md-6">

<label class="fw-semibold">

Joining Date

</label>

<p>

<?php echo date("d M Y",strtotime($row['joining_date'])); ?>

</p>

</div>

<div class="col-md-6">

<label class="fw-semibold">

Status

</label>

<p>

<?php echo htmlspecialchars($row['status']); ?>

</p>

</div>

<div class="col-md-12">

<label class="fw-semibold">

Created At

</label>

<p>

<?php echo date("d M Y h:i A",strtotime($row['created_at'])); ?>

</p>

</div>

</div>

</div>

<div class="modal-footer">

<button
type="button"
class="btn btn-secondary"
data-bs-dismiss="modal">

Close

</button>

</div>

</div>

</div>

</div>

<?php

}

}else{

?>

<tr>

<td colspan="10" class="text-center py-4">

No employees found.

</td>

</tr>

<?php

}

?>

</tbody>

</table>

</div>

<nav class="mt-4">

<ul class="pagination justify-content-center">

<?php

if($page>1){

?>

<li class="page-item">

<a
class="page-link"
href="?search=<?php echo urlencode($search); ?>&page=<?php echo $page-1; ?>">

Previous

</a>

</li>

<?php

}

for($i=1;$i<=$totalPages;$i++){

?>

<li class="page-item <?php if($i==$page){ echo "active"; } ?>">

<a
class="page-link"
href="?search=<?php echo urlencode($search); ?>&page=<?php echo $i; ?>">

<?php echo $i; ?>

</a>

</li>

<?php

}

if($page<$totalPages){

?>

<li class="page-item">

<a
class="page-link"
href="?search=<?php echo urlencode($search); ?>&page=<?php echo $page+1; ?>">

Next

</a>

</li>

<?php

}

?>

</ul>

</nav>

</div>

</div>
</div>

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

</div>