 <?php

if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400'); // cache for 1 day
}

error_reporting(E_ERROR | E_PARSE);

$_SESSION['database'] = $_GET['database'];

$_SESSION['codigo'] = "";

$_SESSION['costo'] = '0';
$_SESSION['precio1'] = '0';
$_SESSION['precio6'] = '0';

$_SESSION['itbm'] = '0';
$_SESSION['familia'] = '0';

include 'conn.php';

include 'funciones.php';

$jsonData = array();
$jsonTempData = array();

$montoesql = "SELECT * from " . $_SESSION['database'] . ".fabricantes";

$qur = $connection->query($montoesql);

if ($_SESSION['codigo'] == "99") {

    $json->datos = "false";

} else {
//logs($montoesql);
    $json->datos = "true";

    while ($r = mysqli_fetch_assoc($qur)) {

        $jsonTempData = array();
        $jsonTempData['fab_id'] = $r['fab_id'];
        $jsonTempData['des_fabricante'] = utf8_encode($r['des_fabricante']);

        $jsonData[] = $jsonTempData;
    }
}

$json->fabricantes = $jsonData;

$jsonData = array();
$jsonTempData = array();

$montoesql = "SELECT * from " . $_SESSION['database'] . ".familia";

$qur = $connection->query($montoesql);

if ($_SESSION['codigo'] == "99") {

    $json->datos = "false";

} else {
//logs($montoesql);
    $json->datos = "true";

    while ($r = mysqli_fetch_assoc($qur)) {

        $jsonTempData = array();
        $jsonTempData['f_id'] = $r['f_id'];
        $jsonTempData['f_descripcion'] = utf8_encode($r['f_descripcion']);

        $jsonData[] = $jsonTempData;
    }
}

$json->familias = $jsonData;

$jsonData = array();
$jsonTempData = array();

$montoesql = "SELECT * from " . $_SESSION['database'] . ".marcas";

$qur = $connection->query($montoesql);

if ($_SESSION['codigo'] == "99") {

    $json->datos = "false";

} else {
//logs($montoesql);
    $json->datos = "true";

    while ($r = mysqli_fetch_assoc($qur)) {

        $jsonTempData = array();
        $jsonTempData['id_marca'] = $r['id_marca'];
        $jsonTempData['des_marca'] = utf8_encode($r['des_marca']);

        $jsonData[] = $jsonTempData;
    }
}

$json->marcas = $jsonData;

print_r(json_encode($json));

//echo $_SESSION['codigo']."@".$_SESSION['costo']."@".$_SESSION['precio1']."@".$_SESSION['precio6']."@".$_SESSION['itbm']."@".$_SESSION['familia'];

$qur = null;
@mysqli_close($connection);
?>