<?php

//  define('SHOW_VARIABLES', 1);
//  define('DEBUG_LEVEL', 1);

//  error_reporting(E_ALL ^ E_NOTICE);
//  ini_set('display_errors', 'On');

set_include_path('.' . PATH_SEPARATOR . get_include_path());


include_once dirname(__FILE__) . '/' . 'components/utils/system_utils.php';
include_once dirname(__FILE__) . '/' . 'components/mail/mailer.php';
include_once dirname(__FILE__) . '/' . 'components/mail/phpmailer_based_mailer.php';
require_once dirname(__FILE__) . '/' . 'database_engine/mysql_engine.php';

//  SystemUtils::DisableMagicQuotesRuntime();

SystemUtils::SetTimeZoneIfNeed('America/Argentina/Buenos_Aires');

function GetGlobalConnectionOptions()
{
    return
        array(
          'server' => '104.154.74.30',
          'port' => '3306',
          'username' => 'DB0_MasterUser',
          'password' => 'BDZer0_-_M@sTT&er*2023',
          'database' => 'Amigo',
          'client_encoding' => 'utf8'
        );
}

function HasAdminPage()
{
    return true;
}

function HasHomePage()
{
    return true;
}

function GetHomeURL()
{
    return 'index.php';
}

function GetHomePageBanner()
{
    return '';
}

function GetPageGroups()
{
    $result = array();
    $result[] = array('caption' => 'VENDAS', 'description' => '');
    $result[] = array('caption' => 'CADASTROS', 'description' => '');
    $result[] = array('caption' => 'NF ELETRÔNICA', 'description' => '');
    return $result;
}

function GetPageInfos()
{
    $result = array();
    $result[] = array('caption' => 'Orçamento / Pedido Interno', 'short_caption' => 'Orçamento / Pedido Interno', 'filename' => 'pedido.php', 'name' => 'pedido', 'group_name' => 'VENDAS', 'add_separator' => false, 'description' => '');
    $result[] = array('caption' => 'Clientes', 'short_caption' => 'Clientes', 'filename' => 'cadastro.php', 'name' => 'cadastro', 'group_name' => 'VENDAS', 'add_separator' => false, 'description' => '');
    $result[] = array('caption' => 'CFOP', 'short_caption' => 'CFOP', 'filename' => 'cfop.php', 'name' => 'cfop', 'group_name' => 'CADASTROS', 'add_separator' => false, 'description' => '');
    $result[] = array('caption' => 'NCM', 'short_caption' => 'NCM', 'filename' => 'ncm.php', 'name' => 'ncm', 'group_name' => 'CADASTROS', 'add_separator' => false, 'description' => '');
    $result[] = array('caption' => 'Estado', 'short_caption' => 'Estado', 'filename' => 'estado.php', 'name' => 'estado', 'group_name' => 'CADASTROS', 'add_separator' => false, 'description' => '');
    $result[] = array('caption' => 'Condicao Pg', 'short_caption' => 'Condição Pagto', 'filename' => 'condicao_pg.php', 'name' => 'condicao_pg', 'group_name' => 'CADASTROS', 'add_separator' => false, 'description' => '');
    $result[] = array('caption' => 'Transportadora', 'short_caption' => 'Transportadora', 'filename' => 'transportadora.php', 'name' => 'transportadora', 'group_name' => 'CADASTROS', 'add_separator' => false, 'description' => '');
    $result[] = array('caption' => 'Produto', 'short_caption' => 'Produto', 'filename' => 'produto.php', 'name' => 'produto', 'group_name' => 'CADASTROS', 'add_separator' => false, 'description' => '');
    $result[] = array('caption' => 'Vendedor', 'short_caption' => 'Vendedor', 'filename' => 'vendedor.php', 'name' => 'vendedor', 'group_name' => 'CADASTROS', 'add_separator' => false, 'description' => '');
    $result[] = array('caption' => 'Caixa de Entrada', 'short_caption' => 'Notas', 'filename' => 'notas.php', 'name' => 'notas', 'group_name' => 'NF ELETRÔNICA', 'add_separator' => false, 'description' => '');
    $result[] = array('caption' => 'Notas Finalizadas', 'short_caption' => 'Notas Finalizadas', 'filename' => 'Notas_Finalizadas.php', 'name' => 'Notas Finalizadas', 'group_name' => 'NF ELETRÔNICA', 'add_separator' => true, 'description' => '');
    $result[] = array('caption' => 'Notas Canceladas', 'short_caption' => 'Notas Canceladas', 'filename' => 'Notas_Canceladas.php', 'name' => 'Notas Canceladas', 'group_name' => 'NF ELETRÔNICA', 'add_separator' => false, 'description' => '');
    $result[] = array('caption' => 'Licença', 'short_caption' => 'Licencas', 'filename' => 'licencas.php', 'name' => 'licencas', 'group_name' => 'CADASTROS', 'add_separator' => false, 'description' => '');
    $result[] = array('caption' => 'Tabelas', 'short_caption' => 'Tabelas', 'filename' => 'tabelas.php', 'name' => 'tabelas', 'group_name' => 'CADASTROS', 'add_separator' => false, 'description' => '');
    return $result;
}

