<?php require_once('../Connections/cmctrfdb.php'); ?>

<?php require_once('../webassist/mysqli/rsobj.php'); ?>

<?php



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

<?php

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

$archivetrflist->setQuery("SELECT * FROM `trf-details` 

LEFT JOIN auth_users ON `trf-details`.iduser=auth_users.id 

LEFT JOIN article_type ON `trf-details`.idarticletype=article_type.idarticletype 

LEFT JOIN certificationtype ON certificationtype.idcertificationtype=`trf-details`.idcertification 

WHERE `trf-details`.idcompany='$idcompany'  

AND `trf-details`.signedon <> '' 

ORDER BY `trf-details`.trfnumber, `trf-details`.revtrf DESC");

$archivetrflist->execute();



?>







<?php





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

$drafttrf->setQuery("SELECT * FROM `trf-details` 

LEFT JOIN article_type ON `trf-details`.idarticletype=article_type.idarticletype 

LEFT JOIN certificationtype ON certificationtype.idcertificationtype=`trf-details`.idcertification 

WHERE `trf-details`.idcompany='$idcompany' 

AND `trf-details`.signedon ='' 

AND `trf-details`.revcs != 'Y' 

ORDER BY `trf-details`.trfnumber");

$drafttrf->execute();

?>

<!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">



    <!-- DataTables -->



    <link rel="shortcut icon" type="image/png" href="/media/images/favicon.png">

    <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="http://www.datatables.net/rss.xml">

    <link rel="stylesheet" type="text/css" href="/media/css/site-examples.css?_=8f7cff5ee7757412879aedf3efbfaee01">

    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.1/css/jquery.dataTables.min.css">

    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/2.3.2/css/buttons.dataTables.min.css">

    <style type="text/css" class="init">



    </style>

    <script type="text/javascript" src="/media/js/site.js?_=1d5abd169416a09a2b389885211721dd" data-domain="datatables.net" data-api="https://plausible.sprymedia.co.uk/api/event"></script>

    <script src="https://media.ethicalads.io/media/client/ethicalads.min.js"></script>

    <script type="text/javascript" src="/media/js/dynamic.php?comments-page=extensions%2Fbuttons%2Fexamples%2Finitialisation%2Fexport.html" async></script>

    <script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>

    <script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.13.1/js/jquery.dataTables.min.js"></script>

    <script type="text/javascript" language="javascript" src="https://cdn.datatables.net/buttons/2.3.2/js/dataTables.buttons.min.js"></script>

    <script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>

    <script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>

    <script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>

    <script type="text/javascript" language="javascript" src="https://cdn.datatables.net/buttons/2.3.2/js/buttons.html5.min.js"></script>

    <script type="text/javascript" language="javascript" src="https://cdn.datatables.net/buttons/2.3.2/js/buttons.print.min.js"></script>

    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <!-- Font Awesome -->

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-RqEzHvnvS1k5K5wzgp5yoWY5U6TD5EoXyj9iikETmdcy1G6dbCVa+ZmzBm7VWzmj8Ov7VwtA9x9X7VWjG8SRFg==" crossorigin="anonymous" referrerpolicy="no-referrer" />

    <!--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" />





    <!-- submit form with button -->

    <script>

        function formSubmit() {

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

        }

    </script>

    </script>

    <script type="text/javascript" class="init">

        $(document).ready(function() {

            var table = $('#example').DataTable({

                pageLength: 20,

                order: [

                    [0, 'desc']

                ],



                dom: 'Bfrtip',

                buttons: [

                    'copy', 'csv', 'excel', 'pdf'

                ]





            });



            $('a.toggle-vis').on('click', function(e) {

                e.preventDefault();



                // Get the column API object

                var column = table.column($(this).attr('data-column'));



                // Toggle the visibility

                column.visible(!column.visible());

            });



        });

    </script>



    <script type="text/javascript" class="init">

        $(document).ready(function() {

            var table = $('#readytrf').DataTable({

                pageLength: 20,

                order: [

                    [0, 'desc']

                ],



                dom: 'Bfrtip',

                buttons: [

                    'copy', 'csv', 'excel', 'pdf'

                ]





            });



            $('a.toggle-vis').on('click', function(e) {

                e.preventDefault();



                // Get the column API object

                var column = table.column($(this).attr('data-column'));



                // Toggle the visibility

                column.visible(!column.visible());

            });



        });

    </script>

    <script>

        document.getElementById('clonetrfalert').addEventListener('click', function(e) {

            e.preventDefault();

            if (confirm("Sei sicuro di voler andare al link clonetrf.php?")) {

                window.location.href = e.target.parentNode.href;

            }

        });

    </script>

    <style>

        .btn-active {

            border-color: #dc3545;

            color: #dc3545;

        }



        .btn-inactive {

            border-color: #6c757d;

            color: #6c757d;

        }



        .btn-inactive:disabled {

            background-color: #e9ecef;

        }

    </style>



</head>



<body>



    <!-- Top Bar Start -->







    <!-- 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);">TRF</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">







                        <!-- DRAFT TRF -->



                        <div class="card">





                            <!-- card for show requirements -->

                            <div class="card">

                                <div class="card-body">

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



                                    <table id="example" class="display table table-striped table-bordered dt-responsive nowrap" style="border-collapse: collapse; border-spacing: 0; width: 100%;">

                                        <thead>

                                            <tr>

                                                <th>TRF N.</th>

                                                <th>REV</th>

                                                <th>Description</th>

                                                <th>Cert Type</th>

                                                <th>Article type</th>

                                                <th>To be Sign</th>

                                                <th><?php echo $action; ?></th>

                                                <th></th>

                                            </tr>

                                        </thead>





                                        <tbody>

                                            <?php

                                            $wa_startindex = 0;

                                            while (!$drafttrf->atEnd()) {

                                                $wa_startindex = $drafttrf->Index;

                                            ?>

                                                <tr><?php $idtrf = $drafttrf->getColumnVal("idtrfdetails"); ?>

                                                    <td><?php echo ($drafttrf->getColumnVal("trfnumber")); ?></td>

                                                    <td><?php if (($drafttrf->getColumnVal("revtrf")) > 0) { ?>R<?php echo $drafttrf->getColumnVal("revtrf");

                                                                                                            } ?></td>



                                                    <td><?php echo ($drafttrf->getColumnVal("sample_description")); ?></td>

                                                    <td><?php echo ($drafttrf->getColumnVal("name_certification")); ?></td>

                                                    <td>

                                                        <?php

                                                        // Assuming session_start() has been called previously if needed.

                                                        $nameField = ($_SESSION['langselect'] == 'en') ? 'name_articletypeeng' : 'name_articletype';

                                                        echo ($drafttrf->getColumnVal($nameField));

                                                        ?>

                                                    </td>

                                                    <td><?php

                                                        $revcs = $drafttrf->getColumnVal("revcs");

                                                        if ($revcs == 's') { ?>



                                                            <a href="declaration.php?idtrf=<?php echo $idtrf; ?>"><button type="button" class="btn btn-success waves-effect waves-light">SIGN</button></a>



                                                    </td>

                                                    <td></td><?php } ?>





                                                <?php if ($drafttrf->getColumnVal("idcertification") == 5 && $drafttrf->getColumnVal("revcs") != 's') { ?>



                                                    <td><a href="typeofcertificate5.php?idtrf=<?php echo $idtrf; ?>"><button type="button" class="btn btn-info waves-effect waves-light"><?php echo $proceedtrf; ?></button></a></td>



                                                <?php } elseif ($drafttrf->getColumnVal("idcertification") == 6  && $drafttrf->getColumnVal("revcs") != 's') { ?>



                                                    <td><a href="typeofcertificate6.php?idtrf=<?php echo $idtrf; ?>"><button type="button" class="btn btn-info waves-effect waves-light"><?php echo $proceedtrf; ?></button></a></td>





                                                <?php } elseif ($drafttrf->getColumnVal("revcs") != 's') { ?>

                                                    <td><a href="trfdetails.php?idtrf=<?php echo $idtrf; ?>"><button type="button" class="btn btn-info waves-effect waves-light"><?php echo $proceedtrf; ?></button></a></td>



                                                <?php } ?>



                                                <td>

                                                    <button type="button" onclick="

        Swal.fire({

          title: 'Sei sicuro di voler duplicare il TRF N. <?php echo $drafttrf->getColumnVal('trfnumber'); ?>?',

          icon: 'warning',

          showCancelButton: true,

          confirmButtonColor: '#3085d6',

          cancelButtonColor: '#d33',

          confirmButtonText: 'Sì, duplica!',

		  cancelButtonText: 'No, chiudi!'

        }).then((result) => {

          if (result.isConfirmed) {

            window.location.href='clonetrf.php?idtrf=<?php echo $drafttrf->getColumnVal('idtrfdetails'); ?>';

          }

        });

    " class="btn btn-outline-warning waves-effect waves-light">

                                                        <i class="far fa-copy"></i>

                                                    </button>

                                                    <button type="button" onclick="

        Swal.fire({

          title: 'Sei sicuro di voler cancellare il  Draft TRF N. <?php echo $drafttrf->getColumnVal('trfnumber'); ?>?',

          icon: 'warning',

          showCancelButton: true,

          confirmButtonColor: '#3085d6',

          cancelButtonColor: '#d33',

          confirmButtonText: 'Sì, procedi!',

		  cancelButtonText: 'No, chiudi!'

        }).then((result) => {

          if (result.isConfirmed) {

            window.location.href='deleteappform.php?idtrf=<?php echo $drafttrf->getColumnVal('idtrfdetails'); ?>';

          }

        });

    " class="btn btn-outline-danger waves-effect waves-light">

                                                        <i class="fas fa-trash-alt text-danger font-16"></i>

                                                    </button>

                                                </td>



                                                </tr>

                                            <?php

                                                $drafttrf->moveNext();

                                            }

                                            $drafttrf->moveFirst(); //return RS to first record

                                            unset($wa_startindex);

                                            unset($wa_repeatcount);

                                            ?>

                                        </tbody>

                                    </table>





                                    <br>









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





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









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



                        <!-- COMPLETE TRF -->



                        <div class="card">





                            <!-- card for show requirements -->

                            <div class="card">

                                <div class="card-body">

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



                                    <table id="readytrf" class="display table table-striped table-bordered dt-responsive nowrap" style="border-collapse: collapse; border-spacing: 0; width: 100%;">

                                        <thead>

                                            <tr>

                                                <th>TRF N.</th>

                                                <th>REV</th>

                                                <th>Signed On</th>

                                                <th>Description</th>

                                                <th>Cert Type</th>

                                                <th>Article type</th>

                                                <th>Insert by</th>

                                                <th>PDF1</th>

                                                <th>PDF2</th>

                                                <th>ZIP</th>

                                                <th>ACTION</th>



                                            </tr>

                                        </thead>





                                        <tbody>

                                            <?php

                                            $wa_startindex = 0;

                                            $last_trfnumber = null;

                                            $last_revtrf = -1;



                                            while (!$archivetrflist->atEnd()) {



                                                // without signed for rev

                                                $idtrf_nosign = $archivetrflist->getColumnVal("idtrfdetails");

                                                $trfnosign = $archivetrflist->getColumnVal("trfnumber");

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

                                                $archivetrflistnosign->setQuery("SELECT MAX(revtrf) as max_revtrf FROM `trf-details` WHERE `trf-details`.trfnumber='$trfnosign'");

                                                $archivetrflistnosign->execute();

                                                $maxrevtrf = $archivetrflistnosign->getColumnVal("max_revtrf");

                                                $currentrevtrf = $archivetrflist->getColumnVal("revtrf");

                                                $current_trfnumber = $archivetrflist->getColumnVal("trfnumber");

                                                $current_revtrf = $archivetrflist->getColumnVal("revtrf");



                                                // Check if this is a new TRF number

                                                if ($last_trfnumber !== $current_trfnumber) {

                                                    $last_trfnumber = $current_trfnumber;

                                                    $last_revtrf = $current_revtrf;

                                                }

                                                $wa_startindex = $archivetrflist->Index;

                                            ?>

                                                <tr>

                                                    <td><?php echo ($archivetrflist->getColumnVal("trfnumber")); ?></td>

                                                    <td><?php if (($archivetrflist->getColumnVal("revtrf")) > 0) { ?>R<?php echo $archivetrflist->getColumnVal("revtrf");

                                                                                                                    } ?></td>

                                                    <td><?php echo ($archivetrflist->getColumnVal("signedon")); ?></td>



                                                    <td><?php echo ($archivetrflist->getColumnVal("sample_description"));



                                                        ?></td>

                                                    <td><?php echo ($archivetrflist->getColumnVal("name_certification")); ?></td>

                                                    <td>

                                                        <?php

                                                        // Assuming session_start() has been called previously if needed.

                                                        $nameField = ($_SESSION['langselect'] == 'en') ? 'name_articletypeeng' : 'name_articletype';

                                                        echo ($archivetrflist->getColumnVal($nameField));

                                                        ?>

                                                    </td>





                                                    <td><?php echo ($archivetrflist->getColumnVal("email")); ?></td>

                                                    <td><a href="pdf/<?php echo $archivetrflist->getColumnVal("pdffilename"); ?>" target="_blank"><i class="fas fa-file-pdf text-danger font-20"></i></a></td>

                                                    <td><?php if (!empty($archivetrflist->getColumnVal("pdffilename2"))) { ?><a href="pdf/<?php echo $archivetrflist->getColumnVal("pdffilename2"); ?>" target="_blank"><i class="fas fa-file-pdf text-danger font-20"></i></a><?php } ?></td>

                                                    <td><?php if (!empty($archivetrflist->getColumnVal("zipname"))) { ?><a href="uploaddocuments/zip/<?php echo $archivetrflist->getColumnVal("zipname"); ?>" target="_blank"><i class="fas fa-file-archive text-warning font-20"></i></a><?php } ?></td>

                                                    <td>

                                                        <button type="button" onclick="

    Swal.fire({

        title: 'Sei sicuro di voler duplicare il TRF N. <?php echo $archivetrflist->getColumnVal('trfnumber'); ?>?',

        icon: 'warning',

        showCancelButton: true,

        confirmButtonColor: '#3085d6',

        cancelButtonColor: '#d33',

        confirmButtonText: 'Sì, duplica!',

        cancelButtonText: 'No, chiudi!'

    }).then((result) => {

        if (result.isConfirmed) {

            window.location.href='clonetrf.php?idtrf=<?php echo $archivetrflist->getColumnVal('idtrfdetails'); ?>';

        }

    });

" class="btn <?php echo ($maxrevtrf > $currentrevtrf) ? 'btn-inactive' : 'btn-outline-warning'; ?> waves-effect waves-light" <?php echo ($maxrevtrf > $currentrevtrf) ? 'disabled' : ''; ?>>

                                                            <i class="far fa-copy"></i>

                                                        </button>



                                                        <button type="button" onclick="

    Swal.fire({

        title: 'Sei sicuro di voler revisionare il TRF N. <?php echo $archivetrflist->getColumnVal('trfnumber'); ?>?',

        icon: 'warning',

        showCancelButton: true,

        confirmButtonColor: '#3085d6',

        cancelButtonColor: '#d33',

        confirmButtonText: 'Sì, procedi!',

        cancelButtonText: 'No, chiudi!'

    }).then((result) => {

        if (result.isConfirmed) {

            window.location.href='revtrf.php?idtrf=<?php echo $archivetrflist->getColumnVal('idtrfdetails'); ?>';

        }

    });

" class="btn <?php echo ($maxrevtrf > $currentrevtrf) ? 'btn-inactive' : 'btn-active'; ?> waves-effect waves-light" <?php echo ($maxrevtrf > $currentrevtrf) ? 'disabled' : ''; ?>>

                                                            Rev

                                                        </button>



                                                        <?php if ((Auth::user()->hasRole('Admin')) || (Auth::user()->hasRole('CustomerService')) || (Auth::user()->hasRole('Superuser'))) : ?>

                                                            <?php

                                                            $idtrfdetailschk = $archivetrflist->getColumnVal('idtrfdetails');

                                                            $ntrfdetailschk = $archivetrflist->getColumnVal('trfnumber');



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

                                                            $query = "SELECT COUNT(*) AS count FROM data_td WHERE idtrf = ?";



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

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

                                                            $stmt->execute();

                                                            $result = $stmt->get_result();

                                                            $rowcheck = $result->fetch_assoc();



                                                            if ($rowcheck['count'] < 1) {

                                                                $buttonColor = '#ff9800';

                                                                $onClick = "Swal.fire({

            title: 'Sei sicuro di voler creare il Fascicolo Tecnico per il TRF N. " . $ntrfdetailschk . "?',

            icon: 'warning',

            showCancelButton: true,

            confirmButtonColor: '#3085d6',

            cancelButtonColor: '#d33',

            confirmButtonText: 'Sì, procedi!',

            cancelButtonText: 'No, chiudi!'

        }).then((result) => {

            if (result.isConfirmed) {

                window.location.href='techdossier_start.php?idtrftd=" . $idtrfdetailschk . "';

            }

        });";

                                                            } else {

                                                                $buttonColor = '#4CAF50';

                                                                $onClick = "window.location.href='archivetd.php';";

                                                            }

                                                            ?>



                                                            <button type="button" onclick="<?php echo $onClick; ?>" class="btn <?php echo ($maxrevtrf > $currentrevtrf) ? 'btn-inactive' : ''; ?>" style="border: 1px solid; color: white; background-color: <?php echo $buttonColor; ?>;" <?php echo ($maxrevtrf > $currentrevtrf) ? 'disabled' : ''; ?>>

                                                                TF

                                                            </button>

                                                        <?php endif; ?>



                                                        <button type="button" onclick="

    Swal.fire({

        title: 'Sei sicuro di voler cancellare il TRF N. <?php echo $archivetrflist->getColumnVal('trfnumber'); ?>?',

        icon: 'warning',

        showCancelButton: true,

        confirmButtonColor: '#3085d6',

        cancelButtonColor: '#d33',

        confirmButtonText: 'Sì, procedi!',

        cancelButtonText: 'No, chiudi!'

    }).then((result) => {

        if (result.isConfirmed) {

            window.location.href='deleteappform.php?idtrf=<?php echo $archivetrflist->getColumnVal('idtrfdetails'); ?>';

        }

    });

" class="btn btn-outline-danger waves-effect waves-light">

                                                            <i class="fas fa-trash-alt text-danger font-16"></i>

                                                        </button>



                                                    </td>



                                                </tr>



                                            <?php

                                                $archivetrflist->moveNext();

                                            }

                                            $archivetrflist->moveFirst(); //return RS to first record

                                            unset($wa_startindex);

                                            unset($wa_repeatcount);

                                            ?>

                                        </tbody>

                                    </table>

                                    <br>









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





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









                        </div>

                    </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/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>