<?php
// admin/accounts/view-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 details
$stmt = $pdo->prepare("SELECT * FROM bank_accounts WHERE id = ?");
$stmt->execute([$id]);
$account = $stmt->fetch();

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

// Get recent transactions
$stmt = $pdo->prepare("SELECT * FROM account_transactions WHERE account_id = ? ORDER BY created_at DESC LIMIT 20");
$stmt->execute([$id]);
$transactions = $stmt->fetchAll();

// Get deposits
$stmt = $pdo->prepare("SELECT * FROM deposits WHERE account_id = ? ORDER BY created_at DESC LIMIT 10");
$stmt->execute([$id]);
$deposits = $stmt->fetchAll();

// Get withdrawals
$stmt = $pdo->prepare("SELECT * FROM withdrawals WHERE account_id = ? ORDER BY created_at DESC LIMIT 10");
$stmt->execute([$id]);
$withdrawals = $stmt->fetchAll();

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

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

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

.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
}

.info-item {
    background: #f8fafc;
    border-radius: 10px;
    padding: 15px 20px;
}

.info-item .label {
    color: #64748b;
    font-size: 13px;
    font-weight: 500;
    display: block;
    margin-bottom: 5px;
}

.info-item .value {
    font-size: 16px;
    font-weight: 600;
    color: #1e293b;
}

.info-item .value.text-success { color: #22c55e; }
.info-item .value.text-danger { color: #ef4444; }

.status-badge {
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 500;
    display: inline-block;
}

.status-badge.active { background: #86efac; color: #166534; }
.status-badge.inactive { background: #fca5a5; color: #991b1b; }

.account-type-badge {
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 500;
}

.account-type-badge.savings { background: #93c5fd; color: #1e40af; }
.account-type-badge.current { background: #fcd34d; color: #92400e; }
.account-type-badge.loan { background: #fca5a5; color: #991b1b; }
.account-type-badge.fixed_deposit { background: #86efac; color: #166534; }

.transaction-badge {
    padding: 3px 10px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 500;
}

.transaction-badge.deposit { background: #86efac; color: #166534; }
.transaction-badge.withdrawal { background: #fca5a5; color: #991b1b; }

.action-buttons {
    display: flex;
    gap: 10px;
    margin-top: 20px;
    flex-wrap: wrap;
}

.summary-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 15px;
    margin: 20px 0;
}

.summary-stat {
    background: #f8fafc;
    border-radius: 10px;
    padding: 15px;
    text-align: center;
}

.summary-stat .stat-number {
    font-size: 22px;
    font-weight: 700;
}

.summary-stat .stat-label {
    font-size: 12px;
    color: #64748b;
}
</style>

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

    <!-- Account Info -->
    <div class="view-card">
        <h5 class="card-title">
            <i class="fa-solid fa-receipt me-2"></i>
            <?php echo htmlspecialchars($account['account_name']); ?>
            <span class="status-badge <?php echo $account['status'] ? 'active' : 'inactive'; ?>">
                <?php echo $account['status'] ? 'Active' : 'Inactive'; ?>
            </span>
        </h5>

        <div class="info-grid">
            <div class="info-item">
                <span class="label">Account Name</span>
                <span class="value"><?php echo htmlspecialchars($account['account_name']); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Account Number</span>
                <span class="value"><code><?php echo htmlspecialchars($account['account_number']); ?></code></span>
            </div>
            <div class="info-item">
                <span class="label">Bank Name</span>
                <span class="value"><?php echo htmlspecialchars($account['bank_name']); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Branch Name</span>
                <span class="value"><?php echo htmlspecialchars($account['branch_name'] ?? 'N/A'); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Account Type</span>
                <span class="value">
                    <span class="account-type-badge <?php echo $account['account_type']; ?>">
                        <?php echo ucfirst(str_replace('_', ' ', $account['account_type'])); ?>
                    </span>
                </span>
            </div>
            <div class="info-item">
                <span class="label">Currency</span>
                <span class="value"><?php echo $account['currency'] ?? 'BDT'; ?></span>
            </div>
            <div class="info-item">
                <span class="label">Opening Balance</span>
                <span class="value"><?php echo number_format($account['opening_balance']); ?> TK</span>
            </div>
            <div class="info-item">
                <span class="label">Current Balance</span>
                <span class="value text-success"><?php echo number_format($account['current_balance']); ?> TK</span>
            </div>
            <div class="info-item">
                <span class="label">Created At</span>
                <span class="value"><?php echo date('d M Y, h:i A', strtotime($account['created_at'])); ?></span>
            </div>
        </div>

        <div class="action-buttons">
            <a href="deposits.php?account=<?php echo $account['id']; ?>" class="btn btn-success">
                <i class="fa-solid fa-arrow-down"></i> Add Deposit
            </a>
            <a href="withdrawals.php?account=<?php echo $account['id']; ?>" class="btn btn-danger">
                <i class="fa-solid fa-arrow-up"></i> Add Withdrawal
            </a>
            <button onclick="window.print()" class="btn btn-secondary">
                <i class="fa-solid fa-print"></i> Print
            </button>
        </div>
    </div>

    <!-- Summary Stats -->
    <div class="summary-stats">
        <div class="summary-stat">
            <div class="stat-number text-success"><?php echo count($deposits); ?></div>
            <div class="stat-label">Total Deposits</div>
        </div>
        <div class="summary-stat">
            <div class="stat-number text-danger"><?php echo count($withdrawals); ?></div>
            <div class="stat-label">Total Withdrawals</div>
        </div>
        <div class="summary-stat">
            <div class="stat-number text-primary"><?php echo count($transactions); ?></div>
            <div class="stat-label">Recent Transactions</div>
        </div>
    </div>

    <!-- Recent Transactions -->
    <div class="view-card">
        <h5 class="card-title">
            <i class="fa-solid fa-arrow-right-arrow-left me-2"></i>
            Recent Transactions
        </h5>

        <?php if (empty($transactions)): ?>
        <p class="text-muted text-center py-3">No transactions found</p>
        <?php else: ?>
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Type</th>
                        <th>Reference</th>
                        <th>Amount</th>
                        <th>Description</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($transactions as $txn): ?>
                    <tr>
                        <td><?php echo date('d M Y', strtotime($txn['transaction_date'])); ?></td>
                        <td>
                            <span class="transaction-badge <?php echo $txn['transaction_type']; ?>">
                                <?php echo ucfirst($txn['transaction_type']); ?>
                            </span>
                        </td>
                        <td><code><?php echo htmlspecialchars($txn['reference_no'] ?? '-'); ?></code></td>
                        <td class="<?php echo $txn['transaction_type'] == 'deposit' ? 'text-success' : 'text-danger'; ?>">
                            <?php echo $txn['transaction_type'] == 'deposit' ? '+' : '-'; ?>
                            <?php echo number_format($txn['amount']); ?> TK
                        </td>
                        <td><?php echo htmlspecialchars($txn['description'] ?? '-'); ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>

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