<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Task.Master" Inherits="System.Web.Mvc.ViewPage<AdministratorConsole.Models.TaskController.TaskSchedulesViewModel>" %>

<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
    <% Html.DevExpress().RenderScripts(Page,
        new Script { ExtensionSuite = ExtensionSuite.GridView }); %>
    <% Html.DevExpress().RenderStyleSheets(Page,
            new StyleSheet { ExtensionSuite = ExtensionSuite.NavigationAndLayout },
            new StyleSheet { ExtensionSuite = ExtensionSuite.Editors },
            new StyleSheet { ExtensionSuite = ExtensionSuite.HtmlEditor },
            new StyleSheet { ExtensionSuite = ExtensionSuite.GridView }
     ); %>

     <script language="javascript" type="text/javascript">
         var baseUrl = "";

         function CustomButtonClicked(s, e) {
             var value = s.GetRowKey(e.visibleIndex);

             if (e.buttonID.toLowerCase() == "deletebutton") {
                 document.location = baseUrl + "DeleteTaskSchedule/" + value;
             }
             else if (e.buttonID.toLowerCase() == "editbutton") {
                 var location = baseUrl + "Edit/" + value;

                 <% if (Model.IsEditInNewWindowOn) { %>
                    window.open(location);
                 <% } else {  %>
                    document.location = location;
                 <% } %>
             }
             else if (e.buttonID.toLowerCase() == "duplicatebutton") {
                 document.location = baseUrl + "Duplicate/" + value;
             }
             else if (e.buttonID.toLowerCase() == "runbutton") {
                 document.location = baseUrl + "Run/" + value;
             }
         }

         function ContextMenuItemClick(s, e) {

             if (e.htmlEvent && e.htmlEvent.target && e.htmlEvent.target.innerHTML === "Edit") {
                 var value = s.GetRowKey(e.index);

                 $("#task-context-menu").data("task-id", value);

                 $(".custom-menu").finish().toggle(100).css({
                         top: event.pageY + "px",
                         left: event.pageX + "px"
                 });

                 $(document).bind("mousedown", hideContextMenu);
             }
             else {
                 e.showBrowserMenu = true;
             }
         }

         function hideContextMenu(e) {

             // If the clicked element is not the menu
             if (!$(e.target).parents(".custom-menu").length > 0) {

                 // Hide it
                 $(".custom-menu").hide(100);
                 $(document).unbind("mousedown", hideContextMenu);
             }
         }

         function openEditInNewTab() {
             var value = $("#task-context-menu").data("task-id");
             var url = baseUrl + "Edit/" + value;
             window.open(url, '_blank').focus();

             $(".custom-menu").hide(100);
             $(document).unbind("mousedown", hideContextMenu);
         }

         function grid_CustomizationWindowCloseUp(s, e) { }

         function UpdateCustomizationWindowVisibility() {
             if (gvDataBinding.IsCustomizationWindowVisible()) gvDataBinding.HideCustomizationWindow();
             else gvDataBinding.ShowCustomizationWindow();
         }

         function DisableCustomizationWindow() {
             if (gvDataBinding.IsCustomizationWindowVisible())
                 gvDataBinding.HideCustomizationWindow();
         }

         window.onload = function () {
             baseUrl = document.getElementById("HiddenCurrentUrl").value;
             baseUrl = baseUrl.substring(0, baseUrl.indexOf("Dummy"));
         };

         $(document).ready(function () {
             $("#btShowCustomizationWindow").click(function () {
                 UpdateCustomizationWindowVisibility();
             });
         });
     </script>
    <div id="content-wrap">
        <% Html.RenderPartial("Navigation/_SideNavigation", Model.MenuNavigationModel); %>
        <div id="content">
	        <h1 class="left-floating-panel">Setup Tasks</h1>
            <div class="right-floating-panel">Server Timezone<br /><b><%= DateTimeHelper.GetTimezoneName() %></b></div>
            <div class="clear"></div>
            <div id="tab-container">
                <ul class="tabs">
			        <li><a href="<%= Url.Action("TaskSchedules", "Task") %>" class="active">Tasks</a></li>
			        <li><a href="<%= Url.Action("Index", "TaskTemplate") %>">Templates</a></li>
                </ul>
            </div>
            <br />
            <% if ((Model.AvailableTasks.Count == 0) && (Model.TaskSchedules.Count == 0))
                { %>
                <div>No Task Types have been registered by a Task Server yet.</div>
            <% } 
                else
                { %>
                <% if (Model.AvailableTasks.Count > 0) { %>
                    <% if (Model.TaskSchedules.Count > 0) { %><div class="left-floating-panel"><input id="btShowCustomizationWindow" type="button" value="Column Select" class="button" /></div><% } %>
                    <div class="right-floating-panel"><input type="button" value="Add a Task" onclick="window.location.href='<%= Url.Action("Select", "Task") %>'"  class="button" /></div>
                    <div class="right-floating-panel"><input type="button" value="Add a Task Sequence" onclick="window.location.href='<%= Url.Action("New", "Task", new { taskType = "task sequence" }) %>'" class="button" /></div>
                    <div class="clear"></div>
                    <br />
                <% } else if (Model.TaskSchedules.Count == 0) { %>
                    <div>No Task Types have been registered by a Task Server.</div>
                <% } %>
                <% if (Model.TaskSchedules.Count > 0)
                    { %>
                    <div id="GridViewContainer"><% Html.RenderPartial("Partials/TaskSchedules", new AdministratorConsole.Models.TaskController.Partials.TaskSchedulesViewModel(Model.TaskSchedules)); %></div>
                <% }
                    else
                    { %>
                    <div>Click &quot;Add a Task&nbsp to create your first task.</div>
                <% } %>
            <% } %>
        </div>
    </div>
    <ul id="task-context-menu" class='custom-menu'>
      <li onclick="openEditInNewTab()">
          <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-up-right" viewBox="0 0 16 16">
              <path fill-rule="evenodd" d="M8.636 3.5a.5.5 0 0 0-.5-.5H1.5A1.5 1.5 0 0 0 0 4.5v10A1.5 1.5 0 0 0 1.5 16h10a1.5 1.5 0 0 0 1.5-1.5V7.864a.5.5 0 0 0-1 0V14.5a.5.5 0 0 1-.5.5h-10a.5.5 0 0 1-.5-.5v-10a.5.5 0 0 1 .5-.5h6.636a.5.5 0 0 0 .5-.5z"/>
              <path fill-rule="evenodd" d="M16 .5a.5.5 0 0 0-.5-.5h-5a.5.5 0 0 0 0 1h3.793L6.146 9.146a.5.5 0 1 0 .708.708L15 1.707V5.5a.5.5 0 0 0 1 0v-5z"/>
           </svg>
          <span>Open in new tab</span>
      </li>
    </ul>

    <script type="text/javascript">
        window.onunload = function () {
            var cookie = getCookieValue("ClarityScheduledTasksView");

            if (typeof (cookie) !== 'undefined') {
                var date = new Date();
                //expires next year
                date.setTime(date.getTime() + (10 * 60 * 1000 * 52560));

                ASPxClientUtils.SetCookie("ClarityScheduledTasksView", cookie, date);
            }
        }
    </script>
</asp:Content>
