<?php
session_start();
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../classes/Database.php';
require_once __DIR__ . '/../../classes/Deposit.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: /IMS/login.php');
    exit();
}

$database = new Database();
$conn = $database->getConnection();
$auth = new Auth($conn);
$deposit = new Deposit($conn);

if (!$auth->isBranchManager($_SESSION['user_id'])) {
    die("Access denied. Only branch managers can access this page.");
}

$branchId = $auth->getUserBranch($_SESSION['user_id']);
if (!$branchId) {
    die("Error: You are not assigned to any branch.");
}

$depositId = isset($_GET['id']) ? filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT) : 0;
$depositData = $deposit->getDepositById($depositId);

if (!$depositData || $depositData['branch_id'] != $branchId || !$deposit->isDepositUnverified($depositId)) {
    header('Location: weekly2.php?error=' . urlencode('Invalid or verified deposit.'));
    exit();
}

?>

<?php include __DIR__ . '/../../includes/header.php'; ?>
    <div class="container mt-5">
        <h2>Edit Deposit</h2>
        <a href="weekly2.php" class="btn btn-secondary mb-3"><i class="fas fa-arrow-left"></i> Back to Weekly Report</a>

        <form action="process_deposit_action.php" method="POST" class="needs-validation" novalidate>
            <input type="hidden" name="deposit_id" value="<?php echo htmlspecialchars($depositId); ?>">
            <div class="form-row">
                <div class="form-group col-md-6">
                    <label for="deposit_date">Deposit Date</label>
                    <input type="date" class="form-control" id="deposit_date" name="deposit_date" value="<?php echo htmlspecialchars($depositData['deposit_date']); ?>" required>
                    <div class="invalid-feedback">Please select a date.</div>
                </div>
                <div class="form-group col-md-6">
                    <label for="deposit_amount">Amount (MWK)</label>
                    <div class="input-group">
                        <div class="input-group-prepend">
                            <span class="input-group-text">MWK</span>
                        </div>
                        <input type="number" step="0.01" min="0.01" class="form-control" id="deposit_amount" name="deposit_amount" value="<?php echo htmlspecialchars($depositData['amount']); ?>" required>
                        <div class="invalid-feedback">Please enter a valid amount.</div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="deposit_notes">Notes (Optional)</label>
                <textarea class="form-control" id="deposit_notes" name="deposit_notes" rows="4"><?php echo htmlspecialchars($depositData['notes'] ?? ''); ?></textarea>
            </div>
            <button type="submit" name="update_deposit" class="btn btn-primary"><i class="fas fa-save"></i> Update Deposit</button>
        </form>
    </div>

    <script>
        (function() {
            'use strict';
            window.addEventListener('load', function() {
                var forms = document.getElementsByClassName('needs-validation');
                Array.prototype.filter.call(forms, function(form) {
                    form.addEventListener('submit', function(event) {
                        if (form.checkValidity() === false) {
                            event.preventDefault();
                            event.stopPropagation();
                        }
                        form.classList.add('was-validated');
                    }, false);
                });
            }, false);
        })();
    </script>
<?php include __DIR__ . '/../../includes/footer.php'; ?>