<?
include 'c_time.php';
include 'u_globals.php';
include 'u_spoj_na_bazu.php';

if ($projekt_name == 'ugo') {
	$l_tip = get_item('select value from settings where name = "tipprog" ');
	if ($l_tip != 'u') {
		print "Baza podataka i podatak u konfiguracijskoj datoteci se ne podudaraju! Projekt UGO mora biti pokrenut na bazi ugo.";
		exit;
	}
} else {
	if ($projekt_name == 'malo') {
		$l_tip = get_item('select value from settings where name = "tipprog" ');
		if ($l_tip != 'm') {
			print "Baza podataka i podatak u konfiguracijskoj datoteci se ne podudaraju! Projekt MALO mora biti pokrenut na bazi malo.";
			exit;
		}
	} else {
		print "Parametri nisu dobro postavljeni.";
		exit;
	}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
	<title>Izvještaji</title>
	<meta name="description" content="Izvještaji">
	<meta name="keywords" content="Izvještaji">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<meta http-equiv="Content-Language" content="hr">
	<link rel="stylesheet" type="text/css" href="css/css-2.css">
	<link rel="stylesheet" type="text/css" href="../css/jquery/jquery-ui.css">
	<script type="text/javascript" src="../js/jquery/jquery.js"></script>
	<script type="text/javascript" src="../js/jquery/jquery-ui.js"></script>
	<script type="text/javascript">
		var g_outer_report = 1;
		<?
		$g_path_sl = '../';
		include '../../../app/views/reports/f.php';
		?>
	</script>
	<style>
		/* Style for the loading spinner */
		.loader {
			display: none;
			/* Hidden by default */
			position: fixed;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
			border: 16px solid #f3f3f3;
			border-radius: 50%;
			border-top: 16px solid #007bff;
			width: 80px;
			height: 80px;
			animation: spin 2s linear infinite;
			z-index: 9999;
			/* Ensure it appears above other elements */
		}

		@keyframes spin {
			0% {
				transform: rotate(0deg);
			}

			100% {
				transform: rotate(360deg);
			}
		}

		/* Overlay to dim the background */
		.overlay {
			display: none;
			/* Hidden by default */
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background-color: rgba(0, 0, 0, 0.5);
			z-index: 9998;
			/* Below the loader but above everything else */
		}
	</style>
</head>

<body>
	<?
	if ($projekt_name == 'ugo') {
		?>
		<ul class="lista">
			<li><a href="index.php?id=1"><img src="slike/h_dnevni_promet.png" border="0"></a><a href="index.php?id=1"
					class="title">Dnevni promet (#1)</a></li>
			<li><a href="index.php?id=2"><img src="slike/h_pregled_prodaje_po_normativu.png" border="0"></a><a
					href="index.php?id=2" class="title">Pregled prodaje po normativu (#2)</a></li>
			<li><a href="index.php?id=3"><img src="slike/h_pregled_prodaje_po_nacinu_placanja.png" border="0"></a><a
					href="index.php?id=3" class="title">Pregled prodaje po načinu plaćanja (#3)</a></li>
			<li><a href="index.php?id=4"><img src="slike/h_rekapitulacija_prometa.png" border="0"></a><a
					href="index.php?id=4" class="title">Rekapitulacija prometa (#4)</a></li>
			<li><a href="index.php?id=5"><img src="slike/h_pregled_prodaje_po_artiklu.png" border="0"></a><a
					href="index.php?id=5" class="title">Pregled prodaje po artiklu (#5)</a></li>
			<li><a href="index.php?id=6"><img src="slike/h_porez_na_potrosnju.png" border="0"></a><a href="index.php?id=6"
					class="title">Porez na potrošnju (#6)</a></li>
			<li><a href="index.php?id=7"><img src="slike/h_stanje_skladista.png" border="0"></a><a href="index.php?id=7"
					class="title">Stanje skladišta (#7)</a></li>
			<li><a href="index.php?id=13"><img src="slike/h_kartica_artikla.png" border="0"></a><a href="index.php?id=13"
					class="title">Kartica artikla (#8)</a></li>
			<li><a href="index.php?id=24"><img src="slike/h_stanje_skladista.png" border="0"></a><a href="index.php?id=24"
					class="title">Stanje skladišta iz artikala(#12)</a></li>
		</ul>
	<?
	}
	?>
	<?
	if ($projekt_name == 'malo') {
		?>
		<ul class="lista">
			<li><a href="index.php?id=8"><img src="slike/h_dnevni_promet.png" border="0"></a><a href="index.php?id=8"
					class="title">Dnevni promet (#1)</a></li>
			<li><a href="index.php?id=9"><img src="slike/h_pregled_prodaje_po_artiklu.png" border="0"></a><a
					href="index.php?id=9" class="title">Prodani artikli (#2)</a></li>
			<li><a href="index.php?id=10"><img src="slike/h_stanje_skladista.png" border="0"></a><a href="index.php?id=10"
					class="title">Stanje skladišta (#3)</a></li>
			<li><a href="index.php?id=11"><img src="slike/h_rekapitulacija_prometa.png" border="0"></a><a
					href="index.php?id=11" class="title">Rekapitulacija prometa (#4)</a></li>
			<li><a href="index.php?id=12"><img src="slike/h_kartica_artikla.png" border="0"></a><a href="index.php?id=12"
					class="title">Kartica artikla (#5)</a></li>
			<li><a href="index.php?id=23"><img src="slike/h_pregled_nabave_materijalno.png" border="0"></a><a
					href="index.php?id=23" class="title">Pregled nabave - materijalno (#11)</a></li>
			<li><a href="index.php?id=25"><img src="slike/h_stanje_skladista.png" border="0"></a><a href="index.php?id=25"
					class="title">Stanje skladišta iz artikala(#12)</a></li>
		</ul>
	<?
	}

	if ($projekt_name == 'ugo' || $projekt_name == 'malo') {
		if (isset($_GET['id']) && $_GET['id'] && isset($l_tip)) {
			$l_naslov = get_item('select name from reports where id = ' . $_GET['id'] . ' and app_type = "' . $l_tip . '" ');

			if ($l_naslov) {
				?>
				<h1><?= $l_naslov ?></h1>
				<?
				switch ($_GET['id']) {
					case 1:
					case 8:
						$l_js_str = '';
						$l_hidden = '';
						if ($projekt_name == 'ugo') {
							$l_js_str = '<script type="text/javascript">$(document).ready(function(){setTimeout(function(){get_users(); }, 200);});</script>';
						}
						if ($projekt_name == 'malo') {
							$l_hidden = ' style="display:none; "';
						}
						$l_forma = 'Odaberite datum<br>' .
							'<input type="text" name="data[Filter][datum]" id="FilterDatum" value="" class="datepicker">' .
							'<br>' .
							'<br>' .
							'Vrsta izvješća (X/Z)<br>' .
							'<select name="data[Filter][vrsta_izvjesca]" id="FilterVrstaIzvjesca">' .
							'<option value="Z">Z</option>' .
							'<option value="X">X</option>' .
							'</select>' .
							'<br>' .
							'<br>' .
							'<span' . $l_hidden . '>' .
							'Korisnik<br>' .
							'<select name="data[Filter][user]" id="FilterUser">' .
							'<option value="null">---</option>' .
							'' .
							'</select>' .
							'</span>' .
							$l_js_str .
							'';
						break;


					case 5:
						if ($projekt_name == 'ugo') {
							$l_js_str = '<script type="text/javascript">$(document).ready(function(){setTimeout(function(){get_suppliers(); }, 300);});</script>';
						}
						$l_forma = 'Od<br>' .
							'<input type="text" name="data[Filter][date_start]" id="FilterDateStart" value="" class="datepicker">' .
							'<br>' .
							'<br>' .
							'Do<br>' .
							'<input type="text" name="data[Filter][date_end]" id="FilterDateEnd" value="" class="datepicker">' .
							'<br>' .
							'<br>' .
							'Grupa prikaza<br>' .
							'<select name="data[Filter][grupa_prikaza]" id="FilterGrupaPrikaza">' .
							'<option value="DA">DA</option>' .
							'<option value="NE">NE</option>' .
							'</select>' .
							'<br>' .
							'<br>' .
							'Dobavljač<br>' .
							'<select name="data[Filter][supplier]" id="FilterSupplier">' .
							'' .
							'</select>' .
							$l_js_str;
						break;

					case 2:
					case 3:
					case 4:
					case 6:
					case 19:
					case 20:
					case 23:
						$l_forma = 'Od<br>' .
							'<input type="text" name="data[Filter][date_start]" id="FilterDateStart" value="" class="datepicker">' .
							'<br>' .
							'<br>' .
							'Do<br>' .
							'<input type="text" name="data[Filter][date_end]" id="FilterDateEnd" value="" class="datepicker">';
						if ($_GET['id'] == 2 || $_GET['id'] == 3) {
							$l_js_str = '';
							if ($projekt_name == 'ugo') {
								$l_js_str = '<script type="text/javascript">$(document).ready(function(){setTimeout(function(){get_users(); }, 200);});</script>';
							}
							$l_forma .= '' .
								'<br>' .
								'<br>' .
								'Korisnik<br>' .
								'<select name="data[Filter][user]" id="FilterUser"' . $l_hidden . '>' .
								'<option value="null">---</option>' .
								'' .
								'</select>' .
								$l_js_str .
								'';
						}
						break;

					case 9:
						$l_js_str = '';
						if ($projekt_name == 'malo') {
							$l_js_str = '<script type="text/javascript">$(document).ready(function(){setTimeout(function(){get_groups("m", 1); }, 200); setTimeout(function(){get_suppliers(); }, 300);});</script>';
						}
						$l_forma = 'Od<br>' .
							'<input type="text" name="data[Filter][date_start]" id="FilterDateStart" value="" class="datepicker">' .
							'<br>' .
							'<br>' .
							'Do<br>' .
							'<input type="text" name="data[Filter][date_end]" id="FilterDateEnd" value="" class="datepicker"><br><br>' .
							'Vrsta<br>' .
							'<select name="data[Filter][vrsta]" style="width:150px; " id="FilterVrsta">
								<option value="">-</option>
								<option value="1" selected="selected">Svi</option>
								<option value="2">Roba</option>
								<option value="3">Usluge</option>
								</select>' .
							'<br>' .
							'<br>' .
							'Grupa<br>' .
							'<select name="data[Filter][group]" id="FilterGroup">' .
							'' .
							'</select>' .
							'<br>' .
							'<br>' .
							'Dobavljač<br>' .
							'<select name="data[Filter][supplier]" id="FilterSupplier">' .
							'' .
							'</select>' .
							$l_js_str .
							'';
						break;

					case 11:
						$l_forma = 'Od<br>' .
							'<input type="text" name="data[Filter][date_start]" id="FilterDateStart" value="" class="datepicker">' .
							'<br>' .
							'<br>' .
							'Do<br>' .
							'<input type="text" name="data[Filter][date_end]" id="FilterDateEnd" value="" class="datepicker">';
						break;

					case 7:
					case 10:
						$l_js_str = '';
						if ($projekt_name == 'ugo') {
							$l_js_str = '<script type="text/javascript">$(document).ready(function(){setTimeout(function(){get_groups("u", 0); }, 200);});</script>';
						}
						if ($projekt_name == 'malo') {
							$l_js_str = '<script type="text/javascript">$(document).ready(function(){setTimeout(function(){get_groups("m", 0); }, 200);});</script>';
						}
						$l_forma = 'Datum<br>' .
							'<input type="text" name="data[Filter][date]" id="FilterDate" value="" class="datepicker">' .
							'<br>' .
							'<br>' .
							'Vrijeme<br>' .
							'<input type="text" name="data[Filter][time]" id="FilterTime" value="' . date('H:i') . '">' .
							'<br>' .
							'<br>' .
							'Grupa<br>' .
							'<select name="data[Filter][group]" id="FilterGroup">' .
							'' .
							'</select>' .
							$l_js_str .
							'';
						break;

					case 12:
					case 13:
						$y = get_data('select id as rbr, name as ime from articles order by name ', 1);
						$l_html = '<option value="0">--</option>' . "\n";
						for ($i = 0; $i < sizeof($y['rbr']); $i++) {
							$l_html .= '<option value="' . $y['rbr'][$i] . '">' . $y['ime'][$i] . '</option>' . "\n";
						}

						$l_forma = 'Odaberi artikl<br>' .
							'<select name="data[Filter][articles_id]" id="FilterArticlesId">' .
							$l_html .
							'</select>';
						break;

					default:
						$l_forma = '';
				}
				?>
				<form name="forma" method="post" onsubmit="return false;" class="forma">
					<?= $l_forma ?>
					<br><br>
					<input type="submit" value="Izradi izvješće" class="sbm"
						onclick="submit_report(<?= $_GET['id'] ?>); return false;">
				</form>
				<span id="report-message"></span>
				<span id="report-after"></span>
			<?
			}
		}
	}
	?>

	<?php
	// Pročitajte 'id' iz URL-a
	$report_id = isset($_GET['id']) ? (int) $_GET['id'] : 0;

	// Prikaži listu meseci samo za odabrane id vrednosti
	$validReportIds = [4, 6, 10, 13];

	if (in_array($report_id, $validReportIds)) {
		// Kreiranje niza imena meseci
		$month_names = [
			"Siječanj",
			"Veljača",
			"Ožujak",
			"Travanj",
			"Svibanj",
			"Lipanj",
			"Srpanj",
			"Kolovoz",
			"Rujan",
			"Listopad",
			"Studeni",
			"Prosinac"
		];

		// Prikaz zaglavlja i liste meseci
		echo '<h2>Izvještaji po Mjesecima</h2>';
		echo '<ul class="lista">';
		for ($month = 1; $month <= 12; $month++) {
			// Izračunaj početni i krajnji datum za svaki mesec
			$year = 2024;
			$start_date = sprintf('01-%02d-%d', $month, $year); // 01-mesec-godina
			$end_date = date("t-m-Y", strtotime("$year-$month-01")); // poslednji dan meseca
	
			// Prikazivanje linkova za svaki mesec
			echo '<li>';
			echo '<a href="#" onclick="generateAndSendReport(' . $report_id . ', \'' . $start_date . '\', \'' . $end_date . '\')">';
			echo '<img src="slike/month_' . $month . '_report_graphic_button.png" border="0">';
			echo '</a>';
			echo '<a href="#" onclick="generateAndSendReport(' . $report_id . ', \'' . $start_date . '\', \'' . $end_date . '\')" class="title">';
			echo 'Rekapitulacija Report: ' . $month_names[$month - 1] . ' ' . $year . ' (#' . $report_id . ')';
			echo '</a>';
			echo '</li>';
		}
		echo '</ul>';
	}
	?>

	<script type="text/javascript">
		$(document).ready(function () {
			$('.datepicker').datepicker({
				dateFormat: 'dd-mm-yy',
			});
			$('.datepicker').datepicker("setDate", "Now");
		});
	</script>

	<script type="text/javascript">
		// Funkcija za generisanje izveštaja i automatsko slanje e-maila
		function generateAndSendReport(reportId, startDate, endDate) {
			// Prikazivanje učitavanja
			showLoader();

			// Popunite formu sa datumima, ako je potrebno
			$('#FilterDateStart').val(startDate);
			$('#FilterDateEnd').val(endDate);

			// Automatski pokrenite izveštaj
			submit_report(reportId);

			// Praćenje statusa generisanja izveštaja
			monitorReportStatus(reportId, startDate, endDate);
		}

		function showLoader() {
			$('.overlay').show();
			$('.loader').show();
		}

		function hideLoader() {
			$('.overlay').hide();
			$('.loader').hide();
		}

		function monitorReportStatus(reportId, startDate, endDate) {
			const interval = setInterval(function () {
				if ($('#report-message').text().includes('Izvještaj je kreiran')) {
					clearInterval(interval);
					email_box_open(1); // Otvori dijalog za e-mail

					// Automatski pošalji e-mail
					sendEmailAutomatically(reportId, startDate, endDate);
				}
			}, 500);
		}

		function sendEmailAutomatically(reportId, startDate, endDate) {
			showLoader();

			const reportType = 'rekapitulacija_prometa';
			const extraParam = '';
			const submitType = g_submit_type; // Pretpostavka da postoji globalna varijabla za tip podnošenja

			// Poziv mailer_send_email funkcije
			mailer_send_email(reportType, reportId, startDate, endDate, extraParam, submitType);

			// Praćenje statusa slanja e-maila
			monitorEmailSuccess();
		}

		function monitorEmailSuccess() {
			const emailInterval = setInterval(function () {
				if ($('body').text().includes('E-mail je poslan!')) {
					clearInterval(emailInterval);
					hideLoader();

					// Redirekcija na indeksnu stranicu izveštaja nakon uspešnog slanja e-maila
					window.location.href = '/ugo/app/webroot/automatika';
				}
			}, 500);
		}

		$(document).ready(function () {
			$('.datepicker').datepicker({
				dateFormat: 'dd-mm-yy',
			});
			$('.datepicker').datepicker("setDate", "Now");
		});
	</script>





	<div class="overlay"></div>
	<div class="loader"></div>



</body>

</html>