<?php
include 'conn.php';
//Select data from database
error_reporting( E_ERROR  | E_PARSE );

include 'funciones.php';
date_default_timezone_set('America/Panama');

session_start();
$_SESSION['database'] = $_POST['dbname'];
$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['est_id'] = $_POST['est_id'];

$_SESSION['pack'] = $_POST['pack'];
$_SESSION['ip'] = $_POST['ip'];

$_SESSION["newsession"] = $_POST['dbname'];

$info = $_SESSION["newsession"];
/*session created*/
//echo $_SESSION["newsession"];

$hoynow = getdate();

$hoynow = getdate();

$timeStamp = $hoynow["year"] . "-" . $hoynow["mon"] . "-" . $hoynow["mday"] . " " . $hoynow["hours"] . ":" . $hoynow["minutes"] . ":" . $hoynow["seconds"];

$timeStamp2 = $hoynow["year"] . "-" . $hoynow["mon"] . "-01 00:00:01";
//echo $timeStamp;

//echo "monor" .$_SESSION['monto']e;
if ($_SESSION['est_id'] == "0") {
    $SQLupdate = "update " . $_SESSION['database'] . ".sucursales set suc_ult_con=now(),suc_pack_r_clie=" . $_SESSION['pack'] . " where  suc_id='" . $_SESSION['suc_id'] . "' limit 1 ";

} else {
    $SQLupdate = "update " . $_SESSION['database'] . ".estaciones set e_ult_con=now(),e_pack_r_clie=" . $_SESSION['pack'] . "  where e_id='" . $_SESSION['est_id'] . "' and suc_id='" . $_SESSION['suc_id'] . "' limit 1 ";

}
//echo $SQLupdate;

logs($SQLupdate);

if ($connection->query($SQLupdate) === true) {
    //  echo "update op";
} else {
    echo "Error: " . $SQLupdate . "<br>" . $conn->error;
}

// $timeStamp= $hoynow["year"]."-".$hoynow["mon"]."-".$hoynow["mday"]." ".$hoynow["hours"] .":".$hoynow["minutes"].":".$hoynow["seconds"] ;

//$timeStamp2= $hoynow["year"]."-".$hoynow["mon"]."-01 00:00:01";
//echo $timeStamp;

if ($_SESSION['est_id'] == "0") {

    $getData = "select suc_ftp_rclie as suc_ftp_rclie FROM " . $_SESSION['database'] . ".sucursales where suc_id='" . $_SESSION['suc_id'] . "' ";
} else {

    $getData = "select e_ftp_rclie as suc_ftp_rclie  FROM " . $_SESSION['database'] . ".estaciones where e_id='" . $_SESSION['est_id'] . "'  and suc_id='" . $_SESSION['suc_id'] . "' ";

}

logs($getData);

//$getData = "select * from user";
$qur = $connection->query($getData);

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

    $msg = array("corre" => $r['suc_ftp_rclie'], "clientes" => $r['suc_ftp_rclie'], "info" => $info);
}
$json = $msg;

// $json = (object)$msg;

header('content-type: application/json');
echo json_encode($json);

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