<?php 

	error_reporting(0);
	ini_set(“display_errors”, 0 );
	set_time_limit(0);

	include "protege_professor.php";

	ini_set("upload_max_filesize", "10M");
	ini_set("max_execution_time", "-1");
	ini_set("memory_limit", "-1");
	ini_set("realpath_cache_size", "-1");
	if (!isset($_SESSION)) {
	  session_start();
	}

// echo "<pre>";
// print_r($_POST);
// echo "</pre>";

// die();


// $dados = json_decode($_POST['dados']);

// $lote = $_POST['lote'];

// include "conexao.php";

// $query = mysqli_query($db, "SELECT * FROM lote INNER JOIN produto ON lote.produto_idproduto = produto.idproduto INNER JOIN empreendimento AS EMP ON produto.empreendimento_idempreendimento = EMP.idempreendimento INNER JOIN empreendimento_cadastro AS EC ON EMP.empreendimento_cadastro_id = EC.idempreendimento_cadastro WHERE `idlote` = $lote")or die(mysqli_error($db));

// $assoc = mysqli_fetch_assoc($query);

// echo "<pre>";
// print_r($assoc);
// echo "</pre>";
// die();

?>

<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	
	<title>Immobile business</title>
	<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport" />
	<meta content="" name="description" />
	<meta content="" name="author" />
	
	<!-- ================== BEGIN BASE CSS STYLE ================== -->
	<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet">
	<link href="https://immobilebusiness.com.br/admin//assets/plugins/jquery-ui/themes/base/minified/jquery-ui.min.css" rel="stylesheet" />
	<link href="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
	<link href="https://immobilebusiness.com.br/admin/assets/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
	<link href="https://immobilebusiness.com.br/admin/assets/css/animate.min.css" rel="stylesheet" />
	<link href="https://immobilebusiness.com.br/admin/assets/css/style.min.css" rel="stylesheet" />
	<link href="https://immobilebusiness.com.br/admin/assets/css/style-responsive.min.css" rel="stylesheet" />
	<link href="https://immobilebusiness.com.br/admin/assets/css/theme/default.css" rel="stylesheet" id="theme" />
	<!-- ================== END BASE CSS STYLE ================== -->
	
	<!-- ================== BEGIN PAGE LEVEL STYLE ================== -->
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.1/bootstrap-editable/css/bootstrap-editable.css" />
	<!-- ================== END PAGE LEVEL STYLE ================== -->
	

	<link rel="stylesheet" href="css/acc-wizard.min.css" />

	<!-- ================== BEGIN BASE JS ================== -->
	<script src="https://immobilebusiness.com.br/admin/assets/plugins/pace/pace.min.js"></script>

	<style type="text/css">
		thead.thead-dark > tr > th{
			font-weight: normal;
			color: #fff;
			background-color: #343a40;
			text-transform: uppercase;
			vertical-align: middle;
			text-align: center;
		}

		tbody > tr > td{
			vertical-align:middle!important;
			color: #000;
			border: 1px solid #343a40!important;
			text-align: center;
		}
	</style>
