<?php
include "db.php";

if(!isset($_GET['id'])) { echo "<p>No ID provided.</p>"; exit; }
$id = intval($_GET['id']);

$result = $conn->query("SELECT * FROM transactions WHERE id=$id");
if($result->num_rows == 0){ echo "<p>Transaction not found.</p>"; exit; }
$trans = $result->fetch_assoc();

if(isset($_POST['submit'])){
    $date = $_POST['date'];
    $type = $_POST['type'];
    $amount = $_POST['amount'];
    $amount_type = $_POST['amount_type'];
    $reference_type = $_POST['reference_type'];
    $reference_id = $_POST['reference_id'];
    $description = $_POST['description'];

    $sql = "UPDATE transactions SET date='$date', type='$type', amount='$amount', amount_type='$amount_type', reference_type='$reference_type', reference_id='$reference_id', description='$description' WHERE id=$id";

    if($conn->query($sql)){
        $success = "Transaction updated!";
        $trans = $conn->query("SELECT * FROM transactions WHERE id=$id")->fetch_assoc();
    } else { $error = $conn->error; }
}
?>

<h2>Edit 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" value="<?php echo $trans['date']; ?>" required><br><br>

    <label>Type:</label><br>
    <select name="type">
        <option value="DEBIT" <?php if($trans['type']=='DEBIT') echo 'selected'; ?>>DEBIT</option>
        <option value="CREDIT" <?php if($trans['type']=='CREDIT') echo 'selected'; ?>>CREDIT</option>
    </select><br><br>

    <label>Amount:</label><br>
    <input type="number" step="0.01" name="amount" value="<?php echo $trans['amount']; ?>" required><br><br>

    <label>Amount Type:</label><br>
    <select name="amount_type">
        <option value="CASH" <?php if($trans['amount_type']=='CASH') echo 'selected'; ?>>CASH</option>
        <option value="BANK" <?php if($trans['amount_type']=='BANK') echo 'selected'; ?>>BANK</option>
    </select><br><br>

    <label>Reference Type:</label><br>
    <select name="reference_type">
        <option value="CUSTOMER" <?php if($trans['reference_type']=='CUSTOMER') echo 'selected'; ?>>CUSTOMER</option>
        <option value="SUPPLIER" <?php if($trans['reference_type']=='SUPPLIER') echo 'selected'; ?>>SUPPLIER</option>
        <option value="EXPENSE" <?php if($trans['reference_type']=='EXPENSE') echo 'selected'; ?>>EXPENSE</option>
        <option value="GENERAL" <?php if($trans['reference_type']=='GENERAL') echo 'selected'; ?>>GENERAL</option>
    </select><br><br>

    <label>Reference ID (Optional):</label><br>
    <input type="number" name="reference_id" value="<?php echo $trans['reference_id']; ?>"><br><br>

    <label>Description:</label><br>
    <textarea name="description"><?php echo $trans['description']; ?></textarea><br><br>

    <input type="submit" name="submit" value="Update 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>
