<?php    
error_reporting(E_ALL);    
ini_set('display_errors', 1);    
    
require_once('includes/load.php'); // Load database connection and functions    
    
// Process form submission for adding a new employee    
if (isset($_POST['submit_new_employee'])) {    
    $name = $_POST['name'];    
    $position = $_POST['position'];    
    $division = $_POST['division'];    
    $status = $_POST['status'];    
    
    // Function to create a new employee record    
    function create_employee($name, $position, $division, $status) {    
        global $db;    
    
        $name = $db->escape($name);    
        $position = $db->escape($position);    
        $division = $db->escape($division);    
        $status = $db->escape($status);    
    
        $sql = "INSERT INTO employees (name, position, division, status)    
                VALUES ('{$name}', '{$position}', '{$division}', '{$status}')";    
    
        return $db->query($sql);    
    }    
    
    if (create_employee($name, $position, $division, $status)) {    
        $msg = "Employee added successfully.";    
    } else {    
        $msg = "Failed to add employee.";    
    }    
    header('Location: nama_pegawai.php?msg=' . urlencode($msg));    
    exit();    
}    
  
// Process deletion of an employee record    
if (isset($_GET['delete'])) {    
    $id = intval($_GET['delete']);    
    if (delete_employee($id)) {    
        $msg = "Employee deleted successfully.";    
    } else {    
        $msg = "Failed to delete employee.";    
    }    
    header('Location: nama_pegawai.php?msg=' . urlencode($msg));    
    exit();    
}  
  
// Function to delete an employee record    
function delete_employee($id) {    
    global $db;    
    $sql = "DELETE FROM employees WHERE id = {$id}";    
    return $db->query($sql);    
}  
  
// Fetch existing employees with optional search and division filter    
$search_query = '';    
$selected_division = '';    
if (isset($_POST['search'])) {    
    $search_query = $db->escape($_POST['search_query']);    
}    
    
if (isset($_POST['filter_division'])) {    
    $selected_division = $db->escape($_POST['division_filter']);    
}    
    
// Modify SQL query to filter by division if selected    
$sql = "SELECT * FROM employees WHERE name LIKE '%{$search_query}%'";    
    
if (!empty($selected_division)) {    
    $sql .= " AND division = '{$selected_division}'";    
}    
    
$sql .= " ORDER BY name ASC";    
$employees = $db->query($sql);    
    
// Simple header    
?>    
<!DOCTYPE html>    
<html lang="en">    
<head>    
    <meta charset="UTF-8">    
    <meta name="viewport" content="width=device-width, initial-scale=1.0">    
    <title>Employee Management - HRIS</title>    
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">    
    <style>    
        body {    
            background-color: #f4f7fa;    
        }    
        .card-header {    
            background-color: #007bff;    
            color: white;    
        }    
        .table th, .table td {    
            vertical-align: middle;    
        }    
        .form-control:focus {    
            border-color: #007bff;    
            box-shadow: 0 0 5px rgba(0, 123, 255, .5);    
        }    
        .btn-primary {    
            background-color: #007bff;    
            border-color: #007bff;    
        }    
        .btn-primary:hover {    
            background-color: #0056b3;    
            border-color: #0056b3;    
        }    
        .alert {    
            margin-top: 20px;    
        }    
    </style>    
