<?php
// admin/accounts/edit-bank-account.php
require_once __DIR__ . '/../../includes/config.php';
require_once __DIR__ . '/../../includes/functions.php';

if (!isAdmin()) {
    header('Location: ../login.php');
    exit;
}

$id = intval($_GET['id'] ?? 0);
if (!$id) {
    header('Location: bank-accounts.php');
    exit;
}

// Get bank account
$stmt = $pdo->prepare("SELECT * FROM bank_accounts WHERE id = ?");
$stmt->execute([$id]);
$account = $stmt->fetch();

if (!$account) {
    header('Location: bank-accounts.php');
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_account'])) {
    $account_name = sanitize($_POST['account_name'] ?? '');
    $account_number = sanitize($_POST['account_number'] ?? '');
    $bank_name = sanitize($_POST['bank_name'] ?? '');
    $branch_name = sanitize($_POST['branch_name'] ?? '');
    $account_type = sanitize($_POST['account_type'] ?? 'savings');
    $currency = sanitize($_POST['currency'] ?? 'BDT');
    $status = isset($_POST['status']) ? 1 : 0;

    if (!empty($account_name) && !empty($account_number) && !empty($bank_name)) {
        try {
            $stmt = $pdo->prepare("UPDATE bank_accounts SET 
                account_name = ?, account_number = ?, bank_name = ?, 
                branch_name = ?, account_type = ?, currency = ?, status = ? 
                WHERE id = ?");
            $stmt->execute([
                $account_name, $account_number, $bank_name,
                $branch_name, $account_type, $currency, $status,
                $id
            ]);

            $_SESSION['success_msg'] = 'Bank account updated successfully!';
            header('Location: bank-accounts.php');
            exit;
        } catch (Exception $e) {
            $error = 'Error: ' . $e->getMessage();
        }
    } else {
        $error = 'Account name, number, and bank name are required';
    }
}

include '../header.php';
?>

<style>
.edit-card {
    background: #fff;
    border-radius: 12px;
    padding: 25px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.edit-card .card-title {
    font-weight: 600;
    margin-bottom: 20px;
    padding-bottom: 10px;
    border-bottom: 2px solid #e2e8f0;
}

.account-info-box {
    background: #f8fafc;
    border-radius: 8px;
    padding: 12px 15px;
    margin-bottom: 20px;
}

.account-info-box .label {
    color: #64748b;
    font-weight: 500;
    font-size: 13px;
}
</style>

<div class="admin-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fa-solid fa-edit me-2"></i> Edit Bank Account</h2>
        <div>
            <a href="view-bank-account.php?id=<?php echo $id; ?>" class="btn btn-primary">
                <i class="fa-solid fa-eye"></i> View
            </a>
            <a href="bank-accounts.php" class="btn btn-secondary">
                <i class="fa-solid fa-arrow-left"></i> Back
            </a>
        </div>
    </div>

    <?php if ($error): ?>
    <div class="alert alert-danger"><?php echo $error; ?></div>
    <?php endif; ?>

    <div class="edit-card">
        <h5 class="card-title">
            <i class="fa-solid fa-university me-2"></i>
            Edit Account - <?php echo htmlspecialchars($account['account_name']); ?>
        </h5>

        <!-- Account Info -->
        <div class="account-info-box">
            <div class="row">
                <div class="col-md-4">
                    <span class="label">Account ID:</span> #<?php echo $account['id']; ?>
                </div>
                <div class="col-md-4">
                    <span class="label">Created:</span> <?php echo date('d M Y', strtotime($account['created_at'])); ?>
                </div>
                <div class="col-md-4">
                    <span class="label">Current Balance:</span> 
                    <strong class="text-success"><?php echo number_format($account['current_balance']); ?> TK</strong>
                </div>
            </div>
        </div>

        <form method="POST">
            <input type="hidden" name="edit_account" value="1">
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Account Name *</label>
                        <input type="text" name="account_name" class="form-control" value="<?php echo htmlspecialchars($account['account_name']); ?>" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Account Number *</label>
                        <input type="text" name="account_number" class="form-control" value="<?php echo htmlspecialchars($account['account_number']); ?>" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Bank Name *</label>
                        <input type="text" name="bank_name" class="form-control" value="<?php echo htmlspecialchars($account['bank_name']); ?>" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Branch Name</label>
                        <input type="text" name="branch_name" class="form-control" value="<?php echo htmlspecialchars($account['branch_name']); ?>">
                    </div>
                </div>

                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Account Type</label>
                        <select name="account_type" class="form-control">
                            <option value="savings" <?php echo $account['account_type'] == 'savings' ? 'selected' : ''; ?>>Savings</option>
                            <option value="current" <?php echo $account['account_type'] == 'current' ? 'selected' : ''; ?>>Current</option>
                            <option value="loan" <?php echo $account['account_type'] == 'loan' ? 'selected' : ''; ?>>Loan</option>
                            <option value="fixed_deposit" <?php echo $account['account_type'] == 'fixed_deposit' ? 'selected' : ''; ?>>Fixed Deposit</option>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Currency</label>
                        <select name="currency" class="form-control">
                            <option value="BDT" <?php echo ($account['currency'] ?? 'BDT') == 'BDT' ? 'selected' : ''; ?>>BDT (Taka)</option>
                            <option value="USD" <?php echo ($account['currency'] ?? 'BDT') == 'USD' ? 'selected' : ''; ?>>USD (Dollar)</option>
                            <option value="EUR" <?php echo ($account['currency'] ?? 'BDT') == 'EUR' ? 'selected' : ''; ?>>EUR (Euro)</option>
                            <option value="GBP" <?php echo ($account['currency'] ?? 'BDT') == 'GBP' ? 'selected' : ''; ?>>GBP (Pound)</option>
                        </select>
                    </div>

                    <div class="mb-3">
                        <div class="form-check">
                            <input type="checkbox" name="status" class="form-check-input" id="status" <?php echo $account['status'] ? 'checked' : ''; ?>>
                            <label class="form-check-label" for="status">Active</label>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Opening Balance</label>
                        <input type="text" class="form-control" value="<?php echo number_format($account['opening_balance']); ?> TK" disabled>
                        <small class="text-muted">Opening balance cannot be changed</small>
                    </div>
                </div>
            </div>

            <div class="mt-3">
                <button type="submit" class="btn btn-primary">
                    <i class="fa-solid fa-save"></i> Update Account
                </button>
                <a href="bank-accounts.php" class="btn btn-secondary">Cancel</a>
            </div>
        </form>
    </div>
</div>

<?php include '../footer.php'; ?>