<?php

///////////////////////////////////////////////////////////////////////////////
// Tool Configuration
///////////////////////////////////////////////////////////////////////////////

define('THIS_TOOL_NAME', 'export-domains');
define('EXPORT_DOMAINS_CONFIG_KEY', 'export_domains_tool');

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

include "includes/base-init.php";

?><!DOCTYPE HTML>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <base href="<?php echo $baseUrl; ?>">
    <link rel="shortcut icon" type="image/x-icon" href="lib/layouts/images/favicon.ico">
    <meta name="author" content="">
    <title>Watch My Domains Server Edition v6</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="dcterms.rightsHolder" content="Softnik Technologies">

    <link rel="preload" as="font" href="lib/layouts/fonts/Quicksand/quicksand-v28-latin-regular.woff2" type="font/woff2" crossorigin="anonymous">
    <link rel="preload" as="font" href="lib/layouts/fonts/Roboto-Slab/roboto-slab-v22-latin-regular.woff2" type="font/woff2" crossorigin="anonymous">
    <link rel="preload" as="font" href="lib/layouts/fonts/Josefin-Sans/josefin-sans-v23-latin-regular.woff2" type="font/woff2" crossorigin="anonymous">
    <link rel="preload" as="font" href="lib/layouts/fonts/Quicksand/quicksand-v28-latin-700.woff2" type="font/woff2" crossorigin="anonymous">
    <link rel="preload" as="font" href="lib/layouts/fonts/Roboto-Slab/roboto-slab-v22-latin-700.woff2" type="font/woff2" crossorigin="anonymous">
    <link rel="preload" as="font" href="lib/layouts/fonts/Josefin-Sans/josefin-sans-v23-latin-600.woff2" type="font/woff2" crossorigin="anonymous">
    <link rel="preload" as="font" href="lib/layouts/fonts/Open-Sans/open-sans-v40-greek_hebrew_latin_vietnamese-regular.woff2" type="font/woff2" crossorigin="anonymous">
    <link rel="preload" as="font" href="lib/layouts/fonts/Open-Sans/open-sans-v40-greek_hebrew_latin_vietnamese-700.woff2" type="font/woff2" crossorigin="anonymous">

    <link href="lib/layouts/fonts/Quicksand/fonts.css" rel="stylesheet">
    <link href="lib/layouts/fonts/Roboto-Slab/fonts.css" rel="stylesheet">
    <link href="lib/layouts/fonts/Josefin-Sans/fonts.css" rel="stylesheet">
    <link href="lib/layouts/fonts/Open-Sans/fonts.css" rel="stylesheet">
    <link href="lib/layouts/css/min/common.min.css" rel="stylesheet">
    <link href="lib/layouts/modules/common/css/min/common.min.css" rel="stylesheet">

    <script src="api.php?c=js&file=data.js&loc=modules/toolbox" type="text/javascript"></script>

    <link rel="stylesheet" type="text/css" href="lib/layouts/colors/light/light.css" id="theme-mode-css">
    <link rel="stylesheet" type="text/css" href="lib/layouts/colors/light/themes/Default/default.css" id="theme-default-css">
    <link rel="stylesheet" type="text/css" href="lib/layouts/colors/light/themes/Default/styles.css" id="theme-styles-css">
    <link rel="stylesheet" type="text/css" href="lib/php/styles.php" id="theme-bg-css">

    <link href="tools/css/common.css" rel="stylesheet">
    <link href="tools/css/common-dark.css" rel="stylesheet">
    <link href="tools/css/toggle.css" rel="stylesheet">
    <link href="tools/css/export-domains.css" rel="stylesheet">
