<?php

//USING
require_once 'controls/verifylogin.php';
include_once('../Database/MySQL.Aux.php');
require_once '../Class/model/FaturasModel.php';
require_once '../Class/controller/FaturasControl.php';
require_once '../Class/model/ClientesModel.php';
require_once '../Class/model/TiposRecebimentosModel.php';
require_once '../Class/model/FaturasDescricaoModel.php';
require_once '../Class/model/PastasModel.php';

$control = new FaturasControl();
$model = new FaturasModel();
$aux = new MySQLAux();

$modelDescricao = new FaturasDescricaoModel();

$control->setAction();
$control->setId();

$action = $control->getAction();
$id = $control->getId();

//LoadDados
if ($id > 0)
{
	$model->load($id);
	
	$modelDescricao->loadbyFatura($id);
}

if($action == 'save')
{
	try 
	{
		$result = $control->GetData();
		
		for ($i = 0; $i < count($_POST['descricao']); $i++) {
			
		    $descricao = $_POST['descricao'][$i];
		    $tiposrecebimentos = $_POST['tiposrecebimentos'][$i];
		    $valor = $_POST['valor'][$i];
		    
		    $control->GetDataDescricao($result, $descricao, $tiposrecebimentos, $valor);
		    
		}
		header( 'Location: faturas.php' );
	}
	catch(Exception $err)
	{
		echo "<script>alert('Ocorreu um problema ao salvar o registro.');</script>";
	}
}
	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Módulo Financeiro</title>

<?php include_once('controls/import.php'); ?>
<script src="js/faturas.js" type="text/javascript"></script>

<script>
$(document).ready(function(){
  $("#btnDuplicar").click(function(){
    $("#teste").clone().appendTo("#teste2");
  });
});
</script>

</head>
<body>

<?php include_once('controls/header.php'); ?>

<div class="content">

<div class="form"><?php include_once('controls/menu.php'); ?>

<div id="conteudo-site">

<div class="titulo-conteudo-site"><img src="images/applications.png"
	title="" alt="" /> Cadastro de Grupos de Contas</div>

<form id="frmRegister" method="post" action=""><input type="hidden"
	id="hdnAction" name="action" value="save" /> <input type="hidden"
	id="hdnId" name="Id" value="<?php echo $model->getId();?>" />

<table border="0" cellpadding="3" cellspacing="0" class="tblGeral"
	width="100%">
	<tr>
		<td><span class="title">Cliente:</span></td>
		<td>
		<select id="clientes" name="clientes" class="content-data">
			<option value="">Selecione</option>
			<?php 
			
				$clientes = new ClientesModel();
				
				foreach ($clientes->listAll() as $item) {
				
					$select = '';
					if ($item->getId() == $model->getCliente())
					{
						$select = "selected='selected'";
					}
					
					echo "<option value='{$item->getId()}' {$select}>{$item->getNomeFantasia()}</option>";
				}
			?>
		
		</select></td>
	</tr>
	<tr>
		<td><span class="title">Pasta:</span></td>
		<td>
		<select id="pastas" name="pastas" class="content-data">
			<option value="">Selecione</option>
			<?php 
			
				$pastas = new PastasModel();
				
				foreach ($pastas->listAll() as $item) {
				
					$select = '';
					if ($item->getId() == $model->getPasta())
					{
						$select = "selected='selected'";
					}
					
					echo "<option value='{$item->getId()}' {$select}>{$item->getNome()}</option>";
				}
			?>
		
		</select></td>
	</tr>
	<tr>
		<td><span class="title">Data de emissão:</span></td>
		<td><input type="text" id="dataemissao" name="dataemissao" class="content-data"
			style="width: 80px;" value="<?php echo $model->getDtEmissao();?>" onkeyup="formataData(this,event);" /></td>
	</tr>
	<tr>
		<td><span class="title">Data de vencimento:</span></td>
		<td><input type="text" id="datavencimento" name="datavencimento" class="content-data"
			style="width: 80px;" value="<?php echo $model->getDtVencimento();?>" onkeyup="formataData(this,event);" /></td>
	</tr>
	<tr>
		<td><span class="title">Núm. Nota:</span></td>
		<td><input type="text" id="numnota" name="numnota" class="content-data"
			style="width: 150px;" value="<?php echo $model->getNumNota();?>" onkeyup="formataInteiro(this,event);" /></td>
	</tr>
	</table>
	
	<br /><br />
	
	<div id="teste">
		<table border="0" cellpadding="3" cellspacing="0" class="tblGeral" width="80%" style="background:#fcfcfc;">
		        <tr>
		            <td><span class="title">Descrição:</span></td>
		            <td><input type='text' name="descricao[]" id="descricao[]" class="content-data" /></td>
		        </tr>
		        <tr>
					<td><span class="title">Tipo recebimento:</span></td>
					<td>
					<select id="tiposrecebimentos[]" name="tiposrecebimentos[]" class="content-data">
						<option value="">Selecione</option>
						<?php 
						
							$tiporecebimento = new TiposRecebimentosModel();
							
							foreach ($pastas->listAll() as $item) {
							
								$select = '';
								if ($item->getId() == $model->getPasta())
								{
									$select = "selected='selected'";
								}
								
								echo "<option value='{$item->getId()}' {$select}>{$item->getNome()}</option>";
							}
						?>
					
					</select></td>
		            
		        </tr>
		        <tr>
		            <td><span class="title">Valor:</span></td>
					<td><input type="text" id="valor[]" name="valor[]" class="content-data"
						style="width: 80px;" onkeyup="formataValor(this,event);" /></td>
		        </tr>
		</table>
		<br />
	</div>
	
	<div id="teste2"></div>
	
	<br />
	<input type="button" id="btnDuplicar" name="btnDuplicar" class="submit" value="Duplicar" />

<div id="pBotoes" style="text-align: right"><span>&nbsp;</span> <br />
<input type="submit" id="btnSalvar" name="btnSalvar" class="submit" value="Salvar" /> <input
	type="button" name="btnCancelar" class="submit" value="Cancelar"
	onclick="window. location.href='faturas.php'" /></div>

</form>

</div>

</div>

</div>

<?php include_once('controls/footer.php'); ?>

</body>
</html>