<?php
//START CAUTA document
if($_GET['pag'] == 'cauta'){
	echo'<form method="post" action=""  enctype="multipart/form-data" autocomplete="off">
	<table width="99%">	
		<tr>
			<td id="td">
				<input id="inputmare1" name="cauta_cui" type="text" value="" placeholder="CUI" title="CUI">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt1.'</span>
				</div>
			</td>
			<td id="td">
				<input id="inputmare1" name="cauta_nume_doc" type="text" value="" placeholder="Nume document" title="Nume document">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt1.'</span>
				</div>
			</td>
			<td id="td">
				<select name="cauta_tip_doc" id="select2001">
					<option '.$selected1c.' value="">Alege tip document</option>
					<option '.$selected2c.' value="Intrare">Intrare</option>
					<option '.$selected3c.' value="Iesire">Iesire</option>
				</select>
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt1.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td">
				<input id="inputmare1" name="cauta_nr_inreg" type="number" min="0" value="" placeholder="Nr inregistrare" title="Nr inregistrare">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt1.'</span>
				</div>
			</td>
			<td id="td">
				<input id="inputmare1" name="cauta_data_inreg" type="date" value="" placeholder="Data inregistrare" title="Data inregistrare">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt1.'</span>
				</div>
			</td>
			<td id="td">
				<input id="inputmare1" name="cauta_emitent" type="text" value="" placeholder="Emis de ..." title="Emis de ...">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt1.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td" colspan="3">
				<input id="butonsubmit" name="cauta_doc" type="submit" value="Cautare" >
			</td>
		</tr>
	</table>
</form>';
	$cautare = $_POST['cautare'];
	$doc = "SELECT * FROM `doc` where cui = '".$_POST['cauta_cui']."' or nume_doc = '".$_POST['cauta_nume_doc']."' or tip_doc = '".$_POST['cauta_tip_doc']."' ";
	//$doc = "SELECT * FROM `doc` ";
	$result_doc = $conn->query($doc);
if ($result_doc->num_rows > 0) {
		echo'<div id="continut">
	<table width="99%">	
		<tr>
			<td id="chenar"><b>Nr<br />Crt</b></td>
			<td id="chenar"><b>Nume firma<br />CUI</b></td>
			<td id="chenar"><b>Nume document<br />Tip document</b></td>
			<td id="chenar"><b>Data inscriere<br />Nr inregistrare</b></td>
			<td id="chenar"><b>Rezumat<br />document</b></td>
			<td id="chenar"><b>Emitent</b></td>
			<td id="chenar"><b>Nr file <br />Nr anexe</b></td>
			<td id="chenar"></td>
		</tr>';
		$nrcrt = '0';
 while($row_doc = $result_doc->fetch_assoc()) {
	echo'<tr>
			<td id="chenar">'.++$nrcrt.'</td>
			<td id="chenar">
				'.$row_doc['nume_firma'].'
					<br />
				'.$row_doc['ro'].''.$row_doc['cui'].'
			</td>
			<td id="chenar">
				'.$row_doc['nume_doc'].'
					<br />
				'.$row_doc['tip_doc'].'	
			</td>
			<td id="chenar">'.$row_doc['data_inreg_doc'].'
					<br />
				'.$row_doc['nr_inreg_doc'].'	
			</td>
			<td id="chenar">'.$row_doc['rezumat'].'</td>
			<td id="chenar">'.$row_doc['emitent'].'</td>
			<td id="chenar">
				'.$row_doc['nr_file'].'
					<br />
				'.$row_doc['nr_anexe'].'
			</td>
			<td id="chenar">
				<a href="'.$row_doc['link_doc'].'" target="_blank" ><b>Descarca</b></a>
			</td>
		</tr>';
	} 
echo'</table>	
		</div>';
	}
	
}
//END CAUTA document
//START ARATA/ADAUGA/MODIFICA document
if($_POST['doc_add']){

	$nume_firma = $_POST['nume_firma'];
	$ro = $_POST['ro'];
	$cui = $_POST['cui'];
	$nume_doc = $_POST['nume_doc'];
	$tip_doc = $_POST['tip_doc'];
	$nr_inreg_doc = $_POST['nr_inreg_doc'];
	$data_inreg_doc = $_POST['data_inreg_doc'];
	$emitent = $_POST['emitent'];
	$nr_doc_emitent = $_POST['nr_doc_emitent'];
	$data_doc_emitent = $_POST['data_doc_emitent'];
	$nr_file = $_POST['nr_file'];
	$nr_anexe = $_POST['nr_anexe'];
	$rezumat = $_POST['rezumat'];
	$compartiment = $_POST['compartiment'];
	$data_expediere = $_POST['data_expediere'];
	$mod_rezolvare = $_POST['mod_rezolvare'];
	$destinatar = $_POST['destinatar'];
	$nr_conexare = $_POST['nr_conexare'];
	$termen = $_POST['termen'];

		$file_size=$_FILES["file"]["size"];
		$file_name=$_FILES["file"]["name"];
		$redenumire = $ymdhis.'_'.$cui.'.pdf';
		$temp_name=$_FILES["file"]["tmp_name"];
		$file_type=$_FILES["file"]["type"];
		$upload='doc/';
		
		if($file_type == 'application/pdf'){
		move_uploaded_file($temp_name,$upload.''.$redenumire);
		echo "Original file name: ".$file_name."<br>";
		echo "Redenumire: ".$redenumire."<br>";
		echo "File name after upload: ".$temp_name."<br>";
		echo "File size: ".$file_size."<br>";
		echo "File type: ".$file_type;

	$link = $upload.''.$redenumire;
	
		$add_doc = ("INSERT INTO `doc`(`data`, `user`, `nume_firma`, `ro`, `cui`, `nume_doc`, `tip_doc`, `nr_inreg_doc`, `data_inreg_doc`, `emitent`, `nr_doc_emitent`, `data_doc_emitent`, `nr_file`, `nr_anexe`, `rezumat`, `compartiment`, `data_expediere`, `mod_rezolvare`, `destinatar`, `nr_inreg_doc_conex`, `termen_pastrare`, `link_doc`) VALUES ('".$ymdhis."', '".$_SESSION['nume_utilizator']."', '".$nume_firma."', '".$ro."', '".$cui."', '".$nume_doc."', '".$tip_doc."', '".$nr_inreg_doc."', '".$data_inreg_doc."', '".$emitent."', '".$nr_doc_emitent."', '".$data_doc_emitent."', '".$nr_file."', '".$nr_anexe."', '".$rezumat."', '".$compartiment."', '".$data_expediere."', '".$mod_rezolvare."', '".$destinatar."', '".$nr_conexare."', '".$termen."', '".$link."')") or die(mysql_error());
	
	if (mysqli_query($conn, $add_doc)) {
		echo "<script>alert('Document adaugat cu SUCCES.')</script>
				<script>location.replace('index.php?pag=doc')</script>";
		
	}else{
  echo "Error: " . $add_doc . "<br>" . mysqli_error($conn);
	}
	}else{
			echo 'Sunt acceptate doar fisiere cu extensia pdf.';
		}
}
if($_GET['pag'] == 'doc'){
	$txt1 ='Introducere Denumire Firma emitenta document';
	$txt2 ='Introducere Cod Unic de Identificare, cu sau fara RO, firma emitenta document';
	$txt2a ='Introducere nume document';
	$txt3 ='Alege tip document - Intrare / Iesire';
	$txt4 ='Introducere numarul de inregistrare atribuit acestui document';
	$txt5 ='Introducere data calendaristica a inregistrarii acestui document';
	$txt6 ='Introducere nume emitent document';
	$txt7 ='Introducere numar document de la emitent';
	$txt8 ='Introducere data calendaristica document de la emitent';
	$txt9 ='Introducere numar file document';
	$txt10 ='Introducere numar anexe continute de document';
	$txt11 ='Introducere scurt rezumat al continutului documentului';
	$txt12 ='Introducere nume compartiment la care a fost repartizat documentul';
	$txt13 ='Introducere data calendaristica a expedierii documentului';
	$txt14 ='Introducere modul in care s-a rezolvat documentul (rezolutie, solutie, ...)';
	$txt15 ='Introducere destinatar document';
	$txt16 ='Introducere numar de inregistrare al documentului la care se conexeaza, daca nu este conexat, lasa necompletat';
	$txt17 ='Introducere termen de pastrare document';
	$txt18 ='Alege si incarca documentul. (sunt acceptate doar documente cu extesia .pdf)';

	echo '
	<form method="post" action=""  enctype="multipart/form-data" autocomplete="off">
	<table width="99%">	
		<tr>
			<td id="td"><b>Firma</b></td>
			<td id="td">
				<input id="inputmare" name="nume_firma" type="text" required value="" placeholder="Denumire firma" title="'.$txt1.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt1.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>CUI</b></td>
			<td id="td">
				<select name="ro" id="select80" >
					<option '.$selected1.' value="RO">RO</option>
					<option '.$selected2.' value="" >-</option>
				</select>
				<input id="inputmic" name="cui" type="number" min="0" required value="" placeholder="CUI FIRMA (cifre)" title="'.$txt2.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt2.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Nume Document</b></td>
			<td id="td">
				<input id="inputmare" name="nume_doc" type="text" required value="" placeholder="Nume document." title="'.$txt2a.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt2a.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Tip Document</b></td>
			<td id="td">
				<select name="tip_doc" id="select200" required>
					<option '.$selected1c.' value="">Alege tip document</option>
					<option '.$selected2c.' value="Intrare">Intrare</option>
					<option '.$selected3c.' value="Iesire">Iesire</option>
				</select>
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt3.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Nr inregistrare document</b></td>
			<td id="td">
				<input id="inputmare" name="nr_inreg_doc" type="number" min="0" required value="" placeholder="Numai cifre." title="'.$txt4.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt4.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Data inregistrare document</b></td>
			<td id="td">
				<input id="inputmare" name="data_inreg_doc" type="date" required value="" placeholder="Numai cifre." title="'.$txt5.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt5.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Emitent</b></td>
			<td id="td">
				<input id="inputmare" name="emitent" type="text" required value="" placeholder="Nume emitent document." title="'.$txt6.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt6.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Nr document de la emitent</b></td>
			<td id="td">
				<input id="inputmare" name="nr_doc_emitent" type="number" min="0" value="" placeholder="Numar document de la emitent." title="'.$txt7.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt7.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Data document de la emitent</b></td>
			<td id="td">
				<input id="inputmare" name="data_doc_emitent" type="date" required value="" placeholder="Data document emitent." title="'.$txt8.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt8.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Nr file</b></td>
			<td id="td">
				<input id="inputmare" name="nr_file" type="number" min="1" required value="" placeholder="Nr file document." title="'.$txt9.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt9.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Nr anexe</b></td>
			<td id="td">
				<input id="inputmare" name="nr_anexe" type="number" min="0" value="" placeholder="Nr anexe document." title="'.$txt10.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt10.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Rezumat continut</b></td>
			<td id="td">
				<input id="inputmare" name="rezumat" type="text" required value="" placeholder="Rezumat continut document." title="'.$txt11.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt11.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Repartizat la compartimentul</b></td>
			<td id="td">
				<input id="inputmare" name="compartiment" type="text" value="" placeholder="Compartimentul la care a fost repartizat documentul." title="'.$txt12.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt12.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Data expedierii</b></td>
			<td id="td">
				<input id="inputmare" name="data_expediere" type="date" required value="" placeholder="Data expediere document." title="'.$txt13.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt13.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Modul rezolvarii</b></td>
			<td id="td">
				<input id="inputmare" name="mod_rezolvare" type="text" required value="" placeholder="Mod rezolvare document." title="'.$txt14.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt14.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Destinatar</b></td>
			<td id="td">
				<input id="inputmare" name="destinatar" type="text" value="" placeholder="Nume destinatar document." title="'.$txt15.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt15.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Nr inregistrare al documentului<br />la care se conexeaza</b></td>
			<td id="td">
				<input id="inputmare" name="nr_conexare" min="0" type="number" value="" placeholder="Numarul de inregistrare al documentului la care se conexeaza." title="'.$txt16.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt16.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Termen pastrare</b></td>
			<td id="td">
				<input id="inputmare" name="termen" type="number" min="0" required value="" placeholder="Termen de pastrare." title="'.$txt17.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt17.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<tr>
			<td id="td"><b>Incarca documentul</b>
					<br />
					(sunt acceptate doar documente cu extesia .pdf)
			</td>
			<td id="td">
				<input id="inputmare" name="file" type="file" accept="application/pdf" required value="" placeholder="Incarca documentul." title="'.$txt18.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt18.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td" colspan="2">
				<input id="butonsubmit" name="doc_add" type="submit" value="Adauga Document" >
			</td>
		</tr>
	</table>
</form>';
}
//END ARATA/ADAUGA/MODIFICA document
//START ARATA/ADAUGA/MODIFICA USER
if($_POST['scrie_user_bd']){
	$username = md5($_POST["username"]);
	$parola = md5($_POST["parola"]);
	
		$add_user = ("INSERT INTO `utilizatori`(`data`, `user`, `parola`, `nume`, `prenume`, `data_autentificare`, `email`, `tel`, `ip`, `cod_validare`, `incercari`, `rol`, `status`) VALUES ('".$ymdhis."', '".$username."', '".$parola."', '".$_POST['nume']."', '".$_POST['prenume']."', '".$ymdhis."', '".$_POST['email']."', '".$_POST['tel']."', '".$_POST['ip']."', '1', '0', '".$_POST['rol']."', '".$_POST['status']."')") or die(mysql_error());
	
	if (mysqli_query($conn, $add_user)) {
		echo "<script>alert('Utilizator adaugat cu SUCCES.')</script>
				<script>location.replace('index.php?pag=user')</script>";
	} else {
		echo "Error: " . $add_user . "<br>" . mysqli_error($conn);
	}		
}
if($_GET['pag'] == 'user_add'){
	$txt1 = 'Numele de utilizator este unic, se pastraza criptat, poate fi schimbat de un utilizator cu drepturi de Colonel si nu poate fi recuperat.<br>Va rugam pastrati-l cu atentie la loc sigur.';
	$txt2 = 'Este recomandat sa folositi o parola cat mai complexa ce contine LITERE MARI, litere mici, cifre si semne de punctuatie.<br>Parola se pastraza criptat, poate fi schimbata de un utilizator cu drepturi de Colonel si nu poate fi recuperata.<br>Va rugam pastrati-o cu atentie la loc sigur.';
	$txt3 = 'Numele si Prenumele utilizatorului se pastraza in clar, poate fi schimbat doar de un utilizator cu drepturi de Colonel si poate fi recuperat.';
	$txt4 = 'Adresa de email se pastraza in clar, poate fi schimbat de un utilizator cu drepturi de Colonel si poate fi recuperata. Pe aceasta se primeste codul de validare necesar la autentificare in aplicatie.';
	$txt5 = 'Numarul de telefon se pastraza in clar, poate fi schimbat de un utilizator cu drepturi de Colonel si poate fi recuperat.';
	$txt6 = 'Atribuire drepturi utilizatorului.
		<br>Locotenent - acces la documente (cauta in arhiva si obtine documente)
		<br>Capitan - acces la documente (adauga documente + functiile Locotenentului)
		<br>Maior - acces la documete si firme (adauga, modifica firme + functiile Capitanului)
		<br>Colonel - acces la documente, firme si utilizatori (adauga, modifica utilizatori + functiile Maiorului)';
	$txt7 = 'Statusul reprezinta starea in care se afla utilizatorul.
	<br>Activ - are drept de utilizare aplicatie
	<br>Inactiv - nu are drept de utilizare aplicatie';
	$txt8 = 'Adresa de IP reprezinta principala cale de acces la aplicatie si poate fi schimbata doar de un utilizator cu drepturi de Colonel.
		<br>
		Aplicatia poate fi accesata de fiecare utilizator inregistrat doar de la adresa de IP asignata utilizatorului respectiv.
		<br>
		Accesarea aplicatiei de la un alt IP decat cel inregistrat pentru Utilizatorul respectiv duce la Blocarea Accesului acestuia, deblocarea poate fi facuta cu actualizarea adresei de IP doar de un Utilizator cu drepturi de Colonel.';
		
	echo'<div id="continut">
	<form method="post" action="index.php?pag=user" autocomplete="off">
	<table width="99%">	
		<tr>
			<td id="td">Username</td>
			<td id="td">
				<input id="inputmare" name="username" type="text" required value="" placeholder="Username unic" title="'.$txt1.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt1.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td">Parola</td>
			<td id="td">
				<input id="inputmare" name="parola" type="text" required value="" placeholder="Parola" title="Schimba Parola">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt2.'</span>
				</div>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td">Nume</b></td>
			<td id="td">
				<input id="inputmare" name="nume" type="text" required value="'.$row_user['nume'].'" placeholder="Nume" title="Nume">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt3.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td">Prenume</td>
			<td id="td">
				<input id="inputmare" name="prenume" type="text" required value="'.$row_user['prenume'].'" placeholder="Prenume" title="Schimba Prenume">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt3.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td">Email</td>
			<td id="td">
				<input id="inputmare" name="email" type="text" required value="'.$row_user['email'].'" placeholder="Email" title="Schimba email">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt4.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td">Telefon</td>
			<td id="td">
				<input id="inputmare" name="tel" type="text" required value="'.$row_user['tel'].'" placeholder="004-1234567890" pattern="[0-9]{3}-[0-9]{10}" title="004-1234567890 / cod tara-nr telefon" title="Schimba Telefon">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt5.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td">Drepturi</td>
			<td id="td">
				<select name="rol" id="select200" >
					<option '.$selected1b.' value="Locotenent">Locotenent</option>
					<option '.$selected2b.' value="Capitan">Capitan</option>
					<option '.$selected3b.' value="Maior">Maior</option>
					<option '.$selected4b.' value="Colonel">Colonel</option>
				</select>
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt6.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td">Status</td>
			<td id="td">
				<select name="status" id="select200" >
					<option '.$selected1c.' value="Activ">Activ</option>
					<option '.$selected2c.' value="Inactiv">Inactiv</option>
				</select>
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt7.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td">IP</td>
			<td id="td">
				<input id="inputmare" name="ip" type="text" required value="'.$row_user['ip'].'" placeholder="123.123.123.123" pattern="[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" title="123.123.123.123 / 1.12.123.12" title="Schimba IP user">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt8.'</span>
				</div>
			</td>
		</tr>
		<TR>
			<TD align="center" colspan="2">
				<input id="butonsubmit" type="submit" name="scrie_user_bd" value="Adauga Utilizator">
			</TD>
		</TR>
	</table>
	</form>
</div>';
}
if($_GET['pag'] == 'user'){
	
	unset($_SESSION['ro']);
	unset($_SESSION['cui']);
	unset($_SESSION['denumire']);
	unset($_SESSION['j']);
	unset($_SESSION['judet']);
	unset($_SESSION['adresa']);
	unset($_SESSION['email']);
	unset($_SESSION['tel']);
	unset($_SESSION['fax']);
	unset($_SESSION['caen']);
  
	$user = "SELECT * FROM `utilizatori`";
	$result_user = $conn->query($user);
if ($result_user->num_rows > 0) {
		echo'<div id="continut">
	<table width="99%">	
		<tr>
			<td id="chenar"><b>Nr<br />Crt</b></td>
			<td id="chenar"><b>Nume<br />Prenume</b></td>
			<td id="chenar"><b>Date<br />contact</b></td>
			<td id="chenar"><b>Data<br />inscriere</b></td>
			<td id="chenar"><b>Ultima<br />autentificare</b></td>
			<td id="chenar"><b>Drepturi</b></td>
			<td id="chenar"><b>Status</b></td>
			<td id="chenar">
				<p id="butonsubmit">
					<a href="index.php?pag=user_add"><b>Adauga<br />Utilizator</b></a>
				</p>
			</td>
		</tr>';
		$nrcrt = '0';
 while($row_user = $result_user->fetch_assoc()) {
	echo'<tr>
			<td id="chenar">'.++$nrcrt.'</td>
			<td id="chenar">'.$row_user['nume'].' '.$row_user['prenume'].'</td>
			<td id="chenar"><a href="mailto:'.$row_user['email'].'">'.$row_user['email'].'</a><br /><a href="tel:'.$row_user['tel'].'">'.$row_user['tel'].'</a></td>
			<td id="chenar">'.substr($row_user["data"],6,2).'-'.substr($row_user["data"],4,2).'-'.substr($row_user["data"],0,4).'<br />'.substr($row_user["data"],8,2).':'.substr($row_user["data"],10,2).':'.substr($row_user["data"],12,2).'</td>
			<td id="chenar">'.substr($row_user["data_autentificare"],6,2).'-'.substr($row_user["data_autentificare"],4,2).'-'.substr($row_user["data_autentificare"],0,4).'<br />'.substr($row_user["data_autentificare"],8,2).':'.substr($row_user["data_autentificare"],10,2).':'.substr($row_user["data_autentificare"],12,2).'</td>
			<td id="chenar">'.$row_user['rol'].'</td>
			<td id="chenar">'.$row_user['status'].'</td>
			<td id="chenar"><a href="index.php?pag=user_edit&id='.$row_user['id'].'"><b>Modifica</b></a></td>
		</tr>';
	} 
echo'</table>	
		</div>';
	}
}
if($_POST['modifica_user']){
	
	$username = md5($_POST["username"]);
	$parola = md5($_POST["parola"]);
	
	$update_user = mysqli_query($conn, "UPDATE `utilizatori` SET `user`='".$username."', `parola`='".$parola."', `nume`='".$_POST["nume"]."', `prenume`='".$_POST["prenume"]."', `email`='".$_POST["email"]."', `tel`='".$_POST["tel"]."', `rol`='".$_POST["rol"]."', `status`='".$_POST['status']."', `ip`='".$_POST['ip']."' WHERE `id`='".$_POST['id_user']."'")or die(mysql_error());
  
	echo "<script>alert('Date modificate cu SUCCES.')</script>
	<script>location.replace('index.php?pag=user')</script>";
}
if($_GET['pag'] == 'user_edit'){
	$txt1 = 'Numele de utilizator este unic, se pastraza criptat, poate fi schimbat de un utilizator cu drepturi de Colonel si nu poate fi recuperat.<br>Va rugam pastrati-l cu atentie la loc sigur.';
	$txt2 = 'Este recomandat sa folositi o parola cat mai complexa ce contine LITERE MARI, litere mici, cifre si semne de punctuatie.<br>Parola se pastraza criptat, poate fi schimbata de un utilizator cu drepturi de Colonel si nu poate fi recuperata.<br>Va rugam pastrati-o cu atentie la loc sigur.';
	$txt3 = 'Numele si Prenumele utilizatorului se pastraza in clar, poate fi schimbat doar de un utilizator cu drepturi de Colonel si poate fi recuperat.';
	$txt4 = 'Adresa de email se pastraza in clar, poate fi schimbat de un utilizator cu drepturi de Colonel si poate fi recuperata. Pe aceasta se primeste codul de validare necesar la autentificare in aplicatie.';
	$txt5 = 'Numarul de telefon se pastraza in clar, poate fi schimbat de un utilizator cu drepturi de Colonel si poate fi recuperat.';
	$txt6 = 'Atribuire drepturi utilizatorului.
		<br>Locotenent - acces la documente (cauta in arhiva si obtine documente)
		<br>Capitan - acces la documente (adauga documente + functiile Locotenentului)
		<br>Maior - acces la documete si firme (adauga, modifica firme + functiile Capitanului)
		<br>Colonel - acces la documente, firme si utilizatori (adauga, modifica utilizatori + functiile Maiorului)';
	$txt7 = 'Statusul reprezinta starea in care se afla utilizatorul.
	<br>Activ - are drept de utilizare aplicatie
	<br>Inactiv - nu are drept de utilizare aplicatie';
	$txt8 = 'Adresa de IP reprezinta principala cale de acces la aplicatie si poate fi schimbata doar de un utilizator cu drepturi de Colonel.
		<br>
		Aplicatia poate fi accesata de fiecare utilizator inregistrat doar de la adresa de IP asignata utilizatorului respectiv.
		<br>
		Accesarea aplicatiei de la un alt IP decat cel inregistrat pentru Utilizatorul respectiv duce la Blocarea Accesului acestuia, deblocarea poate fi facuta cu actualizarea adresei de IP doar de un Utilizator cu drepturi de Colonel.';
$user = "SELECT * FROM `utilizatori` where id = '".$_GET['id']."'";
	$result_user = $conn->query($user);
if ($result_user->num_rows > 0) {
	while($row_user = $result_user->fetch_assoc()) {
		if($row_user['rol'] =='Locotenent'){$selected1b = 'selected';}
		if($row_user['rol'] =='Capitan'){$selected2b = 'selected';}
		if($row_user['rol'] =='Maior'){$selected3b = 'selected';}
		if($row_user['rol'] =='Colonel'){$selected4b = 'selected';}
		
		if($row_user['status'] =='Activ'){$selected1c = 'selected';}
		if($row_user['status'] =='Inactiv'){$selected2c = 'selected';}
		
	echo'<div id="continut">
	<form method="post" action="index.php?pag=user" autocomplete="off">
	<table width="99%">	
		<tr>
			<td id="td"><b>Username</b></td>
			<td id="td">
				<input id="inputmare" name="username" type="text" required value="" placeholder="Username unic" title="'.$txt1.'">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt1.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Parola</b></td>
			<td id="td">
				<input id="inputmare" name="parola" type="text" required value="" placeholder="Parola" title="Schimba Parola">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt2.'</span>
				</div>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Nume</b></td>
			<td id="td">
				<input id="inputmare" name="nume" type="text" required value="'.$row_user['nume'].'" placeholder="Nume" title="Nume">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt3.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Prenume</b></td>
			<td id="td">
				<input id="inputmare" name="prenume" type="text" required value="'.$row_user['prenume'].'" placeholder="Prenume" title="Schimba Prenume">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt3.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Email</b></td>
			<td id="td">
				<input id="inputmare" name="email" type="text" required value="'.$row_user['email'].'" placeholder="Email" title="Schimba email">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt4.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Telefon</b></td>
			<td id="td">
				<input id="inputmare" name="tel" type="text" required value="'.$row_user['tel'].'" placeholder="004-1234567890" pattern="[0-9]{3}-[0-9]{10}" title="004-1234567890 / cod tara-nr telefon" title="Schimba Telefon">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt5.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Drepturi</b></td>
			<td id="td">
				<select name="rol" id="select200" >
					<option '.$selected1b.' value="Locotenent">Locotenent</option>
					<option '.$selected2b.' value="Capitan">Capitan</option>
					<option '.$selected3b.' value="Maior">Maior</option>
					<option '.$selected4b.' value="Colonel">Colonel</option>
				</select>
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt6.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>Status</b></td>
			<td id="td">
				<select name="status" id="select200" >
					<option '.$selected1c.' value="Activ">Activ</option>
					<option '.$selected2c.' value="Inactiv">Inactiv</option>
				</select>
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt7.'</span>
				</div>
			</td>
		</tr>
		<tr>
			<td id="td"><b>IP</b></td>
			<td id="td">
				<input id="inputmare" name="ip" type="text" required value="'.$row_user['ip'].'" placeholder="123.123.123.123" pattern="[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" title="123.123.123.123 / 1.12.123.12" title="Schimba IP user">
				<div class="tooltip">
					<img width="20px" src="img/info.png">
						<span class="tooltiptext">'.$txt8.'</span>
				</div>
			</td>
		</tr>
		<TR>
			<TD align="center" colspan="2">
				<input type="hidden" name="id_user" value="'.$_GET['id'].'">
				<input id="butonsubmit" type="submit" name="modifica_user" value="Salveaza modificari">
			</TD>
		</TR>
	</table>
	</form>
</div>';
		}
	}
}
if($_GET['pag'] == 'user_nou'){
echo'<div id="continut">
		Introduceti datele pentru firma ce are CUI <b>'.$_SESSION['ro'].''.$_SESSION['cui'].'</b>
	<form method="post" action="">
		<input id="inputmare" name="denumire" type="text" required value="'.$_SESSION['denumire'].'" placeholder="Denumire firma" title="Denumire firma">
		<input id="inputmare" name="j" type="text" required value="'.$_SESSION['j'].'" placeholder="J12/1234/1234" pattern="[A-Z]{1}[0-9]{2}/[0-9]{1,6}/[0-9]{4}" title="J00/1234/1234 cu majuscule">
		<input id="inputmare" name="jud" type="text" required value="'.$_SESSION['judet'].'" placeholder="Judet" title="Judet de resedinta">
		<input id="inputmare" name="adresa" type="text" required value="'.$_SESSION['adresa'].'" placeholder="Adresa" title="Adresa de resedinta">
		<input id="inputmare" name="email" type="email" required value="'.$_SESSION['email'].'" placeholder="email@email.com" title="adresa de email cu litere mici">
		<input id="inputmare" name="tel" type="tel" required value="'.$_SESSION['tel'].'" placeholder="004-1234567890" pattern="[0-9]{3}-[0-9]{10}" title="004-1234567890 / cod tara-nr telefon">
		<input id="inputmare" name="fax" type="tel" value="'.$_SESSION['fax'].'" placeholder="004-1234567890" pattern="[0-9]{3}-[0-9]{10}" title="004-1234567890 / cod tara-nr telefon">
		<input id="inputmare" name="caen" type="number" value="'.$_SESSION['caen'].'" placeholder="1234" title="1234 / cod caen principal">
		<input id="butonsubmit" name="add_firma" type="submit" value="Adauga Firma">
	</form>
</div>';	
}
//END ARATA/ADAUGA/MODIFICA USER
//START ADAUGA/MODIFICA FIRMA
if($_POST['add_firma']){
	$_SESSION['denumire'] = $_POST['denumire'];
	$_SESSION['j'] = $_POST['j'];
	$_SESSION['judet'] = $_POST['jud'];
	$_SESSION['adresa'] = $_POST['adresa'];
	$_SESSION['email'] = $_POST['email'];
	$_SESSION['tel'] = $_POST['tel'];
	$_SESSION['fax'] = $_POST['fax'];
	$_SESSION['caen'] = $_POST['caen'];

	$add_firma = ("INSERT INTO `firma`(`data`, `denumire`, `ro`, `cui`, `j`, `judet`, `adresa`, `email`, `tel`, `fax`, `caen`, `user`) VALUES ('".$ymdhis."', '".$_SESSION['denumire']."', '".$_SESSION['ro']."', '".$_SESSION['cui']."', '".$_SESSION['j']."', '".$_SESSION['judet']."', '".$_SESSION['adresa']."', '".$_SESSION['email']."', '".$_SESSION['tel']."', '".$_SESSION['fax']."', '".$_SESSION['caen']."', '".$_SESSION['nume_utilizator']."')") or die(mysql_error());
	
	if (mysqli_query($conn, $add_firma)) {
		echo "<script>alert('Firma adaugata cu SUCCES.')</script>
				<script>location.replace('index.php?index.php?pas=0')</script>";
	} else {
  echo "Error: " . $add_firma . "<br>" . mysqli_error($conn);
  
  unset($_SESSION['ro']);
  unset($_SESSION['cui']);
  unset($_SESSION['denumire']);
  unset($_SESSION['j']);
  unset($_SESSION['judet']);
  unset($_SESSION['adresa']);
  unset($_SESSION['email']);
  unset($_SESSION['tel']);
  unset($_SESSION['fax']);
  unset($_SESSION['caen']);
}
}
if($_POST['modifica_firma']){

	$update_firma = mysqli_query($conn, "UPDATE `firma` SET `data`='".$ymdhis."', `denumire`='".$_POST["denumire"]."', `ro`='".$_POST["ro"]."', `j`='".$_POST["j"]."', `judet`='".$_POST["judet"]."', `adresa`='".$_POST["adresa"]."', `email`='".$_POST["email"]."', `tel`='".$_POST["tel"]."', `fax`='".$_POST["fax"]."', `caen`='".$_POST["caen"]."', `user`='".$_SESSION['nume_utilizator']."' WHERE `cui`='".$_SESSION['cui']."'")or die(mysql_error());
	
	unset($_SESSION['ro']);
	unset($_SESSION['cui']);
	unset($_SESSION['denumire']);
	unset($_SESSION['j']);
	unset($_SESSION['judet']);
	unset($_SESSION['adresa']);
	unset($_SESSION['email']);
	unset($_SESSION['tel']);
	unset($_SESSION['fax']);
	unset($_SESSION['caen']);
  
	echo "<script>alert('Date modificate cu SUCCES.')</script>
	<script>location.replace('index.php?index.php?pas=0')</script>";
}
if($_POST['submit_cui']){
	$_SESSION['ro'] = $_POST['ro'];
	$_SESSION['cui'] = $_POST['cui'];
	// if($_POST['ro'])
	// {$selected1a = 'selected'; $selected2a ='';}
		// else
	// {$selected1a = ''; $selected2a = 'selected';}
	$firma = "SELECT * FROM `firma` where cui ='".$_SESSION['cui']."'";
	$result = $conn->query($firma);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
		if($row["ro"] == 'RO')
			{$selected1a = 'selected'; $selected2a ='';}
				else
			{$selected1a = ''; $selected2a = 'selected';}		
		echo'<div id="continut">
		Firma ce are CUI <b>'.$_SESSION['ro'].''.$_SESSION['cui'].'</b> a fost adaugata la data: '.substr($row["data"],6,2).'-'.substr($row["data"],4,2).'-'.substr($row["data"],0,4).' ora: '.substr($row["data"],8,2).':'.substr($row["data"],10,2).':'.substr($row["data"],12,2).'<br /> de '.$row["user"].'<br />si are urmatoarele date de indentificare:</b>
	<form method="post" action="">
		<select name="ro" id="select80" >
			<option '.$selected1a.' value="RO">RO</option>
			<option '.$selected2a.' value="" >-</option>
		</select>
		<input id="inputmare" name="denumire" type="text" required value="'.$row["denumire"].'" placeholder="Denumire firma" title="Denumire firma">
		<input id="inputmare" name="j" type="text" required value="'.$row['j'].'" placeholder="J12/1234/1234" pattern="[A-Z]{1}[0-9]{2}/[0-9]{1,6}/[0-9]{4}" title="J00/1234/1234 cu majuscule">
		<input id="inputmare" name="judet" type="text" required value="'.$row['judet'].'" placeholder="Judet" title="Judet de resedinta">
		<input id="inputmare" name="adresa" type="text" required value="'.$row['adresa'].'" placeholder="Adresa" title="Adresa de resedinta">
		<input id="inputmare" name="email" type="email" required value="'.$row['email'].'" placeholder="email@email.com" title="adresa de email cu litere mici">
		<input id="inputmare" name="tel" type="tel" required value="'.$row['tel'].'" placeholder="004-1234567890" pattern="[0-9]{3}-[0-9]{10}" title="004-1234567890 / cod tara-nr telefon">
		<input id="inputmare" name="fax" type="tel" value="'.$row['fax'].'" placeholder="004-1234567890" pattern="[0-9]{3}-[0-9]{10}" title="004-1234567890 / cod tara-nr telefon">
		<input id="inputmare" name="caen" type="number" value="'.$row['caen'].'" placeholder="1234" title="1234 / cod caen principal">
		<input id="butonsubmit" name="modifica_firma" type="submit" value="Salveaza modificari">
	</form>
</div>';
		}
	}else{
		echo'<div id="continut">';
			echo 'Firma cu datele de autentificare <b>'.$_SESSION['ro'].''.$_SESSION['cui'].'</b> nu este inregistrata.<br /> Doriti sa inregistrati ?';
			
		echo'</div>';
		echo'<div id="center">
				<a href="index.php?pag=start"><p id="butonprogresrosu">NU</p></a>
				<a href="index.php?pag=adauga_firma"><p id="butonprogresverde">DA</p></a>
			</div>';
	}
}

