<?php
include 'db.php';

// Fetch all employees
$employees = $conn->query("SELECT id, name FROM employees");

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $employee_id = $_POST['employee_id'];
    $amount = $_POST['amount'];
    $payment_date = $_POST['payment_date'];
    $payment_method = $_POST['payment_method'];
    $remarks = $_POST['remarks'];

    $sql = "INSERT INTO payments (employee_id, amount, payment_date, payment_method, remarks) 
            VALUES ('$employee_id','$amount','$payment_date','$payment_method','$remarks')";

    if ($conn->query($sql)) {
        echo "<p>Payment recorded successfully!</p>";
    } else {
        echo "Error: " . $conn->error;
    }
}
?>

<h2>Add Payment</h2>
<form method="POST">
    <label>Employee:</label>
    <select name="employee_id" required>
        <?php while($emp = $employees->fetch_assoc()): ?>
            <option value="<?= $emp['id'] ?>"><?= $emp['name'] ?></option>
        <?php endwhile; ?>
    </select><br><br>

    <label>Amount:</label><input type="number" step="0.01" name="amount" required><br><br>
    <label>Payment Date:</label><input type="date" name="payment_date" required><br><br>
    <label>Payment Method:</label><input type="text" name="payment_method" required><br><br>
    <label>Remarks:</label><textarea name="remarks"></textarea><br><br>

    <button type="submit">Save Payment</button>
</form>