<?php
require '../element/connection.php';
require '../element/auth.php'; // Your admin auth check

// Get filter parameters
$user_filter = isset($_GET['user_id']) ? (int)$_GET['user_id'] : '';
$date_from = isset($_GET['date_from']) ? $_GET['date_from'] : '';
$date_to = isset($_GET['date_to']) ? $_GET['date_to'] : '';

// Build query
$query = "SELECT h.*, u.name as user_name 
          FROM income_history h 
          LEFT JOIN users u ON h.user_id = u.user_id 
          WHERE 1=1";

if ($user_filter) {
    $query .= " AND h.user_id = $user_filter";
}

if ($date_from) {
    $query .= " AND DATE(h.created_at) >= '$date_from'";
}

if ($date_to) {
    $query .= " AND DATE(h.created_at) <= '$date_to'";
}

$query .= " ORDER BY h.id DESC";

$result = $conn->query($query);

// Calculate totals
$totalQuery = "SELECT SUM(amount) as total, SUM(tds) as total_tds, SUM(admin_charges) as total_admin FROM income_history WHERE 1=1";
if ($user_filter) {
    $totalQuery .= " AND user_id = $user_filter";
}
if ($date_from) {
    $totalQuery .= " AND DATE(created_at) >= '$date_from'";
}
if ($date_to) {
    $totalQuery .= " AND DATE(created_at) <= '$date_to'";
}
$totals = $conn->query($totalQuery)->fetch_assoc();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Income History - Admin Panel</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .summary-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
        }
        .filter-card {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="container-fluid mt-4">
        <h2><i class="fas fa-history"></i> Income History</h2>
        
        <!-- Summary Cards -->
        <div class="row">
            <div class="col-md-4">
                <div class="summary-card">
                    <h5>Total Paid</h5>
                    <h3>₹<?= number_format($totals['total'] ?? 0, 2) ?></h3>
                </div>
            </div>
            <div class="col-md-4">
                <div class="summary-card" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);">
                    <h5>TDS Collected</h5>
                    <h3>₹<?= number_format($totals['total_tds'] ?? 0, 2) ?></h3>
                </div>
            </div>
            <div class="col-md-4">
                <div class="summary-card" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);">
                    <h5>Admin Charges</h5>
                    <h3>₹<?= number_format($totals['total_admin'] ?? 0, 2) ?></h3>
                </div>
            </div>
        </div>
        
        <!-- Filter Form -->
        <div class="filter-card">
            <form method="GET" class="row g-3">
                <div class="col-md-3">
                    <label>User ID</label>
                    <input type="number" name="user_id" class="form-control" value="<?= $user_filter ?>" placeholder="Enter User ID">
                </div>
                <div class="col-md-3">
                    <label>Date From</label>
                    <input type="date" name="date_from" class="form-control" value="<?= $date_from ?>">
                </div>
                <div class="col-md-3">
                    <label>Date To</label>
                    <input type="date" name="date_to" class="form-control" value="<?= $date_to ?>">
                </div>
                <div class="col-md-3">
                    <label>&nbsp;</label>
                    <button type="submit" class="btn btn-primary d-block"><i class="fas fa-filter"></i> Filter</button>
                </div>
            </form>
        </div>
        
        <!-- Transactions Table -->
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead class="table-dark">
                    <tr>
                        <th>ID</th>
                        <th>Date & Time</th>
                        <th>User ID</th>
                        <th>User Name</th>
                        <th>Description</th>
                        <th>Gross Amount</th>
                        <th>TDS (10%)</th>
                        <th>Admin Charges</th>
                        <th>Net Amount</th>
                        <th>Type</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($result && $result->num_rows > 0): ?>
                        <?php while($row = $result->fetch_assoc()): 
                            $gross = $row['amount'] + $row['tds'] + $row['admin_charges'];
                        ?>
                        <tr>
                            <td><?= $row['id'] ?></td>
                            <td><?= date('d M Y H:i', strtotime($row['created_at'] ?? 'now')) ?></td>
                            <td><?= $row['user_id'] ?></td>
                            <td><?= htmlspecialchars($row['user_name'] ?? 'N/A') ?></td>
                            <td><?= htmlspecialchars($row['description']) ?></td>
                            <td>₹<?= number_format($gross, 2) ?></td>
                            <td>₹<?= number_format($row['tds'] ?? 0, 2) ?></td>
                            <td>₹<?= number_format($row['admin_charges'] ?? 0, 2) ?></td>
                            <td><strong>₹<?= number_format($row['amount'], 2) ?></strong></td>
                            <td>
                                <?php if($row['credit_debit'] == 1): ?>
                                    <span class="badge bg-success">Credit</span>
                                <?php else: ?>
                                    <span class="badge bg-danger">Debit</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="10" class="text-center">No transactions found</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>