
<?php 
session_start();
if(!isset($_SESSION['idUsuario'])){
	header("location:../");
}
include_once '../conexao/index.php';

error_reporting(E_ALL);

$_SESSION['idUsuario'] = $_SESSION['idUsuario'];
$_SESSION['nomeUsuario'] = $_SESSION['nomeUsuario'];
$_SESSION['categoriaUsuario'] = $_SESSION['categoriaUsuario']; 
/*Variaveis do Sistema*/
/*********************************************/
// echo $_SESSION['idUsuario']." - ".$_SESSION['nomeUsuario']."- ".$_SESSION['categoriaUsuario'];

$caixa = "Aberto";

	if (isset($_GET['apagar'])) {
		$sqlBox = "DELETE FROM artigos_devolvidos_temp WHERE user = '".$_SESSION['idUsuario']."'";
		$rsBox = mysqli_query($db, $sqlBox);
		if ($rsBox > 0) {
			header("location: devolucao.php");
		}
	}
?>

<!DOCTYPE html>
<html>
<head>
	<title>iVone</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width-device-width, initial-scale=1">
	<link rel="shorcut icon"  href="../img/config/5087847.png">
	<link rel="stylesheet" type="text/css" href="css/nota_credito.css">
	<link rel="stylesheet" type="text/css" href="../font-awesome/css/font-awesome.css">
	<link rel="stylesheet" type="text/css" href="../font-awesome/css/font-awesome.min.css">
	<link rel="stylesheet" type="text/css" href="../css/jquery-ui.min.css"> 
	<link rel="stylesheet" type="text/css" href="../css/select2.min.css">
	<link rel="stylesheet" type="text/css" href="../css/jquery-ui.css">
	<script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
	<script type="text/javascript" src="../js/jquery-ui.min.js"></script>
	<script type="text/javascript" src="../js/select2.min.js"></script>
	<script type="text/javascript" src="../js/jquery-ui.js"></script>
	<link href="datatables.min.css" rel="stylesheet"/>
	<script src="datatables.min.js"></script>
	<style type="text/css">
		#examples tbody tr{
			transition: all 0.3s ease-in-out;
		}
		#examples tbody tr:hover{
			background: #ccc;
			cursor: pointer;
		}
		.principal .direita .rodapeDireita .div2{
			margin-top: 5px;
			display: flex;
			flex-direction: row;
			justify-content: flex-end;
		}
		.principal .direita .rodapeDireita .div2 a{
			padding: 5px 10px;
			color: #fff;
			cursor: pointer;
		}
		.principal .direita .rodapeDireita .div2 a:nth-child(1){
			background-color: #F43248;
		}
		.principal .direita .rodapeDireita .div2 a:nth-child(2){
			background-color: #EB7D08;
			margin-left: 10px;
		}
		.principal .direita .rodapeDireita .div1 .right input{
			width: 35%;
			padding: 8px;
			border: 1px solid #ccc;
		}
		.principal .direita .conteudoDireita .tbl{
			width: 100%;
			height: 45vh;
			overflow-y: scroll;
		}
		.principal .direita .conteudoDireita table tr:nth-child(even){
			background-color: none;
		}
	</style>
