<?php

error_reporting(0); 
require_once("db.php");

$idprod = addslashes($_GET['idprod']);
$dispversion = addslashes($_GET['dispversion']);
$acum1med = addslashes($_GET['acum1']);
$acum2med = addslashes($_GET['acum2']);
$acum3med = addslashes($_GET['acum3']);
$acum4med = addslashes($_GET['acum4']);
$creditos = "SC";
$servidor = "OFF";
$lastversion = 0;
$destrava = 0;

date_default_timezone_set('America/Sao_Paulo');
$agora = date ('Y-m-d H:i:s');
mysql_query ("UPDATE tablet SET ultimo_contato = '$agora' WHERE idprod = '$idprod'");

// Botao de Panico
$queryp = "SELECT ligado, destrava, lastversion FROM tablet WHERE ligado = 1 AND idprod = '".$idprod."' ";
$resultp = mysql_query($queryp) or die(mysql_error());
$row = mysql_fetch_array($resultp);

if(mysql_num_rows($resultp) == 0){
    $servidor = "OFF";
} else{
    $servidor = "ON";
    $lastversion = $row['lastversion'];
	$destrava = $row['destrava'];
}

if ($lastversion == $dispversion){
    $sql = mysql_query("UPDATE tablet SET acum1med =  '$acum1med' ,acum2med =  '$acum2med' ,acum3med =  '$acum3med',acum4med =  '$acum4med' WHERE idprod = '$idprod'") or die(mysql_error());
}

// Creditos
$query = "SELECT id, status, valor FROM creditos WHERE idprod='".$idprod."' ORDER BY data DESC LIMIT 1";
$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_array($result);


if(mysql_num_rows($result) == 0 || $row['status'] == 0){
    $creditos = "SC";
}else {
	$creditos = $row['valor'];
	$query = "UPDATE creditos SET status='0' WHERE idprod='".$idprod."'";
	mysql_query($query) or die(mysql_error());
}

        echo $creditos."#"
            .$servidor."#"
            .$lastversion."#"
			.$destrava."#";
?>
