<?php

require('../config.php');
require('../EpiApi.php');
require_once __DIR__ . '/../vendor/autoload.php';
date_default_timezone_set('Europe/Berlin');

$Epi = new Epirent();
$productList = json_decode($Epi->requestEpiApi('/v1/product/all?ia=true&ir=true&cl=' . Epirent_Mandant))->payload;

?>
<!DOCTYPE html>
<html lang="de">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>Labelprint - EpiWebview</title>

    <!-- Styles -->
    <link href="css/styles.css" rel="stylesheet" />
    <link href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap4.min.css" rel="stylesheet" crossorigin="anonymous" />

    <!-- JS -->
    <script src="js/jquery-3.5.1.min.js"></script>
    <script src="https://kit.fontawesome.com/93d71de8bc.js" crossorigin="anonymous"></script>
    <script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js"></script>

    <style>
        .opacity-50 {
            opacity: .5;
        }

        .card .h2,
        .card .display-4 {
            font-weight: 700;
        }

        .kpi-updated {
            font-size: .82rem;
            opacity: .85;
        }
    </style>

    <script>
        $(function() {
            $('#layoutSidenav_nav').load('../sources/getSidenav.php');
            $('#footerholder').load('../sources/getFooter.php');
        });
    </script>
</head>

<body class="sb-nav-fixed">
    <nav class="sb-topnav navbar navbar-expand navbar-dark bg-dark">
        <a class="navbar-brand" href="index.php">Epi Webview</a>
        <button class="btn btn-link btn-sm order-1 order-lg-0" id="sidebarToggle"><i class="fas fa-bars"></i></button>
    </nav>

    <div id="layoutSidenav">
        <div id="layoutSidenav_nav"></div>
        <div id="layoutSidenav_content">
            <main>
                <div class="container-fluid">
                    <h1 class="mt-4">Labelprint</h1>


                    <div class="card mb-4">
                        <div class="card-header d-flex justify-content-between align-items-center">
                            <div><i class="fas fa-table mr-1"></i> Labelprint</div>

                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table id="dataTable" class="table table-bordered" width="100%" cellspacing="0">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>PN</th>
                                            <th>Name</th>
                                            <th>Aktionen</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        foreach ($productList as $product) {
                                            if (!$product->is_rent & $product->is_sale){
                                                continue;
                                            }
                                            if (!$product->is_active){
                                                continue;
                                            }
                                            echo "<tr>";
                                            echo "<td>" . htmlspecialchars($product->primary_key) . "</td>";
                                            echo "<td>" . htmlspecialchars($product->product_no) . "</td>";
                                            echo "<td>" . htmlspecialchars($product->name) . "</td>";
                                            echo "
        <td>
            <a class='btn btn-sm btn-outline-primary'
                target='_blank'
                href='../sources/getProductLabel.php?id=" . urlencode($product->primary_key) . "'>
                Export
            </a>
             <a class='btn btn-sm btn-outline-primary'
                target='_blank'
                href='../sources/getProductLabelSmall.php?id=" . urlencode($product->primary_key) . "'>
                Export small
            </a>

            <input type='number'
                id='nrInput_" . htmlspecialchars($product->primary_key) . "'
                min='1' max='9999'
                style='width:70px; margin-left:6px; text-align:center;'
                placeholder='#'>

            <a class='btn btn-sm btn-outline-secondary'
                target='_blank'
                id='exportWithIdBtn_" . htmlspecialchars($product->primary_key) . "'
                href='#'
                onclick=\"
                    var nr = document.getElementById('nrInput_" . htmlspecialchars($product->primary_key) . "').value;
                    if(nr) {
                        this.href = '../sources/getProductLabel.php?id=" . urlencode($product->primary_key) . "&nr=' + encodeURIComponent(nr);
                    } else {
                        alert('Bitte eine Nummer zwischen 1 und 9999 eingeben.');
                        return false;
                    }
                \">
                Export mit ID
            </a>

               <a class='btn btn-sm btn-outline-secondary'
                target='_blank'
                id='exportWithIdBtn_" . htmlspecialchars($product->primary_key) . "'
                href='#'
                onclick=\"
                    var nr = document.getElementById('nrInput_" . htmlspecialchars($product->primary_key) . "').value;
                    if(nr) {
                        this.href = '../sources/getProductLabelSmall.php?id=" . urlencode($product->primary_key) . "&nr=' + encodeURIComponent(nr);
                    } else {
                        alert('Bitte eine Nummer zwischen 1 und 9999 eingeben.');
                        return false;
                    }
                \">
                Export mit ID small
            </a>
        </td>";
                                            echo "</tr>";
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>

                        </div>
                    </div>
                </div>
            </main>
            <div id="footerholder"></div>
        </div>
    </div>

    <!-- Bootstrap Bundle -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
    <script>
        $(document).ready(function() {
            $('#dataTable').DataTable({
                "pageLength": 25,
                "language": {
                    "url": "//cdn.datatables.net/plug-ins/1.10.20/i18n/German.json"
                }
            });
        });
    </script>

</body>

</html>