<?php
session_start();
include('../function/function.php');
//include('../module/dangnhap.php');
//require("chksession.php");
//*****************************************************************************
//[Modul name]: function.php
//[Purpose]   : Quan ly all function co lien quan toi PHP
//[Date]      : 31.10.05
//[Creator]   : PhongCT+HieuNK+DuongNT
//[Detail]    : Nhung ham khac nam trong trang FunctionJS.js
//******************************************************************************
//==============================================================================================
//********************************************************
//[Creator]   : PhongCT
//********************************************************
include('../include/connect.php'); // get quận huyện
if (isset($_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['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['Type']) && $_GET['Type'] == '3') { // check trùng manv
	if (isset($_GET['Manv']) && $_GET['Manv'] != '') {
		$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['Cmnd']) && $_GET['Cmnd'] != '') {
		$valuecmnd = $_GET['Cmnd'];
		$sql_check = mysqli_query($link, "Select * from data_user where So_CMND='$valuecmnd'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem == 0) {
			$notice = "ok";
			header('Content-Type: application/json');
			echo json_encode($notice);
		} else {
			if (isset($_GET['Get']) && $_GET['Get'] == 'profile') {
				$Manv = $_SESSION['username'];
				$sql_check2 = mysqli_query($link, "Select * from data_user where So_CMND='$valuecmnd' and Maso_NV='$Manv'");
				$dem2 = mysqli_num_rows($sql_check2);
				if ($dem2 == 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 {
				$notice = "error";
				header('Content-Type: application/json');
				echo json_encode($notice);
			}
		}
	} else if (isset($_GET['Cccd']) && $_GET['Cccd'] != '') {
		$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 (isset($_GET['Get']) && $_GET['Get'] == 'profile') {
				$Manv = $_SESSION['username'];
				$sql_check2 = mysqli_query($link, "Select * from data_user where So_CCCD='$value' and Maso_NV='$Manv'");
				$dem2 = mysqli_num_rows($sql_check2);
				if ($dem2 == 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 {
				$notice = "error";
				header('Content-Type: application/json');
				echo json_encode($notice);
			}
		}
	}
} else if (isset($_GET['Type']) && $_GET['Type'] == '4') { // get 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['Type']) && $_GET['Type'] == '5') { // get 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['Type']) && $_GET['Type'] == '6') { // check urlfile
	$url = $_GET['getUrl'];
	if (file_exists($url)) {
		header('Content-Type: application/json');
		echo json_encode('ok');
	} else {
		header('Content-Type: application/json');
		echo json_encode($url);
	}
} else if (isset($_GET['Type']) && $_GET['Type'] == '7') { // 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['Type']) && $_GET['Type'] == '8') { // 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['Type']) && $_GET['Type'] == '9') { // 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['Type']) && $_GET['Type'] == '10') { // check soqqdnv
	$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['Type']) && $_GET['Type'] == '11') { // check email profile
	if (isset($_GET['Email']) && isset($_GET['Manv'])) {
		$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['Type']) && $_GET['Type'] == '12') { // check email user-managerr
	if (isset($_GET['GetType']) && $_GET['GetType'] = '1') { // trường hợp thêm mới nhân viên
		$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['GetType']) && $_GET['GetType'] = '2') { // trường hợp sửa thông tin
		$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['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['Type']) && $_GET['Type'] == '14') { // Get thông tin trong usermanager
	$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 if (isset($_GET['Type']) && $_GET['Type'] == '15') { // 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['Type']) && $_GET['Type'] == '16') { // 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['Type']) && $_GET['Type'] == '17') { // 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 {
	header('location:../template/index.php?mod=error');   //redirect đến trang error.php
	exit();
}
