<?php
// Kết nối đến cơ sở dữ liệu
session_start();
include('../function/function.php');
include('../include/connect.php');
if (isset($_POST['query']) && $_POST['Action'] == 'SANG-HR' && $_POST['Check'] == 'SELECT') {
    $query = $_POST['query'];
    $output = '';
    $bophan = $_SESSION['bophan'];

    $sql = "SELECT * FROM kyhieuca WHERE (apply = 'All' AND name LIKE '%$query%') OR (apply LIKE '%[$bophan]%' AND name LIKE '%$query%')";
    $result = mysqli_query($link, $sql);

    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $output .= '<div class="suggest-item"><font color="'. $row['color'] .'">' . $row['name'] . '</font></div>';
        }
    } else {
        $output .= '<div class="no-suggest" style="color: red;">Ca không hợp lệ.</div>';
    }

    echo $output;
}else if (isset($_POST['query']) && $_POST['Action'] == 'SANG-HR' && $_POST['Check'] == 'CHECK') {
    $query = $_POST['query'];
    $output = '';
    $bophan = $_SESSION['bophan'];

    $sql = "SELECT * FROM kyhieuca WHERE (apply = 'All' AND name = '$query') OR (apply LIKE '%[$bophan]%' AND name = '$query')";
    $result = mysqli_query($link, $sql);

    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $output = $row['color'];
        }
    } else {
        $output = 'error';
    }
    echo $output;
}else {
	header('location:../template/index.php?mod=error');   //redirect đến trang error.php
	exit();
}
?>