<?php
/*
JOHNN GUTIERREZ, 
FECHA DE CREACION   2025-03-10
ULTIMA MODIFICASION 2025-03-12
*/
include_once("utiles.php");
//include '../conn_obj/repo_func.php';
date_default_timezone_set("America/Panama");
#error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
error_reporting(E_ALL);
session_start();
if ($_POST) {
    $_SESSION['datab_orig']   = $_POST['datab_orig'];
    $_SESSION['datab_dest']   = $_POST['datab_dest'];
    $_SESSION['Suc_id']       = $_POST['Suc_id'];
    $_SESSION['cte_id']       = $_POST['cte_id'];
    $_SESSION['drop__tables'] = $_POST['drop__tables'];
    $_SESSION['creat_tables'] = $_POST['creat_tables'];
    $_SESSION['delete__data'] = $_POST['delete__data'];
    $_SESSION['Use_OnDuplic'] = $_POST['Use_OnDuplic'];
    $_SESSION['save_log_fle'] = $_POST['save_log_fle'];

    $cDb_Nam  = $_SESSION['datab_orig'];
    $cDb_NamD = $_SESSION['datab_dest'];
    $cCte_id  = $_SESSION['cte_id'];
    $cDropTb  = $_SESSION['drop__tables'];
    $cCreaTb  = $_SESSION['creat_tables'];
    $cDelReg  = $_SESSION['delete__data'];
    $UseOnDup = $_SESSION['Use_OnDuplic'];
    $SaveLog  = $_SESSION['save_log_fle'];

    if (!$oCnn = connectDb()) {
        die("err db");
    } else {
        $oCnn->query("SET NAMES 'utf8';");
    }
   
    $hoynow = getdate();
    $timeStamp = $hoynow["year"] . "-" . $hoynow["mon"] . "-" . $hoynow["mday"];
    $timeStamp = date("Y-m-d H:i:s");

    if ($SaveLog == 1) {
        echo chr(13);
        echo '++++++++++++++++++++++++++++++++++++++++++++++apt_get_files_fullv3.php++++++++++++++++++++++++++++++++++++++++++ ' . chr(13);
        echo 'DB ORIGEN : ' . $cDb_Nam . chr(13);
        echo 'DB DESTIN : ' . $cDb_NamD . chr(13);
        echo 'CTE_ID    : ' . $cCte_id . chr(13);
        echo 'BORRA TAB : ' . $cDropTb . chr(13);
        echo 'CREAR TAB : ' . $cCreaTb . chr(13);
        echo 'BORRA DAT : ' . $cDelReg . chr(13);
        echo 'UPDATE DAT : ' . $UseOnDup . chr(13);
        echo 'SAVE LOGIN: ' . $SaveLog . chr(13);
        echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  ' . chr(13);
        echo chr(13);
    }

    $str_result = '{}';
    $cSql = "SELECT c_id FROM `$cDb_Nam`.`clientes` WHERE (c_id='$cCte_id') LIMIT 1;" . chr(13);
    if ($SaveLog == 1) {
        //echo $cSql;
    }
    $records=0;
    $oQry_Cte = getRow($oCnn, $cSql, $records, false);
    if ($SaveLog == 1) {
        var_dump($oQry_Cte) . chr(13);
    }
    if (($oQry_Cte['c_id'] != '') && ($oQry_Cte['c_id'] != 'null') && ($oQry_Cte['c_id'] != 'NULL' > 0)) {

        $str_result = "";
        if ($cDropTb == '1') {
            $str_result = $str_result . "DROP TABLE IF EXISTS `$cDb_NamD`.`clientes`;" . chr(10);
        }

        if ($cCreaTb == '1') {
            $cSql = "SHOW CREATE TABLE `$cDb_NamD`.`clientes`";
            $records=0;
            $oQry_Crea_Cte = getRow($oCnn, $cSql, $records, false);

            if ($records = 0) {
                $sValue1 = '';
            } else {
                $sValue1 = $oQry_Crea_Ctes['Create Table'];
                $sValue1 = str_replace('`clientes`', '`' . $cDb_NamD . '`.`clientes`', $sValue1);
                $sValue1 = $sValue1 . ';' . chr(10);
            }
            unset($oQry_Crea_Ctes);
        } else {
            $sValue1 = '';
        }
        $str_result = $str_result . $sValue1 . chr(10);

        if ($cDelReg == '1') {
            $str_sql = $str_sql . "DELETE FROM `$cDb_NamD`.`clientes` WHERE (c_id='$cCte_id');" . chr(10);
        }

        $cSql = "SELECT `$cDb_Nam`.`Get_AllFields_ExeptV5_Not_Null`('clientes','id_autoinc','') AS str_fields;" . chr(13);
        if ($SaveLog == 1) {
            //echo $cSql;
        }
        $records=0;
        $result = getRow($oCnn, $cSql, $records, false);
        $cList_Flds = $result['str_fields'];

        $cSql = 'SELECT ' . $cList_Flds . ' FROM `' . $cDb_Nam . '`.`clientes` WHERE (c_id="' . $cCte_id . '");';
        if ($SaveLog == 1) {
            echo $cSql . chr(13);
        }
        $records=0;
        $TablaCtes = getRowAll($oCnn, $cSql, $recount, false);
  
        if ($SaveLog == 1) {
            print_r($TablaCtes);
        }
        $sValue2 = jsonmysql_insert_result_str_v2($TablaCtes, 'clientes', $oCnn, $cDb_Nam, $cDb_NamD, 'id_autoinc', 1, 1, 1000, 0);

        $str_result = $str_result . $sValue2 . chr(10);
        unset($TablaCtes);

    } else
        $str_result = 'null';

    header('content-type: text/plain; charset=UTF-8');

    echo $str_result;
    session_unset();
    session_destroy();
    mysqli_close($oCnn);
}
