<?php

require_once('../Connections/cmctrfdb.php');

require_once('../webassist/mysqli/rsobj.php');



include('include/headscript.php');

include('languages/' . $_SESSION['langselect'] . '/tdgen.php');



// Recupero delle variabili GET e assegnazione a variabili PHP

$idtrf = isset($_POST['idtrfdetails']) ? $_POST['idtrfdetails'] : null;

$idtd = isset($_POST['idtd']) ? $_POST['idtd'] : null;



if (!$idtrf || !$idtd) {

    die('Errore: le variabili idtrf e idtd sono necessarie.');

}



// recupera info td

$conn = new mysqli($servername, $username, $password, $dbname);



// Verifica la connessione

if ($conn->connect_error) {

    die("Connessione fallita: " . $conn->connect_error);

}



$sql = "SELECT * FROM data_td WHERE iddata_td = ?";

$stmt = $conn->prepare($sql);

$stmt->bind_param("i", $idtd);

$stmt->execute();

$result = $stmt->get_result();

$row = $result->fetch_assoc();



$sql_trfcheck = "SELECT trfnumber FROM `trf-details` WHERE idtrfdetails = ?";

$stmt_trfcheck = $conn->prepare($sql_trfcheck);

$stmt_trfcheck->bind_param("i", $idtrf);

$stmt_trfcheck->execute();

$result_trfcheck = $stmt_trfcheck->get_result();

$row_trfcheck = $result_trfcheck->fetch_assoc();

$newtdnumber = $row_trfcheck['trfnumber'] . 'TF';



if ($row) {

    // Assegna il valore della nuova revisione

    $tdrev = "0";

    $newrev = "0";

    $iduserlogin = 1; // Assumendo che $iduserlogin sia definito



    // clone data_td

    // Costruisci la query di inserimento dinamico

    $columns = array();

    $values = array();

    $placeholders = array();

    foreach ($row as $column => $value) {

        if ($column == 'iddata_td') {

            continue; // Salta la colonna iddata_td

        } elseif ($column == 'td_rev') {

            $value = $newrev;

        } elseif ($column == 'idtrf') {

            $value = $idtrf;

        } elseif ($column == 'tdnumber') {

            $value = $newtdnumber;

        } elseif ($column == 'signedontd' || $column == 'pdffilenametd' || $column == 'zipname' || $column == 'signnametd') {

            $value = NULL;

        } elseif ($column == 'statustd') {

            $value = 'Draft';

        } elseif ($column == 'iduser') {

            $value = $iduserlogin;

        }



        $columns[] = $column;

        $values[] = $value;

        $placeholders[] = '?';

    }



    $columns_list = implode(", ", $columns);

    $placeholders_list = implode(", ", $placeholders);



    $sql_insert = "INSERT INTO data_td ($columns_list) VALUES ($placeholders_list)";

    $stmt_insert = $conn->prepare($sql_insert);



    // Bind dei parametri

    $types = str_repeat("s", count($values)); // Assumendo che tutti i campi siano stringhe; regolare i tipi se necessario

    $stmt_insert->bind_param($types, ...$values);



    if ($stmt_insert->execute()) {

        $new_iddata_td = $conn->insert_id;

    } else {

        echo "Errore nella duplicazione del record: " . $stmt_insert->error;

    }



    $stmt_insert->close();

} else {

    echo "Nessun record trovato con iddata_td = $idtd";

}



$sql_contacts = "SELECT * FROM contacts_td WHERE idtd = ?";

$stmt_contacts = $conn->prepare($sql_contacts);

$stmt_contacts->bind_param("i", $idtd);

$stmt_contacts->execute();

$result_contacts = $stmt_contacts->get_result();



while ($row_contact = $result_contacts->fetch_assoc()) {

    $columns = array();

    $values = array();

    $placeholders = array();



    foreach ($row_contact as $column => $value) {

        if ($column == 'idcontactstd') {

            continue; // Salta la colonna idcontactstd

        } elseif ($column == 'idtd') {

            $value = $new_iddata_td;

        }



        $columns[] = $column;

        $values[] = $value;

        $placeholders[] = '?';

    }



    $columns_list = implode(", ", $columns);

    $placeholders_list = implode(", ", $placeholders);



    $sql_insert_contacts = "INSERT INTO contacts_td ($columns_list) VALUES ($placeholders_list)";

    $stmt_insert_contacts = $conn->prepare($sql_insert_contacts);



    // Bind dei parametri

    $types_contacts = str_repeat("s", count($values)); // Assumendo che tutti i campi siano stringhe; regolare i tipi se necessario

    $stmt_insert_contacts->bind_param($types_contacts, ...$values);



    if ($stmt_insert_contacts->execute()) {

        echo "Record contact duplicato con successo. ";

    } else {

        echo "Errore nella duplicazione del record contact: " . $stmt_insert_contacts->error;

    }



    $stmt_insert_contacts->close();

}



