<?php

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

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


# Includes Section
include_once($_SERVER['DOCUMENT_ROOT'] . '/bin/php/Classes/pageNavbar.php');
include_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['activeTabOnRefresh'] = true;
$jsScriptLoadData['copyInputValue'] = true;
$jsScriptLoadData['updateToggle'] = true;
$jsScriptLoadData['breadCrumbs'] = true;
$jsScriptLoadData['inserve_source'] = true;
$jsScriptLoadData['validateJson'] = 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();

# Set breadcrumb data
array_push($GLOBALS['breadCrumbArray'], array('display' => __('portal_management'), 'href' => '/systemconfig'));
array_push($GLOBALS['breadCrumbArray'], array('display' => __('sources'), 'href' => '/systemconfig#sources'));
array_push($GLOBALS['breadCrumbArray'], array('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 settings</h1>
                    <p>
                        Enter the necessary API details to set up and configure your connection to the Inserve API. This allows Sentri to communicate with Inserve and retrieve the data it needs. </p>
                    <hr>
                </div>

                <form id="FormValidation" method="post" action="/api/v1/system/sources/inserve/">
                    <input type="hidden" name="_method" value="POST">
                    <input type="hidden" name="_return" value="/system/sources/inserve">
                    <input type="hidden" name="source_name" value="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>
                    <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="">Inserve actions</h1>
                    <hr>
                    <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">
                                <div class="card h-100">
                                    <div class="card-body">
                                        <h5 class="card-title">Sync companies from Inserve to Sentri.</h5>
                                        <form method="post" action="/api/v1/customers/companies/sync/">
                                            <input type="hidden" name="_return" value="/system/sources/inserve">
                                            <p class="card-text">This API call retrieves all companies from Inserve and creates or updates them in Sentri.</p>
                                            <button class="btn btn-primary">
                                                <i class="fa-solid fa-arrow-rotate-right"></i> &nbsp;Sync.
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        <?php } ?>

                        <?php if ($GLOBALS['modules_enabled']['servers'] && $API->checkPermissions('servers', 'RW', true)) { ?>
                            <div class="col">
                                <div class="card h-100">
                                    <div class="card-body">
                                        <h5 class="card-title">Sync cloud distributor companies</h5>
                                        <form method="post" action="/api/v1/sources/inserve/sync-cloud-distributor/">
                                            <input type="hidden" name="_return" value="/system/sources/inserve">
                                            <p class="card-text">This API call synchronizes active companies in Sentri with the corresponding cloud distributor companies in Inserve. These cloud distributor companies are required to associate Sentri server licenses with companies in Inserve.</p>
                                            <button class="btn btn-primary">
                                                <i class="fa-solid fa-arrow-rotate-right"></i> &nbsp;Sync
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        <?php } ?>

                        <?php if ($GLOBALS['modules_enabled']['servers'] && $API->checkPermissions('servers', 'RW', true)) { ?>
                            <div class="col">
                                <div class="card h-100">
                                    <div class="card-body">
                                        <h5 class="card-title">Sync servers licenses</h5>
                                        <form method="post" action="/api/v1/sources/inserve/sync-server-licenses/">
                                            <input type="hidden" name="_return" value="/system/sources/inserve">
                                            <p class="card-text">This API call first executes the sync-cloud-distributor action and then synchronizes all servers in an active, deleted, or trial state with Inserve licenses. It creates or updates server licenses in Inserve if they do not exist or if the license quantities differ from those in Sentri.</p>
                                            <button class="btn btn-primary">
                                                <i class="fa-solid fa-arrow-rotate-right"></i> &nbsp;Sync
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                </div>
            </div>
        </div>
    </div>