<?php
session_start();
if(!isset($_SESSION['username'])) {
    header("Location: ../login.php");
    exit();
}

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

$success_message = "";
$error_message = "";

// Add Bank Account
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_account'])) {
    $bank_name = mysqli_real_escape_string($conn, $_POST['bank_name']);
    $account_title = mysqli_real_escape_string($conn, $_POST['account_title']);
    $account_number = mysqli_real_escape_string($conn, $_POST['account_number']);
    $opening_balance = mysqli_real_escape_string($conn, $_POST['opening_balance']);
    $date_time = date('Y-m-d H:i:s');
    
    if(empty($bank_name) || empty($account_title) || empty($account_number)) {
        $error_message = "All fields are required!";
    } else {
        $check_sql = "SELECT id FROM bank_accounts WHERE account_number = '$account_number'";
        $check_result = mysqli_query($conn, $check_sql);
        
        if(mysqli_num_rows($check_result) > 0) {
            $error_message = "Bank account with this number already exists!";
        } else {
            $sql = "INSERT INTO bank_accounts (bank_name, account_title, account_number, balance, date_time) 
                    VALUES ('$bank_name', '$account_title', '$account_number', '$opening_balance', '$date_time')";
            
            if(mysqli_query($conn, $sql)) {
                $success_message = "Bank account added successfully!";
            } else {
                $error_message = "Error: " . mysqli_error($conn);
            }
        }
    }
}

// Get all bank accounts
$sql = "SELECT * FROM bank_accounts ORDER BY id DESC";
$result = mysqli_query($conn, $sql);
$total_accounts = mysqli_num_rows($result);
$total_balance = 0;
while($row = mysqli_fetch_assoc($result)) {
    $total_balance += $row['balance'];
}
mysqli_data_seek($result, 0);
?>