$stmt_contacts->close();



// ora tdfileattached



$sql_tdfileattached = "SELECT * FROM tdfileattached WHERE iddata_td = ?";

$stmt_tdfileattached = $conn->prepare($sql_tdfileattached);

$stmt_tdfileattached->bind_param("i", $idtd);

$stmt_tdfileattached->execute();

$result_tdfileattached = $stmt_tdfileattached->get_result();



while ($row_tdfileattached = $result_tdfileattached->fetch_assoc()) {

    $columns = array();

    $values = array();

    $placeholders = array();



    foreach ($row_tdfileattached as $column => $value) {

        if ($column == 'idtdfileattached') {

            continue; // Salta la colonna idtdfileattached

        } elseif ($column == 'iddata_td') {

            $value = $new_iddata_td;

        }



        $columns[] = $column;

        $values[] = $value;

        $placeholders[] = '?';

    }



    $columns_list = implode(", ", $columns);

    $placeholders_list = implode(", ", $placeholders);



    $sql_insert_tdfileattached = "INSERT INTO tdfileattached ($columns_list) VALUES ($placeholders_list)";

    $stmt_insert_tdfileattached = $conn->prepare($sql_insert_tdfileattached);



    // Bind dei parametri

    $types_tdfileattached = str_repeat("s", count($values)); // Assumendo che tutti i campi siano stringhe; regolare i tipi se necessario

    $stmt_insert_tdfileattached->bind_param($types_tdfileattached, ...$values);



    if ($stmt_insert_tdfileattached->execute()) {

        echo "Record tdfileattached duplicato con successo. ";

    } else {

        echo "Errore nella duplicazione del record tdfileattached: " . $stmt_insert_tdfileattached->error;

    }



    $stmt_insert_tdfileattached->close();

}



$stmt_tdfileattached->close();



//fillrisk_td

$sql_fillrisk_td = "SELECT * FROM fillrisk_td WHERE iddata_td = ?";

$stmt_fillrisk_td = $conn->prepare($sql_fillrisk_td);

$stmt_fillrisk_td->bind_param("i", $idtd);

$stmt_fillrisk_td->execute();

$result_fillrisk_td = $stmt_fillrisk_td->get_result();



while ($row_fillrisk_td = $result_fillrisk_td->fetch_assoc()) {

    $columns = array();

    $values = array();

    $placeholders = array();



    foreach ($row_fillrisk_td as $column => $value) {

        if ($column == 'idfillrisk_td') {

            continue; // Salta la colonna idfillrisk_td

        } elseif ($column == 'iddata_td') {

            $value = $new_iddata_td;

        }



        $columns[] = $column;

        $values[] = $value;

        $placeholders[] = '?';

    }



    $columns_list = implode(", ", $columns);

    $placeholders_list = implode(", ", $placeholders);



    $sql_insert_fillrisk_td = "INSERT INTO fillrisk_td ($columns_list) VALUES ($placeholders_list)";

    $stmt_insert_fillrisk_td = $conn->prepare($sql_insert_fillrisk_td);



    // Bind dei parametri

    $types_fillrisk_td = str_repeat("s", count($values)); // Assumendo che tutti i campi siano stringhe; regolare i tipi se necessario

    $stmt_insert_fillrisk_td->bind_param($types_fillrisk_td, ...$values);



    if ($stmt_insert_fillrisk_td->execute()) {

        echo "Record fillrisk_td duplicato con successo. ";

    } else {

        echo "Errore nella duplicazione del record fillrisk_td: " . $stmt_insert_fillrisk_td->error;

    }



    $stmt_insert_fillrisk_td->close();

}



$stmt_fillrisk_td->close();



header("Location: tdclonestart.php?new_iddata_td=$new_iddata_td&idtrf=$idtrf&newrev=$newrev");

exit();



$stmt->close();

$conn->close();

