<?php
session_start();
include('../function/function.php');
include('../include/connect.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
//********************************************************
// Lấy dữ liệu POST
$data = json_decode(file_get_contents('php://input'), true);

if ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '3') {
	$Gettype = $data['Gettype'];
	$Manv = $data['Manv'];
	if ($Gettype == '1') {
		$sql_check = mysqli_query($link, "Select * from data_user where Maso_NV='$Manv'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem > 0) {
			header('Content-Type: application/json');
			echo json_encode("127"); //trả về mess127
		}
	} elseif ($Gettype == '2') {
		$sql_check = mysqli_query($link, "Select * from data_user where Maso_NV='$Manv'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem == 1) {
			$row = mysqli_fetch_array($sql_check);
			if ($Manv != $row['Maso_NV']) {
				header('Content-Type: application/json');
				echo json_encode("127"); //trả về mess127
			}
		}
	}
	// Gửi dữ liệu JSON về cho client

} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '4') {
	$Gettype = $data['Gettype'];
	$Cccd = $data['Cccd'];
	if ($Gettype == '1') {
		$sql_check = mysqli_query($link, "Select So_CCCD from data_user where So_CCCD='$Cccd'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem > 0) {
			header('Content-Type: application/json');
			echo json_encode("143");
		}
	} elseif ($Gettype == '2') {
		$Manv = $data['Manv'];
		$sql_check = mysqli_query($link, "Select * from data_user where So_CCCD='$Cccd'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem == 1) {
			$row = mysqli_fetch_array($sql_check);
			if ($Manv != $row['Maso_NV']) {
				header('Content-Type: application/json');
				echo json_encode("143"); //trả về mess127
			}
		}
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '5') {
	$Gettype = $data['Gettype'];
	$Cmnd = $data['Cmnd'];
	if ($Gettype == '1') {
		$sql_check = mysqli_query($link, "Select * from data_user where So_CMND='$Cmnd'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem > 0) {
			header('Content-Type: application/json');
			echo json_encode("150");
		}
	} elseif ($Gettype == '2') {
		$Manv = $data['Manv'];
		$sql_check = mysqli_query($link, "Select * from data_user where So_CMND='$Cmnd'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem == 1) {
			$row = mysqli_fetch_array($sql_check);
			if ($Manv != $row['Maso_NV']) {
				header('Content-Type: application/json');
				echo json_encode("150"); //trả về mess127
			}
		}
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '12') {
	$Gettype = $data['Gettype'];
	$Email = $data['Email'];
	if ($Gettype == '1') {
		$sql_check = mysqli_query($link, "SELECT Maso_NV FROM data_user WHERE Email='$Email'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem > 0) {
			header('Content-Type: application/json');
			echo json_encode("129");
		}
	} else if ($Gettype == '2') {
		$Manv = $data['Manv'];
		$sql_check = mysqli_query($link, "Select * from data_user where Email='$Email'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem == 1) {
			$row = mysqli_fetch_array($sql_check);
			if ($Manv != $row['Maso_NV']) {
				header('Content-Type: application/json');
				echo json_encode("129"); //trả về mess127
			}
		}
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '17') {

	$thuoccongty = $data['thuoccongty'];
	$sohieu = $data['sohieu'];
	$sql_checkcongty = mysqli_query($link, "Select sohieu from company where id='$thuoccongty'");
	$demcongty = mysqli_num_rows($sql_checkcongty);
	$rowct = mysqli_fetch_array($sql_checkcongty);
	if ($demcongty > 0) {
		$sohdld = '%-' . $sohieu . '/' . $rowct['sohieu'] . '%';
		$sohieudieuchinh = $rowct['sohieu'];
	} else {
		$sohdld = '%-' . $sohieu . '/' . '...' . '%';
		$sohieudieuchinh = '...';
	}
	$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($row2['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($row3['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($row4['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($row5['So_dieuchinh5'], 0, 3) + 1;
		if ($sodcluong <= $getdcluong5) {
			$sodcluong = $getdcluong5;
		}
	}
	header('Content-Type: application/json');
	echo json_encode(str_pad($sodcluong, 3, '0', STR_PAD_LEFT) . '-' . $sohieu . '/' . $sohieudieuchinh);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '18') { // 

	$sql_check = mysqli_query($link, "SELECT DISTINCT Bo_phan, Khoi FROM posion ORDER BY Khoi");
	$dem = mysqli_num_rows($sql_check);
	$bophanArr = array();
	while ($bienbophan = mysqli_fetch_assoc($sql_check)) {
		$bophanArr[] = $bienbophan;
	}
	header('Content-Type: application/json');
	echo json_encode($bophanArr);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '19') {

	$thuoccongty = $data['thuoccongty'];
	$sohieu = $data['sohieu'];
	$sql_checkcongty = mysqli_query($link, "Select sohieu from company where id='$thuoccongty'");
	$demcongty = mysqli_num_rows($sql_checkcongty);
	$rowct = mysqli_fetch_array($sql_checkcongty);
	if ($demcongty > 0) {
		$sohdld = '%-' . $sohieu . '/' . $rowct['sohieu'] . '%';
		$sohieudieuchinh = $rowct['sohieu'];
	} else {
		$sohdld = '%-' . $sohieu . '/' . '...' . '%';
		$sohieudieuchinh = '...';
	}
	$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);
	$sodcluong = '0';
	if ($dem == 0) {
		$sodcluong = "1";
	} else {
		$row = mysqli_fetch_array($sql_check);
		$gethdld = substr($row['So_HDLD1'], 0, 3) + 1;
		$sodcluong = $gethdld;
	}

	header('Content-Type: application/json');
	echo json_encode(str_pad($sodcluong, 3, '0', STR_PAD_LEFT) . '-' . $sohieu . '/' . $sohieudieuchinh);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '20') {

	$thuoccongty = $data['thuoccongty'];
	$sohieu = $data['sohieu'];
	$sql_checkcongty = mysqli_query($link, "Select sohieu from company where id='$thuoccongty'");
	$demcongty = mysqli_num_rows($sql_checkcongty);
	$rowct = mysqli_fetch_array($sql_checkcongty);
	if ($demcongty > 0) {
		$sohdld = '%-' . $sohieu . '/' . $rowct['sohieu'] . '%';
		$sohieudieuchinh = $rowct['sohieu'];
	} else {
		$sohdld = '%-' . $sohieu . '/' . '...' . '%';
		$sohieudieuchinh = '...';
	}
	$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);
	$sodcluong = '0';
	if ($dem == 0) {
		$sodcluong = "1";
	} else {
		$row = mysqli_fetch_array($sql_check);
		$gethdld = substr($row['So_HDLD2'], 0, 3) + 1;
		$sodcluong = $gethdld;
	}

	header('Content-Type: application/json');
	echo json_encode(str_pad($sodcluong, 3, '0', STR_PAD_LEFT) . '-' . $sohieu . '/' . $sohieudieuchinh);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '21') {

	$thuoccongty = $data['thuoccongty'];
	$sohieu = $data['sohieu'];
	$sql_checkcongty = mysqli_query($link, "Select sohieu from company where id='$thuoccongty'");
	$demcongty = mysqli_num_rows($sql_checkcongty);
	$rowct = mysqli_fetch_array($sql_checkcongty);
	if ($demcongty > 0) {
		$sohdld = '%-' . $sohieu . '/' . $rowct['sohieu'] . '%';
		$sohieudieuchinh = $rowct['sohieu'];
	} else {
		$sohdld = '%-' . $sohieu . '/' . '...' . '%';
		$sohieudieuchinh = '...';
	}
	$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);
	$sodcluong = '0';
	if ($dem == 0) {
		$sodcluong = "1";
	} else {
		$row = mysqli_fetch_array($sql_check);
		$gethdld = substr($row['So_HDLD3'], 0, 3) + 1;
		$sodcluong = $gethdld;
	}

	header('Content-Type: application/json');
	echo json_encode(str_pad($sodcluong, 3, '0', STR_PAD_LEFT) . '-' . $sohieu . '/' . $sohieudieuchinh);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '22') {

	$thuoccongty = $data['thuoccongty'];
	$sohieu = $data['sohieu'];
	$sql_checkcongty = mysqli_query($link, "Select sohieu from company where id='$thuoccongty'");
	$demcongty = mysqli_num_rows($sql_checkcongty);
	$rowct = mysqli_fetch_array($sql_checkcongty);
	if ($demcongty > 0) {
		$sohdld = '%-' . $sohieu . '/' . $rowct['sohieu'] . '%';
		$sohieudieuchinh = $rowct['sohieu'];
	} else {
		$sohdld = '%-' . $sohieu . '/' . '...' . '%';
		$sohieudieuchinh = '...';
	}
	$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);
	$sodcluong = '0';
	if ($dem == 0) {
		$sodcluong = "1";
	} else {
		$row = mysqli_fetch_array($sql_check);
		$gethdld = substr($row['So_QDNV'], 0, 3) + 1;
		$sodcluong = $gethdld;
	}

	header('Content-Type: application/json');
	echo json_encode(str_pad($sodcluong, 3, '0', STR_PAD_LEFT) . '-' . $sohieu . '/' . $sohieudieuchinh);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '23') {
	$Tinh = $data['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 $notice;
	} else {
		$row = mysqli_fetch_array($sql_check);
		$notice = $row['Tinh_TP'];
		header('Content-Type: application/json');
		echo $notice;
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '24') {
	$Tinh = $data['Tinh'];
	$Huyen = $data['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 $notice;
	} else {
		$row = mysqli_fetch_array($sql_check);
		$notice = $row['Quan_Huyen'];
		header('Content-Type: application/json');
		echo $notice;
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '25') {
	$Tinh = $data['Tinh'];
	$Huyen = $data['Huyen'];
	$Phuong = $data['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 $notice;
	} else {
		$row = mysqli_fetch_array($sql_check);
		$notice = $row['Phuong_Xa'];
		header('Content-Type: application/json');
		echo $notice;
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '26') {
	if ($data['Finkey'] == "CCCD") {
		$CCCD = $data['CCCD'];
		$sql_check = mysqli_query($link, "SELECT * FROM data_user WHERE So_CCCD='$CCCD'ORDER BY So_CCCD DESC LIMIT 1;");
		$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);
		}
	} elseif ($data['Finkey'] == "Manv") {
		$Manv = $data['Manv'];
		$sql_check = mysqli_query($link, "SELECT * FROM data_user WHERE Maso_NV='$Manv'ORDER BY Maso_NV DESC LIMIT 1;");
		$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);
		}
	} elseif ($data['Finkey'] == "Hoten") {
		$Hoten = $data['Hoten'];
		$sql_check = mysqli_query($link, "SELECT * FROM data_user WHERE Hoten LIKE'" . "%" . $Hoten . "%" . "';");
		$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['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '27') { // Get data form theo id
	$id = $_GET['id'];
	$sql_check = mysqli_query($link, "SELECT * FROM form WHERE id = '$id'");
	$dem = mysqli_num_rows($sql_check);
	if ($dem > 0) {
		$formArr = array();
		while ($bienform = mysqli_fetch_assoc($sql_check)) {
			$formArr[] = $bienform;
		}
		header('Content-Type: application/json');
		echo json_encode($formArr);
	} else {
		header('Content-Type: application/json');
		echo json_encode("error");
	}
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '28') { // Get data form All
	$sql_check = mysqli_query($link, "SELECT * FROM form ");
	$dem = mysqli_num_rows($sql_check);
	if ($dem > 0) {
		$formArr = array();
		while ($bienform = mysqli_fetch_assoc($sql_check)) {
			$formArr[] = $bienform;
		}
		header('Content-Type: application/json');
		echo json_encode($formArr);
	} else {
		header('Content-Type: application/json');
		echo json_encode("error");
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == '29') { // 

	$sql_check = mysqli_query($link, "SELECT DISTINCT * FROM form_group ORDER BY id");
	$dem = mysqli_num_rows($sql_check);
	$bienArr = array();
	while ($bien = mysqli_fetch_assoc($sql_check)) {
		$bienArr[] = $bien;
	}
	header('Content-Type: application/json');
	echo json_encode($bienArr);
} else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '30') { // Get data form theo id
	$id = $_GET['id'];
	$sql_check = mysqli_query($link, "SELECT * FROM form_group WHERE id = '$id'");
	$dem = mysqli_num_rows($sql_check);
	if ($dem > 0) {
		$formArr = array();
		while ($bienform = mysqli_fetch_assoc($sql_check)) {
			$formArr[] = $bienform;
		}
		header('Content-Type: application/json');
		echo json_encode($formArr);
	} else {
		header('Content-Type: application/json');
		echo json_encode("error");
	}
}else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '31') { // Get All bộ phận

	$sql_check = mysqli_query($link, "SELECT DISTINCT Bo_phan, Khoi FROM posion ORDER BY Khoi");
	$dem = mysqli_num_rows($sql_check);
	$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'] == '32') { // Get data form All
	$sql_check = mysqli_query($link, "SELECT * FROM kyhieuca ORDER BY grouptype ASC, name ASC ");
	$dem = mysqli_num_rows($sql_check);
	if ($dem > 0) {
		$formArr = array();
		while ($bienform = mysqli_fetch_assoc($sql_check)) {
			$formArr[] = $bienform;
		}
		header('Content-Type: application/json');
		echo json_encode($formArr);
	} else {
		header('Content-Type: application/json');
		echo json_encode("error");
	}
}else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '33') { // Get form theo id kyhieuca
	$name = $_GET['name'];
	$sql_check = mysqli_query($link, "SELECT * FROM kyhieuca WHERE name = '$name'");
	$dem = mysqli_num_rows($sql_check);
	if ($dem > 0) {
		$formArr = array();
		while ($bienform = mysqli_fetch_assoc($sql_check)) {
			$formArr[] = $bienform;
		}
		header('Content-Type: application/json');
		echo json_encode($formArr);
	} else {
		header('Content-Type: application/json');
		echo json_encode("error");
	}
}else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '34') { // Get roster từ Mysql 
	$bophan = $_SESSION['bophan'];
	mysqli_query($link, "SET @row_number = 0");
	$sql_check = mysqli_query($link, "SELECT (@row_number:=@row_number + 1) AS Stt, data_user.Maso_NV, data_user.Hoten, data_user.Chucvu FROM data_user WHERE Bophan = '$bophan' AND  (Ngayketthuc IS NULL OR Ngayketthuc = '') ORDER BY Permisson DESC, Posion ASC;");
	$dem = mysqli_num_rows($sql_check);
	if ($dem > 0) {
		$formArr = array();
		while ($bienform = mysqli_fetch_assoc($sql_check)) {
			$formArr[] = $bienform;
		}
		header('Content-Type: application/json');
		echo json_encode($formArr);
	} else {
		header('Content-Type: application/json');
		echo json_encode("error");
	}
}else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '35') { // Get roster từ data.txt
	// Đường dẫn đến tệp txt chứa dữ liệu
	$tuan = $_GET['Week'];
	$yearget = $_GET['Year'];
	$bophan = $_SESSION['bophan'];	
	$duongDanTep = "../data/roster/". $yearget . "/week-" . $tuan . "/" . $bophan . ".txt";
	// Đọc nội dung từ tệp txt và chuyển thành mảng PHP
	$fileContent = file_get_contents($duongDanTep);
	$formArr = json_decode($fileContent, true);

	// Kiểm tra nếu có dữ liệu trong mảng
	if (!empty($formArr)) {
		header('Content-Type: application/json');
		echo json_encode($formArr);
	} else {
		header('Content-Type: application/json');
		echo json_encode("error");
	}
}else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '36') { // Get meal từ data.txt
	// Đường dẫn đến tệp txt chứa dữ liệu
	$month = $_GET['Month'];
	$yearget = $_GET['Year'];
	$bophan = $_SESSION['bophan'];	
	$duongDanTep = "../data/meal/". $yearget . "/" . $month . "/" . $bophan . ".txt";
	// Đọc nội dung từ tệp txt và chuyển thành mảng PHP
	$fileContent = file_get_contents($duongDanTep);
	$formArr = json_decode($fileContent, true);

	// Kiểm tra nếu có dữ liệu trong mảng
	if (!empty($formArr)) {
		header('Content-Type: application/json');
		echo json_encode($formArr);
	} else {
		header('Content-Type: application/json');
		echo json_encode("error");
	}
}else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '37') { // check file
	// Đường dẫn đến tệp txt chứa dữ liệu
	$url = $_GET['url'];
	if(file_exists($url)){
		header('Content-Type: application/json');
		echo json_encode('ok');
	} else {
		header('Content-Type: application/json');
		echo json_encode('error');
	}
}else if (isset($_GET['Action']) && $_GET['Action'] == 'SANG-HR' && isset($_GET['Type']) && $_GET['Type'] == '38') { // Get timesheet từ data.txt
	// Đường dẫn đến tệp txt chứa dữ liệu
	$month = $_GET['Month'];
	$yearget = $_GET['Year'];
	$bophan = $_SESSION['bophan'];	
	$duongDanTep = "../data/timesheet/". $yearget . "/" . $month . "/" . $bophan . ".txt";
	// Đọc nội dung từ tệp txt và chuyển thành mảng PHP
	$fileContent = file_get_contents($duongDanTep);
	$formArr = json_decode($fileContent, true);

	// Kiểm tra nếu có dữ liệu trong mảng
	if (!empty($formArr)) {
		header('Content-Type: application/json');
		echo json_encode($formArr);
	} else {
		header('Content-Type: application/json');
		echo json_encode("error");
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'sohieudcluong') { // 
	$Gettype = $data['Gettype'];
	$Manv = $data['Manv'];
	$Value = $data['Value'];
	$Getid = $data['Getid'];
	if ($Gettype == "1") {
		$sql_check = mysqli_query($link, "select Maso_NV from data_user where So_dieuchinh1='$Value' Or So_dieuchinh2='$Value' Or So_dieuchinh3='$Value' Or So_dieuchinh4='$Value' Or So_dieuchinh5='$Value'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem > 0) {
			header('Content-Type: application/json');
			echo json_encode("219");
		}
	} elseif ($Gettype == "2") {
		$sql_check = mysqli_query($link, "select Maso_NV from data_user where So_dieuchinh1='$Value' Or So_dieuchinh2='$Value' Or So_dieuchinh3='$Value' Or So_dieuchinh4='$Value' Or So_dieuchinh5='$Value'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem == 1) {
			$row = mysqli_fetch_array($sql_check);
			if ($Manv != $row['Maso_NV']) {
				header('Content-Type: application/json');
				echo json_encode("256"); //trả về mess127
			} else {
				$sql_check2 = mysqli_query($link, "select * from data_user where Maso_NV = '$Manv'");
				$row2 = mysqli_fetch_array($sql_check2);
				if ($Getid == 'sodcluong1') {
					if ($Value != $row2['So_dieuchinh1']) {
						header('Content-Type: application/json');
						echo json_encode("255"); //trả về mess127
					}
				} elseif ($Getid == 'sodcluong2') {
					if ($Value != $row2['So_dieuchinh2']) {
						header('Content-Type: application/json');
						echo json_encode("255"); //trả về mess127
					}
				} elseif ($Getid == 'sodcluong3') {
					if ($Value != $row2['So_dieuchinh3']) {
						header('Content-Type: application/json');
						echo json_encode("255"); //trả về mess127
					}
				} elseif ($Getid == 'sodcluong4') {
					if ($Value != $row2['So_dieuchinh4']) {
						header('Content-Type: application/json');
						echo json_encode("255"); //trả về mess127
					}
				} elseif ($Getid == 'sodcluong5') {
					if ($Value != $row2['So_dieuchinh5']) {
						header('Content-Type: application/json');
						echo json_encode("255"); //trả về mess127
					}
				}
			}
		}
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'sohdld1') { // 
	$Value = $data['Value'];
	$Gettype = $data['Gettype'];
	$Manv = $data['Manv'];
	if ($Gettype == "1") {
		$sql_check = mysqli_query($link, "select So_HDLD1 from data_user where So_HDLD1='$Value'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem > 0) {
			header('Content-Type: application/json');
			echo json_encode("219");
		}
	} elseif ($Gettype == "2") {
		$sql_check = mysqli_query($link, "Select * from data_user where So_HDLD1='$Value'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem == 1) {
			$row = mysqli_fetch_array($sql_check);
			if ($Manv != $row['Maso_NV']) {
				header('Content-Type: application/json');
				echo json_encode("257"); //trả về mess127
			}
		}
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'sohdld2') { // 
	$Value = $data['Value'];
	$Gettype = $data['Gettype'];
	$Manv = $data['Manv'];
	if ($Gettype == "1") {
		$sql_check = mysqli_query($link, "select So_HDLD2 from data_user where So_HDLD2='$Value'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem > 0) {
			header('Content-Type: application/json');
			echo json_encode("219");
		}
	} elseif ($Gettype == "2") {
		$sql_check = mysqli_query($link, "Select * from data_user where So_HDLD2='$Value'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem == 1) {
			$row = mysqli_fetch_array($sql_check);
			if ($Manv != $row['Maso_NV']) {
				header('Content-Type: application/json');
				echo json_encode("258"); //trả về mess127
			}
		}
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'sohdld3') { // 
	$Value = $data['Value'];
	$Gettype = $data['Gettype'];
	$Manv = $data['Manv'];
	if ($Gettype == "1") {
		$sql_check = mysqli_query($link, "select So_HDLD3 from data_user where So_HDLD3='$Value'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem > 0) {
			header('Content-Type: application/json');
			echo json_encode("219");
		}
	} elseif ($Gettype == "2") {
		$sql_check = mysqli_query($link, "Select * from data_user where So_HDLD3='$Value'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem == 1) {
			$row = mysqli_fetch_array($sql_check);
			if ($Manv != $row['Maso_NV']) {
				header('Content-Type: application/json');
				echo json_encode("259"); //trả về mess127
			}
		}
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'soqdnv') { // 
	$Value = $data['Value'];
	$Gettype = $data['Gettype'];
	$Manv = $data['Manv'];
	if ($Gettype == "1") {
		$sql_check = mysqli_query($link, "select So_QDNV from data_user where So_QDNV='$Value'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem > 0) {
			header('Content-Type: application/json');
			echo json_encode("219");
		}
	} elseif ($Gettype == "2") {
		$sql_check = mysqli_query($link, "Select * from data_user where So_QDNV='$Value'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem == 1) {
			$row = mysqli_fetch_array($sql_check);
			if ($Manv != $row['Maso_NV']) {
				header('Content-Type: application/json');
				echo json_encode("219"); //trả về mess127
			}
		}
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'mabhxh') { // 
	$Value = $data['MaBHXH'];
	$Gettype = $data['Gettype'];
	$Manv = $data['Manv'];
	if ($Gettype == "1") {
		$sql_check = mysqli_query($link, "select Maso_BHXH from data_user where Maso_BHXH='$Value'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem > 0) {
			header('Content-Type: application/json');
			echo json_encode("219");
		}
	} elseif ($Gettype == "2") {
		$sql_check = mysqli_query($link, "Select * from data_user where Maso_BHXH='$Value'");
		$dem = mysqli_num_rows($sql_check);
		if ($dem == 1) {
			$row = mysqli_fetch_array($sql_check);
			if ($Manv != $row['Maso_NV']) {
				header('Content-Type: application/json');
				echo json_encode("219"); //trả về mess127
			}
		}
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'quoctich') {

	$sql_check = mysqli_query($link, "Select Quoctich from plugin where Quoctich <> ''");
	$returnArr = array();
	while ($bienreturn = mysqli_fetch_assoc($sql_check)) {
		$returnArr[] = $bienreturn;
	}
	// Gửi dữ liệu JSON về cho client
	header('Content-Type: application/json');
	echo json_encode($returnArr);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'tinhtt') {

	$sql_check = mysqli_query($link, "select DISTINCT Tinh_TP from address ORDER BY Tinh_TP ASC");
	$returnArr = array();
	while ($bienreturn = mysqli_fetch_assoc($sql_check)) {
		$returnArr[] = $bienreturn;
	}
	// Gửi dữ liệu JSON về cho client
	header('Content-Type: application/json');
	echo json_encode($returnArr);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'quanhuyentt') {
	$TinhTT = $data['tinhtt'];
	$sql_check = mysqli_query($link, "select DISTINCT Quan_Huyen from address where Tinh_TP='$TinhTT' ORDER BY Tinh_TP ASC");
	$returnArr = array();
	while ($bienreturn = mysqli_fetch_assoc($sql_check)) {
		$returnArr[] = $bienreturn;
	}
	// Gửi dữ liệu JSON về cho client
	header('Content-Type: application/json');
	echo json_encode($returnArr);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'phuongxatt') {
	$TinhTT = $data['tinhtt'];
	$QuanHuyenTT = $data['quanhuyentt'];
	$sql_check = mysqli_query($link, "select DISTINCT Phuong_Xa from address where Tinh_TP='$TinhTT' and Quan_Huyen='$QuanHuyenTT' ORDER BY Phuong_Xa ASC");
	$returnArr = array();
	while ($bienreturn = mysqli_fetch_assoc($sql_check)) {
		$returnArr[] = $bienreturn;
	}
	// Gửi dữ liệu JSON về cho client
	header('Content-Type: application/json');
	echo json_encode($returnArr);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'bank') {

	$sql_check = mysqli_query($link, "Select Bank from plugin where Bank <> ''");
	$returnArr = array();
	while ($bienreturn = mysqli_fetch_assoc($sql_check)) {
		$returnArr[] = $bienreturn;
	}
	// Gửi dữ liệu JSON về cho client
	header('Content-Type: application/json');
	echo json_encode($returnArr);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'congty') {

	//$sql_check = mysqli_query($link, "Select * from Company");
	//$name = ''; // Khởi tạo biến name
	if (isset($_SESSION['language']) && $_SESSION['language'] == 'en') {
		$sql = "SELECT id, name_en as name FROM Company";
	} else {
		$sql = "SELECT id, name_vn as name FROM Company";
	}
	$sql_check = mysqli_query($link, $sql);
	$returnArr = array();
	while ($bienreturn = mysqli_fetch_assoc($sql_check)) {
		$returnArr[] = $bienreturn;
	}
	// Gửi dữ liệu JSON về cho client
	header('Content-Type: application/json');
	echo json_encode($returnArr);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'thuockhoi') {

	$sql_check = mysqli_query($link, 'select DISTINCT Khoi from posion');
	$returnArr = array();
	while ($bienreturn = mysqli_fetch_assoc($sql_check)) {
		$returnArr[] = $bienreturn;
	}
	// Gửi dữ liệu JSON về cho client
	header('Content-Type: application/json');
	echo json_encode($returnArr);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'bophan') {
	$khoi = $data['Thuockhoi'];
	$sql_check = mysqli_query($link, "select DISTINCT Bo_phan from posion where Khoi='$khoi'");
	$returnArr = array();
	while ($bienreturn = mysqli_fetch_assoc($sql_check)) {
		$returnArr[] = $bienreturn;
	}
	// Gửi dữ liệu JSON về cho client
	header('Content-Type: application/json');
	echo json_encode($returnArr);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'chucvu') {
	$khoi = $data['Thuockhoi'];
	$bophan = $data['Bophan'];
	$sql_check = mysqli_query($link, "select DISTINCT Chuc_vu from posion where Khoi='$khoi' and Bo_phan='$bophan'");
	$returnArr = array();
	while ($bienreturn = mysqli_fetch_assoc($sql_check)) {
		$returnArr[] = $bienreturn;
	}
	// Gửi dữ liệu JSON về cho client
	header('Content-Type: application/json');
	echo json_encode($returnArr);
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'posion') {
	$Bophan = $data['Bophan'];
	$Thuockhoi = $data['Thuockhoi'];
	$Chucvu = $data['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);
	$row = mysqli_fetch_array($sql_check);
	if ($dem > 0) {
		header('Content-Type: application/json');
		echo $row['Posion'];
	} else {
		header('Content-Type: application/json');
		echo "error";
	}
} elseif ($data && isset($_SESSION['username']) && isset($_SESSION['password']) && $data['Action'] == "SANG-HR" && $data['Type'] == 'quanhe') {
	$sql_check = mysqli_query($link, "SELECT Quanhe FROM plugin where Quanhe IS NOT NULL;");
	$returnArr = array();
	while ($bienreturn = mysqli_fetch_assoc($sql_check)) {
		$returnArr[] = $bienreturn;
	}
	// Gửi dữ liệu JSON về cho client
	header('Content-Type: application/json');
	echo json_encode($returnArr);
} else {
	header('location:../production/index.php?mod=error');   //redirect đến trang error.php
	exit();
}