function GetPagesHeader()
{
    return
        '<span class="navbar-brand">
    <span>
        <img src="logo.png" style="height: 60px; margin-top: -5px;">
    </span>
</span>';
}

function GetPagesFooter()
{
    return
        '';
}

function ApplyCommonPageSettings(Page $page, Grid $grid)
{
    $page->SetShowUserAuthBar(true);
    $page->setShowNavigation(false);
    $page->OnCustomHTMLHeader->AddListener('Global_CustomHTMLHeaderHandler');
    $page->OnGetCustomTemplate->AddListener('Global_GetCustomTemplateHandler');
    $page->OnGetCustomExportOptions->AddListener('Global_OnGetCustomExportOptions');
    $page->getDataset()->OnGetFieldValue->AddListener('Global_OnGetFieldValue');
    $page->getDataset()->OnGetFieldValue->AddListener('OnGetFieldValue', $page);
    $grid->BeforeUpdateRecord->AddListener('Global_BeforeUpdateHandler');
    $grid->BeforeDeleteRecord->AddListener('Global_BeforeDeleteHandler');
    $grid->BeforeInsertRecord->AddListener('Global_BeforeInsertHandler');
    $grid->AfterUpdateRecord->AddListener('Global_AfterUpdateHandler');
    $grid->AfterDeleteRecord->AddListener('Global_AfterDeleteHandler');
    $grid->AfterInsertRecord->AddListener('Global_AfterInsertHandler');
}

function GetAnsiEncoding() { return 'windows-1252'; }

function Global_OnGetCustomPagePermissionsHandler(Page $page, PermissionSet &$permissions, &$handled)
{

}

function Global_CustomHTMLHeaderHandler($page, &$customHtmlHeaderText)
{

}

function Global_GetCustomTemplateHandler($type, $part, $mode, &$result, &$params, CommonPage $page = null)
{

}

function Global_OnGetCustomExportOptions($page, $exportType, $rowData, &$options)
{

}

function Global_OnGetFieldValue($fieldName, &$value, $tableName)
{

}

function Global_GetCustomPageList(CommonPage $page, PageList $pageList)
{

}

function Global_BeforeInsertHandler($page, &$rowData, $tableName, &$cancel, &$message, &$messageDisplayTime)
{

}

function Global_BeforeUpdateHandler($page, $oldRowData, &$rowData, $tableName, &$cancel, &$message, &$messageDisplayTime)
{

}

function Global_BeforeDeleteHandler($page, &$rowData, $tableName, &$cancel, &$message, &$messageDisplayTime)
{

}

function Global_AfterInsertHandler($page, $rowData, $tableName, &$success, &$message, &$messageDisplayTime)
{

}

function Global_AfterUpdateHandler($page, $oldRowData, $rowData, $tableName, &$success, &$message, &$messageDisplayTime)
{

}

function Global_AfterDeleteHandler($page, $rowData, $tableName, &$success, &$message, &$messageDisplayTime)
{

}

function GetDefaultDateFormat()
{
    return 'd.m.Y';
}

function GetFirstDayOfWeek()
{
    return 0;
}

function GetPageListType()
{
    return PageList::TYPE_MENU;
}

function GetNullLabel()
{
    return null;
}

function UseMinifiedJS()
{
    return true;
}

function GetOfflineMode()
{
    return false;
}

function GetInactivityTimeout()
{
    return 0;
}

function GetMailer()
{
    $mailerOptions = new MailerOptions(MailerType::Sendmail, '', '');
    
    return PHPMailerBasedMailer::getInstance($mailerOptions);
}

function sendMailMessage($recipients, $messageSubject, $messageBody, $attachments = '', $cc = '', $bcc = '')
{
    GetMailer()->send($recipients, $messageSubject, $messageBody, $attachments, $cc, $bcc);
}

function createConnection()
{
    $connectionOptions = GetGlobalConnectionOptions();
    $connectionOptions['client_encoding'] = 'utf8';

    $connectionFactory = MyConnectionFactory::getInstance();
    return $connectionFactory->CreateConnection($connectionOptions);
}
