<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Project.Master" Inherits="System.Web.Mvc.ViewPage<AdministratorConsole.Models.ProjectController.EditViewModel>" ValidateRequest="false" %>

<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
    <% Html.DevExpress().RenderScripts(Page,
            new Script { ExtensionSuite = ExtensionSuite.HtmlEditor },
            new Script { ExtensionSuite = ExtensionSuite.Editors }); %>
    <% Html.DevExpress().RenderStyleSheets(Page,
                new StyleSheet { ExtensionSuite = ExtensionSuite.HtmlEditor }
         ); %>
    <script src="<%= Request.ApplicationPath.TrimEnd(new char[] { '/' }) %>/Scripts/jquery-tokeninput/jquery.tokeninput.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        function viewProjectDetails() {
            _removeTabStyles();

            $("#project-details-tab").addClass("active");

            $("#project-overview-container").show();
        }

        function submitProjectParameters() {
            $("#edit-project-validation").empty();

            var cppRequiredFields = $('#project-parameters-container input.cpp_required').filter(function () { return $(this).val() == ""; }).length;

            if (cppRequiredFields > 0) {
                $("#edit-project-validation").html("Project parameters marked with a * require a value");
                return;
            }

            $("#set-project-parameters-form").submit();
        }

        function viewProjectType() {
            _removeTabStyles();

            $("#project-type-tab").addClass("active");

            $("#project-detail-container").show();
        }

        function viewProjectData() {
            _removeTabStyles();

            $("#project-data-tab").addClass("active");

            $("#project-data-container").show();
        }

        function viewProjectParameters() {
            _removeTabStyles();

            $("#project-parameters-tab").addClass("active");

            $("#custom-parameters-form").show();
        }

        function _removeTabStyles() {
            $("#project-details-tab").removeClass("active");
            $("#project-type-tab").removeClass("active");
            $("#project-parameters-tab").removeClass("active");

            $("#project-overview-container").hide();
            $("#project-detail-container").hide();
            $('#project-data-container').hide();
            $("#custom-parameters-form").hide();
        }

        function loadBrowseWindow() {
            $.ajax({
                type: 'GET',
                url: '<%= Url.Action("GetBrowseFolders", "Project") %>',
                data: {
                    targetInput: "WorkingFolder",
                    workingDirectory: $("#WorkingFolder").val()
                },
                cache: false,
                success: function (response) {
                    $("#modal-overlay").html(response);
                },
                complete: function () {
                    showModelDialog();
                }
            });
        }

        function loadUpgradeWindow() {
            $.ajax({
                type: 'GET',
                url: '<%= Url.Action("GetRevitServerUpgrade", "Project") %>',
                data: {
                    projectId: '<%= Model.CurrentProject.ExternalId %>'
                },
                cache: false,
                success: function (response) {
                    $("#modal-overlay").html(response);
                },
                complete: function () {
                    showModelDialog();
                }
            });
        }

        function loadPurgeDatabase() {
            $.ajax({
                type: 'GET',
                url: '<%= Url.Action("PurgeDatabaseConfirm", "Project") %>',
                data: {
                    id: '<%= Model.CurrentProject.ExternalId %>'
                },
                cache: false,
                success: function (response) {
                    $("#modal-overlay").html(response);
                },
                complete: function () {
                    showModelDialog();
                }
            });
        }

        function loadPurgeReportViews() {
            $.ajax({
                type: 'GET',
                url: '<%= Url.Action("PurgeReportViewsConfirm", "Project") %>',
                data: {
                    id: '<%= Model.CurrentProject.ExternalId %>'
                },
                cache: false,
                success: function (response) {
                    $("#modal-overlay").html(response);
                },
                complete: function () {
                    showModelDialog();
                }
            });
        }

        function validateUpdateForm() {
            let isCurrentlySecure = <%= Model.CurrentProject.IsSecure.ToString().ToLower() %>;

            $("#project-tags").val("");

            var tags = $("#project-tags-select").tokenInput("get"),
                tagString = "";

            for (var i = 0; i < tags.length; i++) {
                tagString += tags[i].name.replace(/,/g, " ");
                if (i != (tags.length - 1)) tagString += ",";
            }

            $("#project-tags").val(tagString);

            if ($("#IsSecure").is(":checked") === isCurrentlySecure) {
                submitUpdateProject();
            }
            else {
                $.ajax({
                    type: 'GET',
                    url: '<%= Url.Action("GetProjectSecurityUpdated", "Project") %>',
                    cache: false,
                    success: function (response) {
                        $("#modal-overlay").html(response);
                    },
                    complete: function () {
                        showModelDialog();
                    }
                });
            }
        }

        function submitUpdateProject() {
            $("#edit-project-form").submit();
        }

        function loadProjectDetail() {
            <% if (ProjectTypeHelper.GetIsRevitServerProject(Model.CurrentProject.RevitServerVersion))
        { %>
            var getURL = '<%= Url.Action("EditProjectDetail", "RevitServer") %>';
            <% }
        else
        { %>
            var getURL = '<%= Url.Action("EditProjectDetail", Model.CurrentProject.RevitServerVersion) %>';
            <% } %>

            $.ajax({
                type: 'GET',
                url: getURL,
                data: {
                    projectId: '<%= Model.CurrentProject.ExternalId %>'
                },
                cache: false,
                success: function (response) {
                    $('#project-detail-container').html(response);
                }
            });
        }

        function reportModelEvent(id) {
            $.ajax({
                type: 'GET',
                url: '<%=Url.Action("ModelEvents")%>',
                data: {
                    id: id
                },
                cache: false,
                success: function (response) {
                    $("#modal-overlay").html(response);
                },
                complete: function () {
                    showModelDialog();

                    $("#model").focus();
                }
            });
        }

        function generateExternalLink() {
            $.ajax({
                type: 'GET',
                url: '<%= Url.Action("GetExternalReportLink", "Project") %>',
                data: {
                    projectId: '<%= Model.CurrentProject.ExternalId %>'
                },
                cache: false,
                success: function (response) {
                    $("#modal-overlay").html(response);
                },
                complete: function () {
                    showModelDialog();
                }
            });
        }

        window.onload = function () {

            $("#projectImage").change(function (e) {
                $("#project-image-upload").submit();
            });

            if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
                $('#projectImageSelect').click(function () {
                    $('#projectImage').click();
                });
            }

            $("#project-tags-select").tokenInput("<%= Url.Action("GetProjectTags", "Project") %>", {
                theme: "facebook",
                queryParam: "searchTerm",
                prePopulate: <%= Model.GetProjectTagJSON() %>,
                allowFreeTagging: true,
                tokenValue: "name"
            });

            $("#token-input-project-tags-select").keydown(function (e) {
                if ((e.keyCode === 220) || (e.keyCode === 191)) e.preventDefault();
            });

            loadProjectDetail();
        };
    </script>
    <div id="banner-space">
        <div id="banner-detail-container">
            <div class="left-floating-panel">
                <div id="project-title"><span><%= Html.Encode(Model.CurrentProject.Name) %></span><span id="project-number"><%= Html.Encode(Model.CurrentProject.Number) %></span></div>
                <div id="project-database-export">
                    <b>Last Database Export:</b> <% if (Model.CurrentProject.LastDatabaseExport.HasValue)
                                                     { %><%= DateTimeHelper.ToString(Model.CurrentProject.LastDatabaseExport.Value) %><% }
                                                                                                                                          else
                                                                                                                                          { %>No Exports<% } %>
                </div>
            </div>
            <% if (Model.LicenseType != LicenseType.ODP)
                { %>
            <div class="right-floating-panel total-time-container-outer">
                <div id="total-time-container">
                    <div class="header">Total Time Saved</div>
                    <div class="value"><%= Math.Ceiling(Model.CurrentProject.CompletedTasksTime.TotalHours).ToString("000") %> hrs</div>
                </div>
            </div>
            <% } %>
            <% Html.RenderPartial("Partials/LaunchableTasks", new LaunchableTasksModel(Model.CurrentProject, Model.TaskSchedules, Model.CurrentUser)); %>
            <div class="clear"></div>
        </div>
    </div>
    <div id="content-wrap">
        <% Html.RenderPartial("Navigation/_SideNavigation", Model.MenuNavigationModel); %>
        <div id="content">
            <div class="project-actions">
                <nav class="action-dropdown page-admin">
                    <a href="#">Edit</a>
                    <ul>
                        <li><%= Html.ActionLink(TextHelper.GetText("Edit Project"), "Edit/" + Model.CurrentProject.ExternalId, "Project")%></li>
                        <li><%= Html.ActionLink("Request Rescan", "RequestModelRescanEdit/" + Model.CurrentProject.ExternalId, "Project")%></li>
                        <li><a href="<%= Url.Action("Index/" + Model.CurrentProject.ExternalId, "ProjectUsers")%>">Manage Users</a></li>
                        <li><%= Html.ActionLink("File Filters", "FileFilters/" + Model.CurrentProject.ExternalId, "Project")%></li>
                        <li><a href="<%= Url.Action("Index/" + Model.CurrentProject.ExternalId, "CopyFrom")%>"><%= TextHelper.GetText("Copy From Project") %></a></li>
                        <li><a href="<%= Url.Action("Archive/" + Model.CurrentProject.ExternalId, "Project")%>"><%= TextHelper.GetText("Archive Project") %></a></li>
                        <li><a href="<%= Url.Action("Index/" + Model.CurrentProject.ExternalId, "ProjectLogs")%>">View Logs</a></li>
                        <li><a onclick="reportModelEvent('<%= Model.CurrentProject.ExternalId %>')">Report Model Event</a></li>
                        <li><a href="<%= Url.Action("Edit/" + Model.CurrentProject.ExternalId, "Glue")%>">Manage Glue</a></li>
                        <li><a href="<%= Url.Action("Edit/" + Model.CurrentProject.ExternalId, "Viewer")%>">Manage Viewer</a></li>
                        <% if (Model.CurrentProject.Active)
                            { %>
                        <li><a href="<%= Url.Action("Deactivate/" + Model.CurrentProject.ExternalId, "Project")%>"><%= TextHelper.GetText("Deactivate Project") %></a></li>
                        <% }
                            else
                            { %>
                        <li><a href="<%= Url.Action("Activate/" + Model.CurrentProject.ExternalId, "Project")%>"><%= TextHelper.GetText("Activate Project") %></a></li>
                        <% } %>
                        <li><a href="<%= Url.Action("Delete/" + Model.CurrentProject.ExternalId, "Project")%>"><%= TextHelper.GetText("Delete Project") %></a></li>
                    </ul>
                </nav>
            </div>
            <h1><%= TextHelper.GetText("Edit Project") %></h1>
            <div id="tab-container">
                <ul class="tabs">
                    <li><a id="project-details-tab" href="javascript:viewProjectDetails()" class="active">Details</a></li>
                    <li><a id="project-type-tab" href="javascript:viewProjectType()">Project Type</a></li>
                    <li><a id="project-data-tab" href="javascript:viewProjectData()">Project Data</a></li>
                    <li><a id="project-parameters-tab" href="javascript:viewProjectParameters()">Project Parameters</a></li>
                </ul>
            </div>
            <br />
            <div class="group">
                <div class="edit-project-image-container">
                    <div>
                        <img src="<%= Url.Action("GetProjectImage/" + Model.CurrentProject.ExternalId, "Project") %>" border="0" class="edit-project-image" />
                    </div>
                    <div style="margin-top: 15px; margin-bottom: 10px; text-align: center">
                        <% using (Html.BeginForm("UpdateProjectImage/" + Model.CurrentProject.ExternalId, "Project", FormMethod.Post, new { @id = "project-image-upload", @enctype = "multipart/form-data" }))
                            { %><input type="file" id="projectImage" name="projectImage" style="display: none" /><% } %><label for="projectImage" id="projectImageSelect" class="button">Upload</label>
                    </div>
                    <div style="text-align: center"><%= Html.Encode("(260x150)") %></div>
                </div>
                <div id="project-overview-container" class="edit-project-details-container left-floating-panel">
                    <table>
                        <% using (Html.BeginForm("Update/" + Model.CurrentProject.ExternalId, "Project", FormMethod.Post, new { id = "edit-project-form" }))
                            { %>
                        <tr>
                            <td valign="middle" style="width: 170px;">Name:</td>
                            <td valign="middle" colspan="2"><%= Html.TextBox("name", Model.CurrentProject.Name, new { @class="standard-textbox" })%></td>
                        </tr>
                        <tr>
                            <td align="left" class="label-container" style="width: 170px;">Number:</td>
                            <td align="left" class="project-detail-input-container" colspan="2"><%= Html.TextBox("Number", Model.CurrentProject.Number, new { @class = "standard-textbox"  })%></td>
                        </tr>
                        <tr>
                            <td align="left" class="label-container" style="width: 170px;">Office:</td>
                            <!--<td align="left" class="project-detail-input-container" colspan="2"><%= Html.TextBox("Office", Model.CurrentProject.Office, new { @class = "standard-textbox"  })%></td>-->
                            <td align="left" class="project-detail-input-container" colspan="2"><%= Html.DropDownList("OfficeId", new SelectList(Model.AvailableOffices, "Id", "Name", Model.CurrentProject.OfficeId), new {  @class = "standard-textbox" })  %></td>
                        </tr>
                        <tr>
                            <td valign="middle" class="label-container" style="width: 170px;">Address:</td>
                            <td valign="middle" class="project-detail-input-container" colspan="2"><%= Html.TextBox("Location", Model.CurrentProject.Location, new { @class="standard-textbox", @placeHolder = "Address, Town, State Zip" })%></td>
                        </tr>
                        <tr>
                            <td align="left" class="label-container" style="width: 170px;">Tags:</td>
                            <td align="left" class="project-detail-input-container" colspan="2">
                                <input type="hidden" id="project-tags" name="ProjectTags" /><input type="text" id="project-tags-select" /></td>
                        </tr>
                        <tr>
                            <td align="left" style="width: 170px;">Secured:</td>
                            <td align="left" class="project-detail-input-container">
                                <input type="checkbox" id="IsSecure" name="IsSecure" value="1" <% if (Model.CurrentProject.IsSecure)
                                    { %>
                                    checked="checked" <% } %> /></td>
                            <td valign="middle" style="padding: 10px 0px 0px 10px;">
                                <input type="button" value="Update" class="button" onclick="validateUpdateForm();" /></td>
                        </tr>
                        <% } %>
                        <% if (!string.IsNullOrEmpty(Model.CurrentProject.CreatedBy))
                            { %>
                        <tr>
                            <td valign="middle" class="label-container" style="width: 170px;"><%= Html.Encode("Created By:")%></td>
                            <td valign="middle" style="padding-top: 10px;" colspan="2"><%= Html.Encode(Model.CurrentProject.CreatedBy)%></td>
                        </tr>
                        <% } %>
                        
                    </table>
                </div>
                <div id="project-detail-container" class="left-floating-panel" style="display: none;">
                    <img src='<%= Request.ApplicationPath.TrimEnd(new char[] { '/' }) %>/Images/loading.gif' title='Loading' />
                </div>

                <div id="project-data-container" class="left-floating-panel p-4" style="display: none;">
                    <% if (!string.IsNullOrEmpty(Model.CurrentProject.ProjectDatabase)) { %>
                     <div class="d-flex">
                         <div  class="label-container" style="width: 170px;"><%= Html.Encode("Database:")%></div>
                         <div ><%= Html.Encode(Model.CurrentProject.ProjectDatabase)%></div>
                     </div>
                    <div>&nbsp;</div>
                     <a href="javascript:generateExternalLink()">(External Data Link)</a>
                    <br />
                    <div>&nbsp;</div>
                    <div>
                        <div><a href="<%=Url.Action("DownloadPowerBIProject", "Project" , new { projectId = Model.CurrentProject.ExternalId })%>">Microsoft PowerBI Project</a> <span class="ms-5">Note: Enable PowerBI preview feature .pbip file format to open the project file</span></div>
                    </div>
                     <div>
                         <div>&nbsp;</div>
                         <div valign="middle" style="padding-top: 10px;" colspan="2">
                             <input type="button" value="Purge Report Views" class="button" onclick="loadPurgeReportViews()" />&nbsp;<input type="button" value="Purge Database" class="button" onclick="loadPurgeDatabase()" /></div>
                     </div>
                     <% } %>
                </div>

                <div id="custom-parameters-form" class="left-floating-panel" style="display: none">
                    <div id="edit-project-validation" class="validation-error-content"></div>

                    <% using (Html.BeginForm("SetProjectParameters", "Project", FormMethod.Post, new { @id = "set-project-parameters-form" }))
                        { %>


                    <%= Html.Hidden("ProjectId", Model.CurrentProject.ExternalId) %>

                    <% Html.RenderPartial("Partials/AddProjectParametersForm", Model.ProjectParameters); %>

                    <input type="button" class="button" value="Update"  style="float:right" onclick="submitProjectParameters()" />

                    <% } %>
                </div>


                <div class="clear"></div>
            </div>
            <div class="group">
                <% using (Html.BeginForm("UpdateGlobalMessage/" + Model.CurrentProject.ExternalId, "Project"))
                    { %>
                <h2><%= TextHelper.GetText("Project Message")%></h2>
                <div><% Html.RenderPartial("Partials/ProjectHtmlEditor", Model.CurrentProject); %></div>
                <br />
                <div class="right-floating-panel">
                    <input type="submit" value="Set Message" class="button" />
                </div>
                <div class="clear"></div>
                <% } %>
            </div>
        </div>
    </div>
</asp:Content>