</head>
<body>

	<!-- begin #page-loader -->
	<div id="page-loader" class="fade in"><span class="spinner"></span></div>
	<!-- end #page-loader -->
    <div id="page-container" class="fade page-header-fixed page-sidebar-fixed">
    <!-- begin #header -->
    <?php include "topo.php";?>

    <div class="sidebar-bg"></div>
		<div id="content" class="content">	

			<div class="panel panel-inverse">
				<div class="panel-heading" style="height: 150%;">
					<h4 class="panel-title">
						Simulações de Venda
					</h4>
				</div>
				<div class="panel-body">

					<form action="relatorio_simulacao.php" method="POST">
						
						<div class="row">

							<div class="form-group row col-md-6">
								<label for="nome_cli" class="col-md-4">Digite o Nome do Cliente</label>
								<div class="col-md-8">
									<input class="form-control" type="text" name="nome_cli" id="nome_cli">
								</div>
							</div>

							<div class="form-group row col-md-6">
								<label for="num_simulacao" class="col-md-4">Digite o Número da Simulação</label>
								<div class="col-md-8">
									<input class="form-control" type="number" name="num_simulacao" id="num_simulacao">
								</div>
							</div>

						</div>

						<div class="form-group">
							<input type="text" class="hidden" name="id_corretor" value="<?php echo $imobiliaria_idimobiliaria;  ?>">
							<button class="btn btn-success" type="submit" name="enviar">Consultar</button>
						</div>

					</form>


					 <table class="table table-bordered" id='table'>
	                	<thead class="thead-dark">
	                		<tr>
	                			<th>Simulação</th>
	                			<th>Cliente</th>
	                			<th>Data</th>
	                			<th>Status</th>
	                			<th>Abrir</th>
	                		</tr>
	                	</thead>
	                	<tbody>

	                		<?php 

	                			include "conexao.php";

	                			if(isset($_POST['enviar'])){

	                				isset($_POST['nome_cli']) ? $nome_cli = addslashes($_POST['nome_cli']) : $nome_cli = '';
	                				isset($_POST['num_simulacao']) &&  ($_POST['num_simulacao']) != '' ? $num_simulacao = $_POST['num_simulacao'] : $num_simulacao = '000';
	                				isset($_POST['id_corretor'])  &&  ($_POST['id_corretor']) != '' ? $id_corretor = $_POST['id_corretor'] : $id_corretor = '000';

	                				if(in_array('49', $idrota)){

	                					$query = mysqli_query($db, "SELECT cliente.nome_cli, SV.id, SV.cliente, SV.corretor, SV.lote, SV.status, SV.timestamp FROM simulacao_venda AS SV INNER JOIN cliente ON cliente.idcliente = SV.cliente WHERE cliente.nome_cli LIKE '%$nome_cli%' OR SV.id = $num_simulacao")or die(mysqli_error($db));
                                  
                                        
	                					if(mysqli_num_rows($query)){
	                						while ($assoc = mysqli_fetch_assoc($query)) {
	                								
	                							$data = explode(' ', $assoc['timestamp']);
	                							$data = date('d/m/Y', strtotime(date($data[0])));

	                							$assoc['status'] != '1' ? $status = "Enviado" :  $status = "Não Enviado";


	                							?>

	                							<tr>
	                								<td><?php echo $assoc['id']; ?></td>
	                								<td><?php echo $assoc['nome_cli']; ?></td>
	                								<td><?php echo $data ?></td>
	                								<td><?php echo $status ?></td>
	                								<td><a href="demonstra_simulacao.php?id=<?php echo($assoc['id']) ?>" target="_blank" class="btn btn-success btn-sm">Ver Simulação</a></td>
	                							</tr>


	                							<?php

	                						}
	                					}

	                				}else{

	                					$query = mysqli_query($db, "SELECT cli.nome_cli, SV.id, SV.cliente, SV.corretor, SV.lote, SV.status, SV.timestamp FROM simulacao_venda AS SV INNER JOIN cliente AS cli ON cli.idcliente = SV.cliente INNER JOIN cliente AS imob ON SV.corretor = imob.idcliente WHERE (SV.corretor = $id_corretor OR imob.imob_id = $id_corretor) AND  (cli.nome_cli = '$nome_cli' OR SV.id = '$num_simulacao')")or die(mysqli_error($db));

	                					if(mysqli_num_rows($query)){
	                						while ($assoc = mysqli_fetch_assoc($query)) {
	                								
	                							$data = explode(' ', $assoc['timestamp']);
	                							$data = date('d/m/Y', strtotime(date($data[0])));

	                							?>

	                							<tr>
	                								<td><?php echo $assoc['id']; ?></td>
	                								<td><?php echo $assoc['nome_cli']; ?></td>
	                								<td><?php echo $data ?></td>
	                								<td><a href="demonstra_simulacao.php?id=<?php echo($assoc['id']) ?>" target="_blank" class="btn btn-success btn-sm">Ver Simulação</a></td>
	                							</tr>


	                							<?php

	                						}
	                					}

	                				}
	                			}

	                		 ?>

	                	</tbody>
	                </table>
				</div>
			</div>
		</div>
	</div>


	<div id="modal-aviso" class="modal fade" role="dialog">
	    <div class="modal-dialog">
	        <!-- Modal content-->
	        <div class="modal-content" >
	            <div class="modal-body">
	                <h3 class="page-title">Simulação de de Venda</h3>

	                <table class="table" id='table'>
	                	<thead class="thead-dark">
	                		<tr>
	                			<th>Parcela</th>
	                			<th>Quantidade Parcelas</th>
	                			<th>Valor</th>
	                			<th>Vencimento Primeira Parcela</th>
	                		</tr>
	                	</thead>
	                	<tbody>

	                		


	                		<?php 
	                			// 0 == parcela sinal
	                			// 1 == parcela entrada
	                			// 2 == parcela intermediaria
	                			// 3 == parcelamento

	                			$today = date('d/m/Y');

	                			foreach ($dados as $key => $value) {
	                				if($key == 0){
	                					?>
	                					<tr>
	                						<td>Parcela Sinal</td>
	                						<td><?php echo $key+1; ?></td>
	                						<td><?php echo 'R$ '.number_format($value[0], 2, ',', '.'); ?></td>
	                						<td><?php echo $today; ?></td>
	                					</tr>
	                					<?php
	                				}elseif($key == 1){

	                					$total_dias = 0;

	                					foreach ($value as $chave => $valor) {
	                						?>
	                						<tr>
	                							<td>Parcela Entrada</td>
	                							<td><?php echo str_pad($valor[0] , 3 , '0' , STR_PAD_LEFT).'/'.str_pad($valor[0] , 3 , '0' , STR_PAD_LEFT); ?></td>
	                							<td><?php echo 'R$ '.number_format($valor[1], 2, ',', '.'); ?></td>
	                							<td><?php echo date('d/m/Y', strtotime(date($valor[2]))); ?></td>
	                						</tr>
	                						<?php
	                					}
	                				}elseif($key == 2){
	                					$total_dias = 0;

	                					$aux = count($value);

	                					foreach ($value as $chave => $valor) {
	                						?>
	                						<tr>
	                							<td>Parcela Intermediaria</td>
	                							<td><?php echo str_pad($aux , 3 , '0' , STR_PAD_LEFT).'/'.str_pad($valor[0] , 3 , '0' , STR_PAD_LEFT); ?></td>
	                							<td><?php echo 'R$ '.number_format($valor[1], 2, ',', '.'); ?></td>
	                							<td><?php echo date('d/m/Y', strtotime(date($valor[2]))); ?></td>
	                						</tr>
	                						<?php
	                					}

	                					if(isset($dados[4])){

	                						?>
	                						<tr>
	                							<td>Parcela Unica</td>
	                							<td>01/01</td>
	                							<td><?php echo 'R$ '.number_format($dados[4], 2, ',', '.'); ?></td>
	                							<td>#</td>
	                						</tr>
	                						<?php

	                					}

	                				}elseif($key == 3){

	                					$aux = count($value);

	                					foreach ($value as $chave => $valor) {

	                						$aux == 1 ? $aux = $valor[0] : '';

	                						?>
	                						<tr>
	                							<td>Financiamento</td>
	                							<td><?php echo str_pad(($chave+1) , 3 , '0' , STR_PAD_LEFT).'/'.str_pad($aux, 3 , '0' , STR_PAD_LEFT);; ?></td>
	                							<td><?php echo 'R$ '.number_format($valor[1], 2, ',', '.'); ?></td>
	                							<td><?php echo date('d/m/Y', strtotime(date($valor[2]))); ?></td>
	                						</tr>
	                						<?php
	                					}
	                				}
	                			}


	                		 ?>

	                		<tr>
	                			<td colspan="1" style="background-color: #ccc; color: #000; font-weight: bold; text-align: center;"><?php echo $assoc['descricao_empreendimento']; ?></td>
	                			<td colspan="1" style="background-color: #ccc; color: #000; font-weight: bold; text-align: center;"><?php echo 'Quadra: '.$assoc['quadra'].' Lote: '.$assoc['lote']; ?></td>
	                			<td colspan="1" style="background-color: #ccc; color: #000; font-weight: bold; text-align: center;">VALOR DO LOTE: </td>
	                			<td colspan="1" style="background-color: #ccc; color: #369b4f; font-weight: bold; text-align: center;"><?php echo 'R$ '.number_format($assoc['valor'], 2, ',', '.'); ?></td>
	                		</tr>
	                	</tbody>
	                </table>
	            </div>
	            <div class="modal-footer" >
	                <button type="button" class="btn btn-defaut" data-dismiss="modal" >Fechar</button>
	            </div>
	        </div>
	    </div>
	</div>


	<script type="text/javascript" src="tableExport/libs/jsPDF/jspdf.min.js"></script>
	<script type="text/javascript" src="tableExport/libs/jsPDF-AutoTable/jspdf.plugin.autotable.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
	<script type="text/javascript" src="tableExport/libs/FileSaver/FileSaver.min.js"></script>
	<script type="text/javascript" src="tableExport/libs/js-xlsx/xlsx.core.min.js"></script>
	<script type="text/javascript" src="tableExport/tableExport.min.js"></script>
	<script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery/jquery-1.9.1.min.js"></script>
	<script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery/jquery-migrate-1.1.0.min.js"></script>
	<script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery-ui/ui/minified/jquery-ui.min.js"></script>
	<script src="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap/js/bootstrap.min.js"></script>
	<script src="https://immobilebusiness.com.br/admin/assets/plugins/slimscroll/jquery.slimscroll.min.js"></script>
	<script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery-cookie/jquery.cookie.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
    <script type="text/javascript" src="tableExport/libs/FileSaver/FileSaver.min.js"></script>
    <script type="text/javascript" src="tableExport/libs/js-xlsx/xlsx.core.min.js"></script>
    <script type="text/javascript" src="tableExport/tableExport.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/js/apps.min.js"></script>
    <!-- ================== BEGIN BASE JS ================== -->
	<script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery/jquery-1.9.1.min.js"></script>
	<script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery/jquery-migrate-1.1.0.min.js"></script>
	<script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery-ui/ui/minified/jquery-ui.min.js"></script>
	<script src="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap/js/bootstrap.min.js"></script>
	<script src="https://immobilebusiness.com.br/admin/assets/plugins/slimscroll/jquery.slimscroll.min.js"></script>
	<script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery-cookie/jquery.cookie.js"></script>

    <script type="text/javascript">
    	$(document).ready(function(){
    		App.init();

    		$('#table').tableExport({
    			type:'pdf',
    			jspdf: {orientation: 'p',
    			margins: {left:20, top:10},
    			autotable: false},
    			postCallback: function(){
    				console.log('done loading my humugoid file')
    			}
    		});

    		print();


    	});


    </script>

	
</body>
</html>