</head>
<body>
<div class="principal">
	<div class="esquerda">
		<div class="cabecalhoEsquerda">
			<h4 class="operador">Operador: <?php echo $_SESSION['nomeUsuario']; ?></h4>
			<p class="relogio">
				<?php 
					$data_hora = date("Y-m-d H:i:s");
					echo $data_hora;
				?>
			</p>
			<a style="margin-left: 5px;" href="factura.php"><i class="fa fa-refresh" aria-hidden="true"></i></a>
		</div>
		<div class="conteudoEsquerda" style="padding: 15px;overflow-y: scroll;background: #fff;">
				<table id="examples" style="height: 75vh;">
					<thead>
						<td>#</td>
						<td>Descricao da factura</td>
						<td>Valor</td>
						<td>Cliente</td>
					</thead>
					<tbody>
				<?php 
						$sql = "SELECT idpedido, descpedido, pagamentopedido, (SELECT CONCAT(nome, ' ', apelido) FROM clientes as c WHERE c.id = f.clientepedido) as cl FROM pedido as f";
						$rs = mysqli_query($db, $sql);
						while ($dados = mysqli_fetch_array($rs)) {
					?>
						<tr>
							<td><?php echo $dados['idpedido']; ?></td>
							<td><?php echo $dados['descpedido'] ?></td>
							<td><?php echo $dados['pagamentopedido'] ?></td>
							<td><?php echo $dados['cl'] ?></td>
						</tr>
					<?php
						}
					?>
					</tbody>
				</table>
		</div>
	</div>
	<div class="direita">
		<div class="cabecalhoDireita" style="width: 100%;">
			<a class="fecharSistema"><i class="fa fa-arrow-left" aria-hidden="true"></i></i> Sair</a>
			<a href="dash.php" style="margin-left: 10px;"><i class="fa fa-arrow-left" aria-hidden="true"></i></i> Dashboard</a>
			<h3 class="vnd">DV: 
				<?php 
					$sql = "SELECT MAX(id) as idp FROM devolucao";
					$rs = mysqli_query($db, $sql);
					while ($dados = mysqli_fetch_array($rs)) {
						$f = $dados['idp'] + 1;
				?>
					<input type="number" name="" value="<?php echo $f; ?>" size="2" style="padding: 5px 2px;width: 30%;text-align: right;" max="<?php echo $f; ?>" min="1">
				<?php } ?>
			</h3>
		</div>
		<style type="text/css">
			.form{
				width: 100%;
				display: flex;
				flex-direction: column;
				align-items: center;
			}
			.form .gr{
				width: 90%;
				margin: 5px auto;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
			}
			.form .gr .fa{
				width: 30%;
			}
			.form .gr .fa p{
				color: #fff;
				margin-bottom: 3px;
			}
			.form .gr .fa input{
				width: 90%;
				padding: 5px;
				text-align: center;
				font-size: 14pt;
			}
			.form .gr .ct{
				width: 65%;
			}
			.form .gr .ct p{
				color: #fff;
			}
			.form .gr .ct input{
				width: 95%;
				padding: 5px;
				text-align: center;
				font-size: 14pt;
			}
			.fm p{
				color: #fff;
			}
			button{
				padding: 10px 10px;
				background: #eee;
				color: #333;
				width: 100%;
				border: none;
				cursor: pointer;
			}
		</style>
		<div class="conteudoDireita">
			<div class="form">
				<div class="gr">
					<div class="fa">
						<p>Pedido </p>
						<input type="number" id="fa" readonly="">
					</div>
					<div class="ct">
						<p>Cliente</p>
						<input type="text" id="clt" readonly="">
					</div>
				</div>
			</div>
			<form>
					<input type="text" id="motivo" placeholder="Informe o motivo de emissao da nota de credito">
			</form>
			<form>
				<button type="button" id="proc_artigos">Processar artigos</button>
			</form>
			<form class="fm">
					<input type="hidden" id="valor_tot">
			</form>
			<div class="tbl">
					<table>
				<thead>
					<th>#</th>
					<th>Descricao</th>
					<th>Qtd</th>
					<th>Preco</th>
					<th>Total</th>
					<th style="color: #EB7D08;"><a><i class="fa fa-window-close" aria-hidden="true"></i></a></th>
				</thead>
				<tbody class="tbody">
					
						
				</tbody>
			</table>
			</div>
		</div>
		
		<div class="rodapeDireita">
			<div class="div1">
				<div class="left">
						<h2 style="color: #fff;">
							Total: <span id="totls" ></span> Mt
						</h2>
				</div>
			</div>
			<div class="div2">
					<a href="?apagar=true"><i class="fa fa-lock" aria-hidden="true"></i> Cancelar</a>
					<a class="processar"><i class="fa fa-lock" aria-hidden="true"></i> Processar</a>
			</div>
		</div>
	</div>
