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

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

$success = $error = '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $transaction_date = $_POST['transaction_date'] ?? '';
    $reference = trim($_POST['reference'] ?? '');
    $amount = floatval($_POST['amount'] ?? 0);
    $type = $_POST['type'] ?? '';
    $description = trim($_POST['description'] ?? '');
    $reconciled = isset($_POST['reconciled']) ? 1 : 0;
    $created_at = date('Y-m-d H:i:s');
    $updated_at = $created_at;

    // Validate required fields
    if (!$transaction_date || !$reference || !$amount || !$type) {
        $error = "Please fill in all required fields.";
    } else {
        // Insert into bank_transactions
        $stmt = $conn->prepare("INSERT INTO bank_transactions 
            (transaction_date, reference, amount, type, description, reconciled, created_at, updated_at) 
            VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->bind_param(
            'ssdssiss',
            $transaction_date,
            $reference,
            $amount,
            $type,
            $description,
            $reconciled,
            $created_at,
            $updated_at
        );

        if ($stmt->execute()) {
            $success = "✅ Bank transaction added successfully.";
        } else {
            $error = "❌ Failed to add bank transaction: " . $stmt->error;
        }
        $stmt->close();
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Add Bank Transaction</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style>
        /* Basic styling similar to your glassmorphism theme */
        body {
            background: #1e1e2f url('../../images/profile.jpg') no-repeat center center fixed;
            background-size: cover;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0; color: #eee;
        }
        .overlay {
            position: fixed; top: 0; left: 220px;
            width: calc(100% - 220px); height: 100%;
            background: rgba(30, 30, 47, 0.8);
            z-index: -1;
        }
        .content {
            margin-left: 220px;
            padding: 40px 50px 50px 50px;
            max-width: 700px;
            min-height: 100vh;
            box-sizing: border-box;
        }
        .card {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 18px;
            padding: 40px 50px;
            box-shadow: 0 12px 36px rgba(255, 165, 0, 0.3);
            backdrop-filter: blur(16px);
            border: 1px solid rgba(255, 165, 0, 0.4);
            color: #fff;
            box-sizing: border-box;
        }
        h1.page-title {
            font-size: 28px; font-weight: 700; margin-bottom: 30px;
            color: #ffa500;
            text-shadow: 0 0 10px #ffa500;
        }
        label {
            display: block;
            font-weight: 600;
            color: #ffa500;
            margin-bottom: 8px;
            letter-spacing: 0.04em;
            user-select: none;
            text-shadow: 0 0 3px rgba(255, 165, 0, 0.7);
        }
        input[type="text"], input[type="date"], input[type="number"], select, textarea {
            width: 100%;
            padding: 14px 18px;
            border-radius: 12px;
            border: none;
            outline: none;
            background: rgba(255,255,255,0.9);
            color: #222;
            font-weight: 600;
            font-size: 16px;
            box-shadow: inset 0 3px 8px rgba(0,0,0,0.1);
            transition: box-shadow 0.3s ease;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            user-select: text;
            margin-bottom: 24px;
        }
        input[type="text"]:focus, input[type="date"]:focus, input[type="number"]:focus, select:focus, textarea:focus {
            box-shadow: 0 0 12px #ffa500;
            background: #fff;
        }
        textarea {
            resize: vertical;
            min-height: 90px;
        }
        .btn-submit {
            width: 100%;
            background: #ffa500;
            color: #222;
            font-weight: 700;
            font-size: 18px;
            padding: 14px;
            border: none;
            border-radius: 14px;
            cursor: pointer;
            box-shadow: 0 4px 14px rgba(255, 165, 0, 0.7);
            transition: background-color 0.3s ease, color 0.3s ease;
            user-select: none;
            letter-spacing: 0.05em;
        }
        .btn-submit:hover, .btn-submit:focus {
            background: #e59400;
            color: #111;
            box-shadow: 0 6px 20px rgba(230, 140, 0, 0.9);
        }
        .alert-success, .alert-error {
            padding: 16px 20px;
            border-radius: 12px;
            margin-bottom: 28px;
            font-weight: 700;
            font-size: 16px;
            letter-spacing: 0.05em;
            box-shadow: 0 4px 14px rgba(0,0,0,0.15);
            user-select: none;
        }
        .alert-success {
            background: rgba(16,185,129,0.18);
            border-left: 7px solid #10b981;
            color: #10b981;
            text-shadow: 0 0 6px #10b981;
        }
        .alert-error {
            background: rgba(239,68,68,0.18);
            border-left: 7px solid #ef4444;
            color: #ef4444;
            text-shadow: 0 0 6px #ef4444;
        }
        @media (max-width: 950px) {
            .content { margin-left: 0; padding: 30px 20px 50px 20px; }
            .overlay { left: 0; width: 100%; }
        }
    </style>
</head>
<body>
<div class="overlay"></div>
<div class="content" role="main" aria-label="Add Bank Transaction">
    <h1 class="page-title">➕ Bank Transactions: Add Manual Entry</h1>

    <?php if ($success): ?>
        <div class="alert-success" role="alert" aria-live="polite"><?= htmlspecialchars($success) ?></div>
    <?php elseif ($error): ?>
        <div class="alert-error" role="alert" aria-live="assertive"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <div class="card" role="form" aria-labelledby="form-title">
        <h2 id="form-title">Transaction Entry</h2>

        <form method="POST" novalidate>
            <label for="transaction_date">Transaction Date *</label>
            <input id="transaction_date" type="date" name="transaction_date" required aria-required="true" value="<?= htmlspecialchars($_POST['transaction_date'] ?? '') ?>" />

            <label for="reference">Reference *</label>
            <input id="reference" type="text" name="reference" required aria-required="true" autocomplete="off" value="<?= htmlspecialchars($_POST['reference'] ?? '') ?>" />

            <label for="amount">Amount (MWK) *</label>
            <input id="amount" type="number" step="0.01" min="0.01" name="amount" required aria-required="true" value="<?= htmlspecialchars($_POST['amount'] ?? '') ?>" />

            <label for="type">Transaction Type *</label>
            <select id="type" name="type" required aria-required="true">
                <option value="">-- Select Type --</option>
                <option value="deposit" <?= (($_POST['type'] ?? '') === 'deposit') ? 'selected' : '' ?>>Deposit</option>
                <option value="withdrawal" <?= (($_POST['type'] ?? '') === 'withdrawal') ? 'selected' : '' ?>>Withdrawal</option>
                <option value="fee" <?= (($_POST['type'] ?? '') === 'fee') ? 'selected' : '' ?>>Bank Fee</option>
                <option value="interest" <?= (($_POST['type'] ?? '') === 'interest') ? 'selected' : '' ?>>Interest</option>
                <option value="other" <?= (($_POST['type'] ?? '') === 'other') ? 'selected' : '' ?>>Other</option>
            </select>

            <label for="description">Description</label>
            <textarea id="description" name="description" autocomplete="off"><?= htmlspecialchars($_POST['description'] ?? '') ?></textarea>

            <label>
                <input type="checkbox" name="reconciled" <?= isset($_POST['reconciled']) ? 'checked' : '' ?> />
                Mark as Reconciled
            </label>

            <button type="submit" class="btn-submit" aria-label="Save bank transaction">💾 Save Transaction</button>
        </form>
    </div>
</div>
</body>
</html>
