<?php
session_start();
include('../function/function.php');
//*****************************************************************************
//[Modul name]: function.php
//[Purpose]   : Quan ly all function co lien quan toi PHP
//[Date]      : 31.10.05
//[Creator]   : SANG HR - 0946384832
//******************************************************************************
//==============================================================================================
//********************************************************
//[Creator]   : SANGHR
//********************************************************
include('../include/connect.php'); // get quận huyện
if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && $_GET['Type'] && $_GET['Type'] == '1') {
	$TinhTT = $_GET['Tinh_TP'];
	$sql_check = mysqli_query($link, "select DISTINCT Quan_Huyen from address where Tinh_TP='$TinhTT' ORDER BY Tinh_TP ASC");
	$quanhuyenArr = array();
	while ($bienquanhuyen = mysqli_fetch_assoc($sql_check)) {
		$quanhuyenArr[] = $bienquanhuyen;
	}
	// Gửi dữ liệu JSON về cho client
	header('Content-Type: application/json');
	echo json_encode($quanhuyenArr);
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '2') { // get phường xã
	$TinhTT = $_GET['Tinh_TP'];
	$QuanHuyenTT = $_GET['QuanHuyen_TP'];
	$sql_check2 = mysqli_query($link, "select DISTINCT Phuong_Xa from address where Tinh_TP='$TinhTT' and Quan_Huyen='$QuanHuyenTT' ORDER BY Phuong_Xa ASC");
	$phuongxaArr = array();
	while ($bienphuongxa = mysqli_fetch_assoc($sql_check2)) {
		$phuongxaArr[] = $bienphuongxa;
	}
	header('Content-Type: application/json');
	echo json_encode($phuongxaArr);
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Gettype']) && $_GET['Gettype'] == '1' && isset($_GET['Type']) && $_GET['Type'] == '3') { //check trùng manv thêm mới
	$Manv = $_GET['Manv'];
	$sql_check = mysqli_query($link, "Select * from data_user where Maso_NV='$Manv'");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "ok";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else {
		$notice = "error";
		header('Content-Type: application/json');
		echo json_encode($notice);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Gettype']) && $_GET['Gettype'] == '2' && isset($_GET['Type']) && $_GET['Type'] == '3') { //check trùng manv update
	$Manv = $_GET['Manv'];
	$sql_check = mysqli_query($link, "Select * from data_user where Maso_NV='$Manv'");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 1) {
		$notice = "ok";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else {
		$notice = "error";
		header('Content-Type: application/json');
		echo json_encode($notice);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Gettype']) && $_GET['Gettype'] == '1' && isset($_GET['Type']) && $_GET['Type'] == '4') { //check trùng CCCD thêm mới
	$value = $_GET['Cccd'];
	$sql_check = mysqli_query($link, "Select * from data_user where So_CCCD='$value'");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "ok";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else {
		$notice = "error";
		header('Content-Type: application/json');
		echo json_encode($notice);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Gettype']) && $_GET['Gettype'] == '2' && isset($_GET['Type']) && $_GET['Type'] == '4') { //check trùng CCCD update
	$value = $_GET['Cccd'];
	$sql_check = mysqli_query($link, "Select * from data_user where So_CCCD='$value'");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "ok";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else if ($dem >= 1) {
		$Manv = $_GET['Manv'];
		$row = mysqli_fetch_array($sql_check);
		if ($Manv == $row['Maso_NV']) {
			$notice = "ok";
			header('Content-Type: application/json');
			echo json_encode($notice);
		} else {
			$notice = "error";
			header('Content-Type: application/json');
			echo json_encode($notice);
		}
	} else {
		$notice = "error";
		header('Content-Type: application/json');
		echo json_encode($notice);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Gettype']) && $_GET['Gettype'] == '1' && isset($_GET['Type']) && $_GET['Type'] == '5') { //check trùng CMND thêm mới
	$value = $_GET['Cmnd'];
	$sql_check = mysqli_query($link, "Select * from data_user where So_CMND='$value'");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "ok";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else {
		$notice = "error";
		header('Content-Type: application/json');
		echo json_encode($notice);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Gettype']) && $_GET['Gettype'] == '2' && isset($_GET['Type']) && $_GET['Type'] == '5') { //check trùng CMND update
	$value = $_GET['Cmnd'];
	$sql_check = mysqli_query($link, "Select * from data_user where So_CMND='$value'");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "ok";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else if ($dem >= 1) {
		$Manv = $_GET['Manv'];
		$row = mysqli_fetch_array($sql_check);
		if ($Manv == $row['Maso_NV']) {
			$notice = "ok";
			header('Content-Type: application/json');
			echo json_encode($notice);
		} else {
			$notice = "error";
			header('Content-Type: application/json');
			echo json_encode($notice);
		}
	} else {
		$notice = "error";
		header('Content-Type: application/json');
		echo json_encode($notice);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '6') { //check bophan
	$khoi = $_GET['Thuockhoi'];
	$sql_check = mysqli_query($link, "select DISTINCT Bo_phan from posion where Khoi='$khoi'");
	$bophanArr = array();
	while ($bienbophan = mysqli_fetch_assoc($sql_check)) {
		$bophanArr[] = $bienbophan;
	}
	header('Content-Type: application/json');
	echo json_encode($bophanArr);
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '7') { //check chucvu
	$khoi = $_GET['Thuockhoi'];
	$bophan = $_GET['Bophan'];
	$sql_check = mysqli_query($link, "select DISTINCT Chuc_vu from posion where Khoi='$khoi' and Bo_phan='$bophan'");
	$chucvuArr = array();
	while ($bienchucvu = mysqli_fetch_assoc($sql_check)) {
		$chucvuArr[] = $bienchucvu;
	}
	header('Content-Type: application/json');
	echo json_encode($chucvuArr);
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '8') { //check sohdld1
	$sohdld = $_GET['sohieu'];
	$sql_check = mysqli_query($link, "SELECT So_hdld1 FROM data_user WHERE So_hdld1 LIKE '$sohdld' ORDER BY So_hdld1 DESC LIMIT 1;");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "1";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else {
		$row = mysqli_fetch_array($sql_check);
		$gethdld = substr($row['So_hdld1'], 0, 3) + 1;
		header('Content-Type: application/json');
		echo json_encode($gethdld);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '9') { //check sohdld2
	$sohdld = $_GET['sohieu'];
	$sql_check = mysqli_query($link, "SELECT So_hdld2 FROM data_user WHERE So_hdld2 LIKE '$sohdld' ORDER BY So_hdld2 DESC LIMIT 1;");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "1";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else {
		$row = mysqli_fetch_array($sql_check);
		$gethdld = substr($row['So_hdld2'], 0, 3) + 1;
		header('Content-Type: application/json');
		echo json_encode($gethdld);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '10') { //check sohdld3
	$sohdld = $_GET['sohieu'];
	$sql_check = mysqli_query($link, "SELECT So_hdld3 FROM data_user WHERE So_hdld3 LIKE '$sohdld' ORDER BY So_hdld3 DESC LIMIT 1;");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "1";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else {
		$row = mysqli_fetch_array($sql_check);
		$gethdld = substr($row['So_hdld3'], 0, 3) + 1;
		header('Content-Type: application/json');
		echo json_encode($gethdld);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '11') { //check soqdnv
	$sohdld = $_GET['sohieu'];
	$sql_check = mysqli_query($link, "SELECT So_QDNV FROM data_user WHERE So_QDNV LIKE '$sohdld' ORDER BY So_QDNV DESC LIMIT 1;");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "1";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else {
		$row = mysqli_fetch_array($sql_check);
		$gethdld = substr($row['So_QDNV'], 0, 3) + 1;
		header('Content-Type: application/json');
		echo json_encode($gethdld);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Gettype']) && $_GET['Gettype'] == '1' && isset($_GET['Type']) && $_GET['Type'] == '12') { //check trùng Email thêm mới
	$Email = $_GET['Email'];
	$sql_check = mysqli_query($link, "SELECT Maso_NV FROM data_user WHERE Email='$Email'");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "ok";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else {
		$notice = "error";
		header('Content-Type: application/json');
		echo json_encode($notice);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Gettype']) && $_GET['Gettype'] == '2' && isset($_GET['Type']) && $_GET['Type'] == '12') { //check trùng Email update
	$Email = $_GET['Email'];
	$Manv = $_GET['Manv'];
	$sql_check = mysqli_query($link, "SELECT Maso_NV FROM data_user WHERE Email='$Email'");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "ok";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else if ($dem >= 1) {
		$row = mysqli_fetch_array($sql_check);
		if ($Manv == $row['Maso_NV']) {
			$notice = "ok";
			header('Content-Type: application/json');
			echo json_encode($notice);
		} else {
			$notice = "error";
			header('Content-Type: application/json');
			echo json_encode($notice);
		}
	} else {
		$notice = "error";
		header('Content-Type: application/json');
		echo json_encode($notice);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '13') { //check posion
	$Bophan = $_GET['Bophan'];
	$Thuockhoi = $_GET['Thuockhoi'];
	$Chucvu = $_GET['Chucvu'];

	$sql_check = mysqli_query($link, "SELECT Posion FROM posion WHERE Khoi='$Thuockhoi' and Bo_phan='$Bophan' and Chuc_vu='$Chucvu'");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "error";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else {
		$row = mysqli_fetch_array($sql_check);
		$posion = $row['Posion'];
		header('Content-Type: application/json');
		echo json_encode($posion);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '14') { // Xác thực tỉnh/ tren cccd qr
	$Tinh = $_GET['Tinh'];
	$sql_check = mysqli_query($link, "SELECT Tinh_TP FROM address WHERE Tinh_TP LIKE '$Tinh' ORDER BY Tinh_TP DESC LIMIT 1;");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "error";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else {
		$row = mysqli_fetch_array($sql_check);
		$notice = $row['Tinh_TP'];
		header('Content-Type: application/json');
		echo json_encode($notice);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '15') { // Xác thực huyen/ tren cccd qr
	$Tinh = $_GET['Tinh'];
	$Huyen = $_GET['Huyen'];
	$sql_check = mysqli_query($link, "SELECT Quan_Huyen FROM address WHERE Tinh_TP='$Tinh' and Quan_Huyen LIKE '$Huyen' ORDER BY Quan_Huyen DESC LIMIT 1;");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "error";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else {
		$row = mysqli_fetch_array($sql_check);
		$notice = $row['Quan_Huyen'];
		header('Content-Type: application/json');
		echo json_encode($notice);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '16') { // Xác thực Phuong/ tren cccd qr
	$Tinh = $_GET['Tinh'];
	$Huyen = $_GET['Huyen'];
	$Phuong = $_GET['Phuong'];
	$sql_check = mysqli_query($link, "SELECT Phuong_Xa FROM address WHERE Tinh_TP='$Tinh' and Quan_Huyen='$Huyen' and Phuong_Xa LIKE '$Phuong' ORDER BY Phuong_Xa DESC LIMIT 1;");
	$dem = mysqli_num_rows($sql_check);
	if ($dem == 0) {
		$notice = "error";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else {
		$row = mysqli_fetch_array($sql_check);
		$notice = $row['Phuong_Xa'];
		header('Content-Type: application/json');
		echo json_encode($notice);
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '17') { //check sodcluong
	$sohdld = $_GET['sohieu'];
	$sql_check = mysqli_query($link, "SELECT So_dieuchinh1 FROM data_user WHERE So_dieuchinh1 LIKE '$sohdld' ORDER BY So_dieuchinh1 DESC LIMIT 1;");
	$dem = mysqli_num_rows($sql_check);
	$sodcluong = '0';
	if ($dem == 0) {
		$sodcluong = "1";
	} else {
		$row = mysqli_fetch_array($sql_check);
		$gethdld = substr($row['So_dieuchinh1'], 0, 3) + 1;
		$sodcluong = $gethdld;
	}
	$sql_dcluong2 = mysqli_query($link, "SELECT So_dieuchinh2 FROM data_user WHERE So_dieuchinh2 LIKE '$sohdld' ORDER BY So_dieuchinh2 DESC LIMIT 1;");
	$dem2 = mysqli_num_rows($sql_dcluong2);
	if ($dem2 >= 1) {
		$row2 = mysqli_fetch_array($sql_dcluong2);
		$getdcluong2 = substr($row['So_dieuchinh2'], 0, 3) + 1;
		if ($sodcluong <= $getdcluong2) {
			$sodcluong = $getdcluong2;
		}
	}
	$sql_dcluong3 = mysqli_query($link, "SELECT So_dieuchinh3 FROM data_user WHERE So_dieuchinh3 LIKE '$sohdld' ORDER BY So_dieuchinh3 DESC LIMIT 1;");
	$dem3 = mysqli_num_rows($sql_dcluong3);
	if ($dem3 >= 1) {
		$row3 = mysqli_fetch_array($sql_dcluong3);
		$getdcluong3 = substr($row['So_dieuchinh3'], 0, 3) + 1;
		if ($sodcluong <= $getdcluong3) {
			$sodcluong = $getdcluong3;
		}
	}
	$sql_dcluong4 = mysqli_query($link, "SELECT So_dieuchinh4 FROM data_user WHERE So_dieuchinh4 LIKE '$sohdld' ORDER BY So_dieuchinh4 DESC LIMIT 1;");
	$dem4 = mysqli_num_rows($sql_dcluong4);
	if ($dem4 >= 1) {
		$row4 = mysqli_fetch_array($sql_dcluong4);
		$getdcluong4 = substr($row['So_dieuchinh4'], 0, 3) + 1;
		if ($sodcluong <= $getdcluong4) {
			$sodcluong = $getdcluong4;
		}
	}
	$sql_dcluong5 = mysqli_query($link, "SELECT So_dieuchinh5 FROM data_user WHERE So_dieuchinh5 LIKE '$sohdld' ORDER BY So_dieuchinh5 DESC LIMIT 1;");
	$dem5 = mysqli_num_rows($sql_dcluong5);
	if ($dem5 >= 1) {
		$row5 = mysqli_fetch_array($sql_dcluong5);
		$getdcluong5 = substr($row['So_dieuchinh5'], 0, 3) + 1;
		if ($sodcluong <= $getdcluong5) {
			$sodcluong = $getdcluong5;
		}
	}
	header('Content-Type: application/json');
	echo json_encode($sodcluong);
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '1000') { //Get Info User
	$Manv = $_GET['Manv'];
	$sql_check = mysqli_query($link, "select * from data_user where Maso_NV='$Manv'");
	$getArr = array();
	while ($bien = mysqli_fetch_assoc($sql_check)) {
		$getArr[] = $bien;
	}
	$dem = mysqli_num_rows($sql_check);

	if ($dem == 0) {
		$notice = "error";
		header('Content-Type: application/json');
		echo json_encode($notice);
	} else {
		// Gửi dữ liệu JSON về cho client
		header('Content-Type: application/json');
		echo json_encode($getArr);
	}
} else {
	header('location:../template/index.php?mod=error');   //redirect đến trang error.php
	exit();
}
