<?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";

# Check permissions
$API = new API();
$API->checkPermissions('admin-sources', 'RO');

# Page functions


# JS Scripts to load for this page
$jsScriptLoadData['/src/js/sentri/activeTabOnRefresh.js'] = true;
$jsScriptLoadData['/src/js/sentri/copyInputValue.js'] = true;
$jsScriptLoadData['/src/js/sentri/updateToggle.js'] = true;
$jsScriptLoadData['/src/js/sentri/breadCrumbs.js'] = true;
$jsScriptLoadData['/src/js/sentri/inserve_source.js'] = true;
$jsScriptLoadData['/src/js/sentri/validateJson.js'] = true;

# PageClasses Setup
$pageNavbar = new pageNavbar(true);

# Retrieve Information for the page
$inserve_settings = $GLOBALS['conn']->query("SELECT * FROM system_sources WHERE source_name = 'inserve'")->fetch_assoc();
$inserve_custom_data = ($inserve_settings['source_custom_data'] ? json_encode(json_decode($inserve_settings['source_custom_data']), JSON_PRETTY_PRINT) : "{}");

# Set breadcrumb data
$_SESSION['breadCrumbArray'][] = ['display' => __('portal_management'), 'href' => '/portal-management'];
$_SESSION['breadCrumbArray'][] = ['display' => __('sources'), 'href' => '/portal-management#sources'];
$_SESSION['breadCrumbArray'][] = ['display' => 'Inserve', 'href' => ''];

# Start page output
$pageNavbar->outPutNavbar();
?>

