<?php include('../include/headscript.php'); ?>
<?php include("../class/company.php"); ?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <?php include('../include/seo.php'); ?>

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

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

    <!-- Bootstrap -->
    <link href="../assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link href="../assets/css/icons.css" rel="stylesheet" type="text/css">
    <link href="../assets/css/style.css" rel="stylesheet" type="text/css">
    <link href="https://cdn.jsdelivr.net/npm/boxicons@2.0.7/css/boxicons.min.css" rel="stylesheet">

    <!-- SweetAlert -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@10/dist/sweetalert2.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@10/dist/sweetalert2.min.css">

    <!-- DataTables CSS -->
    <link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.4.1/css/buttons.dataTables.min.css">

    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>

    <!-- DataTables JS -->
    <script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.1/js/dataTables.buttons.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.1/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.1/js/buttons.print.min.js"></script>

    <style>
        .table-custom tr {
            height: 40px;
            line-height: 40px;
        }

        .table-custom td,
        .table-custom th {
            padding: 4px 8px;
        }

        .table-custom .btn {
            padding: 5px 15px;
            font-size: 14px;
        }
    </style>
</head>

<body class="fixed-left">

    <div id="wrapper">
        <?php include('../include/navigationbar.php'); ?>

        <div class="content-page">
            <div class="content">
                <?php include('../include/topbar.php'); ?>

                <div class="page-content-wrapper ">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-sm-12">
                                <div class="page-title-box">
                                    <div class="btn-group float-right">
                                        <ol class="breadcrumb hide-phone p-0 m-0">
                                            <li class="breadcrumb-item"><a href="#">Reportify</a></li>
                                            <li class="breadcrumb-item active">EasySpec</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title">EasySpec</h4>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xl-12">
                                <div class="card">
                                    <div class="card-body">
                                        <h5 class="header-title pb-3 mt-0">EasySpec: Standards</h5>
                                        <div class="mb-3">
                                            <a class="btn btn-danger" href="insert-standards.php" role="button">Insert new Standard</a> <a class="btn btn-danger" href="rsl-category.php" role="button">RSL Category</a> <a class="btn btn-danger" href="material.php" role="button"><?php echo $materialstitle; ?></a> <a class="btn btn-danger" href="analysis.php" role="button">Analysis</a><?php if ($infobox == "wizard") { ?> <a class="btn btn-dark" href="rslwizard1.php" role="button">Back to Wizard</a><?php     } ?>
                                            <a href="component.php"><button type="button" class="btn btn-danger w-md waves-effect waves-light">Components</button></a>
                                            <a href="rsl.php"><button type="button" class="btn btn-danger w-md waves-effect waves-light">RSL</button></a>

                                            <a href="saytrl-newsletter.php"><button type="button" class="btn btn-success w-lg waves-effect waves-light">SayTRL</button></a>
                                            <br><br>
                                        </div>

                                        <div class="table-responsive">
                                            <table id="datatable-standards" class="table table-striped table-sm">
                                                <thead>
                                                    <tr>
                                                        <th><?php echo $active_lang; ?></th>
                                                        <th>Standards Name</th>
                                                        <th>Number</th>
                                                        <th>Year</th>
                                                        <th>Active From</th>
                                                        <th>Active To</th>
                                                        <th>PDF</th>
                                                        <th>Actions</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                    $rsllist = new WA_MySQLi_RS("rsl", $repnew, 0);
                                                    $rsllist->setQuery("SELECT * FROM standards WHERE company_id='$idcompany'");
                                                    $rsllist->execute();

                                                    while (!$rsllist->atEnd()) {
                                                        $idstandards = $rsllist->getColumnVal("idstandards");
                                                        $conn = new mysqli($servername, $username, $password, $database);
                                                        $query = $conn->prepare("SELECT pdffilename FROM pdfstandards WHERE idstandards = ?");
                                                        $query->bind_param("i", $idstandards);
                                                        $query->execute();
                                                        $result = $query->get_result();
                                                        $pdfFiles = [];
                                                        while ($row = $result->fetch_assoc()) {
                                                            $pdfFiles[] = $row['pdffilename'];
                                                        }
                                                        $pdfCount = count($pdfFiles);
                                                    ?>
                                                        <tr>
                                                            <td>
                                                                <?php if ($rsllist->getColumnVal("status") == "A") { ?>
                                                                    <button class="btn btn-success" title="Active"><i class="bx bx-check-double"></i></button>
                                                                <?php } else { ?>
                                                                    <button class="btn btn-danger" title="Inactive"><i class="bx bx-block"></i></button>
                                                                <?php } ?>
                                                            </td>
                                                            <td><?php echo $rsllist->getColumnVal("titlestandards"); ?></td>
                                                            <td><?php echo $rsllist->getColumnVal("numberstandards"); ?></td>
                                                            <td><?php echo $rsllist->getColumnVal("yearstandards"); ?></td>
                                                            <td><?php echo $rsllist->getColumnVal("activefrom"); ?></td>
                                                            <td><?php echo $rsllist->getColumnVal("activeto"); ?></td>
                                                            <td>
                                                                <?php if ($pdfCount === 1) {
                                                                    $pdfUrl = "../pdfstandards/" . $pdfFiles[0];
                                                                ?>
                                                                    <a href="<?php echo $pdfUrl; ?>" target="_blank" class="btn btn-danger">Open PDF</a>
                                                                <?php } elseif ($pdfCount > 1) { ?>
                                                                    <a href="update-standards.php?id=<?php echo $idstandards; ?>" class="btn btn-danger">Manage PDFs</a>
                                                                <?php } else { ?>
                                                                    <button class="btn btn-secondary" disabled>No PDFs</button>
                                                                <?php } ?>
                                                            </td>
                                                            <td>
                                                                <a class="btn btn-success" href="update-standards.php?id=<?php echo $idstandards; ?>" title="Edit">
                                                                    <i class="fas fa-edit"></i>
                                                                </a>
                                                                <a class="btn btn-danger canc-btn" href="cancel-standards.php?id=<?php echo $idstandards; ?>" title="Delete">
                                                                    <i class="fas fa-trash"></i>
                                                                </a>
                                                            </td>
                                                        </tr>
                                                    <?php
                                                        $rsllist->moveNext();
                                                    }
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div><!-- end table-responsive -->
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div><!-- container -->
                </div> <!-- Page content Wrapper -->
            </div> <!-- content -->
            <?php include('../include/footer.php'); ?>
        </div> <!-- End content-page -->
    </div><!-- END wrapper -->

    <script>
        $(document).ready(function() {
            $('#datatable-standards').DataTable({
                dom: 'lBfrtip',
                buttons: ['copy', 'csv', 'excel', 'pdf', 'print'],
                pageLength: 10,
                responsive: true,
                language: {
                    url: "https://cdn.datatables.net/plug-ins/1.13.6/i18n/en-GB.json"
                }
            });

            $('.canc-btn').on('click', function(e) {
                e.preventDefault();
                const url = $(this).attr('href');
                Swal.fire({
                    title: 'Are you sure?',
                    text: "You won't be able to revert this!",
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, delete it!'
                }).then((result) => {
                    if (result.isConfirmed) {
                        window.location.href = url;
                    }
                });
            });
        });
    </script>

    <!-- Plugin JS -->
    <script src="../assets/js/popper.min.js"></script>
    <script src="../assets/js/bootstrap.min.js"></script>
</body>

</html>