<?php

include('include/headscript.php'); ?>

<?php

// pickup the get variable

if (isset($_POST["idtrf"])) {

    $idtrf = $_POST["idtrf"];

}

if (isset($_GET["idtrf"])) {

    $idtrf = $_GET["idtrf"];

}







if (isset($_GET["codestep"])) {

    $code = $_GET["codestep"];



    $InsertQuery = new WA_MySQLi_Query($cmctrfdb);

    $InsertQuery->Action = "insert";

    $InsertQuery->Table = "wheretrfstep";

    $InsertQuery->bindColumn("idtrf", "i", "$idtrf", "WA_DEFAULT");

    $InsertQuery->bindColumn("code", "i", "$code", "WA_DEFAULT");

    $InsertQuery->saveInSession("");

    $InsertQuery->execute();

    $InsertGoTo = "";



    $InsertQuery->redirect($InsertGoTo);

}



?>





<?php

$trfnumberfinal = new WA_MySQLi_RS("trfnumberfinal", $cmctrfdb, 1);

$trfnumberfinal->setQuery("SELECT * FROM `trf-details` WHERE `trf-details`.idtrfdetails='$idtrf'");

$trfnumberfinal->execute();

$idcertn = $trfnumberfinal->getColumnVal("idcertification");

$idarticletype = $trfnumberfinal->getColumnVal("idarticletype");

$otherclient = $trfnumberfinal->getColumnVal("otherclient");

?>

<?php $idcert = $trfnumberfinal->getColumnVal("idcertification") ?>

<?php

$certname = new WA_MySQLi_RS("certname", $cmctrfdb, 1);

$certname->setQuery("SELECT * FROM certificationtype WHERE certificationtype.idcertificationtype='$idcert'");

$certname->execute(); ?>

<?php

$chemicalagentlist = new WA_MySQLi_RS("chemicalagentlist", $cmctrfdb, 0);

$chemicalagentlist->setQuery("SELECT * FROM chemicalagent ORDER BY chemicalagent.name_chemicalagent");

$chemicalagentlist->execute();

?>

<?php

$dpicattwo = 'N'; // Default value is 'N'



$conn = mysqli_connect($servername, $username, $password, $dbname);

// Prepariamo la query SQL per controllare se esiste almeno un record con iddpicategory = 2

$query = "SELECT COUNT(*) as cnt FROM trfstandards WHERE idtrfdetails = ? AND iddpicategory = 2";



// Preparare uno statement SQL

if ($stmt = $conn->prepare($query)) {

    // Legare i parametri per i marcatori

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



    // Esecuzione dello statement

    $stmt->execute();



    // Ottenere i risultati

    $result = $stmt->get_result();

    if ($row = $result->fetch_assoc()) {

        if ($row['cnt'] > 0) {

            $dpicattwo = 'Y';

        }

    }



    // Chiudere lo statement

    $stmt->close();

} else {

    echo "Errore nella preparazione della query: " . $conn->error;

}



?>

<?php

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



// Verifica connessione

if ($conn->connect_error) {

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

}



if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // Recupera i dati dal form

    $idtrf = $_POST['idtrf'];

    $otherclient = $_POST['customRadio'];



    // Prepara e esegui la query di aggiornamento

    $sql = "UPDATE `trf-details` SET otherclient = ? WHERE idtrfdetails = ?";

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

    $stmt->bind_param("si", $otherclient, $idtrf);



    if ($stmt->execute()) {

    } else {

        echo "Errore nell'aggiornamento del record: " . $conn->error;

    }



    // Chiudi lo statement

    $stmt->close();

}



// Chiudi la connessione

$conn->close();

?>



<!DOCTYPE html>

<html lang="en">



