<?php
session_start();
require_once "db.php";

$user_id = $_SESSION['user_id'] ?? 0;
if($user_id == 0){
    die("User not logged in");
}

/* ===== SUMMARY QUERIES ===== */

// My commission count
$q1 = $conn->query("SELECT COUNT(*) c FROM referral_bonuses WHERE user_id = $user_id");
$my_commission = $q1->fetch_assoc()['c'] ?? 0;

// My commission benefits
$q2 = $conn->query("SELECT IFNULL(SUM(bonus_amount),0) s FROM referral_bonuses WHERE user_id = $user_id");
$my_commission_amount = $q2->fetch_assoc()['s'] ?? 0;

// Subordinate task benefits
$q3 = $conn->query("SELECT IFNULL(SUM(bonus_amount),0) s FROM video_task_bonus_records WHERE upline_id = $user_id");
$sub_task_amount = $q3->fetch_assoc()['s'] ?? 0;

// Subordinate completed tasks
$q4 = $conn->query("
    SELECT COUNT(*) c FROM user_video_tasks 
    WHERE user_id IN (SELECT id FROM users WHERE referred_by = $user_id)
");
$sub_tasks = $q4->fetch_assoc()['c'] ?? 0;

// Total revenue
$total_revenue = $my_commission_amount + $sub_task_amount;


/* ===== LAST 30 DAYS DATA ===== */
$days = [];
for($i=0;$i<30;$i++){
    $date = date("Y-m-d", strtotime("-$i days"));

    // Referral bonuses
    $r = $conn->query("
        SELECT COUNT(*) c, IFNULL(SUM(bonus_amount),0) s
        FROM referral_bonuses
        WHERE user_id = $user_id AND DATE(created_at) = '$date'
    ")->fetch_assoc();

    // Subordinate tasks
    $t = $conn->query("
        SELECT COUNT(*) c
        FROM user_video_tasks
        WHERE user_id IN (SELECT id FROM users WHERE referred_by = $user_id)
        AND DATE(completed_at) = '$date'
    ")->fetch_assoc();

    // Subordinate task bonuses
    $b = $conn->query("
        SELECT IFNULL(SUM(bonus_amount),0) s
        FROM video_task_bonus_records
        WHERE upline_id = $user_id AND DATE(created_at) = '$date'
    ")->fetch_assoc();

    $days[] = [
        'date' => date("m-d", strtotime($date)),
        'qty' => $r['c'],
        'task' => $r['s'],
        'sub' => $t['c'],
        'consumption' => $b['s']
    ];
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Daily statement</title>

<style>
*{
    box-sizing:border-box;
    margin:0;
    padding:0;
    font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Arial,sans-serif;
}
body::before{
    content:"";
    position:fixed;
    inset:0;
    background:url("bodyw.jpg") center/cover no-repeat;
    filter:blur(40px);
    transform:scale(1.1);
    z-index:-2;
}
body{
    background:#e5e5e5;
    padding-top:52px;
}
.top-bar{
    background:#147238;
    color:#fff;
    height:52px;
    display:flex;
    align-items:center;
    justify-content:center;
    position:fixed;
    top:0;
    left:0;
    right:0;
    z-index:1000;
    font-size:18px;
    font-weight:600;
}
.top-bar .back{
    position:absolute;
    left:14px;
    font-size:22px;
}
.wrapper{padding:14px;}
.summary-card{
    background:#147238;
    border-radius:14px;
    padding:14px;
    color:#fff;
}
.total{text-align:center;margin-bottom:12px;}
.total .value{font-size:30px;font-weight:700;}
.total .label{font-size:13px;}
.grid{
    display:grid;
    grid-template-columns:1fr 1fr;
    gap:10px;
}
.box{
    background:#f4f5ff;
    border-radius:10px;
    padding:12px 6px;
    text-align:center;
}
.box .num{font-size:20px;font-weight:600;color:black;}
.box .text{font-size:12px;color:#666;}
.section-title{
    margin:14px 4px 8px;
    font-size:14px;
    font-weight:600;
    color:white;
}
.table-wrapper{
    background:#fff;
    border-radius:12px;
    overflow:hidden;
}
table{
    width:100%;
    border-collapse:collapse;
    font-size:13px;
}
thead{background:#f3f3f3;}
th,td{padding:10px 6px;text-align:center;}
tbody tr{border-bottom:1px solid #eee;}
</style>
</head>

<body>

<div class="top-bar">
    <div class="back">‹</div>
    Daily Statement
</div>

<div class="wrapper">

<div class="summary-card">
    <div class="total">
        <div class="value"><?= number_format($total_revenue,2) ?></div>
        <div class="label">Total Revenue</div>
    </div>

    <div class="grid">
        <div class="box">
            <div class="num"><?= $my_commission ?></div>
            <div class="text">My Commission</div>
        </div>

        <div class="box">
            <div class="num"><?= number_format($my_commission_amount,2) ?></div>
            <div class="text">Commission Earnings (TSH)</div>
        </div>

        <div class="box">
            <div class="num"><?= $sub_tasks ?></div>
            <div class="text">Tasks Completed by Subordinates</div>
        </div>

        <div class="box">
            <div class="num"><?= number_format($sub_task_amount,2) ?></div>
            <div class="text">Subordinate Task Earnings (TSH)</div>
        </div>
    </div>
</div>

<div class="section-title">Last 30 Days</div>

<div class="table-wrapper">
<table>
<thead>
<tr>
<th>Date</th>
<th>Quantity</th>
<th>Task</th>
<th>Subordinate</th>
<th>Consumption</th>
</tr>
</thead>

<tbody>
<?php foreach($days as $d): ?>
<tr>
<td><?= $d['date'] ?></td>
<td><?= $d['qty'] ?></td>
<td><?= number_format($d['task'],2) ?></td>
<td><?= $d['sub'] ?></td>
<td><?= number_format($d['consumption'],2) ?></td>
</tr>
<?php endforeach; ?>
</tbody>

</table>
</div>

</div>

<?php include 'translate.php'; ?>

</body>
</html>