<?php if (Session::get("tipo_usuario") != "1"){ show_403();} ?>
<?php import_file(DIR_TEMPLATES."header.php", ["title" => "Relatório de Saques por Mês", "active_page" => "financeiro"]); ?>
<?php import_file(DIR_MODELS."Model.class.php"); ?>

<?php 

	$mes = isset($_GET["mes"]) ? filter_input(INPUT_GET, "mes", FILTER_SANITIZE_NUMBER_INT) : date("m");
	$ano = isset($_GET["ano"]) ? filter_input(INPUT_GET, "ano", FILTER_SANITIZE_NUMBER_INT) : date("Y");

	$model = new Model();

	if (empty($mes)) {
		$data_inicio  = "{$ano}-01-01 00:00:00";
		$data_termino = "{$ano}-12-31 23:59:59";
	}
	else{
		$data_inicio  = "{$ano}-{$mes}-01 00:00:00";
		$data_termino = date("Y-m-t 23:59:59", strtotime($data_inicio));
	}

	$result = $model->query("
		SELECT
			SUM(valor_saque) AS valor_total_solicitado,
			SUM(valor_taxa_saque) AS taxa_total_saque,
			(SUM(valor_saque) - SUM(valor_taxa_saque)) AS valor_total_sacado
		FROM saque 
		WHERE 
			status_saque = 'S' AND 
			datahora_saque BETWEEN :data_inicio AND :data_termino
	", [
		":data_inicio" => $data_inicio,
		":data_termino" => $data_termino
	]);

	$data = $result->fetch();

?>

<h4 class="mb-3">
	<i class="fa-regular fa-file-invoice-dollar"></i> Relatório de Saques por Mês
</h4>

<div class="form-row justify-content-center">
	<div class="col-xl-6 col-lg-8 col-md-10 col-sm-12">
		<form action="" method="get">
			<div class="form-group">
				<label for="mes">Filtros (Data do Saque)</label>
				<div class="input-group">
					<select name="mes" id="mes" class="form-control">
						<option value="">Todos</option>
						<option value="01">Janeiro</option>
						<option value="02">Fevereiro</option>
						<option value="03">Março</option>
						<option value="04">Abril</option>
						<option value="05">Maio</option>
						<option value="06">Junho</option>
						<option value="07">Julho</option>
						<option value="08">Agosto</option>
						<option value="09">Setembro</option>
						<option value="10">Outubro</option>
						<option value="11">Novembro</option>
						<option value="12">Dezembro</option>
					</select>
					<select name="ano" id="ano" class="form-control" required>
						<?php for ($i = 2020; $i <= date("Y"); $i++): ?>
							<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
						<?php endfor; ?>
					</select>
					<div class="input-group-append">
						<button type="submit" class="btn btn-primary">
							<i class="fa-regular fa-search"></i>
						</button>
					</div>
				</div>
			</div>
		</form>
	</div>
</div>
<script>
	$(function(){
		$("#mes").val("<?php echo $mes; ?>");
		$("#ano").val("<?php echo $ano; ?>");
	});
</script>

<div class="form-row">
	<div class="col-md-4">
		<div class="content-box mb-3">
			<h3 class="text-center">
				<small>Valor Total Solicitado</small><br>
				<?php echo money_mask_BR($data->valor_total_solicitado); ?>
			</h3>
		</div>
	</div>

	<div class="col-md-4">
		<div class="content-box mb-3">
			<h3 class="text-center">
				<small>Valor Total das Taxas</small><br>
				<?php echo money_mask_BR($data->taxa_total_saque); ?>
			</h3>
		</div>
	</div>

	<div class="col-md-4">
		<div class="content-box mb-3">
			<h3 class="text-center">
				<small>Valor Total Sacado</small><br>
				<?php echo money_mask_BR($data->valor_total_sacado); ?>
			</h3>
		</div>
	</div>
</div>

<div class="content-box">
	<?php 

		$result = $model->query("
			SELECT * FROM saque saq
			INNER JOIN banco bnc ON (saq.id_banco = bnc.id_banco) 
			INNER JOIN usuario usr ON (saq.id_usuario = usr.id_usuario) 
			WHERE 
				saq.status_saque = 'S' AND 
				saq.datahora_saque BETWEEN :data_inicio AND :data_termino
		", [
			":data_inicio" => $data_inicio,
			":data_termino" => $data_termino
		]);

	?>

	<h5 class="mb-3">Solicitações de Saque</h5>

	<div class="table-responsive">
		<table class="table table-sm table-striped table-hover">
			<thead>
				<tr>
					<th class="align-middle text-left" style="width: 30%;">Usuário</th>
					<th class="align-middle text-left" style="width: 20%;">Conta Bancária</th>
					<th class="text-center align-middle" style="width: 10%;">Valor Solicidado</th>
					<th class="text-center align-middle" style="width: 10%;">Taxa</th>
					<th class="text-center align-middle" style="width: 10%;">Valor do Saque</th>
					<th class="text-center align-middle" style="width: 10%;">Data da Solicitação</th>
					<th class="text-center align-middle" style="width: 10%;">Data do Saque</th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ($result->fetchAll() as $i): ?>
					<tr>
						<td class="text-left align-middle">
							<?php 

								echo "
									{$i->nome_usuario}<br>
									<small>{$i->cpf_usuario}</small><br>
									<small>{$i->email_usuario} | {$i->telefone_usuario}</small>
								"; 

							?>
						</td>
						<td class="align-middle text-left">
							<?php 

								echo "
									{$i->codigo_banco} - {$i->nome_banco}<br>
									Agência: {$i->numero_agencia_saque}<br>
									Conta: {$i->numero_conta_saque}<br>
									PIX: {$i->chave_pix_saque}
								"; 

							?>	
						</td>
						<td class="text-center align-middle"><?php echo money_mask_BR($i->valor_saque); ?></td>
						<td class="text-center align-middle"><?php echo money_mask_BR($i->valor_taxa_saque); ?></td>
						<td class="text-center align-middle"><?php echo money_mask_BR($i->valor_saque - $i->valor_taxa_saque); ?></td>
						<td class="text-center align-middle"><?php echo convert_date($i->datahora_solicitacao); ?></td>
						<td class="text-center align-middle"><?php echo convert_date($i->datahora_saque); ?></td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
	</div>
</div>

<?php import_file(DIR_TEMPLATES."footer.php"); ?>