<?php 

require_once ('dbClass.php');

// session_start();

$companyID = isset($_POST['companyID']) ? $_POST['companyID'] : "";
$divisionID = isset($_POST['divisionID']) ? $_POST['divisionID'] : "";
$yearID = isset($_POST['yearID']) ? $_POST['yearID'] : "";


// echo $query = "SELECT CompanyID, DivisionID, Year, Database_name, Ip, Port, Sql_User_Id, 
//             Decrypted_Password from Aireg where CompanyID='".$companyID."' 
//             and DivisionID='".$divisionID."' and Year='".$yearID."'";

    $sql = "UPDATE users SET LastCompanyID = ".$companyID.", LastDivisionID = ".$divisionID.", LastYear = '".$yearID."' WHERE ID = " . $_SESSION['user_id'];
    $result = db::getInstanceMaster()->db_update($sql);
    // $lastID = $result['last_id'];

    $query = "SELECT CONCAT(AiCompany.Label,' - ', AiDivision.Label, ' - ', AiReg.Year) as Name, Aireg.CompanyID, Aireg.DivisionID, Aireg.Year, Aireg.Database_name, Aireg.Ip, Aireg.Port, Aireg.Sql_User_Id, 
        Aireg.Decrypted_Password, AiCompany.GroupID, Aireg.dtbases
        from Aireg 
        LEFT JOIN AiCompany ON AiReg.CompanyID = AiCompany.ID 
        LEFT JOIN AiGroup ON AiCompany.GroupID = AiGroup.ID
        LEFT JOIN AiDivision ON AiReg.DivisionID = AiDivision.ID
        where Aireg.CompanyID='".$companyID."' and Aireg.DivisionID='".$divisionID."' and Aireg.Year='".$yearID."' ";

// $sql1="Select CONCAT(AiCompany.Label,' - ', AiDivision.Label, ' - ', AiReg.Year) as Name,AiReg.ID From AiReg 
//     LEFT JOIN AiCompany ON AiReg.CompanyID = AiCompany.ID 
//     LEFT JOIN AiGroup ON AiCompany.GroupID = AiGroup.ID
//     LEFT JOIN AiDivision ON AiReg.DivisionID = AiDivision.ID
    


$result = db::getInstanceMaster()->db_select($query);
// print_r($result);
$row = $result['result_set'];
if(sizeof($row) > 0){
    for($i = 0; $i < count($row); $i++){
        $_SESSION['dbCompanyName'] = $row[$i]['Name'];

        $_SESSION['dbHost'] = $row[$i]['Ip'] . (!empty($row[$i]['Port']) ? ',' . $row[$i]['Port'] : '');

        // $_SESSION['dbHost'] = $row[$i]['Ip'].','.$row[$i]['Port'];
        
        $_SESSION['dbUser'] = $row[$i]['Sql_User_Id'];
        
        $_SESSION['dbPass'] = $row[$i]['Decrypted_Password'];
        
        $_SESSION['dbName'] = $row[$i]['Database_name'];
        // $_SESSION['dtbases'] = $row[$i]['dtbases'];
        
        $_SESSION['dbCompany'] = $row[$i]['CompanyID'];
        
        $_SESSION['dbDivision'] = $row[$i]['DivisionID'];
        if($_SESSION['Category'] == 2)
            $_SESSION['group_id'] = $row[$i]['GroupID'];
        
        $_SESSION['dbYear'] = $row[$i]['Year']; //only used at dbMySQL. To be removed.

        $_SESSION['dbYearID'] = $yearID;    //Doesn't have Year Code ID. Please Add it

    }
}



$showMessage = "success";
$arr = array("message" => $showMessage);


// if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) {
//     ob_start("ob_gzhandler");
// } else {
//     ob_start();
// }
if (!ini_get('zlib.output_compression') && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) {
    ob_start("ob_gzhandler");
} else {
    ob_start();
}
header('Content-Encoding: gzip');
header('Content-Type: application/json');
echo json_encode($arr);
ob_end_flush(); 

 ?>