if($_POST['ro'])
	{$selected1 = 'selected'; $selected2 ='';}
		else
	{$selected1 = ''; $selected2 = 'selected';}

if($_GET['pag'] == 'firma' and $_POST['submit_cui'] == ''){
	echo'<div id="continut">';
		echo'Introduceti CUI firma';	
			echo'<form method="post" action="">
				<select name="ro" id="select80" >
					<option '.$selected1.' value="RO">RO</option>
					<option '.$selected2.' value="" >-</option>
				</select>
				<input id="inputmic" name="cui" type="number" min="1" required value="'.$_SESSION['cui'].'">';
					echo'<div id="divbutonsubmit">';
				echo'<input id="butonsubmit" name="submit_cui" type="submit" value="Valideaza">';
					echo'</div>';	
			echo'</form>';
	echo'</div>';	
}
if($_GET['pag'] == 'adauga_firma'){
echo'<div id="continut">
		Introduceti datele pentru firma ce are CUI <b>'.$_SESSION['ro'].''.$_SESSION['cui'].'</b>
	<form method="post" action="">
		<input id="inputmare" name="denumire" type="text" required value="'.$_SESSION['denumire'].'" placeholder="Denumire firma" title="Denumire firma">
		<input id="inputmare" name="j" type="text" required value="'.$_SESSION['j'].'" placeholder="J12/1234/1234" pattern="[A-Z]{1}[0-9]{2}/[0-9]{1,6}/[0-9]{4}" title="J00/1234/1234 cu majuscule">
		<input id="inputmare" name="jud" type="text" required value="'.$_SESSION['judet'].'" placeholder="Judet" title="Judet de resedinta">
		<input id="inputmare" name="adresa" type="text" required value="'.$_SESSION['adresa'].'" placeholder="Adresa" title="Adresa de resedinta">
		<input id="inputmare" name="email" type="email" required value="'.$_SESSION['email'].'" placeholder="email@email.com" title="adresa de email cu litere mici">
		<input id="inputmare" name="tel" type="tel" required value="'.$_SESSION['tel'].'" placeholder="004-1234567890" pattern="[0-9]{3}-[0-9]{10}" title="004-1234567890 / cod tara-nr telefon">
		<input id="inputmare" name="fax" type="tel" value="'.$_SESSION['fax'].'" placeholder="004-1234567890" pattern="[0-9]{3}-[0-9]{10}" title="004-1234567890 / cod tara-nr telefon">
		<input id="inputmare" name="caen" type="number" value="'.$_SESSION['caen'].'" placeholder="1234" title="1234 / cod caen principal">
		<input id="butonsubmit" name="add_firma" type="submit" value="Adauga Firma">
	</form>
</div>';	
}
//END ADAUGA/MODIFICA FIRMA
?>