<?php

///////////////////////////////////////////////////////////////////////////////

define('THIS_TOOL_NAME', 'portfolio');
define('PORTFOLIO_CONFIG_KEY', str_replace('-', '_', THIS_TOOL_NAME) . '_tool');

///////////////////////////////////////////////////////////////////////////////

$tpconfig = __DIR__. DIRECTORY_SEPARATOR . "config.tools.php";
if(is_file($tpconfig))
	require $tpconfig;

///////////////////////////////////////////////////////////////////////////////

include "includes/base-init.php";
include "includes/head-portfolio.php";
?>

<body>

<div class="theme-select theme-bg" id="themechange"></div>
<div class="menu-select theme-bg" id="menuselect"></div>
<div id="contentwrap">
<?php include "includes/head-logo-toolbar.php" ?>
<div id="content">
	<div id="domain-portfolio-header"></div>
<?php include "includes/filters-portfolio.php"; ?>
	<div id="domain-portfolio-table"></div>
	<div id="domain-portfolio-footer">
		<button class="btn btn-sm btn-success"  id="btn-add-group"         title="Add domains to one or more Categories">Add To Group</button>
		<button class="btn btn-sm btn-primary"  id="btn-remove-group"      title="Remove domains from the current Category">Remove From Group</button>
		<button class="btn btn-sm btn-danger"   id="btn-mark-sold"         title="Add domains to the &quot;Sold Domains&quot; category"    style="display:none">Mark Sold</button>
		<button class="btn btn-sm btn-danger"   id="btn-add-ignore"        title="Add domains to the &quot;Ignored Domains&quot; category"  style="display:none">Add To Ignore List</button>
		<button class="btn btn-sm btn-danger"   id="btn-mark-deleted"      title="Add domains to the &quot;Deleted Domains&quot; category"  style="display:none">Mark Deleted</button>
		<button class="btn btn-sm btn-danger"   id="btn-delete-permanent"  title="Permanently delete domains from the database">Delete Permanently</button>
		<button class="btn btn-sm btn-secondary" id="btn-download-all"      title="Download all domains and columns to a CSV file"><img src="lib/layouts/colors/light/images/64/toolbar/file-download.png" class="pf-filter-btn-icon" alt=""></button>
		<button class="btn btn-sm btn-secondary" id="btn-scheduled-reports" title="View and download previously scheduled reports" style="display:none"><img src="lib/layouts/colors/light/images/64/toolbar/reports.png" class="pf-filter-btn-icon" alt=""></button>
	</div>
	<div id="csvdownload" style="display:none"></div>
</div>
</div>


<?php
///////////////////////////////////////////////////////////////////////////////
$jsApiBaseUrl      = htmlspecialchars($apiBaseUrl, ENT_QUOTES, 'UTF-8');
$jsExcludedColumns = json_encode($excludedColumns);
$jsPostedDynamic   = json_encode($postedDynamic);
///////////////////////////////////////////////////////////////////////////////
?>

<?php
$portfolioNameserverDisplayLimit = 0;
if (isset($PORTFOLIO_NAMESERVER_DISPLAY_LIMIT)) {
    $portfolioNameserverDisplayLimit = (int)$PORTFOLIO_NAMESERVER_DISPLAY_LIMIT;
    if ($portfolioNameserverDisplayLimit < 0) {
        $portfolioNameserverDisplayLimit = 0;
    }
}

$portfolioCategoryLayout = 'top';
if (isset($PORTFOLIO_CATEGORY_LAYOUT)) {
    $portfolioCategoryLayout = strtolower(trim((string)$PORTFOLIO_CATEGORY_LAYOUT));
    if (!in_array($portfolioCategoryLayout, ['top', 'left'], true)) {
        $portfolioCategoryLayout = 'top';
    }
}

$portfolioSearchFilterMode = 'bw';
if (isset($PORTFOLIO_DEFAULT_SEARCH_FILTER_MODE)) {
    $portfolioSearchFilterMode = strtolower(trim((string)$PORTFOLIO_DEFAULT_SEARCH_FILTER_MODE));
    if (!in_array($portfolioSearchFilterMode, ['bw', 'ew', 'cn', 'eq'], true)) {
        $portfolioSearchFilterMode = 'bw';
    }
}

$portfolioDomainsOfInterestName = '';
if (!empty($PORTFOLIO_DOMAINS_OF_INTEREST) && is_string($PORTFOLIO_DOMAINS_OF_INTEREST)) {
    $portfolioDomainsOfInterestName = trim($PORTFOLIO_DOMAINS_OF_INTEREST);
}

