<?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["w_id"] = $_POST["w_id"];
$_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"];
$cw__id = $_SESSION["w_id"];
$cCreaTb = $_SESSION["crea_tables"];
$cDropTb = $_SESSION["drop_tables"];
$cDeleTb = $_SESSION["dele_tables"];
$SaveLog = $_SESSION["swsavelog"];
$cTableN = "wind_pos";

/*
  $cDb_Ori = 'facilmatriz';
  $cDb_Des = 'ocadb';
  $cSuc_id = 1;
  $cw__id = '201';
  $cCreaTb = 0;
  $cDropTb = 0;
  $cDeleTb = 1;
  $SaveLog = 1;
*/

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';
}

$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('Ventana : ' . $cw___id);
    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 . '`.`wind_pos` WHERE  (w_suc_id="' . $cSuc_id . '") AND (w_id="' . $cw__id . '");' . chr(10);
else {

    if ($cDropTb == '1') {
        $str_sql = $str_sql . 'DROP TABLE IF EXISTS `' . $cDb_Des . '`.`wind_pos`;' . 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 . '`.`wind_pos`', $sValue1);
            $sValue1 = $sValue1 . ';' . chr(10);
        }
    } else {
        $sValue1 = '';
    }
}

$str_sql = $str_sql . $sValue1;

$recount = 0;
$cSql = 'SELECT * FROM `' . $cDb_Ori . '`.`wind_pos` WHERE (w_suc_id="' . $cSuc_id . '") AND (w_id="' . $cw__id . '") ';

$TablaWp = getRowAll($connection, $cSql, $recount, false);


$Excl_Field = 'autoin';
$sValue2 = jsonmysql_insert_result_str($TablaWp, $recount, 'wind_pos', $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);
?>