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

$error = '';
$success = '';

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) {

        // Check if account_code already exists
        $checkStmt = $conn->prepare("SELECT id FROM accounts WHERE account_code = ?");
        $checkStmt->bind_param("s", $account_code);
        $checkStmt->execute();
        $checkStmt->store_result();

        if ($checkStmt->num_rows > 0) {
            $error = "An account with this code already exists. Please use a different code.";
        } else {
            $stmt = $conn->prepare("INSERT INTO accounts (account_code, account_name, account_type, bank_name, account_number, created_at) VALUES (?, ?, ?, ?, ?, NOW())");
            $stmt->bind_param("sssss", $account_code, $account_name, $account_type, $bank_name, $account_number);

            if ($stmt->execute()) {
                $success = "Account created successfully.";
            } else {
                $error = "An error occurred while creating the account. Please try again.";
            }
            $stmt->close();
        }
        $checkStmt->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;
}
.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;
}
.message {
    margin-bottom: 15px;
    padding: 10px 15px;
    border-radius: 8px;
}
.error {
    background-color: rgba(255, 99, 99, 0.2);
    color: #f87171;
}
.success {
    background-color: rgba(100, 255, 100, 0.15);
    color: #98fb98;
}
</style>

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

    <?php if (!empty($error)): ?>
        <div class="message error"><?= htmlspecialchars($error) ?></div>
    <?php elseif (!empty($success)): ?>
        <div class="message success"><?= htmlspecialchars($success) ?></div>
    <?php endif; ?>

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

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

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