</head>    
<body>    
<div class="container-fluid mt-5">  <!-- Changed to container-fluid -->  
    <h2 class="text-center mb-4">Employee Management System</h2>    
        
    <!-- Back Button -->    
    <div class="text-right mb-3">    
        <a href="human_resource.php" class="btn btn-secondary">Back to Human Resources</a>    
    </div>    
    
    <?php if (isset($_GET['msg'])): ?>    
        <div class="alert alert-info"><?php echo htmlspecialchars($_GET['msg']); ?></div>    
    <?php endif; ?>    
    
    <!-- Add Employee Form -->    
    <div class="card mb-4">    
        <div class="card-header">    
            <h4 class="card-title">Add New Employee</h4>    
        </div>    
        <div class="card-body">    
            <form action="nama_pegawai.php" method="post">    
                <div class="form-row">    
                    <div class="form-group col-md-6">    
                        <label for="name">Name:</label>    
                        <input type="text" name="name" id="name" class="form-control" required>    
                    </div>    
                    <div class="form-group col-md-6">    
                        <label for="position">Position:</label>    
                        <input type="text" name="position" id="position" class="form-control">    
                    </div>    
                </div>    
                <div class="form-row">    
                    <div class="form-group col-md-6">    
                        <label for="division">Division:</label>    
                        <select name="division" id="division" class="form-control" required>    
                            <option value="">Select Division</option>    
                            <option value="warehouse surabaya">Warehouse Surabaya</option>    
                            <option value="warehouse jakarta">Warehouse Jakarta</option>    
                            <option value="warehouse makassar">Warehouse Makassar</option>    
                            <option value="warehouse bali">Warehouse Bali</option>    
                            <option value="warehouse semarang">Warehouse Semarang</option>    
                            <option value="produksi">Produksi</option>    
                        </select>    
                    </div>    
                    <div class="form-group col-md-6">    
                        <label for="status">Status:</label>    
                        <input type="text" name="status" id="status" class="form-control">    
                    </div>    
                </div>    
                <button type="submit" name="submit_new_employee" class="btn btn-primary">Add Employee</button>    
            </form>    
        </div>    
    </div>    
    
    <!-- Existing Employees Section -->    
    <div class="card mb-4">    
        <div class="card-header">    
            <h4 class="card-title">Existing Employees</h4>    
        </div>    
        <div class="card-body">    
            <form action="nama_pegawai.php" method="post" class="mb-3">    
                <div class="form-row">    
                    <div class="form-group col-md-4">    
                        <input type="text" name="search_query" class="form-control" placeholder="Search by name..." value="<?php echo htmlspecialchars($search_query); ?>">    
                    </div>    
                    <div class="form-group col-md-4">    
                        <select name="division_filter" class="form-control">    
                            <option value="">Select Division</option>    
                            <option value="warehouse surabaya" <?php echo ($selected_division == 'warehouse surabaya') ? 'selected' : ''; ?>>Warehouse Surabaya</option>    
                            <option value="warehouse jakarta" <?php echo ($selected_division == 'warehouse jakarta') ? 'selected' : ''; ?>>Warehouse Jakarta</option>    
                            <option value="warehouse makassar" <?php echo ($selected_division == 'warehouse makassar') ? 'selected' : ''; ?>>Warehouse Makassar</option>    
                            <option value="warehouse bali" <?php echo ($selected_division == 'warehouse bali') ? 'selected' : ''; ?>>Warehouse Bali</option>    
                            <option value="warehouse semarang" <?php echo ($selected_division == 'warehouse semarang') ? 'selected' : ''; ?>>Warehouse Semarang</option>    
                            <option value="produksi" <?php echo ($selected_division == 'produksi') ? 'selected' : ''; ?>>Produksi</option>    
                        </select>    
                    </div>    
                    <div class="form-group col-md-4">    
                        <button type="submit" name="search" class="btn btn-primary">Search</button>    
                        <button type="submit" name="filter_division" class="btn btn-secondary">Filter</button>    
                    </div>    
                </div>    
            </form>    
    
            <div class="table-responsive">  <!-- Added table-responsive for better layout -->  
                <table class="table table-bordered">    
                    <thead class="thead-dark">    
                        <tr>    
                            <th>ID</th>    
                            <th>Name</th>    
                            <th>Position</th>    
                            <th>Division</th>    
                            <th>Status</th>    
                            <th>Action</th>    
                        </tr>    
                    </thead>    
                    <tbody>    
                        <?php if ($employees && $db->num_rows($employees) > 0): ?>    
                            <?php while ($row = $db->fetch_assoc($employees)): ?>    
                                <tr>    
                                    <td><?php echo $row['id']; ?></td>    
                                    <td><?php echo htmlspecialchars($row['name']); ?></td>    
                                    <td><?php echo htmlspecialchars($row['position']); ?></td>    
                                    <td><?php echo htmlspecialchars($row['division']); ?></td>    
                                    <td><?php echo htmlspecialchars($row['status']); ?></td>    
                                    <td>    
                                        <button class="btn btn-warning" data-toggle="modal" data-target="#editModal" data-id="<?php echo $row['id']; ?>" data-name="<?php echo htmlspecialchars($row['name']); ?>" data-position="<?php echo htmlspecialchars($row['position']); ?>" data-division="<?php echo htmlspecialchars($row['division']); ?>" data-status="<?php echo htmlspecialchars($row['status']); ?>">    
                                            <i class="fas fa-edit"></i> Edit    
                                        </button>    
                                        <a href="nama_pegawai.php?delete=<?php echo $row['id']; ?>" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this employee?');">    
                                            <i class="fas fa-trash"></i> Delete    
                                        </a>    
                                    </td>    
                                </tr>    
                            <?php endwhile; ?>    
                        <?php else: ?>    
                            <tr>    
                                <td colspan="6" class="text-center">No employees found.</td>    
                            </tr>    
                        <?php endif; ?>    
                    </tbody>    
                </table>    
            </div>    
        </div>    
    </div>    
