<?php

use api\classes\API;
use bin\php\Classes\pageNavbar;

if (!defined('APP_INIT')) {
    exit;
}
# IDE Section

# Includes Section
require_once "{$_SERVER['DOCUMENT_ROOT']}/bin/php/Classes/pageNavbar.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_devices.php";

# Check permissions
$API = new API();
if (!$API->checkPermissions('admin-devices', 'RO', true)) {
    echo 'error 401 unauthorized';
    exit;
}

# Page functions

# JS Scripts to load for this page
$jsScriptLoadData['/src/js/sentri/multiFilterSelect.js'] = true;
$jsScriptLoadData['/src/js/sentri/delete_confirmation.js'] = true;
$jsScriptLoadData['/src/js/plugin/datatables/datatables.min.js'] = true;
$jsScriptLoadData['/src/js/sentri/breadCrumbs.js'] = true;

# PageClasses Setup
$pageNavbar = new pageNavbar(false, '<i class="fas fa-fax"></i> ' . __('devices'));
if ($API->checkPermissions('admin-devices', 'RW', true)) {
    $pageNavbar->AddHTMLButton('<div class="btn-group dropdown">
            <button class="btn btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown">
                <i class="fas fa-plus text-success"></i>' . __('add_device') . '</button>
            <ul class="dropdown-menu bg-black2" role="menu">
                <li>
                    <a class="dropdown-item text-white-50 bg-black2" href="?add=phone">Phone</a>
                    <a class="dropdown-item text-white-50 bg-black2" href="?add=module">Module</a>
                    <a class="dropdown-item text-white-50 bg-black2" href="?add=base">Base</a>
                    <a class="dropdown-item text-white-50 bg-black2" href="?add=handset">Handset</a>
                </li>
            </ul>
        </div>');
}
# Retrieve Information for the page
$data = $GLOBALS['conn']->query("SELECT * FROM autop_devices INNER JOIN autop_vendors ON autop_devices.device_vendor_uuid = autop_vendors.vendor_uuid");

# Set breadcrumb data
$_SESSION['breadCrumbArray'][] = ['display' => __('devices'), 'href' => '/devices/'];

# Start page output
$pageNavbar->outPutNavbar();
?>
<div class="card-body">
    <div class="table-responsive">
        <table class="multi-filter-select table table-hover" data-skip-columns="0,5" data-datatables-order='[ [1, "desc"], [3, "asc"] ]' data-page-length="25">
            <thead>
            <tr>
                <th>Image</th>
                <th>Type</th>
                <th>Vendor</th>
                <th>Name</th>
                <th>Enabled</th>
                <th>Actions</th>
            </tr>
            </thead>
            <tfoot>
            <tr>
                <th>Image</th>
                <th>Type</th>
                <th>Vendor</th>
                <th>Name</th>
                <th>Enabled</th>
                <th>Actions</th>
            </tr>
            </tfoot>
            <tbody>
            <?php while ($row = $data->fetch_assoc()) { ?>
                <tr data-item-id="<?php echo $row['device_uuid']; ?>">
                    <td style="padding-top: 0!important;padding-bottom: 0!important;">
                        <img class="img-upload-preview" src="data:image/png;base64, <?php echo $row['device_image_thumbnail'] ?>" alt="">
                    </td>
                    <td><?php echo ucfirst($row['device_type']) ?></td>
                    <td><?php echo $row['vendor_name'] ?></td>
                    <td><?php echo $row['device_name'] ?></td>
                    <td>
                        <?php echo(($row['device_enabled'] == 1) ? __('yes') : __('no')) ?>
                    </td>
                    <td>
                        <?php if ($API->checkPermissions('admin-devices', 'RW', true)) { ?>
                            <a href="?edit=<?php echo $row['device_uuid'] ?>" class="btn btn-primary btn-sm btn-rounded"><i class="fas fa-edit"></i></a>
                        <?php } ?>
                        <a href="?view=<?php echo $row['device_uuid'] ?>" class="btn btn-info btn-sm btn-rounded"><i class="far fa-eye"></i></a>
                        <?php if ($API->checkPermissions('admin-devices', 'RW', true)) { ?>
                            <a href="#" class="btn btn-danger btn-sm btn-rounded delete-btn" data-item-uuid="<?php echo $row['device_uuid'] ?>" data-api-url="/api/v1/autop/devices/" data-item-name="device_uuid"><i class="fas fa-trash-alt"></i></a>
                        <?php } ?>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
        </table>
    </div>
</div>