<head>

    <meta charset="utf-8" />

    <title><?php echo $titlepage; ?> </title>

    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <meta content="CIMAC TRF Portal" name="description" />

    <meta content="" name="author" />

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />



    <!-- App favicon -->

    <link rel="shortcut icon" href="../images/favicon.ico">



    <!--Form Wizard-->

    <link href="../plugins/jquery-steps/jquery.steps.css" rel="stylesheet" type="text/css">



    <!-- App css -->

    <link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

    <link href="assets/css/jquery-ui.min.css" rel="stylesheet">

    <link href="assets/css/icons.min.css" rel="stylesheet" type="text/css" />

    <link href="assets/css/metisMenu.min.css" rel="stylesheet" type="text/css" />

    <link href="assets/css/app.min.css" rel="stylesheet" type="text/css" />



    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>



    <!-- submit form with button -->

    <style>

        body {

            font-family: arial;

        }



        .hide {

            display: none;

        }



        p {

            font-weight: bold;

        }

    </style>

    <script>

        function formSubmit() {

            document.forms["myForm"].submit();

        }

    </script>

    <script>

        function show1() {

            document.getElementById('div1').style.display = 'none';

        }



        function show2() {

            document.getElementById('div1').style.display = 'block';

        }



        function show3() {

            document.getElementById('div3').style.display = 'none';

        }



        function show4() {

            document.getElementById('div3').style.display = 'block';

        }



        function show5() {

            document.getElementById('div5').style.display = 'none';

        }



        function show6() {

            document.getElementById('div5').style.display = 'block';

        }

    </script>



</head>