</div>
	<script type="text/javascript" src="../js/data-table-act.js"></script>
	<script type="text/javascript" src="../js/jquery.dataTables.min.js"></script>
	<script type="text/javascript" src="../js/funcoes.js"></script>
	<script type="text/javascript" src="../js/configuracoes.js"></script>
	<script type="text/javascript">
		$(function() {
			$('.cliente').select2();
			$('.id_categoria').select2();
			$('.pedidos').select2();
			$('.clientes').select2();
			$('.usuario').select2();
    	
		function contentEditable() {
				$("table tbody tr td.editavel").on('dblclick', function() {
				if ($('td > input').length > 0) {
					return;
				}
				var conteudoOriginal = $(this).text();
				var novoElemento = $('<input/>', {type: 'text', value:conteudoOriginal});
				$(this).html(novoElemento.bind('blur keydown', function(e) {
					var keyCode = e.which;
					var conteudoNovo = $(this).val();
					if (keyCode == 13 && conteudoNovo != "" && conteudoNovo != conteudoOriginal) {
						var objecto = $(this);
						$.ajax({
							url: 'atualizarQtdNC.php',
							type: 'POST',
							data: {
								id: $(this).parents('tr').children().first().text(),
								nome: $(this).parents('tr').children().eq(1).text(),
								valor: conteudoNovo 
							},
							success: function(data) {
								objecto.parent().html(conteudoNovo);
								location.reload();
							}
						});
					}
					if (e.type == "blur" || keyCode == 27) {
						$(this).parent().html(conteudoOriginal);
						
					}

				}));
				$(this).children().select();
			});
		}


			setInterval(lg, 1000);

			function lg(){
				$.ajax({
						url: 'relogio.php',
						type: 'GET',
						success: function(data) {
							$(".relogio").html(data);
						},
						error: function() {
							$(".relogio").html("Ocorreu um erro ao popular a hora!");
						}
				});
			}
			$("#examples tbody tr").on('click', function() {
				var id_f = $(this).children().first().text();
				$("#fa").val(id_f);
				$.ajax({
					url: 'popClienteDev.php',
					type: 'GET',
					data: {
						id: id_f
					},
					success: function(data) {
						$("#clt").val(data);
					}
				});
			});
			

			$("#proc_artigos").on('click', function() {
				$.ajax({
					url: 'addTempArtDV.php',
					type: 'GET',
					data: {
						dv: $("#fa").val()
					},
					success: function(data) {
						popularArtigosTemp();
						totls();
					}
				});
			});
			
			popularArtigosTemp();
			function popularArtigosTemp() {
				$.ajax({
					url: 'popularArtigosTempDV.php',
					type: 'GET',
					success: function(data) {
						$(".tbody").html(data);
						del();
						// contentEditable();
					}
				});
			}
			function del() {
				$("td.del").on('click', function() {
					var idd = $(this).parents('tr').children().first().text();
					$.ajax({
						url: 'delTempDV.php',
						type: 'GET',
						data: {
							id: idd
						},
						success: function(data) {
							popularArtigosTemp();
							totls();
						}
					});
				});
			}
			totls();
			function totls() {
				$.ajax({
					url: 'totlsDV.php',
					type: 'GET',
					success: function(data) {
						$("#totls").html(data);
						$("#valor_tot").val(data);
					}
				});
			}

			$(".processar").on('click', function() {
				$.ajax({
					url: 'criarDevolucao.php',
					type: 'POST',
					data: {
						cliente: $("#clt").val(),
						fact: $("#fa").val(),
						val : $("#valor_tot").val(),
						motivo: $("#motivo").val()
					},
					success: function(data) {
						// window.location.href="fa.php?id_fatura="+data+"";
						window.open("dv.php?id_dv="+data+"");
						location.reload();
					}
				});
			});

			$('#examples').DataTable();
		});
	</script>
</body>
</html>