<?php

error_reporting( E_ERROR  | E_PARSE );

session_start();
/*
$_SESSION['database'] = $_GET['database'];

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

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

$_SESSION['id_pack'] = $_GET['id_pack'];
 */

$_SESSION['database'] = $_POST['database'];
$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['est_id'] = $_POST['est_id'];
$_SESSION['id_pack'] = $_POST['id_pack'];

include 'conn.php';
include 'funciones.php';

//logs( 'xxxxxxxxxxxxxxxxxx' );

date_default_timezone_set('America/Panama');

$hoynow = getdate();

//echo print_r( $hoynow );

//echo date_default_timezone_get();

$hora = str_pad($hoynow['hours'], 2, '0', STR_PAD_LEFT);

$minutos = str_pad($hoynow['minutes'], 2, '0', STR_PAD_LEFT);

$mes = str_pad($hoynow['mon'], 2, '0', STR_PAD_LEFT);

$dia = str_pad($hoynow['mday'], 2, '0', STR_PAD_LEFT);

$timeStamp = $hoynow['year'] . '-' . $mes . '-' . $dia . ' ' . $hora . ':' . $minutos . ':' . $hoynow['seconds'];
if ($_SESSION['est_id'] == '0') {
    $SQLupdate = 'update ' . $_SESSION['database'] . ".sucursales set suc_ult_con='" . $timeStamp . "',suc_pack_imagen=" . $_SESSION['id_pack'] . "  where  suc_id='" . $_SESSION['suc_id'] . "' limit 1 ";
} else {
    $SQLupdate = 'update ' . $_SESSION['database'] . ".estaciones set e_ult_con='" . $timeStamp . "',e_pack_imagen=" . $_SESSION['id_pack'] . "  where suc_id='" . $_SESSION['suc_id'] . "'  and e_id='" . $_SESSION['est_id'] . "' limit 1 ";
}

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

$montoesql1 = " select id_pack,b.path,c.id_sucursal,c.estado,b.name from " . $_SESSION['database'] . "_tmp_pack.image_visor_pack a

left outer join " . $_SESSION['database'] . ".images_sucursal b on b.images_id= a.id_imagen

left outer join " . $_SESSION['database'] . ".sucursal_imagen c on c.id_imagen= a.id_imagen

where  id_pack='" . $_SESSION['id_pack'] . "'  ";

$qur1 = $connection->query($montoesql1);

$i = 0;

while ($r1 = mysqli_fetch_assoc($qur1)) {

    $jsonTempData = array();
    $jsonTempData['campo1'] = utf8_encode($r1['id_sucursal']);
    $jsonTempData['campo2'] = utf8_encode($r1['estado']);
    $jsonTempData['campo3'] = utf8_encode($r1['path']);
    $jsonTempData['campo4'] = utf8_encode($r1['id_pack']);
    $jsonTempData['campo5'] = utf8_encode($r1['name']);

    $jsonData[] = $jsonTempData;

    $i = $i + 1;
}

$json->registros = $i;

$json->imagen_visor = $jsonData;

if (1 == 1) {

    $montoesql3 = 'select  ' . $_SESSION['database'] . '.images_sucursal.images_id  from ' . $_SESSION['database'] . '.images_sucursal where usermodify=1  limit 1';

    //echo  $montoesql;
    logs($montoesql3);

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

    $_SESSION['images_id'] = 'NO';

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

        $_SESSION['images_id'] = $r['images_id'];

    }

    logs('listo prommmmm' . $_SESSION['images_id']);

    if ($_SESSION['images_id'] != 'NO') {

        $SQLupdate = 'UPDATE   ' . $_SESSION['database'] . '.images_sucursal SET syncprocesing=1,usermodify=0 WHERE  usermodify=1  limit 1';

        //echo $SQLupdate;

        logs('uuuuuuuuuuuuuuuuuuuuuuuuuuu' . $SQLupdate);

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

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

            $getData = 'select suc_corre_eimagen+1 as corre_e,suc_id FROM ' . $_SESSION['database'] . '.sucursales  where   suc_id=1';
        } else {

            $getData = 'select e_corre_eimagen+1 as corre_e,e_id FROM ' . $_SESSION['database'] . '.estaciones  where   e_id=1 limit 1';
        }

        logs('ccccccccccccccccccccccccccccccc' . $getData);

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

        $_SESSION['corre'] = 0;

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

            $_SESSION['corre'] = $r['corre_e'];
        }

        if ($_SESSION['corre'] > 0) {

            $SQLupdate2 = "  insert into  " . $_SESSION['database'] . "_tmp_pack.image_visor_pack  (id_pack,id_imagen) select   " . $_SESSION['corre'] . "  as id,
            images_sucursal.images_id


				  from
				 " . $_SESSION['database'] . ".images_sucursal where images_sucursal.syncprocesing=1 limit 1 ";

            logs($SQLupdate2);

            if ($connection->query($SQLupdate2) === true) {

                $SQLupdate3 = 'select id_pack from ' . $_SESSION['database'] . '_tmp_pack.image_visor_pack where  id_pack=' . $_SESSION['corre'] . ' limit 1';

                logs('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' . $SQLupdate3);

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

                $empaco = 'NO';
                while ($r = mysqli_fetch_assoc($qur)) {

                    $empaco = $r['id_pack'];

                    logs('si consiguio un registro  ' . $empaco);

                }

                if ($_SESSION['corre'] == $empaco) {

                    logs('   empaco' . $empaco . ' corre  ' . $_SESSION['corre']);

                    $SQLupdate4 = 'UPDATE ' . $_SESSION['database'] . '.images_sucursal SET syncprocesing=0,usermodify=0 WHERE   syncprocesing=1 ';

                    logs('   update prom xxxxxxxxxxxxxxxxxxxxxx ' . $SQLupdate4);

                    if ($connection->query($SQLupdate4) === true) {

                        $sqlmax = 'select max(id_pack) as id_pack from ' . $_SESSION['database'] . '_tmp_pack.image_visor_pack limit 1';

                        $qur12 = $connection->query($sqlmax);

                        $_SESSION['empaco'] = 0;

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

                            $_SESSION['empaco'] = $r['id_pack'];
                        }

                        if ($_SESSION['est_id'] == '0') {
                            $connection->query('update ' . $_SESSION['database'] . '.sucursales set suc_corre_eimagen=' . $_SESSION['empaco'] . '');

                            logs('xxxxxxxxxxxxxxxxxxxxxxxxxxxxx update ' . $_SESSION['database'] . '.sucursales set suc_corre_eimagen=' . $_SESSION['empaco'] . '');

                        } else {

                            $connection->query('update ' . $_SESSION['database'] . '.estaciones set e_corre_eimagen=' . $_SESSION['empaco'] . '');

                            logs('update ' . $_SESSION['database'] . '.estaciones set e_corre_eimagen=' . $_SESSION['empaco'] . '');

                        }

                    } else {
                        echo 'Error: ' . $SQLupdate4 . '<br>' . $conn->error;
                    }

                } else {
                    echo 'Errorde empaque: ' . $_SESSION['corre'] . ' ' . $empaco . '<br>';
                }

            }

        } else {

            echo 'error en busca de corre';

        }

    }

} else {

    logs('Esta ocupado por la estacionnnnnnnn ' . $_SESSION['est_id'] . ' Bandera ' . $_SESSION['flags']);

}

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

//empacando( $_SESSION['database'] );

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