<?php
if (file_exists('../../../' . $_REQUEST['tenant'] . '/config.php')) {
    require_once('../../../' . $_REQUEST['tenant'] . '/config.php');
}
if (file_exists('/config.php')) {
    require_once('/config.php');
}
?>
<style>
.dataTables_wrapper .dataTables_paginate .paginate_button.current,
.dataTables_wrapper .dataTables_paginate .paginate_button.current:hover {
    background-color: #2a3f54 !important;
    color: #ffffff !important;
}

.dataTables_wrapper .dataTables_paginate .paginate_button:hover {
    background-color: #2a3f54 !important;
    color: #ffffff !important;
}
</style>

<div id="loadingContent" class="text-center" style="display:block"><i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
    LOADING...</div>
<div id="contentBlock" style="display:none">

    <?php
    $fil = "";

    $r = mysqli_query($GLOBALS['con'], "SELECT DISTINCT(" . $_REQUEST['type'] . ") FROM `itemdetails` ORDER BY `" . $_REQUEST['type'] . "` ASC") or die(mysqli_error($con));
    ?>
    <table id="datatables" class="table table-striped table-sm table-bordered table-hover responsive" width="100%">
        <thead>
            <tr>
                <th>ID</th>
                <th></th>
            </tr>
        </thead>
        <tbody>

            <?php while ($rc = mysqli_fetch_array($r)) {
                if ($_REQUEST['type'] == "model") { ?>
            <tr>
                <td><?= $rc['model'] ?></td>
                <td align="center"><a class="btn btn-success btn-circle btn-sm" href="javascript:void(0)"
                        onClick="updateField('<?= $rc['model'] ?>')"><i class="fad fa-check"></i></a></td>
            </tr>
            <?php }

                if ($_REQUEST['type'] == "manf") { ?>
            <tr>
                <td><?= $rc['manf'] ?></td>
                <td align="center"><a class="btn btn-success btn-circle btn-sm" href="javascript:void(0)"
                        onClick="updateField('<?= $rc['manf'] ?>')"><i class="fad fa-check"></i></a></td>
            </tr>
            <?php }
            } ?>


        </tbody>
    </table>
    <?php require_once('../table.engine'); ?>
</div>
<script>
$('#popupBox .modal-header').html(
    '<a href="javascript:void(0)" class="sa">Asset Options</a><div class="pull-right"><a href="javascript:void(0)" class="btn" onclick="kill()"><i class="fad fa-window-close fa-2x" aria-hidden="true"></i></a></div>'
    );

$('#datatables').dataTable({
    "pageLength": 5
});
$('#datatables_filter input').addClass('form-control form-control-sm');

function updateField(s) {
    $('#<?= $_REQUEST['source'] ?>').val(s);

    $('#popupBox .modal-body').html('');
    $('#popupBox').modal('hide');
}

function kill() {
    $('#popupBox .modal-body').html('');
    $('#popupBox').modal('hide');
}

$(document).ready(function() {
    $('#loadingContent').css('display', 'none');
    $('#contentBlock').css('display', 'block')
})
</script>