</head>
<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">

        <?php if (!$isAdmin): ?>
        <div id="menucontrol">
            <?php include "includes/export-domains-sidebar.php"; ?>
        </div>
        <?php endif; ?>

        <div id="maincontent">
            <div id="domaindata">
                <div class="export-domain-panel">

                    <!-- ==================== PANEL HEADER ==================== -->
                    <div class="panel-header">
                        <h1>Export Domains</h1>
                        <?php if ($isAdmin): ?>
                            <button type="button" class="btn btn-secondary btn-sm" id="btn_open_drawer">
                                ⚙ Configure
                            </button>
                        <?php else: ?>
                            <!-- User: dataset selector sits in main area too for convenience -->
                            <div class="user-dataset-bar">
                                <select id="dataset_select_main" class="dataset-select-inline">
                                    <option value="">No data set selected</option>
                                </select>
                            </div>
                        <?php endif; ?>
                    </div>

                    <?php if ($isAdmin): ?>
                    <!-- Admin dataset bar in main area -->
                    <div class="admin-dataset-bar">
                        <select id="dataset_select" class="dataset-select-inline">
                            <option value="">No data set selected</option>
                        </select>
                        <button type="button" class="btn btn-secondary btn-sm" id="btn_new_dataset">+ New</button>
                    </div>
                    <?php endif; ?>

                    <!-- ==================== COLUMNS TABLE ==================== -->
                    <div class="field-row">
                        <label>Columns included in export</label>
                        <div class="help-text">
                            Customise the CSV column headings below. Leave blank to use the default name.
                            <span class="user-session-note">Heading changes apply to this session only.</span>
                        </div>
                        <div class="help-text">
                            Selected columns appear first in the order shown. When <strong>All Columns</strong> is active, remaining data columns are appended alphabetically.
                        </div>
                        <div id="selected-columns-config" class="selected-columns-config">
                            <div class="no-columns-msg">
                                <?php if ($isAdmin): ?>
                                    Click <strong>⚙ Configure</strong> to select columns and set up data sets.
                                <?php else: ?>
                                    Load a data set from the left panel to get started.
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>

                    <!-- ==================== EXPORT OPTIONS ==================== -->
                    <div class="export-options-section">
                        <div class="field-row">
                            <label for="bool_format">Boolean display</label>
                            <select id="bool_format">
                                <option value="10">1 / 0</option>
                                <option value="YN">Y / N</option>
                                <option value="YESNO">Yes / No</option>
                                <option value="TF">True / False</option>
                                <option value="EN">Enabled / Disabled</option>
                            </select>
                            <div class="help-text">How Yes/No values appear in the exported CSV.</div>
                        </div>
                        <div class="field-row">
                            <label for="rows_per_page">Export batch size</label>
                            <input type="number" id="rows_per_page" value="500" min="50" max="5000">
                            <div class="help-text">Controls how much data is processed at a time. Leave at 500 unless you have a reason to change it.</div>
                        </div>
                    </div>

                    <!-- ==================== SUMMARY ==================== -->
                    <div class="export-summary-section">
                        <div class="export-summary-label">Export summary</div>
                        <div id="export-summary" class="export-summary">No data set loaded.</div>
                    </div>

                    <!-- ==================== ACTIONS ==================== -->
                    <div class="form-actions-section">
                        <div id="status-area" class="submit-status-text"></div>
                        <button type="button" class="btn btn-secondary" id="btn_include_all_columns">All Columns</button>
                        <button type="button" class="btn btn-secondary" id="btn_preview">Preview 20 rows</button>
                        <button type="button" class="btn btn-primary" id="btn_export">Download CSV</button>
                    </div>

                    <!-- ==================== PREVIEW ==================== -->
                    <div id="preview-area" class="preview-area"></div>

                </div>
            </div>
        </div>

    </div>

</div>

