<?php
session_start();

require_once('functions.php');
require_once('dbconnection.php');
require_once('includes/init.php');

$password = $_SESSION['password'];
$admin_data = get_admin($conn, $password);
if ($password != $admin_data['password']) {
	header('location: login.php');
}
if ( !isset($_SESSION['password']) ) {
	header('location: login.php');
}
if ( empty( $admin_data ) ) header('location: login.php');

$pageTitle = "Refer Details";


if (isset($_GET['delete'])) {
    $id = $_GET['delete'];
    $sql = "DELETE FROM task WHERE id = $id";
	$result = $conn -> query($sql);
    if ($result === TRUE) {
		snack ("success", "Success");
    } else {
    	snack ("error", "Failed");
    	echo $conn->error;
    }
}

$list = 'all';
$refert = '1234';

$sql = "SELECT * FROM users ORDER BY date DESC";

if (isset($_GET['user'])) {
	$list = $_GET['user'];
	$pageTitle = $pageTitle . ' - ' . $list;
    $sql = "SELECT * FROM users WHERE ref = $list";
}

$data = mysqli_query($conn, $sql);

$rows = [];
while($row = mysqli_fetch_array($data))
{
    $rows[] = $row;
}

$index = 1 ;

$sql2 = "SELECT
		( SELECT COUNT(1) FROM history) AS history_count,
		( SELECT SUM(amount) FROM history WHERE status = 'Paid') AS paid_amount,
		( SELECT SUM(amount) FROM history WHERE status = 'Pending') AS pending_amount
		FROM dual";
if (isset($_GET['list'])) {
	$list = $_GET['list'];
	$sql2 = "SELECT
		( SELECT COUNT(1) FROM history WHERE number = '$list') AS history_count,
		( SELECT SUM(amount) FROM history WHERE status = 'Paid') AS paid_amount,
		( SELECT SUM(amount) FROM history WHERE status = 'Pending') AS pending_amount
		FROM dual";
}		
$result2 = $conn -> query($sql2);
$data2 = $result2 -> fetch_assoc();

?>
<!DOCTYPE html>
<html>
	<head>
		<title><?php echo $pageTitle ?></title>
		<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=false, shrink-to-fit=no">
		<link rel="stylesheet" href="css/style.css">
		<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
		<style>
			*, .select {
				user-select: text !important;
			}
			main {
				overflow: auto;
				padding: 1em;
			}
			table {
				white-space: nowrap;
			}
			tbody > tr:nth-child(even) {
				background: #F8F8F8;
			}
			.Paid {
				color: green;
			}
			.Pending {
				color: red;
			}
		</style>
	</head>
	<body class="bg-gray-200">
		<?php include("header.php") ?>
		<main>
			<section class="fr-container fr-lg" style="width: 800px;">
									<section class="table-wrapper">
						<table class="table table-divo">
							<thead><tr>
					<th scope="col">ID</th>
					<th scope="col">Name</th>
					<th scope="col">Mobile</th>
					<th scope="col">Task</th>
					<th scope="col">Ref</th>
				
				</tr></thead>
				<tbody>
				<?php foreach ($rows as $row) : ?>
				<tr>
				    
					<td><?php echo $index ?></td>
					<td><?php echo $row['fname'] ?></td>
					<td><?php echo $row['mobile'] ?></td>
					<td><?php echo $row['today_task'] ?></td>
					<td><?php echo $row['t_ref'] ?></td>
				</tr>
				<?php $index++ ?>
				<?php endforeach ?>
				</tbody>
			</table>
		</main>
		
		<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>


		<?php if (isset($_GET['success'])) : ?>
			<p class="snackbar success">Success</p>
		<?php endif ?>
		<?php include('footer.php') ?>
	
	</body>
</html>