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

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

// Handle Add Bank Account
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_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');
    $opening_balance = floatval($_POST['opening_balance'] ?? 0);
    $status = isset($_POST['status']) ? 1 : 0;

    if (!empty($account_name) && !empty($account_number) && !empty($bank_name)) {
        try {
            $stmt = $pdo->prepare("INSERT INTO bank_accounts 
                (account_name, account_number, bank_name, branch_name, account_type, opening_balance, current_balance, status) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
            $stmt->execute([$account_name, $account_number, $bank_name, $branch_name, $account_type, $opening_balance, $opening_balance, $status]);
            $_SESSION['success_msg'] = 'Bank account added successfully!';
            header('Location: bank-accounts.php');
            exit;
        } catch (Exception $e) {
            $_SESSION['error_msg'] = 'Error: ' . $e->getMessage();
        }
    }
}

// Get bank accounts
$accounts = $pdo->query("SELECT * FROM bank_accounts ORDER BY id DESC")->fetchAll();

// Calculate total balance
$total_balance = 0;
foreach ($accounts as $acc) {
    $total_balance += $acc['current_balance'];
}

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

<style>
.account-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 15px;
    margin-bottom: 25px;
}

.stat-box {
    background: #fff;
    border-radius: 12px;
    padding: 18px 20px;
    text-align: center;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

.stat-box .stat-number {
    font-size: 28px;
    font-weight: 700;
}

.stat-box .stat-label {
    font-size: 13px;
    color: #64748b;
}

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

.account-card .card-title {
    font-weight: 600;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid #f1f5f9;
}

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

.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: 11px;
    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; }

.action-btns {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
}
</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 Accounts</h2>
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#accountModal">
            <i class="fa-solid fa-plus"></i> Add Account
        </button>
    </div>

    <!-- Stats -->
    <div class="account-stats">
        <div class="stat-box">
            <div class="stat-number text-primary"><?php echo count($accounts); ?></div>
            <div class="stat-label">Total Accounts</div>
        </div>
        <div class="stat-box">
            <div class="stat-number text-success"><?php echo number_format($total_balance); ?> TK</div>
            <div class="stat-label">Total Balance</div>
        </div>
    </div>

    <?php if ($_SESSION['success_msg'] ?? ''): ?>
    <div class="alert alert-success"><?php echo $_SESSION['success_msg']; unset($_SESSION['success_msg']); ?></div>
    <?php endif; ?>

    <div class="account-card">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Account Name</th>
                        <th>Account No</th>
                        <th>Bank</th>
                        <th>Branch</th>
                        <th>Type</th>
                        <th>Balance</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($accounts)): ?>
                    <tr><td colspan="8" class="text-center">No bank accounts found</td></tr>
                    <?php else: ?>
                    <?php foreach ($accounts as $acc): ?>
                    <tr>
                        <td><strong><?php echo htmlspecialchars($acc['account_name']); ?></strong></td>
                        <td><code><?php echo htmlspecialchars($acc['account_number']); ?></code></td>
                        <td><?php echo htmlspecialchars($acc['bank_name']); ?></td>
                        <td><?php echo htmlspecialchars($acc['branch_name'] ?? '-'); ?></td>
                        <td>
                            <span class="account-type-badge <?php echo $acc['account_type']; ?>">
                                <?php echo ucfirst(str_replace('_', ' ', $acc['account_type'])); ?>
                            </span>
                        </td>
                        <td class="fw-bold text-success"><?php echo number_format($acc['current_balance']); ?> TK</td>
                        <td>
                            <span class="status-badge <?php echo $acc['status'] ? 'active' : 'inactive'; ?>">
                                <?php echo $acc['status'] ? 'Active' : 'Inactive'; ?>
                            </span>
                        </td>
                        <td>
    <div class="action-btns">
        <a href="view-bank-account.php?id=<?php echo $acc['id']; ?>" class="btn btn-sm btn-primary" title="View">
            <i class="fa-solid fa-eye"></i>
        </a>
        <a href="edit-bank-account.php?id=<?php echo $acc['id']; ?>" class="btn btn-sm btn-warning" title="Edit">
            <i class="fa-solid fa-edit"></i>
        </a>
        <a href="deposits.php?account=<?php echo $acc['id']; ?>" class="btn btn-sm btn-success" title="Add Deposit">
            <i class="fa-solid fa-arrow-down"></i>
        </a>
        <a href="withdrawals.php?account=<?php echo $acc['id']; ?>" class="btn btn-sm btn-danger" title="Add Withdrawal">
            <i class="fa-solid fa-arrow-up"></i>
        </a>
    </div>
</td>
                    </tr>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Add Account Modal -->
<div class="modal fade" id="accountModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Add Bank Account</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <input type="hidden" name="add_account" value="1">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Account Name *</label>
                        <input type="text" name="account_name" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Account Number *</label>
                        <input type="text" name="account_number" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Bank Name *</label>
                        <input type="text" name="bank_name" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Branch Name</label>
                        <input type="text" name="branch_name" class="form-control">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Account Type</label>
                        <select name="account_type" class="form-control">
                            <option value="savings">Savings</option>
                            <option value="current">Current</option>
                            <option value="loan">Loan</option>
                            <option value="fixed_deposit">Fixed Deposit</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Opening Balance</label>
                        <input type="number" name="opening_balance" class="form-control" step="0.01" value="0">
                    </div>
                    <div class="mb-3">
                        <div class="form-check">
                            <input type="checkbox" name="status" class="form-check-input" id="status" checked>
                            <label class="form-check-label" for="status">Active</label>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">Save</button>
                </div>
            </form>
        </div>
    </div>
</div>

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