<div class="row">
    <div class="col-md-12 col-lg-12">
        <div class="tab-content" id="v-pills-with-icon-tabContent">
            <div class="card">
                <div class="mx-2 pb-0 card-body">
                    <h1 class="">Inserve <?php echo __('settings') ?></h1>
                    <p>
                        <?php echo __('source_description') ?></p>
                    <hr>
                </div>

                <form id="FormValidation" method="post" action="/api/v1/portal-management/sources/inserve/">
                    <input type="hidden" name="X-HTTP-Method-Override" value="POST">
                    <input type="hidden" name="_return" value="/portal-management/sources/inserve">
                    <div class="card-body">

                        <div class="form-group form-show-validation row">
                            <label for="source_url" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('inserve_url') ?></label>
                            <div class="col-lg-9 col-md-12 col-sm-10">
                                <input type="text" class="form-control" id="source_url" name="source_url" value="<?php echo ($inserve_settings) ? $inserve_settings['source_url'] : '' ?>" placeholder="" required/>
                            </div>
                        </div>

                        <div class="form-group form-show-validation row">
                            <label for="source_auth_token" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('api_token') ?></label>
                            <div class="col-lg-9 col-md-12 col-sm-10">
                                <input type="text" class="form-control" id="source_auth_token" name="source_auth_token" value="" autocomplete="off" placeholder="<?php echo ($inserve_settings) ? substr($inserve_settings['source_auth_token'], 0, 6) . str_repeat('*', max(0, strlen($inserve_settings['source_auth_token']) - 6)) : ''; ?>" required/>
                            </div>
                        </div>

                        <div class="form-group form-show-validation row">
                            <label for="source_custom_data" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('source_custom_data') ?></label>
                            <div class="col-lg-9 col-md-12 col-sm-10">
                                <textarea class="form-control" id="source_custom_data" name="source_custom_data" rows="5" data-validate-json="true"><?php echo $inserve_custom_data ?></textarea>
                                <small id="source_custom_data_help" class="form-text text-muted">
                                    <?php echo __('json_enter') ?>
                                </small>
                            </div>
                        </div>
                    </div>
                    <div class="card-footer py-4">
                        <div class="row">
                            <div class="col d-flex justify-content-end">
                                <button class="btn btn-success mx-2 test-inserve-connection-btn">
                                    <i class="fa-solid fa-spell-check"></i> <?php echo __('test_connection') ?>
                                </button>
                                <?php if ($API->checkPermissions('admin-sources', 'RW', true)) { ?>
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fa-solid fa-floppy-disk"></i> <?php echo __('save') ?>
                                    </button>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12 col-lg-12">
        <div class="tab-content" id="v-pills-with-icon-tabContent">
            <div class="card">
                <div class="card-body">
                    <h1 class="mx-2 pb-0">Inserve actions</h1>
                    <div class="row row-cols-1 row-cols-md-3 g-2">
                        <?php if ($GLOBALS['modules_enabled']['customers'] && $API->checkPermissions('customer-companies', 'RW', true)) { ?>
                            <div class="col d-flex">
                                <form method="post" action="/api/v1/sources/inserve/sync-companies/">
                                    <div class="card h-100 w-100">
                                        <div class="card-body">
                                            <h5 class="card-title"><?php echo __('source_inserve_sync_companies') ?></h5>
                                            <input type="hidden" name="_return" value="/portal-management/sources/inserve">
                                            <p class="card-text"><?php echo __('source_inserve_sync_companies_desc') ?></p>
                                        </div>
                                        <div class="card-footer mt-auto">
                                            <button class="btn btn-primary">
                                                <i class="fa-solid fa-arrow-rotate-right"></i> &nbsp;<?php echo __('sync') ?>
                                            </button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        <?php } ?>

                        <?php if ($GLOBALS['modules_enabled']['servers'] && $API->checkPermissions('servers', 'RW', true)) { ?>
                            <div class="col d-flex">
                                <form method="post" action="/api/v1/sources/inserve/sync-cloud-distributor/" class="w-100">
                                    <div class="card h-100 w-100">
                                        <div class="card-body">
                                            <h5 class="card-title"><?php echo __('source_inserve_sync_cloud') ?></h5>
                                            <input type="hidden" name="_return" value="/portal-management/sources/inserve">
                                            <p class="card-text"><?php echo __('source_inserve_sync_cloud_desc') ?></p>
                                        </div>
                                        <div class="card-footer mt-auto">
                                            <button class="btn btn-primary">
                                                <i class="fa-solid fa-arrow-rotate-right"></i> &nbsp;<?php echo __('sync') ?>
                                            </button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        <?php } ?>

                        <?php if ($GLOBALS['modules_enabled']['servers'] && $API->checkPermissions('servers', 'RW', true)) { ?>
                            <div class="col d-flex">
                                <form method="post" action="/api/v1/sources/inserve/sync-server-licenses/" class="w-100">
                                    <div class="card h-100 w-100">
                                        <div class="card-body">
                                            <h5 class="card-title"><?php echo __('source_inserve_sync_licenses') ?></h5>
                                            <input type="hidden" name="_return" value="/portal-management/sources/inserve">
                                            <p class="card-text"><?php echo __('source_inserve_sync_licenses_desc') ?></p>
                                        </div>
                                        <div class="card-footer mt-auto">
                                            <button class="btn btn-primary">
                                                <i class="fa-solid fa-arrow-rotate-right"></i> &nbsp;<?php echo __('sync') ?>
                                            </button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        <?php } ?>

                        <?php if ($GLOBALS['modules_enabled']['servers'] && $API->checkPermissions('servers', 'RW', true)) { ?>
                            <div class="col d-flex">
                                <form method="post" action="/api/v1/sources/inserve/sync-server-subscriptions/" class="w-100">
                                    <div class="card h-100 w-100">
                                        <div class="card-body">
                                            <h5 class="card-title"><?php echo __('source_inserve_sync_subscriptions') ?></h5>
                                            <input type="hidden" name="_return" value="/portal-management/sources/inserve">
                                            <p class="card-text"><?php echo __('source_inserve_sync_subscriptions_desc') ?></p>
                                        </div>
                                        <div class="card-footer mt-auto">
                                            <button class="btn btn-primary">
                                                <i class="fa-solid fa-arrow-rotate-right"></i> &nbsp;<?php echo __('sync') ?>
                                            </button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        <?php } ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>