<?php if ($isAdmin): ?>
<!-- ==================== ADMIN CONFIG DRAWER ==================== -->
<div id="drawer-overlay" class="drawer-overlay"></div>
<div id="admin-drawer" class="admin-drawer">

    <div class="drawer-header">
        <h2>Export Configuration</h2>
        <button type="button" class="drawer-close-btn" id="btn_close_drawer">&times;</button>
    </div>

    <div class="drawer-body">

        <!-- Left: Column list -->
        <div class="drawer-col-list">
            <div class="drawer-section-title">
                Select &amp; order columns
                <span class="drawer-hint">Drag to reorder</span>
            </div>
            <div class="drawer-search-wrap">
                <input type="text" id="column-search" class="sidebar-search"
                       placeholder="Search fields…" autocomplete="off">
            </div>
            <div id="columns-list" class="columns-list drawer-columns-list">
                Loading fields…
            </div>
        </div>

        <!-- Right: Computed + Datasets -->
        <div class="drawer-col-config">

            <!-- Computed columns -->
            <div class="drawer-section">
                <div class="drawer-section-title">Computed Columns</div>

                <div id="computed-list" class="computed-list">None defined.</div>

                <div class="computed-add-label">
                    Add computed column
                    <button type="button" class="help-toggle-btn" id="btn_computed_help">help</button>
                </div>

                <div id="computed-help" class="computed-help">
                    <div class="computed-help-block">
                        <div class="computed-help-title">Invert a boolean field</div>
                        <code>!field_name</code> — uses global boolean display format.<br>
                        <code>!field_name:FalseLabel:TrueLabel</code> — custom labels.<br>
                        <span class="computed-help-note">First label = result is false, second = result is true.</span><br>
                        <span class="computed-help-example">e.g. <code>!list_for_sale:Not Listed:Listed</code></span>
                    </div>
                    <div class="computed-help-block">
                        <div class="computed-help-title">Serial index</div>
                        <code>#1</code> — row number starting at 1.<br>
                        <code>#100</code> — row number starting at 100.<br>
                        <span class="computed-help-note">Numbers continue across export pages.</span>
                    </div>
                </div>

                <div class="computed-input-row">
                    <input type="text" id="computed_col_name" placeholder="Name e.g. not_auto_renew">
                    <input type="text" id="computed_col_expr" placeholder="Expr e.g. !auto_renew">
                    <button type="button" class="btn btn-secondary btn-sm btn-block" id="btn_add_computed">Add</button>
                </div>
            </div>

            <!-- Dataset management -->
            <div class="drawer-section">
                <div class="drawer-section-title">Data Set</div>

                <label for="filter_category" class="drawer-field-label">Category filter</label>
                <select id="filter_category">
                    <option value="">All categories</option>
                </select>

                <label for="filter_query" class="drawer-field-label">
                    Query filter <span class="label-note">optional</span>
                </label>
                <input type="text" id="filter_query"
                       placeholder="e.g. d.days_to_expiry &lt; 30">

                <label for="dataset_name" class="drawer-field-label">Save as</label>
                <input type="text" id="dataset_name" placeholder="Data set name…">

                <button type="button" class="btn btn-danger btn-sm btn-block"
                        id="btn_delete_dataset" style="display:none; margin-top:10px;">
                    Delete this data set
                </button>

                <div class="drawer-save-row">
                    <button type="button" class="btn btn-primary btn-block" id="btn_save_dataset">
                        Save as New Dataset
                    </button>
                    <div id="config-save-status" class="submit-status-text"></div>
                </div>
            </div>

        </div>

    </div>

    <div class="drawer-footer">
        <button type="button" class="btn btn-secondary" id="btn_close_drawer_bottom">
            Close
        </button>
    </div>

</div>
<?php endif; ?>

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

<script>
var baseurl                   = "<?php echo $baseUrl; ?>";
var API_BASE_URL              = <?php echo json_encode($jsApiBaseUrl); ?>;
var EXCLUDED_COLUMNS          = <?php echo $jsExcludedColumns; ?>;
var IS_ADMIN                  = <?php echo $isAdmin ? 'true' : 'false'; ?>;
var CURRENT_USER              = <?php echo json_encode($currentUserName); ?>;
var EXPORT_DOMAINS_CONFIG_KEY = "<?php echo EXPORT_DOMAINS_CONFIG_KEY; ?>";
var CONFIG_CREATED_AT         = <?php echo time(); ?>;
</script>

<script type="text/javascript" src="tools/js/common.js"></script>
<script type="text/javascript" src="tools/js/domain-export.js"></script>

<script>
setTheme();
</script>

</body>
</html>