<%@ Page Language="C#" AutoEventWireup="true" UICulture="auto" Culture="auto" meta:resourcekey="PageResource1" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Globalization" %>

<script runat="server">
    String version = WebAppVersion.GetWebAppVersion();

    protected override void InitializeCulture()
    {
        HttpCookie lang = Request.Cookies["siteLanguage"];

        String selectedLanguage = (lang != null) ? lang.Value : "en";
        UICulture = selectedLanguage;
        Culture = selectedLanguage;

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
        base.InitializeCulture();
    }
</script>

<%
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.Now.AddMinutes(-30)); %>

<%--view template starts here (will be appended inside a row-fluid)--%>

<link href="<%= ResolveUrl("~/app/pages/mes/" + Path.GetFileNameWithoutExtension(this.Request.PhysicalPath) + "/" + Path.GetFileNameWithoutExtension(this.Request.PhysicalPath) + ".css")%>" rel="stylesheet" />
<link href="<%= ResolveUrl("~/assets/ag-grid/ag-grid.css") %>" rel="stylesheet" />
<link href="<%= ResolveUrl("~/assets/ag-grid/ag-theme-alpine.css") %>" rel="stylesheet" />
<link href="<%= ResolveUrl("~/app/components/ag-grid/ag-grid-styles.css") %>" rel="stylesheet" />
<link href="<%= ResolveUrl("~/assets/fonts/reboto-font.css") %>" rel="stylesheet" />
<link href="<%= ResolveUrl("~/assets/tailwind/tailwind.min.css") %>" rel="stylesheet" />

<style>
    .content-container { padding-top: 0px !important; }
    #subnavbar-inner { display: none !important; }

    .professional-title {
        font-size: 22px;
        font-weight: 400;
        color: #202124;
        padding-bottom: 1px;
        border-bottom: 1px solid #dadce0;
        margin-bottom: 2px;
        font-family: 'Roboto', Arial, sans-serif;
    }

    @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
    }
    .spinner {
        animation: spin 1s linear infinite;
        transform-origin: center;
    }
</style>

<h2 class="professional-title">Power Peaks</h2>