</div>    
    
<!-- Edit Employee Modal -->    
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">    
    <div class="modal-dialog" role="document">    
        <div class="modal-content">    
            <div class="modal-header">    
                <h5 class="modal-title" id="editModalLabel">Edit Employee</h5>    
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">    
                    <span aria-hidden="true">&times;</span>    
                </button>    
            </div>    
            <div class="modal-body">    
                <form action="edit_pegawai.php" method="post">    
                    <input type="hidden" name="employee_id" id="modal_employee_id">    
                    <div class="form-group">    
                        <label for="modal_name">Name:</label>    
                        <input type="text" name="name" id="modal_name" class="form-control" required>    
                    </div>    
                    <div class="form-group">    
                        <label for="modal_position">Position:</label>    
                        <input type="text" name="position" id="modal_position" class="form-control">    
                    </div>    
                    <div class="form-group">    
                        <label for="modal_division">Division:</label>    
                        <select name="division" id="modal_division" class="form-control" required>    
                            <option value="">Select Division</option>    
                            <option value="warehouse surabaya">Warehouse Surabaya</option>    
                            <option value="warehouse jakarta">Warehouse Jakarta</option>    
                            <option value="warehouse makassar">Warehouse Makassar</option>    
                            <option value="warehouse bali">Warehouse Bali</option>    
                            <option value="warehouse semarang">Warehouse Semarang</option>    
                            <option value="produksi">Produksi</option>    
                        </select>    
                    </div>    
                    <div class="form-group">    
                        <label for="modal_status">Status:</label>    
                        <input type="text" name="status" id="modal_status" class="form-control">    
                    </div>    
                    <button type="submit" name="submit_employee" class="btn btn-primary">Update Employee</button>    
                </form>    
            </div>    
        </div>    
    </div>    
</div>    
    
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>    
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>    
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>    
<script>    
    $('#editModal').on('show.bs.modal', function (event) {    
        var button = $(event.relatedTarget); // Button that triggered the modal    
        var id = button.data('id'); // Extract info from data-* attributes    
        var name = button.data('name');    
        var position = button.data('position');    
        var division = button.data('division');    
        var status = button.data('status');    
    
        // Update the modal's content    
        var modal = $(this);    
        modal.find('#modal_employee_id').val(id);    
        modal.find('#modal_name').val(name);    
        modal.find('#modal_position').val(position);    
        modal.find('#modal_division').val(division);    
        modal.find('#modal_status').val(status);    
    });    
</script>    
</body>    
</html>    
