<?php 

	if (Session::get("tipo_usuario") != "4"){ show_403();}
	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		import_file(DIR_MODELS."Model.class.php");
		import_file(DIR_LIBRARIES."functions.php");

		$id = encrypt_decode(filter_input(INPUT_POST, "id_saque", FILTER_SANITIZE_STRING));
		$status = filter_input(INPUT_POST, "status_saque", FILTER_SANITIZE_STRING);

		$model = new Model();

		if (!empty($id) and !empty($status)) {
			$saque = $model->query("SELECT * FROM saque WHERE id_saque = :id", [":id" => $id])->fetch();

			if ($status == "S") {
				$config = obter_configuracao();

				if (!empty($saque->valor_taxa_saque)) {
					$result = $model->insert("entrada", [
						"id_usuario" => 1,
						"valor_entrada" => $saque->valor_taxa_saque,
						"descricao_entrada" => "Pagamento de Taxa de Saque do Usuário de ID: ".$saque->id_usuario,
						"datahora_entrada" => date("Y-m-d H:i:s"),
						"status_entrada" => "S"
					]);

					if ($result == 0) {
						$model->rollback();
						echo "<script>window.location.href='".show_link("/solicitacoes-saque/?status=false")."';</script>";
					}

					$last_query = $model->lastQuery();
					$model->insert("historico", [
						"comando_sql" => $last_query,
						"datahora_acao" => date("Y-m-d H:i:s"),
						"id_usuario" => Session::get("id_usuario")
					]);
				}

				$result = $model->update("saida", [
					"descricao_saida" => "Saque Realizado",
					"datahora_saida" => date("Y-m-d H:i:s"),
					"status_saida" => "S"
				], [
					"id_saida" => $saque->id_saida
				]);

				if ($result > 0) {
					$last_query = $model->lastQuery();
					$model->insert("historico", [
						"comando_sql" => $last_query,
						"datahora_acao" => date("Y-m-d H:i:s"),
						"id_usuario" => Session::get("id_usuario")
					]);
				}
				else{
					$model->rollback();
					echo "<script>window.location.href='".show_link("/solicitacoes-saque/?status=false")."';</script>";
					exit();
				}
			}
			else{
				$result = $model->update("saida", [
					"status_saida" => "N"
				], [
					"id_saida" => $saque->id_saida
				]);

				if ($result > 0) {
					$last_query = $model->lastQuery();
					$model->insert("historico", [
						"comando_sql" => $last_query,
						"datahora_acao" => date("Y-m-d H:i:s"),
						"id_usuario" => Session::get("id_usuario")
					]);
				}
				else{
					$model->rollback();
					echo "<script>window.location.href='".show_link("/solicitacoes-saque/?status=false")."';</script>";
					exit();
				}
			}

			$datahora_saque = ($status == "S") ? date("Y-m-d H:i:s") : null;

			// Muda o status da saque
			$result = $model->update("saque", [
				"status_saque" => $status,
				"datahora_saque" => $datahora_saque
			],[
				"id_saque" => $id
			]);

			if ($result > 0) {	
				// Gera o histórico da avaliação da saque
				$last_query = $model->lastQuery();
				$model->insert("historico", [
					"comando_sql" => $last_query,
					"datahora_acao" => date("Y-m-d H:i:s"),
					"id_usuario" => Session::get("id_usuario")
				]);

				$model->commit();
				echo "<script>window.location.href='".show_link("/solicitacoes-saque/?status=true")."';</script>";
			}
			else{
				$model->rollback();
				echo "<script>window.location.href='".show_link("/solicitacoes-saque/?status=false")."';</script>";
			}
		}
		else{
			echo "<script>window.location.href='".show_link("/solicitacoes-saque/?status=false")."';</script>";
			exit();
		}
	}
	else{
		show_403();
	}

?>