<?php

use api\classes\API;

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


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

# Check permissions
$API = new API();

# 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;

# PageClasses Setup

# Retrieve Information for the page
$portal_settings = $GLOBALS['conn']->query("SELECT * FROM system_settings")->fetch_assoc();

$system_modules_data = $GLOBALS['conn']->query("SELECT * FROM system_modules");
$system_modules = array();
while ($module = $system_modules_data->fetch_assoc()) {
    $system_modules[] = $module;
}


# Set breadcrumb data

# Start page output

?>
<div class="card-body activeTabOnRefresh">
    <div class="row">
        <div class="col-md-1 col-lg-1">
            <div class="nav flex-column nav-pills nav-secondary nav-pills-no-bd nav-pills-icons" id="v-pills-tab-with-icon" role="tablist" aria-orientation="vertical">
                <?php if ($API->checkPermissions('admin-portalsettings', 'RO', true)) { ?>
                    <a class="nav-link" id="global-settings-tab" data-bs-toggle="pill" href="#global-settings" role="tab" aria-controls="global-settings" aria-selected="true">
                        <i class="fas fa-globe-americas"></i><?php echo __('global_settings'); ?>
                    </a>
                    <?php
                } ?>
                <?php if ($API->checkPermissions('admin-mailsettings', 'RO', true)) {

                    ?>
                    <a class="nav-link" id="mail-settings-tab" data-bs-toggle="pill" href="#mail-settings" role="tab" aria-controls="mail-settings">
                        <i class="fa-solid fa-envelope"></i><?php echo __('mail_settings'); ?>
                    </a>
                    <?php
                } ?>
                <?php if ($API->checkPermissions('admin-modules', 'RO', true)) {
                    $first_nav_active = true; ?>
                    <a class="nav-link" id="mail-settings-tab" data-bs-toggle="pill" href="#modules" role="tab" aria-controls=modules">
                        <i class="fa-solid fa-cubes"></i><?php echo __('modules'); ?>
                    </a>
                    <?php $first_nav_active = true;
                } ?>
                <?php if ($API->checkPermissions('admin-sources', 'RO', true)) {
                    $first_nav_active = true; ?>
                    <a class="nav-link" id="sources-tab" data-bs-toggle="pill" href="#sources" role="tab" aria-controls=sources">
                        <i class="fa-solid fa-cloud"></i><?php echo __('sources'); ?>
                    </a>
                    <?php $first_nav_active = true;
                } ?>
            </div>
        </div>
        <div class="col-md-11 col-lg-11">
            <div class="tab-content" id="v-pills-with-icon-tabContent">
                <?php if ($API->checkPermissions('admin-portalsettings', 'RO', true)) { ?>
                    <div class="tab-pane fade show active" id="global-settings" role="tabpanel" aria-labelledby="global-settings-tab">
                        <div class="row mb-3">
                            <div class="col-12">
                                <h2>
                                    <i class="fas fa-globe-americas"></i> &nbsp; <?php echo __('global_settings') ?>
                                </h2>
                            </div>
                        </div>
                        <div class="card">
                            <form id="FormValidation" method="post" action="/api/v1/portal-management/configure/">
                                <input type="hidden" name="X-HTTP-Method-Override" value="PUT">
                                <input type="hidden" name="_return" value="/systemconfig/#global-settings">
                                <input type="hidden" name="portal_uuid" value="<?php echo $portal_settings['portal_uuid']; ?>">
                                <div class="card-body">
                                    <div class="form-group form-show-validation row">
                                        <label for="portal_name" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('portal_name') ?></label>
                                        <div class="col-lg-9 col-md-12 col-sm-10">
                                            <input type="text" class="form-control" id="portal_name" name="portal_name" value="<?php echo $portal_settings['portal_name'] ?>" placeholder="" required/>
                                        </div>
                                    </div>
                                    <div class="form-group form-show-validation row">
                                        <label for="portal_slugify" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('portal_slugify') ?></label>
                                        <div class="col-lg-9 col-md-12 col-sm-10">
                                            <input type="text" class="form-control" id="portal_slugify" name="portal_slugify" value="<?php echo $portal_settings['portal_slugify'] ?>" placeholder="" required disabled/>
                                        </div>
                                    </div>
                                    <div class="form-group form-show-validation row">
                                        <label for="portal_provider_name" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('portal_provider_name') ?></label>
                                        <div class="col-lg-9 col-md-12 col-sm-10">
                                            <input type="text" class="form-control" id="portal_provider_name" name="portal_provider_name" value="<?php echo $portal_settings['portal_provider_name'] ?>" placeholder="" required/>
                                        </div>
                                    </div>
                                    <div class="form-group form-show-validation row">
                                        <label for="portal_provider_slugify" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('portal_provider_slugify') ?></label>
                                        <div class="col-lg-9 col-md-12 col-sm-10">
                                            <input type="text" class="form-control" id="portal_provider_slugify" name="portal_provider_slugify" value="<?php echo $portal_settings['portal_provider_slugify'] ?>" placeholder="" disabled/>
                                        </div>
                                    </div>

                                    <div class="form-group form-show-validation row">
                                        <label for="admin_auth_methods" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('admin_auth_methods') ?></label>
                                        <div class="col-lg-9 col-md-12 col-sm-10">
                                            <input type="text" class="form-control" id="admin_auth_methods" name="admin_auth_methods" value="<?php echo $portal_settings['admin_auth_methods'] ?>" placeholder="" required/>
                                        </div>
                                    </div>
                                    <div class="form-group form-show-validation row">
                                        <label for="cacert_url" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2">
                                            <?php echo __('cacert_url') ?>
                                        </label>
                                        <div class="col-lg-9 col-md-12 col-sm-10">
                                            <div class="input-group">
                                                <input type="text" class="form-control" id="cacert_url" name="cacert_url" value="<?php echo $portal_settings['cacert_url'] ?>" placeholder="" disabled/>
                                                <button class="btn btn-outline-secondary" type="button" data-copy-target="cacert_url">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group form-show-validation row">
                                        <label for="autop_url" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2">
                                            <?php echo __('autop_url') ?>
                                        </label>
                                        <div class="col-lg-9 col-md-12 col-sm-10">
                                            <div class="input-group">
                                                <input type="text" class="form-control" id="autop_url" name="autop_url" value="<?php echo $portal_settings['autop_url'] ?>" placeholder="" disabled/>
                                                <button class="btn btn-outline-secondary" type="button" data-copy-target="autop_url">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="card-footer py-4">
                                    <div class="row">
                                        <div class="col d-flex justify-content-end">
                                            <?php if ($API->checkPermissions('admin-portalsettings', 'RW', true)) { ?>
                                                <button type="submit" class="btn btn-primary">
                                                    <i class="fas fa-edit"></i> <?php echo __('edit') ?>
                                                </button>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                <?php } ?>
                <?php if ($API->checkPermissions('admin-mailsettings', 'RO', true)) { ?>
                    <div class="tab-pane fade show active" id="mail-settings" role="tabpanel" aria-labelledby="mail-settings-tab">
                        <div class="row mb-3">
                            <div class="col-12">
                                <h2>
                                    <i class="fa-solid fa-envelope"></i> &nbsp; <?php echo __('mail_settings') ?>
                                </h2>
                            </div>
                        </div>
                        <div class="card">
                            <form id="FormValidation" method="post" action="/api/v1/portal-management/mail/configure/">
                                <input type="hidden" name="portal_uuid" value="<?php echo $portal_settings['portal_uuid']; ?>">
                                <input type="hidden" name="X-HTTP-Method-Override" value="PUT">
                                <input type="hidden" name="_return" value="/systemconfig/#mail-settings">
                                <div class="card-body">
                                    <div class="form-group form-show-validation row">
                                        <label for="mail_from_name" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('mail_from_name') ?></label>
                                        <div class="col-lg-9 col-md-12 col-sm-10">
                                            <input type="text" class="form-control" id="mail_from_name" name="mail_from_name" value="<?php echo $portal_settings['mail_from_name'] ?>" placeholder="" required/>
                                        </div>
                                    </div>
                                    <div class="form-group form-show-validation row">
                                        <label for="mail_from_address" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('mail_from_address') ?></label>
                                        <div class="col-lg-9 col-md-12 col-sm-10">
                                            <label>
                                                <input type="email" class="form-control" name="mail_from_address" value="<?php echo $portal_settings['mail_from_address'] ?>" placeholder="" required/>
                                            </label>
                                        </div>
                                    </div>
                                    <div class="form-group form-show-validation row">
                                        <label for="mail_smtp_host" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('mail_smtp_host') ?></label>
                                        <div class="col-lg-9 col-md-12 col-sm-10">
                                            <label>
                                                <input type="text" class="form-control" name="mail_smtp_host" value="<?php echo $portal_settings['mail_smtp_host'] ?>" placeholder="" required/>
                                            </label>
                                        </div>
                                    </div>

                                    <div class="form-group form-show-validation row">
                                        <label for="mail_smtp_secure" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('mail_smtp_secure') ?></label>
                                        <div class="col-lg-9 col-md-12 col-sm-10">
                                            <select id="mail_smtp_secure" name="mail_smtp_secure" class="form-control" required>
                                                <option value="tls" <?php echo ($portal_settings['mail_smtp_secure'] == 'tls') ? 'selected' : '' ?>>TLS (standard port 587)</option>
                                                <option value="ssl" <?php echo ($portal_settings['mail_smtp_secure'] == 'ssl') ? 'selected' : '' ?>>SSL (standard port 465)</option>
                                                <option value="no" <?php echo ($portal_settings['mail_smtp_secure'] == 'no') ? 'selected' : '' ?>>No Encryption (standard port 25)</option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="form-group form-show-validation row">
                                        <label for="mail_smtp_port" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('mail_smtp_port') ?></label>
                                        <div class="col-lg-2 col-md-2 col-sm-2">
                                            <input type="number" class="form-control" id="mail_smtp_port" name="mail_smtp_port" value="<?php echo $portal_settings['mail_smtp_port'] ?>" placeholder="" required/>
                                        </div>
                                    </div>


                                    <div class="form-group form-show-validation row">
                                        <label for="mail_smtp_auth" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('mail_smtp_auth') ?></label>
                                        <div class="col-lg-2 col-md-2 col-sm-2">
                                            <select id="mail_smtp_auth" name="mail_smtp_auth" class="form-control" required>
                                                <option value="1" <?php echo ($portal_settings['mail_smtp_auth'] == 1) ? 'selected' : '' ?>>true</option>
                                                <option value="0" <?php echo ($portal_settings['mail_smtp_auth'] == 0) ? 'selected' : '' ?>>false</option>
                                            </select>
                                        </div>
                                    </div>

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

                                    <div class="form-group form-show-validation row">
                                        <label for="mail_smtp_pass" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('mail_smtp_pass') ?></label>
                                        <div class="col-lg-9 col-md-12 col-sm-10">
                                            <input type="password" class="form-control" id="mail_smtp_pass" name="mail_smtp_pass" value="*******************" placeholder="" autocomplete="new-password" required/>
                                        </div>
                                    </div>

                                </div>
                                <div class="card-footer py-4">
                                    <div class="row">
                                        <div class="col d-flex justify-content-end">
                                            <a href="#" class="btn btn-success mx-2" data-bs-toggle="modal" data-bs-target="#testMailModal">
                                                <i class="fa-solid fa-envelope-circle-check"></i> <?php echo __('test') ?>
                                            </a>
                                            <?php if ($API->checkPermissions('admin-mailsettings', 'RW', true)) { ?>
                                                <button type="submit" class="btn btn-primary">
                                                    <i class="fas fa-edit"></i> <?php echo __('edit') ?>
                                                </button>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            </form>
                            <div class="modal fade" id="testMailModal" tabindex="-1" aria-labelledby="infoModalLabel" aria-hidden="true">
                                <div class="modal-dialog modal-dialog-centered modal-lg">
                                    <div class="modal-content bg-black2">
                                        <div class="modal-header">
                                            <h5 class="modal-title" id="infoModalLabel">
                                                <i class="fa-solid fa-envelope-circle-check"></i> <?php echo __('send_test_mail') ?>
                                            </h5>
                                            <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                                        </div>
                                        <div class="modal-body">
                                            <p>
                                                <?php echo __('send_test_mail_explanation') ?>
                                            </p>
                                            <form id="FormValidation" method="post" action="/api/v1/portal-management/mail/test/">
                                                <input type="hidden" name="portal_uuid" value="<?php echo $portal_settings['portal_uuid']; ?>">
                                                <input type="hidden" name="X-HTTP-Method-Override" value="POST">
                                                <input type="hidden" name="_return" value="/systemconfig/#mail-settings">
                                                <div class="form-group form-show-validation row">
                                                    <label for="mail_to" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('mail_to') ?>:</label>
                                                    <div class="col-lg-9 col-md-12 col-sm-10">
                                                        <input type="email" class="form-control" id="mail_to" name="mail_to" value="" placeholder="Send test mail to..." required/>
                                                    </div>
                                                </div>
                                                <hr class="text-light">
                                                <div class="row">
                                                    <div class="col d-flex justify-content-end">
                                                        <button type="submit" class="btn btn-success">
                                                            <i class="fa-solid fa-paper-plane"></i> <?php echo __('send_mail') ?>
                                                        </button>
                                                    </div>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php } ?>
                <?php if ($API->checkPermissions('admin-modules', 'RO', true)) { ?>
                    <div class="tab-pane fade show" id="modules" role="tabpanel" aria-labelledby="modules-tab">
                        <div class="row mb-3">
                            <div class="col-12">
                                <h2>
                                    <i class="fa-solid fa-cubes"></i> &nbsp; <?php echo __('modules') ?>
                                </h2>
                            </div>
                        </div>

                        <div class="card p-3">
                            <form id="FormValidation" method="post" action="/api/v1/modules/">
                                <input type="hidden" name="portal_uuid" value="<?php echo $portal_settings['portal_uuid']; ?>">
                                <input type="hidden" name="X-HTTP-Method-Override" value="PUT">
                                <input type="hidden" name="_return" value="/systemconfig/#modules">
                                <table class="multi-filter-select display table table-striped table-hover" data-skip-columns="0,5">
                                    <thead>
                                    <tr>
                                        <th><?php echo __('module_name') ?></th>
                                        <th><?php echo __('enabled') ?></th>
                                        <th><?php echo __('configure') ?></th>
                                    </tr>
                                    </thead>
                                    <tfoot>
                                    <tr>
                                        <th><?php echo __('module_name') ?></th>
                                        <th><?php echo __('enabled') ?></th>
                                        <th><?php echo __('configure') ?></th>
                                    </tr>
                                    </tfoot>
                                    <tbody>
                                    <?php
                                    foreach ($system_modules as $module) { ?>
                                        <tr>
                                            <td><?php echo $module['module_name'] ?> </td>
                                            <td>
                                                <label class="switch">

                                                    <input type="checkbox" class="checkbox" data-api-url="/api/v1/portal-management/modules/" data-api-data='<?php echo json_encode(['module_uuid' => $module['module_uuid'], 'module_enabled' => $module['module_enabled'] ? 1 : 0]) ?>' data-api-changevalue="module_enabled"
                                                            <?php echo(($module['module_enabled']) ? 'checked' : '') ?>
                                                            <?php echo ($API->checkPermissions('admin-modules', 'RW', true)) ? '' : 'disabled' ?>
                                                            <?php echo(($module['module_slugify'] == 'system') ? 'disabled' : '') ?>>
                                                    <span class="slider"></span>
                                                </label>
                                            </td>
                                            <td class="text-nowrap">
                                                <?php if ($module['module_slugify'] === 'office') { ?>
                                                    <a href="/portal-management/modules/office/" class="btn btn-primary btn-sm btn-rounded"><i class="fa-solid fa-gear"></i></a>
                                                <?php } ?>
                                            </td>
                                        </tr>
                                    <?php } ?>
                                    </tbody>
                                </table>
                            </form>
                        </div>
                    </div>
                <?php } ?>

                <?php if ($API->checkPermissions('admin-sources', 'RO', true)) { ?>
                    <div class="tab-pane fade show" id="sources" role="tabpanel" aria-labelledby="sources-tab">
                        <div class="row mb-3">
                            <div class="col-12">
                                <h2>
                                    <i class="fa-solid fa-cloud"></i> &nbsp; <?php echo __('sources') ?>
                                </h2>
                            </div>
                        </div>

                        <div class="card p-3">
                            <div class="row row-cols-1 row-cols-md-3 g-4">
                                <div class="col">
                                    <div class="card card-post card-round">
                                        <img class="card-img-top" src="/src/images/sources/inserve/inserve_logo.webp" alt="Inserve Logo">
                                        <div class="card-body">
                                            <h3 class="card-title">
                                                Inserve </h3>
                                            <p class="card-text">
                                                Inserve is Dutch software that helps MSPs support their customers in a personal and structured way. </p>
                                            <a href="/portal-management/sources/inserve" class="btn btn-primary">View</a>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>
                <?php } ?>
            </div>
        </div>
    </div>
</div>