<body>



    <!-- Top Bar Start -->

    <?php include('include/topbar.php'); ?>

    <!-- Top Bar End -->





    <!-- Left Sidenav -->

    <?php include('include/leftsidenav.php'); ?>

    <!-- end left-sidenav-->



    <div class="page-wrapper">

        <!-- Page Content-->

        <div class="page-content">



            <div class="container-fluid">

                <!-- Page-Title -->

                <div class="row">

                    <div class="col-sm-12">

                        <div class="page-title-box">

                            <div class="float-right">

                                <ol class="breadcrumb">

                                    <li class="breadcrumb-item"><a href="javascript:void(0);"><?php echo $titlepage; ?></a></li>

                                    <li class="breadcrumb-item active">Starter</li>

                                </ol>

                            </div>

                            <h4 class="page-title"><?php echo $titlewb; ?></h4>

                        </div><!--end page-title-box-->

                    </div><!--end col-->

                </div>

                <div class="row">

                    <div class="col-sm-12">

                        <div class="card">

                            <div class="card-body">

                                <div class="media">



                                    <?php include('include/appform.php'); ?>

                                </div><!--end media-->



                            </div><!--end card-body-->

                        </div><!--end card-->



                        <div class="progress mb-4">

                            <div class="progress-bar" role="progressbar" style="width: 95%;" aria-valuenow="95" aria-valuemin="0" aria-valuemax="100">95%</div>

                        </div>

                        <!-- card for optional TRF -->

                        <div class="card">

                            <div class="card-body">

                                <h4 class="mt-0 header-title"><?php echo $certtotesttitle; ?></h4>

                                <p class="text-muted mb-3"><?php echo $addparameterstitle_help; ?></p>

                                <form id="otherclientForm" action="" method="POST">

                                    <div class="col-md-9">

                                        <div class="my-2">

                                            <div class="custom-control custom-radio">

                                                <input type="radio" id="customRadio3" name="customRadio" class="custom-control-input" value="N" <?php if ($otherclient == 'N') echo 'checked'; ?>>

                                                <label class="custom-control-label" for="customRadio3"><?php echo $formetitle; ?></label>

                                            </div>

                                        </div>

                                        <div class="my-2">

                                            <div class="custom-control custom-radio">

                                                <input type="radio" id="customRadio4" name="customRadio" class="custom-control-input" value="Y" <?php if ($otherclient == 'Y') echo 'checked'; ?>>

                                                <label class="custom-control-label" for="customRadio4"><?php echo $foranotherclienttitle; ?></label>

                                            </div>

                                        </div>

                                        <input type="hidden" id="idtrf" name="idtrf" class="custom-control-input" value='<?php echo $idtrf; ?>'>

                                        <button class="btn btn-gradient-primary" type="submit"><?php echo $nextsteptitle; ?></button>

                                    </div>

                                </form>

                                <script>

                                    // Ottenere il valore di $otherclient da PHP e passarlo a JavaScript

                                    var otherclient = '<?php echo $otherclient; ?>';



                                    // Seleziona il radio button in base al valore della variabile

                                    if (otherclient === 'Y') {

                                        document.getElementById('customRadio4').checked = true;

                                    } else if (otherclient === 'N') {

                                        document.getElementById('customRadio3').checked = true;

                                    }

                                </script>

                            </div>

                        </div>





                        <div class="card">

                            <div class="card-body">

                                <h4 class="mt-0 header-title"><?php echo $addparameterstitle; ?></h4>

                                <p class="text-muted mb-3"><?php echo $addparameterstitle_help; ?></p>









                                <?php if ($idcert != 5) { ?>

                                    <p><?php echo $reportheadertitle; ?></p>

                                    <?php

                                    $kindcont = "headertest";

                                    include('include/insertcont.php'); ?>

                                    <?php

                                    $audticontactdata = new WA_MySQLi_RS("audticontactdata", $cmctrfdb, 1);

                                    $audticontactdata->setQuery("SELECT * FROM contacts LEFT JOIN countries ON contacts.country=countries.idcountries WHERE contacts.idtrf='$idtrf' AND contacts.kindofcontacts='$kindcont'");

                                    $audticontactdata->execute();

                                    ?>

                                    <table class="table table-bordered mb-0 table-centered">



                                        <thead>

                                            <tr>



                                                <th scope="col"><?php echo $anagraficacompany; ?></th>

                                                <th scope="col"><?php echo $anagraficaaddress; ?></th>

                                                <th scope="col"><?php echo $anagraficacity; ?></th>



                                            </tr>

                                        </thead>

                                        <tbody>

                                            <tr>



                                                <td><?php echo ($audticontactdata->getColumnVal("companyname")); ?></td>

                                                <td><?php echo ($audticontactdata->getColumnVal("address")); ?></td>

                                                <td><?php echo ($audticontactdata->getColumnVal("city")); ?></td>



                                            </tr>

                                        </tbody>

                                    </table><br>



                                    <a class="badge_btn" onclick="window.open('contactpop.php?kindcontacts=headertest&idtrf=<?php echo $idtrf; ?>&idcontacts=<?php echo ($audticontactdata->getColumnVal("idcontacts")); ?>', '_blank', 'location=yes,height=500,width=850,scrollbars=yes,status=yes');"><button type="button" class="btn btn-primary waves-effect waves-light"><?php echo $edittitle; ?></button></a>



                                    <br>

                                    <br>





                                <?php } ?>

                                <?php if ($idcert != 5) { ?>

                                    <p><?php echo $certificateheadertitle; ?></p>

                                    <?php

                                    $kindcont = "headercertificate";

                                    include('include/insertcont.php'); ?>

                                    <?php

                                    $audticontactdata = new WA_MySQLi_RS("audticontactdata", $cmctrfdb, 1);

                                    $audticontactdata->setQuery("SELECT * FROM contacts LEFT JOIN countries ON contacts.country=countries.idcountries WHERE contacts.idtrf='$idtrf' AND contacts.kindofcontacts='$kindcont'");

                                    $audticontactdata->execute();

                                    ?>

                                    <table class="table table-bordered mb-0 table-centered">



                                        <thead>

                                            <tr>



                                                <th scope="col"><?php echo $anagraficacompany; ?></th>

                                                <th scope="col"><?php echo $anagraficaaddress; ?></th>

                                                <th scope="col"><?php echo $anagraficacity; ?></th>

                                                <th scope="col">CE</th>

                                            </tr>

                                        </thead>

                                        <tbody>

                                            <tr>



                                                <td><?php echo ($audticontactdata->getColumnVal("companyname")); ?></td>

                                                <td><?php echo ($audticontactdata->getColumnVal("address")); ?></td>

                                                <td><?php echo ($audticontactdata->getColumnVal("city")); ?></td>

                                                <td><?php echo ($audticontactdata->getColumnVal("ce")); ?></td>

                                                <?php $ceinside = $audticontactdata->getColumnVal("ce"); ?>

                                            </tr>

                                        </tbody>

                                    </table><br>

                                    <?php if ($otherclient == 'Y') { ?>

                                        <a class="badge_btn" onclick="window.open('contactpop.php?kindcontacts=headercertificate&idtrf=<?php echo $idtrf; ?>&idcontacts=<?php echo ($audticontactdata->getColumnVal("idcontacts")); ?>', '_blank', 'location=yes,height=500,width=850,scrollbars=yes,status=yes');"><button type="button" class="btn btn-primary waves-effect waves-light"><?php echo $edittitle; ?></button></a>

                                    <?php } ?>

                                    <br>

                                    <br>

                                    <?php if ($ceinside != 'Y') { ?>

                                        <p><?php echo $extracetitleline; ?></p>

                                        <?php

                                        $kindcont = "extrace";

                                        include('include/insertcontempty.php'); ?>

                                        <?php

                                        $audticontactdata = new WA_MySQLi_RS("audticontactdata", $cmctrfdb, 1);

                                        $audticontactdata->setQuery("SELECT * FROM contacts WHERE contacts.idtrf='$idtrf' AND contacts.kindofcontacts='$kindcont'");

                                        $audticontactdata->execute();

                                        ?>

                                        <table class="table table-bordered mb-0 table-centered">



                                            <thead>

                                                <tr>



                                                    <th scope="col"><?php echo $anagraficacompany; ?></th>

                                                    <th scope="col"><?php echo $anagraficaaddress; ?></th>

                                                    <th scope="col"><?php echo $anagraficacity; ?></th>

                                                </tr>

                                            </thead>

                                            <tbody>

                                                <tr>



                                                    <td><?php echo ($audticontactdata->getColumnVal("companyname")); ?></td>

                                                    <td><?php echo ($audticontactdata->getColumnVal("address")); ?></td>

                                                    <td><?php echo ($audticontactdata->getColumnVal("city")); ?></td>

                                                </tr>

                                            </tbody>

                                        </table> <br>



                                        <a class="badge_btn" onclick="window.open('contactpop.php?kindcontacts=extrace&idtrf=<?php echo $idtrf; ?>&idcontacts=<?php echo ($audticontactdata->getColumnVal("idcontacts")); ?>', '_blank', 'location=yes,height=500,width=850,scrollbars=yes,status=yes');"><button type="button" class="btn btn-primary waves-effect waves-light"><?php echo $edittitle; ?></button></a>



                                        <br>

                                        <br>

                                    <?php } ?>



                                <?php } ?>

                                <p><?php echo $invoiceheadertitle; ?></p>

                                <?php



                                $kindcont = "invoice";

                                include('include/insertcont.php'); ?>

                                <?php

                                $audticontactdata = new WA_MySQLi_RS("audticontactdata", $cmctrfdb, 1);

                                $audticontactdata->setQuery("SELECT * FROM contacts LEFT JOIN countries ON contacts.country=countries.idcountries WHERE contacts.idtrf='$idtrf' AND contacts.kindofcontacts='$kindcont'");

                                $audticontactdata->execute();

                                ?>

                                <table class="table table-bordered mb-0 table-centered">



                                    <thead>

                                        <tr>



                                            <th scope="col"><?php echo $anagraficacompany; ?></th>

                                            <th scope="col"><?php echo $anagraficaaddress; ?></th>

                                            <th scope="col"><?php echo $anagraficacity; ?></th>



                                        </tr>

                                    </thead>

                                    <tbody>

                                        <tr>



                                            <td><?php echo ($audticontactdata->getColumnVal("companyname")); ?></td>

                                            <td><?php echo ($audticontactdata->getColumnVal("address")); ?></td>

                                            <td><?php echo ($audticontactdata->getColumnVal("city")); ?></td>



                                        </tr>

                                    </tbody>

                                </table> <br>



                                <a class="badge_btn" onclick="window.open('contactpop.php?kindcontacts=invoice&idtrf=<?php echo $idtrf; ?>&idcontacts=<?php echo ($audticontactdata->getColumnVal("idcontacts")); ?>', '_blank', 'location=yes,height=500,width=850,scrollbars=yes,status=yes');"><button type="button" class="btn btn-primary waves-effect waves-light"><?php echo $edittitle; ?></button></a>



                                <br>

                                <br>





                                <form action="declaration.php" method="post" name="myForm" class="form-parsley">

                                    <div class="form-group">

                                        <label for="reportlanguage"><?php echo $languagereporttitle; ?></label>

                                        <select class="form-control" id="combo" name="reportlanguage" required>



                                            <option><?php echo $italianlang; ?></option>

                                            <option><?php echo $englishlang; ?></option>

                                            <option value="other"><?php echo $otherlang; ?></option>

                                        </select>

                                    </div>







                                    <div class="col-md-4 mb-3">



                                        <input type="text" class="form-control" id="reportlanguageother" name="reportlanguageother" placeholder="<?php echo $otherlangtitle; ?>" disabled>

                                    </div>



                                    <script type="text/javascript">

                                        //<![CDATA[





                                        $(document).on('change', '#combo', function() {

                                                var shouldEnable = $(this).val() !== 'other';

                                                $('#reportlanguageother').prop('disabled', shouldEnable);





                                            }



                                        );







                                        //]]>

                                    </script>





                                    <div class="form-group">

                                        <label for="certificatelanguage"><?php echo $languagecertificatetitle; ?></label>

                                        <select class="form-control" id="combobis" name="certificatelanguage" required>



                                            <option><?php echo $itaenglang; ?></option>

                                            <option value="other"><?php echo $otherlang; ?></option>

                                        </select>

                                    </div>





                                    <div class="col-md-4 mb-3">



                                        <input type="text" class="form-control" id="certificatelanguageother" name="certificatelanguageother" placeholder="<?php echo $otherlangtitle; ?>" disabled>

                                    </div>



                                    <script type="text/javascript">

                                        //<![CDATA[





                                        $(document).on('change', '#combobis', function() {

                                                var shouldEnable = $(this).val() !== 'other';

                                                $('#certificatelanguageother').prop('disabled', shouldEnable);





                                            }



                                        );







                                        //]]>

                                    </script>







                                    <p><?php echo $sendofferanddocument; ?></p>



                                    <div class="form-row"><br>









                                        <div class="col-md-3 mb-3">

                                            <label for="contactsendmail"><?php echo $contactnametitle; ?></label>

                                            <input type="text" class="form-control" id="contactsendmail" name="contactsendmail" value="<?php echo $nameuser; ?>" placeholder="<?php echo $contactnametitle; ?>" required="">



                                        </div>



                                        <div class="col-md-3 mb-3">

                                            <label for="emailsendmail">Email</label>

                                            <div class="input-group">

                                                <div class="input-group-prepend">

                                                    <span class="input-group-text" id="inputGroupPrepend3">@</span>

                                                </div>

                                                <input type="text" class="form-control" id="emailsendmail" name="emailsendmail" placeholder="Email" value="<?php echo $emailuser; ?>" aria-describedby="inputGroupPrepend3" required="">



                                            </div>

                                        </div>

                                    </div>

                                    <p><?php echo $certukca; ?></p>







                                    <div class="custom-control custom-checkbox">

                                        <input type="checkbox" class="custom-control-input" id="ukcacert" name="ukcacert" data-parsley-multiple="groups" value="Y" data-parsley-mincheck="2">

                                        <label class="custom-control-label" for="ukcacert"><?php echo $certukca; ?>

                                        </label>

                                    </div><br>

                                    <?php if ($dpicattwo != 'Y') { ?>

                                        <?php if ($idcert != 5) { ?>

                                            <p><?php echo $surveillancetitle; ?></p>



                                            <div class="col-md-3 mb-3">

                                                <select class="form-control" id="surveillanceselectoption" name="surveillanceselectoption">

                                                    <option>No</option>

                                                    <option><?php echo $modulec2; ?></option>

                                                    <option><?php echo $moduled; ?></option>





                                                </select>

                                            </div>

                                        <?php } ?>

                                    <?php } ?>

                                    <br>

                                    <input type="hidden" name="idtrf" id="idtrf" value="<?php echo ($trfnumberfinal->getColumnVal("idtrfdetails")); ?>">

                                    <input type="hidden" name="optionform" id="optionform" value="OK">

                                    <input type="button" onclick="formSubmit()" class="btn btn-gradient-success waves-effect waves-light" value="<?php echo $nextsteptitle; ?>">



                                </form>

                            </div><!--end card-body-->









                        </div><!--end card-->









                    </div><!--end col-->

                </div>

                <!-- end page title end breadcrumb -->





            </div><!-- container -->

            <!-- footer start -->

            <?php include('include/footer.php'); ?>

            </footer><!--end footer-->

        </div>

        <!-- end page content -->

    </div>

    <!-- end page-wrapper -->









    <!-- jQuery  -->

    <script src="assets/js/jquery.min.js"></script>

    <script src="assets/js/bootstrap.bundle.min.js"></script>

    <script src="assets/js/metismenu.min.js"></script>

    <script src="assets/js/waves.js"></script>

    <script src="assets/js/feather.min.js"></script>

    <script src="assets/js/jquery.slimscroll.min.js"></script>

    <script src="assets/js/jquery-ui.min.js"></script>



    <script src="../plugins/jquery-steps/jquery.steps.min.js"></script>

    <script src="assets/pages/jquery.form-wizard.init.js"></script>



    <!-- App js -->

    <script src="assets/js/app.js"></script>



</body>



</html>