<?php
include_once("utiles.php");
//include_once("apt_funciones.php");

date_default_timezone_set("America/Panama");
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);

if (!$connection = connectDb()) {
    die("err db");
} else {
    $connection->query("SET NAMES 'utf8';");
}

session_start();

$_SESSION['datab_orig'] = $_POST['datab_orig'];
$_SESSION['datab_dest'] = $_POST['datab_dest'];
$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['table_name'] = $_POST['table_name'];
$_SESSION['selec_where'] = $_POST['selec_where'];
$_SESSION['crea_tables'] = $_POST['crea_tables'];
$_SESSION['drop_tables'] = $_POST['drop_tables'];
$_SESSION['dele_tables'] = $_POST['dele_tables'];
$_SESSION['swsavelog'] = $_POST['swsavelog'];

$cDb_Ori = $_SESSION['datab_orig'];
$cDb_Des = $_SESSION['datab_dest'];
$cSuc_id = $_SESSION['suc_id'];
$cTableN = $_SESSION['table_name'];
$cSelWhe = $_SESSION['selec_where'];
$cCreaTb = $_SESSION['crea_tables'];
$cDropTb = $_SESSION['drop_tables'];
$cDeleTb = $_SESSION['dele_tables'];
$SaveLog = $_SESSION['swsavelog'];

$cDb_Ori = trim($cDb_Ori);
$cDb_Des = trim($cDb_Des);
$cTableN = trim($cTableN);

$cDb_Ori = str_replace("`", "", $cDb_Ori);
$cDb_Des = str_replace("`", "", $cDb_Des);
$cTableN = str_replace("`", "", $cTableN);

if (($cCreaTb == '') or ($cCreaTb == 'NULL') or ($cCreaTb == 'null')) {
    $cCreaTb = '0';
}

if (($cDropTb == '') or ($cDropTb == 'NULL') or ($cDropTb == 'null')) {
    $cDropTb = '0';
}

if (($cDeleTb == '') or ($cDeleTb == 'NULL') or ($cDeleTb == 'null')) {
    $cDeleTb = '0';
}

if (($SaveLog == '') or ($SaveLog == 'NULL') or ($SaveLog == 'null')) {
    $SaveLog = '0';
}

if (($cSelWhe == 'NULL') or ($cSelWhe == 'null')) {
    $cSelWhe = '1=1';
}

$hoynow = getdate();
$timeStamp = $hoynow["year"] . "-" . $hoynow["mon"] . "-" . $hoynow["mday"];
$timeStamp = date("Y-m-d H:i:s");

if ($SaveLog == 1) {
    logs2('', false);
    logs2('+++++++++++++++++++++++++++++++++++++++++++++++++gen_get_windpos.php++++++++++++++++++++++++++++++++++++++++++');
    logs2('Origen  : ' . $cDb_Ori);
    logs2('Destino : ' . $cDb_Des);
    logs2('Tabla   : ' . $cTableN);
    logs2('CreaTabl: ' . $cCreaTb);
    logs2('BorrTabl: ' . $cDropTb);
    logs2('BorrData: ' . $cDeleTb);
    logs2('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
    logs2('', false);
}

$str_sql = '';

if ($cDeleTb == '1')
    $str_sql = $str_sql . 'DELETE FROM `' . $cDb_Des . '`.`' . $cTableN . '` WHERE ' . $cSelWhe . ';' . chr(10);
else {

    if ($cDropTb == '1') {
        $str_sql = $str_sql . 'DROP TABLE IF EXISTS `' . $cDb_Des . '`.`' . $cTableN . '`;' . chr(10);
    }

    if ($cCreaTb == '1') {
        $cSql = 'SHOW CREATE TABLE `' . $cDb_Des . '`.`' . $cTableN . '`;';
        $data = getRow($connection, $cSql, $records, false);

        if ($records = 0) {
            $sValue1 = '';
        } else {
            $sValue1 = $data['Create Table'];
            $sValue1 = str_replace('`' . $cTableN . '`', '`' . $cDb_Des . '`.`' . $cTableN . '`', $sValue1);
            $sValue1 = $sValue1 . ';' . chr(10);
        }
    } else {
        $sValue1 = '';
    }
}
$str_sql = $str_sql . $sValue1;

$cSql = 'SELECT * FROM `' . $cDb_Ori . '`.`' . $cTableN . '` WHERE ' . $cSelWhe . ';';
$TablaWp = getRowAll($connection, $cSql, $recount, false);
// print_r( $TablaOp);

$Excl_Field = 'autoin';
$sValue2 = jsonmysql_insert_result_str($TablaWp, $recount, $cTableN, $connection, $cDb_Ori, $cDb_Des, $SaveLog, $Excl_Field);
$str_sql = $str_sql . $sValue2;
unset($TablaWp);

if ($SaveLog == 1) {
    logs2('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
    logs2('SQL : ' . $str_sql);
    logs2('++++++++++++++++++++++++++++++++++++++++++++++insert_json_op_gen.php++++++++++++++++++++++++++++++++++++++++++');
    logs2('', false);
}

header('content-type: application/json; charset=UTF-8');
echo $str_sql;

@mysqli_close($connection);
?>
