<?php if (Session::get("tipo_usuario") != "6"){ show_403();} ?>
<?php import_file(DIR_TEMPLATES."header.php", ["title" => "Lançamento de Compra Loja Física"]); ?>
<?php import_file(DIR_MODELS."Model.class.php"); ?>

<h4 class="mb-3">
	<i class="fa-regular fa-receipt"></i> Lançamento de Compra Loja Física
</h4>

<div class="content-box">
	<form id="form-lancamento-compra">
		<div class="form-row">
			<div class="col-xl-4 col-lg-4 col-md-6 col-sm-12">
				<div class="form-group">
					<label for="cpf-usuario">CPF do Cliente<span class="text-danger">*</span></label>
					<input type="text" name="cpf_usuario" id="cpf-usuario" class="form-control cpf" autocomplete="off" required>
				</div>
			</div>
			
			<div class="col-xl-4 col-lg-4 col-md-6 col-sm-12">
				<div class="form-group">
					<label for="valor-compra">Valor da Compra<span class="text-danger">*</span></label>
					<div class="input-group">
						<div class="input-group-prepend">
							<div class="input-group-text">
								<i class="fa-regular fa-dollar-sign"></i>
							</div>
						</div>
						<input type="text" name="valor_compra" id="valor-compra" class="form-control valor-monetario" autocomplete="off" required>
					</div>
				</div>
			</div>

			<div class="col-xl-4 col-lg-4 col-md-6 col-sm-12">
				<div class="form-group">
					<label for="id-plano-loja">Plano<span class="text-danger">*</span></label>
					<select name="id_plano_loja" id="id-plano-loja" class="form-control" required>
						<option value="">Selecione um...</option>

						<?php 

							$model = new Model();

							$result = $model->query("
								SELECT * FROM plano_loja a
								INNER JOIN loja b ON (a.id_loja = b.id_loja)
								WHERE
									a.status_plano_loja = 'S' AND 
									b.id_usuario = :id_usuario 
							", [
								":id_usuario" => Session::get("id_usuario")
							]);

							foreach ($result->fetchAll() as $i):
								$id_loja = $i->id_loja;

						?>
							<option value="<?php echo $i->id_plano_loja; ?>"><?php echo $i->nome_plano_loja; ?></option>
						<?php endforeach; ?>
					</select>
				</div>
			</div>
		</div>

		<input type="hidden" name="id_loja" value="<?php echo $id_loja; ?>">

		<button type="submit" class="btn btn-primary">
			<i class="fa-regular fa-save"></i> Salvar
		</button>
	</form>
</div>

<script>
	$("#form-lancamento-compra").submit(function(event){
		event.preventDefault();
		var form = $(this);
		$.ajax({
			type: "POST",
			data: form.serialize(),
			url: "<?php echo show_link("/cadastrar-lancamento-compra/"); ?>",
			async: true
		}).then(function(response){
			var result = $.parseJSON(response);

			if (result.status == true) {
				$.toast({
					heading: "Aviso",
					text: "Cadastro Realizado com Sucesso!",
					icon: "success",
					loader: true
				});

				form.each(function(){
					this.reset();
				});
			}
			else if (result.status == false && result.reason == "invalid_cpf") {
				$("#cpf-usuario").addClass("is-warning");
				$.toast({
					heading: "Aviso",
					text: "CPF Inválido!",
					icon: "warning",
					loader: true
				});
				$("#cpf-usuario").keyup(function(){
					$(this).removeClass("is-warning");
					$(this).off("keyup");
				});
			}
			else if (result.status == false && result.reason == "not_existing_cpf") {
				$("#cpf-usuario").addClass("is-warning");
				$.toast({
					heading: "Aviso",
					text: "CPF não cadastrado no sistema!",
					icon: "warning",
					loader: true
				});
				$("#cpf-usuario").keyup(function(){
					$(this).removeClass("is-warning");
					$(this).off("keyup");
				});
			}
			else{
				$.toast({
					heading: "Aviso",
					text: "Erro ao Realizar o Cadastro!",
					icon: "error",
					loader: true
				});
			}
		}, function(error){
			console.log("Erro: "+error);
		});
	});
</script>

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