$portfolioStickyColumns = true;
if (isset($PORTFOLIO_STICKY_COLUMNS)) {
    $portfolioStickyColumns = ($PORTFOLIO_STICKY_COLUMNS === true);
}

// Inline editable fields — sanitize before emitting to JS
$_blockedEditFields = ['id', 'sid', 'domain', 'tld', 'ascii_domain', 'r_h_disp'];
$_validEditTypes    = ['text'];
$portfolioEditableFields = [];

if (!empty($PORTFOLIO_EDITABLE_FIELDS) && is_array($PORTFOLIO_EDITABLE_FIELDS)) {
    foreach ($PORTFOLIO_EDITABLE_FIELDS as $entry) {
        if (!is_array($entry))            continue;
        if (empty($entry['field']))       continue;
        if (!preg_match('/^[A-Za-z0-9_]+$/', $entry['field'])) continue;
        if (in_array($entry['field'], $_blockedEditFields, true)) continue;
        if (isset($entry['disable']) && $entry['disable'] === true) continue;

        $type  = (isset($entry['type']) && in_array($entry['type'], $_validEditTypes, true))
                 ? $entry['type'] : 'text';
        $label = (isset($entry['label']) && is_string($entry['label']) && $entry['label'] !== '')
                 ? $entry['label'] : $entry['field'];

        $portfolioEditableFields[] = [
            'field' => $entry['field'],
            'label' => $label,
            'type'  => $type,
        ];
    }
}
?>

<script>
var baseurl           = "<?php echo $baseUrl; ?>";
var API_BASE_URL      = <?php echo json_encode($jsApiBaseUrl); ?>;
var IS_ADMIN          = <?php echo $effectiveIsAdmin ? 'true' : 'false'; ?>;
var REAL_ADMIN        = <?php echo $isAdmin ? 'true' : 'false'; ?>;
var VIEW_MODE         = <?php echo json_encode($viewMode); ?>;
var CURRENT_USER      = <?php echo json_encode($currentUserName); ?>;
var PORTFOLIO_CONFIG_KEY = "<?php echo PORTFOLIO_CONFIG_KEY; ?>";
var CONFIG_CREATED_AT = <?php echo time(); ?>;
var SHOW_TOOL_TIPS    = false;
var hidden_categories = [
	"Low Traffic Domains",
	"Keyword Domains", "High Traffic Domains", "Important Domains"

];
var PORTFOLIO_NAMESERVER_DISPLAY_LIMIT = <?= (int)$portfolioNameserverDisplayLimit ?>;
var PORTFOLIO_CATEGORY_LAYOUT = <?= json_encode($portfolioCategoryLayout) ?>;
var PORTFOLIO_DEFAULT_SEARCH_FILTER_MODE = <?= json_encode($portfolioSearchFilterMode) ?>;
var PORTFOLIO_DOMAINS_OF_INTEREST_NAME  = <?= json_encode($portfolioDomainsOfInterestName) ?>;
var PORTFOLIO_STICKY_COLUMNS = <?= $portfolioStickyColumns ? 'true' : 'false' ?>;
var PORTFOLIO_EDITABLE_FIELDS = <?= json_encode($portfolioEditableFields, JSON_UNESCAPED_UNICODE) ?>;
</script>
<script type="text/javascript" src="lib/jquery/jquery-3.5.1.min.js"></script>
<script type="text/javascript" src="lib/jquery/jquery-ui.min.js"></script>
<script type="text/javascript" src="lib/jquery/jquery.cookie.js"></script>

<script type="text/javascript" src="tools/js/datatable.js"></script>
<script type="text/javascript" src="tools/js/tooltip.js"></script>
<script type="text/javascript" src="tools/js/theme.js"></script>
<script type="text/javascript" src="tools/js/cookie.js"></script>
<script type="text/javascript" src="tools/js/modules.js"></script>
<script type="text/javascript" src="tools/js/sedapi.js"></script>

<script type="text/javascript" src="lib/js/dayjs.min.js"></script>
<script type="text/javascript" src="tools/js/common.js"></script>
<script type="text/javascript" src="tools/js/ui.js"></script>
<script type="text/javascript" src="tools/js/domainColumnDefs.js"></script>
<script type="text/javascript" src="tools/js/portfolio-editable.js"></script>
<script type="text/javascript" src="tools/js/portfolio.js"></script>
<script type="text/javascript" src="../api.php?c=js&file=data.js&loc=modules/login"></script>
<script type="text/javascript" src="tools/js/columns.js"></script> 
<script type="text/javascript" src="tools/js/domainactions.js"></script>
<script type="text/javascript" src="tools/js/portfolio-filters.js"></script>
</body>
</html>