<?php
header("Access-Control-Allow-Origin: *"); //enable resourse sharing(allows this resource to be shared with "all[*]" requesting clients

define("MAIN_DB_USERNAME", "initia19_localization");
define("MAIN_DB_PASSWORD", "queritel_localization");
define("MAIN_DB_NAME", "initia19_localization");

include("../includes/localization-classes-v1.php");
include("../includes/localization-miscellaneous-v1.php");

$localization = new Database(MAIN_DB_USERNAME, MAIN_DB_PASSWORD, MAIN_DB_NAME);

//http://api.queritel.com/api/localization/query/country_query.php

$country_code = text_clean($_REQUEST["ccode"]);

if (!empty($country_code)){

    $result = array();
    $country_code = stringCase($country_code,STRING_CASE_UPPER);

    $country = "SELECT * FROM country_detail where country_code = '$country_code'";
    $country_result = $localization->query($country);

    if ($country_result != null && $country_result->is_row_found()){
        $row = $country_result->fetch_array();

        $result["country_name"] = $row["country_name"];
        $result["country_code"] = $row["country_code"];
        $result["country_code_3"] = $row["country_code_3"];
        $result["country_code_numeric"] = $row["country_code_numeric"];
        $result["iso_3166_2"] = $row["iso_3166_2"];
        $result["region"] = $row["region"];
        $result["sub_region"] = $row["sub_region"];
        $result["intermediate_region"] = $row["intermediate_region"];
        $result["region_code"] = $row["region_code"];
        $result["sub_region_code"] = $row["sub_region_code"];
        $result["intermediate_region_code"] = $row["intermediate_region_code"];
        $result["country_flag"] = "https://api.queritel.com/api/localization/resources/flags/" . stringCase($row["country_code"],STRING_CASE_LOWER) . ".svg";

    }


    echo json_export($result);

}
else{
    $client = new HostInfoLocalization($localization);

    $result = array();
    $country_code = stringCase($client->getCountry_code(),STRING_CASE_UPPER);

    $country = "SELECT * FROM country_detail where country_code = '$country_code'";
    $country_result = $localization->query($country);

    if ($country_result != null && $country_result->is_row_found()){
        $row = $country_result->fetch_array();

        $result["country_name"] = $row["country_name"];
        $result["country_code"] = $row["country_code"];
        $result["country_code_3"] = $row["country_code_3"];
        $result["country_code_numeric"] = $row["country_code_numeric"];
        $result["iso_3166_2"] = $row["iso_3166_2"];
        $result["region"] = $row["region"];
        $result["sub_region"] = $row["sub_region"];
        $result["intermediate_region"] = $row["intermediate_region"];
        $result["region_code"] = $row["region_code"];
        $result["sub_region_code"] = $row["sub_region_code"];
        $result["intermediate_region_code"] = $row["intermediate_region_code"];
        $result["country_flag"] = "https://api.queritel.com/api/localization/resources/flags/" . stringCase($row["country_code"],STRING_CASE_LOWER) . ".svg";

    }

    echo json_export($result);
}


