<?php
include "db.php";

if(isset($_POST['submit'])){
    $date = $_POST['date'];
    $description = $_POST['description'];
    $amount = $_POST['amount'];
    $type = $_POST['type'];
    $reference_id = $_POST['reference_id'] ?: NULL;

    $sql = "INSERT INTO cash (date, description, amount, type, reference_id)
            VALUES ('$date','$description','$amount','$type','$reference_id')";

    if($conn->query($sql)){
        $success = "Cash transaction added successfully!";
    } else {
        $error = "Error: ".$conn->error;
    }
}
?>

<h2>Add Cash Transaction</h2>
<?php if(isset($success)) echo "<p style='color:green;'>$success</p>"; ?>
<?php if(isset($error)) echo "<p style='color:red;'>$error</p>"; ?>

<form method="POST">
    <label>Date:</label><br>
    <input type="date" name="date" required><br><br>
    <label>Description:</label><br>
    <textarea name="description"></textarea><br><br>
    <label>Amount:</label><br>
    <input type="number" step="0.01" name="amount" required><br><br>
    <label>Type:</label><br>
    <select name="type" required>
        <option value="DEBIT">DEBIT</option>
        <option value="CREDIT">CREDIT</option>
    </select><br><br>
    <label>Reference ID:</label><br>
    <input type="number" name="reference_id"><br><br>
    <input type="submit" name="submit" value="Add Cash Transaction">
</form>

<style>
form{max-width:600px;background:#fff;padding:20px;border-radius:8px;box-shadow:0 2px 5px rgba(0,0,0,0.2);}
input,textarea,select{width:100%;padding:8px;margin:4px 0;box-sizing:border-box;}
input[type="submit"]{width:auto;background:#333;color:#fff;cursor:pointer;}
input[type="submit"]:hover{background:#555;}
@media screen and (max-width:768px){form{width:100%;padding:10px;}}
</style>