<div class="bg-white shadow-md rounded-lg p-2 mb-4">

    <%-- ============ ACTION BAR ============ --%>
    <div class="bg-white shadow-md rounded-lg mb-2">
        <div class="flex items-center justify-between flex-wrap">

            <%-- Auto-refresh control + last metered update --%>
            <div class="flex items-center space-x-3 mb-2">
                <button type="button" id="autoRefreshToggle"
                        class="auto-refresh-pill flex items-center rounded-full px-3 py-1 text-sm font-medium transition-colors"
                        data-bind="classes: { 'auto-on': autoRefreshEnabled, 'auto-off': not(autoRefreshEnabled) }"
                        title="<asp:Localize runat=server meta:resourcekey=auto_refresh_btn_title />">
                    <span class="auto-refresh-dot mr-2" data-bind="classes: { 'is-on': autoRefreshEnabled }"></span>
                    <span data-bind="classes: { hide: not(autoRefreshEnabled) }">
                        <asp:Localize runat="server" meta:resourcekey="auto_refresh_on_label" />&nbsp;<span data-bind="text: countdown"></span>s
                    </span>
                    <span data-bind="classes: { hide: autoRefreshEnabled }">
                        <asp:Localize runat="server" meta:resourcekey="auto_refresh_off_label" />
                    </span>
                </button>

                <div class="text-xs text-gray-500 flex items-center">
                    <i class="fa fa-bolt mr-1"></i>
                    <asp:Localize runat="server" meta:resourcekey="last_metered_update_label" />:&nbsp;
                    <span class="text-gray-700 font-medium" data-bind="text: lastMeteredUpdate">-</span>
                </div>
            </div>

            <%-- Manual refresh. Excel export is handled by AG Grid's right-click context menu. --%>
            <div class="flex space-x-2 mb-2">
                <button id="refreshBtn" class="bg-blue-600 hover:bg-blue-700 text-white py-2 px-4 rounded flex items-center justify-center" style="width: 120px; height: 36.5px; font-size: 14px;" title="<asp:localize runat=server meta:resourcekey=refresh_btn_title />">
                    <div data-bind="classes: { hide: isLoading }">
                        <i class="fa fa-refresh mr-2"></i>
                        <span>Refresh</span>
                    </div>
                    <div data-bind="classes: { hide: not(isLoading) }">
                        <div class="flex items-center">
                            <svg class="h-5 w-5 mr-2" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                                <g class="spinner">
                                    <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" fill="none"/>
                                    <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"/>
                                </g>
                            </svg>
                            <span>Loading...</span>
                        </div>
                    </div>
                </button>
            </div>
        </div>
    </div>

    <%-- ============ KPI TILES (top peak per market) ============ --%>
    <div class="grid grid-cols-1 md:grid-cols-2 gap-3 mb-3">
        <div class="kpi-card kpi-pjm bg-white shadow-md rounded-lg p-3 flex items-center justify-between">
            <div>
                <div class="kpi-label text-xs text-gray-500 uppercase tracking-wide"><asp:Localize runat="server" meta:resourcekey="kpi_pjm_label" /></div>
                <div class="kpi-value text-3xl font-semibold text-gray-800" data-bind="text: topPjmLoad">-</div>
                <div class="kpi-sub text-xs text-gray-500 mt-1">
                    <i class="fa fa-clock-o mr-1"></i>
                    <span data-bind="text: topPjmWhen">-</span>
                </div>
            </div>
            <div class="kpi-icon text-blue-500"><i class="fa fa-bolt fa-2x"></i></div>
        </div>
        <div class="kpi-card kpi-aep bg-white shadow-md rounded-lg p-3 flex items-center justify-between">
            <div>
                <div class="kpi-label text-xs text-gray-500 uppercase tracking-wide"><asp:Localize runat="server" meta:resourcekey="kpi_aep_label" /></div>
                <div class="kpi-value text-3xl font-semibold text-gray-800" data-bind="text: topAepLoad">-</div>
                <div class="kpi-sub text-xs text-gray-500 mt-1">
                    <i class="fa fa-clock-o mr-1"></i>
                    <span data-bind="text: topAepWhen">-</span>
                </div>
            </div>
            <div class="kpi-icon text-amber-500"><i class="fa fa-bolt fa-2x"></i></div>
        </div>
    </div>

    <%-- ============ INFO STRIP ============ --%>
    <div class="info-strip bg-white shadow-md rounded-lg p-2 mb-3 flex items-center flex-wrap">
        <span class="text-xs text-gray-500 mr-3 uppercase tracking-wide"><asp:Localize runat="server" meta:resourcekey="info_label" />:</span>
        <span class="info-chip chip-pjm">PJM = RTO Metered Load</span>
        <span class="info-chip chip-aep">AEP = Utility Metered Load</span>
        <span class="info-chip chip-sdi">SDI = Site Metered Load</span>
        <span class="info-chip chip-info"><i class="fa fa-info-circle mr-1"></i><asp:Localize runat="server" meta:resourcekey="excel_hint" /></span>
    </div>

    <%-- ============ GRIDS ============ --%>
    <div class="grid grid-cols-1 gap-3">

        <div class="peak-card peak-pjm bg-white shadow-md rounded-lg p-2">
            <div class="peak-header flex items-center justify-between mb-2 px-1">
                <h3 class="text-sm font-semibold text-gray-700 m-0"><asp:Localize runat="server" meta:resourcekey="pjm_grid_title" /></h3>
                <span class="text-xs text-gray-500" data-bind="text: pjmCount">0 peaks</span>
            </div>
            <div id="grid-pjm" class="processes-grid-container ag-theme-alpine w-full"></div>
        </div>

        <div class="peak-card peak-aep bg-white shadow-md rounded-lg p-2">
            <div class="peak-header flex items-center justify-between mb-2 px-1">
                <h3 class="text-sm font-semibold text-gray-700 m-0"><asp:Localize runat="server" meta:resourcekey="aep_grid_title" /></h3>
                <span class="text-xs text-gray-500" data-bind="text: aepCount">0 peaks</span>
            </div>
            <div id="grid-aep" class="processes-grid-container ag-theme-alpine w-full"></div>
        </div>

    </div>
</div>

<div class="row-fluid screen-content" id="main-div"></div>