<?php include('../includes/log.php'); ?>
<?php include('../includes/sd.php'); ?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bank Accounts Management</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800&display=swap');
        
        * { font-family: 'Poppins', sans-serif; }
        h1, h2, h3, h4, h5, h6 { font-family: 'Times New Roman', serif; }
        body { background: #f5f5f5; }
        
        .page-header {
            background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
            border-radius: 15px;
            padding: 25px 30px;
            margin-bottom: 30px;
            color: #fff;
        }
        .page-header h2 { margin: 0 0 8px 0; font-size: 28px; color: #fff; }
        .page-header p { margin: 0; opacity: 0.95; font-weight: 500; }
        
        .form-card, .table-card {
            background: #fff;
            border-radius: 15px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            overflow: hidden;
            margin-bottom: 30px;
        }
        .card-header-custom {
            background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
            padding: 15px 20px;
            color: #fff;
        }
        .card-header-custom h4 { margin: 0; font-size: 18px; }
        .card-header-custom i { margin-right: 10px; }
        .card-body-custom { padding: 25px; }
        
        .form-label {
            font-weight: 600;
            color: #333;
            margin-bottom: 8px;
        }
        .form-label i { color: #A04657; margin-right: 8px; }
        .form-control {
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            padding: 12px 15px;
            font-weight: 500;
        }
        .form-control:focus {
            border-color: #A04657;
            box-shadow: 0 0 0 3px rgba(160,70,87,0.1);
        }
        
        .btn-submit {
            background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
            color: #fff;
            border: none;
            padding: 12px 30px;
            border-radius: 10px;
            font-weight: 600;
            font-size: 16px;
            width: 100%;
        }
        .btn-submit:hover { transform: translateY(-2px); box-shadow: 0 5px 15px rgba(160,70,87,0.3); color: #fff; }
        
        .alert-custom {
            padding: 15px 20px;
            border-radius: 10px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
            font-weight: 500;
        }
        .alert-success { background: #d4edda; color: #155724; border-left: 4px solid #28a745; }
        .alert-danger { background: #f8d7da; color: #721c24; border-left: 4px solid #dc3545; }
        
        .stat-card {
            background: #fff;
            border-radius: 15px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            border-left: 4px solid #A04657;
        }
        .stat-value { font-size: 32px; font-weight: 700; color: #A04657; }
        .stat-label { color: #666; font-size: 14px; font-weight: 500; margin-top: 5px; }
        
        .table th {
            background: #f8f9fa;
            font-weight: 700;
            color: #444;
        }
        .table td { vertical-align: middle; font-weight: 500; }
        .balance-positive { color: #28a745; font-weight: 700; }
        
        .btn-sm-custom {
            background: #A04657;
            color: #fff;
            padding: 5px 12px;
            border-radius: 8px;
            text-decoration: none;
            font-size: 12px;
            font-weight: 600;
        }
        .btn-sm-custom:hover { background: #6b2a36; color: #fff; }
        .required { color: #dc3545; }
        
        @media (max-width: 768px) {
            .page-header { padding: 20px; }
            .page-header h2 { font-size: 22px; }
            .stat-value { font-size: 24px; }
        }
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="page-header">
        <div>
            <h2><i class="fas fa-university"></i> Bank Accounts Management</h2>
            <p>Manage bank accounts and track balances</p>
        </div>
        <div><i class="fas fa-building" style="font-size: 48px; opacity: 0.5;"></i></div>
    </div>
    
    <?php if($success_message): ?>
        <div class="alert-custom alert-success"><i class="fas fa-check-circle fa-lg"></i> <?php echo $success_message; ?></div>
    <?php endif; ?>
    <?php if($error_message): ?>
        <div class="alert-custom alert-danger"><i class="fas fa-exclamation-triangle fa-lg"></i> <?php echo $error_message; ?></div>
    <?php endif; ?>
    
    <div class="row">
        <div class="col-lg-5">
            <div class="form-card">
                <div class="card-header-custom">
                    <h4><i class="fas fa-plus-circle"></i> Add New Bank Account</h4>
                </div>
                <div class="card-body-custom">
                    <form method="POST" action="">
                        <div class="mb-3">
                            <label class="form-label"><i class="fas fa-university"></i> Bank Name <span class="required">*</span></label>
                            <input type="text" name="bank_name" class="form-control" placeholder="e.g., HBL, UBL, Alfalah" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label"><i class="fas fa-user"></i> Account Title <span class="required">*</span></label>
                            <input type="text" name="account_title" class="form-control" placeholder="Full name as per bank account" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label"><i class="fas fa-credit-card"></i> Account Number <span class="required">*</span></label>
                            <input type="text" name="account_number" class="form-control" placeholder="Enter account number" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label"><i class="fas fa-rupee-sign"></i> Opening Balance</label>
                            <input type="number" step="0.01" name="opening_balance" class="form-control" value="0.00" placeholder="Opening balance amount">
                        </div>
                        <button type="submit" name="add_account" class="btn-submit">
                            <i class="fas fa-save"></i> Add Bank Account
                        </button>
                    </form>
                </div>
            </div>
        </div>
        
        <div class="col-lg-7">
            <div class="table-card">
                <div class="card-header-custom">
                    <h4><i class="fas fa-list"></i> Bank Accounts List</h4>
                </div>
                <div class="card-body-custom">
                    <div class="row mb-4">
                        <div class="col-md-6">
                            <div class="stat-card">
                                <div class="stat-value"><?php echo $total_accounts; ?></div>
                                <div class="stat-label">Total Bank Accounts</div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="stat-card">
                                <div class="stat-value">Rs <?php echo number_format($total_balance, 2); ?></div>
                                <div class="stat-label">Total Bank Balance</div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr><th>ID</th><th>Bank Name</th><th>Account Title</th><th>Account Number</th><th>Balance</th><th>Action</th></tr>
                            </thead>
                            <tbody>
                                <?php if(mysqli_num_rows($result) > 0): ?>
                                    <?php while($account = mysqli_fetch_assoc($result)): ?>
                                        <tr>
                                            <td><?php echo $account['id']; ?></td>
                                            <td><strong><?php echo htmlspecialchars($account['bank_name']); ?></strong></td>
                                            <td><?php echo htmlspecialchars($account['account_title']); ?></td>
                                            <td><?php echo htmlspecialchars($account['account_number']); ?></td>
                                            <td class="balance-positive">Rs <?php echo number_format($account['balance'], 2); ?></td>
                                            <td>
                                                <a href="bank_book.php?bank_id=<?php echo $account['id']; ?>" class="btn-sm-custom">
                                                    <i class="fas fa-book"></i> Ledger
                                                </a>
                                            </td>
                                        </tr>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <tr><td colspan="6" class="text-center py-5"><i class="fas fa-university" style="font-size: 48px; color: #ccc;"></i><p class="mt-3">No bank accounts found</p><small>Add your first bank account</small></td></tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include('../includes/ft.php'); ?>
</body>
</html>