<?php
session_start();
if (!isset($_SESSION['role_id']) || $_SESSION['role_id'] != 4) {
    header("Location: ../../index.php");
    exit();
}

include('../../includes/config.php');

$id = intval($_GET['id'] ?? 0);

// Fetch existing account first
$stmt = $conn->prepare("SELECT * FROM accounts WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$account = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$account) {
    header("Location: chart_of_accounts.php?msg=Invalid+Account+ID");
    exit();
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $account_code = trim($_POST['account_code']);
    $account_name = trim($_POST['account_name']);
    $account_type = trim($_POST['account_type']);
    $bank_name = trim($_POST['bank_name']);
    $account_number = trim($_POST['account_number']);

    if ($account_code && $account_name && $account_type) {
        $stmt = $conn->prepare("UPDATE accounts SET account_code=?, account_name=?, account_type=?, bank_name=?, account_number=? WHERE id = ?");
        $stmt->bind_param("sssssi", $account_code, $account_name, $account_type, $bank_name, $account_number, $id);

        if ($stmt->execute()) {
            header("Location: chart_of_accounts.php?msg=Account+Updated+Successfully");
            exit();
        } else {
            $error = "Failed to update the account. Please try again.";
        }
        $stmt->close();
    } else {
        $error = "Please fill in all required fields.";
    }
}

include('../../includes/header.php');
?>

<style>
body {
    background: url('../../images/profile.jpg') center/cover fixed;
    font-family: 'Segoe UI', sans-serif;
    color: #eee;
    margin: 0;
}
.form-container {
    max-width: 700px;
    margin: 80px auto;
    background: rgba(255,255,255,0.08);
    padding: 30px;
    border-radius: 15px;
}
h2 {
    color: #ffa500;
}
.form-group {
    margin-bottom: 15px;
}
.form-group label {
    display: block;
    font-weight: bold;
    margin-bottom: 5px;
}
.form-group input, .form-group select {
    width: 100%;
    padding: 8px 10px;
    border-radius: 8px;
    border: none;
}
button {
    background: #ffa500;
    color: #222;
    padding: 10px 18px;
    border-radius: 8px;
    border: none;
    cursor: pointer;
}
button:hover {
    background: rgba(255,165,0,0.8);
}
.error {
    color: #f87171;
    margin-bottom: 10px;
}
</style>

<div class="form-container">
    <h2>Edit Account</h2>

    <?php if (!empty($error)): ?>
        <p class="error"><?= htmlspecialchars($error) ?></p>
    <?php endif; ?>

    <form method="POST">
        <div class="form-group">
            <label>Account Code</label>
            <input type="text" name="account_code" value="<?= htmlspecialchars($account['account_code']) ?>" required>
        </div>
        <div class="form-group">
            <label>Account Name</label>
            <input type="text" name="account_name" value="<?= htmlspecialchars($account['account_name']) ?>" required>
        </div>
        <div class="form-group">
            <label>Account Type</label>
            <select name="account_type" required>
                <?php foreach (['asset', 'liability', 'equity', 'income', 'expense'] as $type): ?>
                    <option value="<?= $type ?>" <?= ($account['account_type'] == $type) ? 'selected' : '' ?>>
                        <?= ucfirst($type) ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="form-group">
            <label>Bank Name (Optional)</label>
            <input type="text" name="bank_name" value="<?= htmlspecialchars($account['bank_name']) ?>">
        </div>
        <div class="form-group">
            <label>Account Number (Optional)</label>
            <input type="text" name="account_number" value="<?= htmlspecialchars($account['account_number']) ?>">
        </div>

        <button type="submit">Update Account</button>
        <a href="chart_of_accounts.php" style="margin-left:15px; color:#ffa500;">Cancel</a>
    </form>
</div>

<?php include('../../includes/footer.php'); ?>
