<commit_before>﻿@inherits Umbraco.Web.Mvc.UmbracoViewPage<Chalmers.ILL.Models.Page.ChalmersILLOrderListPageModel>
@using umbraco.cms.businesslogic.member;
@using System.Web.Security;
@using Examine;
@using Chalmers.ILL.Controllers.SurfaceControllers;
@using Chalmers.ILL.Extensions;
@using Umbraco.Core.Logging;
@using System.Globalization;


@{
    Layout = "ChalmersILL.cshtml";
}

@functions
{
    /// <summary>
    /// Checks who, if any, the given node is being edited by. Using the relation locking system.
    /// </summary>
    /// <param name="nodeId">The node which should be checked if open for editing.</param>
    /// <returns>A string with the member ID of the editor, or an empty string if no one is editing the node.</returns>
    string EditedBy(int nodeId)
    {
        string ret = "";

        var relType = umbraco.cms.businesslogic.relation.RelationType.GetByAlias(OrderItemSurfaceController.lockRelationType);
        var relations = umbraco.cms.businesslogic.relation.Relation.GetRelationsAsList(nodeId).Where(rel => rel.Child.Id == nodeId && rel.RelType.Id == relType.Id);

        if (relations.Count() > 0)
        {
            ret = relations.First().Parent.Id.ToString();
        }

        return ret;
    }

    /// <summary>
    /// Appends the given arguments to the query string in the given URL.
    /// </summary>
    /// <param name="url">The URL string which should be modified.</param>
    /// <param name="arguments">The arguments which should be added.</param>
    /// <returns>The new URL string containing the modified query string.</returns>
    string AppendArgumentsToQueryString(string url, List<Tuple<string, string>> arguments)
    {
        var uriBuilder = new UriBuilder(url);
        var query = HttpUtility.ParseQueryString(uriBuilder.Query);
        foreach (var arg in arguments)
        {
            query[arg.Item1] = arg.Item2;
        }
        uriBuilder.Query = query.ToString();
        return uriBuilder.ToString();
    }

    /// <summary>
    /// Looks at an order item and returns the correct sorting order index based on status.
    /// </summary>
    /// <param name="item">The search result from Examine containing the order item.</param>
    /// <returns>An index reflecting the sort order.</returns>
    int GetSortOrderFromOrderItemSearchResult(SearchResult item)
    {
        var status = item.Fields["Status"];
        var i = Convert.ToInt32(status == "" ? "0" : status.Split(':').First());

        // Fix the fact that the status indexes does not always decide the sorting order
        i = i * 100;
        if (i == 900)
        {
            i = 450;
        }

        return i;
    }
}

@{
    var orderCriterium = "Status";
    string theQuery = "";
    string debugMessage = "";
    if (!String.IsNullOrEmpty(Request.QueryString["orderCriterium"]))
    {
        orderCriterium = Request.QueryString["orderCriterium"];
    }
}

<div class="text-center">
    <div id="library-filter-buttons" class="btn-group" data-pending-filter-change="0">
        <button id="library00-button" type="button" value="" class="btn btn-default active">Alla <span id="allaBibliotek" class="badge">0</span></button>
        <button id="library01-button" type="button" value=".Huvudbiblioteket" class="btn btn-default">Huvudbiblioteket <span id="Huvudbiblioteket" class="badge">0</span></button>
        <button id="library02-button" type="button" value=".Lindholmenbiblioteket" class="btn btn-default">Lindholmenbiblioteket <span id="Lindholmenbiblioteket" class="badge">0</span></button>
        <button id="library03-button" type="button" value=".Arkitekturbiblioteket" class="btn btn-default">Arkitekturbiblioteket <span id="Arkitekturbiblioteket" class="badge">0</span></button>
    </div>
</div>

<div>
    <h2 id="order-list-title">Beställningar - Alla bibliotek</h2>  
    <div id="filter-buttons" class="btn-group" data-pending-filter-change="0">
        <button id="status00-button" type="button" value="" class="btn btn-default active">Alla <span id="status00-counter" class="badge">0</span></button>
        <button id="status01-button" type="button" value=".chillin-status-01" class="btn btn-default btn-success">Ny <span id="status01-counter" class="badge">0</span></button>
        <button id="status02-button" type="button" value=".chillin-status-02" class="btn btn-default btn-danger">Åtgärda <span id="status02-counter" class="badge">0</span></button>
        <button id="status03-button" type="button" value=".chillin-status-03" class="btn btn-default btn-danger">Beställd <span id="status03-counter" class="badge">0</span></button>
        <button id="status04-button" type="button" value=".chillin-status-04" class="btn btn-default btn-danger">Väntar <span id="status04-counter" class="badge">0</span></button>
        <button id="status09-button" type="button" value=".chillin-status-09" class="btn btn-default btn-danger">Mottagen <span id="status09-counter" class="badge">0</span></button>
        <button id="status05-button" type="button" value=".chillin-status-05" class="btn btn-default btn-info">Levererad <span id="status05-counter" class="badge">0</span></button>
        <button id="status06-button" type="button" value=".chillin-status-06" class="btn btn-default btn-info">Annullerad <span id="status06-counter" class="badge">0</span></button>
        <button id="status07-button" type="button" value=".chillin-status-07" class="btn btn-default btn-info">Överförd <span id="status07-counter" class="badge">0</span></button>
        <button id="status08-button" type="button" value=".chillin-status-08" class="btn btn-default btn-info">Inköpt <span id="status08-counter" class="badge">0</span></button>
    </div>
</div>

@if (!String.IsNullOrEmpty(Request.QueryString["query"]))
{
    theQuery += "query=";
    theQuery += Request.QueryString["query"];
    <h3>Beställningar som matchar <strong>@Request.QueryString["query"]</strong></h3>
}

<div class="order-list container-fluid">

    <div class="row header">
        <div class="col-sm-1"><a id="sort-on-follow-up-link" href="#">Följ upp </a></div>
        <div class="col-sm-5">Referens</div>
        <div class="col-sm-2">Namn</div>
        <div class="col-sm-2">Bibliotek</div>
        <div class="col-sm-1"><a id="sort-on-type-link" href="#">Typ </a></div>
        <div class="col-sm-1"><a id="sort-on-status-link" href="#">Status <span id="sort-icon" class="glyphicon glyphicon-chevron-down"></span></a></div>
    </div>

    @{
        int loggedInMember = Model.CurrentMemberId;
        if (!String.IsNullOrEmpty(Request.QueryString["query"]))
        {
            // Connect to an Examine Search Provider
            var searcher = ExamineManager.Instance.SearchProviderCollection["ChalmersILLOrderItemsSearcher"];

            // Specify Search Criteria
            var searchCriteria = searcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);
            
            // Specify the query            
            var query = searchCriteria.RawQuery(Request.Params["query"]);
            
            // Loop trough results
            var results = searcher.Search(query);
            foreach (var item in results.OrderBy(GetSortOrderFromOrderItemSearchResult).ThenBy(y => DateTime.ParseExact(y.Fields["FollowUpDate"], "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture, DateTimeStyles.None)))
            {          
                @RenderOrderList(loggedInMember, item.Id, DateTime.ParseExact(item.Fields.GetValueString("FollowUpDate"), "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture, DateTimeStyles.None),
                    item.Fields.GetValueString("PatronName"), item.Fields.GetValueString("DeliveryLibrary"), item.Fields.GetValueString("Status"), item.Fields.GetValueString("Type"), 
                    item.Fields.GetValueString("Reference"), EditedBy(item.Id));
            }
            
            // Add debug message
            debugMessage += "query.BooleanOperation=" + query.BooleanOperation + ", ";
            debugMessage += "query.ToString()=" + query.ToString();
        }
        else
        {
            // Connect to an Examine Search Provider
            var searcher = ExamineManager.Instance.SearchProviderCollection["ChalmersILLOrderItemsSearcher"];

            // Specify Search Criteria
            var searchCriteria = searcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);

            // Specify the query
            var longAgo = DateTime.MinValue;
            var now = DateTime.Now;
            var query = searchCriteria.RawQuery(@"nodeTypeAlias:ChalmersILLOrderItem AND 
                (Status:01\:Ny OR 
                 Status:02\:Åtgärda OR
                 Status:09\:Mottagen OR 
                 (Status:03\:Beställd AND FollowUpDate:[197501010000000 TO " + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "]))");
            
            // Search for our items
            var results = searcher.Search(query);

            foreach (var result in results)
            {
                DateTime convertDate = DateTime.ParseExact(result.Fields.GetValueString("FollowUpDate"), "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture, DateTimeStyles.None);
                @RenderOrderList(loggedInMember, result.Id, convertDate, result.Fields.GetValueString("PatronName"), result.Fields.GetValueString("DeliveryLibrary"), result.Fields.GetValueString("Status"), 
                    result.Fields.GetValueString("Type"), result.Fields.GetValueString("Reference"), EditedBy(result.Id));
            }
        }
    }
</div><!-- /order-list -->

@* Debug for Search or List *@
@if (Request.Params["debug"] == "1" && debugMessage != "")
{
    <div class="debug-message">
        <strong>debugMessage:</strong> @debugMessage
    </div>
}


@helper ParseStatusPrevalue(string prevalue)
{
    @prevalue.Split(':').Last()<commit_msg>Readded correct sort order when loading order list page.<commit_after>﻿@inherits Umbraco.Web.Mvc.UmbracoViewPage<Chalmers.ILL.Models.Page.ChalmersILLOrderListPageModel>
@using umbraco.cms.businesslogic.member;
@using System.Web.Security;
@using Examine;
@using Chalmers.ILL.Controllers.SurfaceControllers;
@using Chalmers.ILL.Extensions;
@using Umbraco.Core.Logging;
@using System.Globalization;


@{
    Layout = "ChalmersILL.cshtml";
}

@functions
{
    /// <summary>
    /// Checks who, if any, the given node is being edited by. Using the relation locking system.
    /// </summary>
    /// <param name="nodeId">The node which should be checked if open for editing.</param>
    /// <returns>A string with the member ID of the editor, or an empty string if no one is editing the node.</returns>
    string EditedBy(int nodeId)
    {
        string ret = "";

        var relType = umbraco.cms.businesslogic.relation.RelationType.GetByAlias(OrderItemSurfaceController.lockRelationType);
        var relations = umbraco.cms.businesslogic.relation.Relation.GetRelationsAsList(nodeId).Where(rel => rel.Child.Id == nodeId && rel.RelType.Id == relType.Id);

        if (relations.Count() > 0)
        {
            ret = relations.First().Parent.Id.ToString();
        }

        return ret;
    }

    /// <summary>
    /// Appends the given arguments to the query string in the given URL.
    /// </summary>
    /// <param name="url">The URL string which should be modified.</param>
    /// <param name="arguments">The arguments which should be added.</param>
    /// <returns>The new URL string containing the modified query string.</returns>
    string AppendArgumentsToQueryString(string url, List<Tuple<string, string>> arguments)
    {
        var uriBuilder = new UriBuilder(url);
        var query = HttpUtility.ParseQueryString(uriBuilder.Query);
        foreach (var arg in arguments)
        {
            query[arg.Item1] = arg.Item2;
        }
        uriBuilder.Query = query.ToString();
        return uriBuilder.ToString();
    }

    /// <summary>
    /// Looks at an order item and returns the correct sorting order index based on status.
    /// </summary>
    /// <param name="item">The search result from Examine containing the order item.</param>
    /// <returns>An index reflecting the sort order.</returns>
    int GetSortOrderFromOrderItemSearchResult(SearchResult item)
    {
        var status = item.Fields["Status"];
        var i = Convert.ToInt32(status == "" ? "0" : status.Split(':').First());

        // Fix the fact that the status indexes does not always decide the sorting order
        i = i * 100;
        if (i == 900)
        {
            i = 450;
        }

        return i;
    }
}

@{
    var orderCriterium = "Status";
    string theQuery = "";
    string debugMessage = "";
    if (!String.IsNullOrEmpty(Request.QueryString["orderCriterium"]))
    {
        orderCriterium = Request.QueryString["orderCriterium"];
    }
}

<div class="text-center">
    <div id="library-filter-buttons" class="btn-group" data-pending-filter-change="0">
        <button id="library00-button" type="button" value="" class="btn btn-default active">Alla <span id="allaBibliotek" class="badge">0</span></button>
        <button id="library01-button" type="button" value=".Huvudbiblioteket" class="btn btn-default">Huvudbiblioteket <span id="Huvudbiblioteket" class="badge">0</span></button>
        <button id="library02-button" type="button" value=".Lindholmenbiblioteket" class="btn btn-default">Lindholmenbiblioteket <span id="Lindholmenbiblioteket" class="badge">0</span></button>
        <button id="library03-button" type="button" value=".Arkitekturbiblioteket" class="btn btn-default">Arkitekturbiblioteket <span id="Arkitekturbiblioteket" class="badge">0</span></button>
    </div>
</div>

<div>
    <h2 id="order-list-title">Beställningar - Alla bibliotek</h2>  
    <div id="filter-buttons" class="btn-group" data-pending-filter-change="0">
        <button id="status00-button" type="button" value="" class="btn btn-default active">Alla <span id="status00-counter" class="badge">0</span></button>
        <button id="status01-button" type="button" value=".chillin-status-01" class="btn btn-default btn-success">Ny <span id="status01-counter" class="badge">0</span></button>
        <button id="status02-button" type="button" value=".chillin-status-02" class="btn btn-default btn-danger">Åtgärda <span id="status02-counter" class="badge">0</span></button>
        <button id="status03-button" type="button" value=".chillin-status-03" class="btn btn-default btn-danger">Beställd <span id="status03-counter" class="badge">0</span></button>
        <button id="status04-button" type="button" value=".chillin-status-04" class="btn btn-default btn-danger">Väntar <span id="status04-counter" class="badge">0</span></button>
        <button id="status09-button" type="button" value=".chillin-status-09" class="btn btn-default btn-danger">Mottagen <span id="status09-counter" class="badge">0</span></button>
        <button id="status05-button" type="button" value=".chillin-status-05" class="btn btn-default btn-info">Levererad <span id="status05-counter" class="badge">0</span></button>
        <button id="status06-button" type="button" value=".chillin-status-06" class="btn btn-default btn-info">Annullerad <span id="status06-counter" class="badge">0</span></button>
        <button id="status07-button" type="button" value=".chillin-status-07" class="btn btn-default btn-info">Överförd <span id="status07-counter" class="badge">0</span></button>
        <button id="status08-button" type="button" value=".chillin-status-08" class="btn btn-default btn-info">Inköpt <span id="status08-counter" class="badge">0</span></button>
    </div>
</div>

@if (!String.IsNullOrEmpty(Request.QueryString["query"]))
{
    theQuery += "query=";
    theQuery += Request.QueryString["query"];
    <h3>Beställningar som matchar <strong>@Request.QueryString["query"]</strong></h3>
}

<div class="order-list container-fluid">

    <div class="row header">
        <div class="col-sm-1"><a id="sort-on-follow-up-link" href="#">Följ upp </a></div>
        <div class="col-sm-5">Referens</div>
        <div class="col-sm-2">Namn</div>
        <div class="col-sm-2">Bibliotek</div>
        <div class="col-sm-1"><a id="sort-on-type-link" href="#">Typ </a></div>
        <div class="col-sm-1"><a id="sort-on-status-link" href="#">Status <span id="sort-icon" class="glyphicon glyphicon-chevron-down"></span></a></div>
    </div>

    @{
        int loggedInMember = Model.CurrentMemberId;
        if (!String.IsNullOrEmpty(Request.QueryString["query"]))
        {
            // Connect to an Examine Search Provider
            var searcher = ExamineManager.Instance.SearchProviderCollection["ChalmersILLOrderItemsSearcher"];

            // Specify Search Criteria
            var searchCriteria = searcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);
            
            // Specify the query            
            var query = searchCriteria.RawQuery(Request.Params["query"]);
            
            // Loop trough results
            var results = searcher.Search(query);
            foreach (var item in results.OrderBy(GetSortOrderFromOrderItemSearchResult).ThenBy(y => DateTime.ParseExact(y.Fields["FollowUpDate"], "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture, DateTimeStyles.None)))
            {          
                @RenderOrderList(loggedInMember, item.Id, DateTime.ParseExact(item.Fields.GetValueString("FollowUpDate"), "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture, DateTimeStyles.None),
                    item.Fields.GetValueString("PatronName"), item.Fields.GetValueString("DeliveryLibrary"), item.Fields.GetValueString("Status"), item.Fields.GetValueString("Type"), 
                    item.Fields.GetValueString("Reference"), EditedBy(item.Id));
            }
            
            // Add debug message
            debugMessage += "query.BooleanOperation=" + query.BooleanOperation + ", ";
            debugMessage += "query.ToString()=" + query.ToString();
        }
        else
        {
            // Connect to an Examine Search Provider
            var searcher = ExamineManager.Instance.SearchProviderCollection["ChalmersILLOrderItemsSearcher"];

            // Specify Search Criteria
            var searchCriteria = searcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);

            // Specify the query
            var longAgo = DateTime.MinValue;
            var now = DateTime.Now;
            var query = searchCriteria.RawQuery(@"nodeTypeAlias:ChalmersILLOrderItem AND 
                (Status:01\:Ny OR 
                 Status:02\:Åtgärda OR
                 Status:09\:Mottagen OR 
                 (Status:03\:Beställd AND FollowUpDate:[197501010000000 TO " + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "]))");
            
            // Search for our items
            var results = searcher.Search(query);

            foreach (var result in results.OrderBy(GetSortOrderFromOrderItemSearchResult).ThenBy(y => y.Fields["FollowUpDate"]))
            {
                DateTime convertDate = DateTime.ParseExact(result.Fields.GetValueString("FollowUpDate"), "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture, DateTimeStyles.None);
                @RenderOrderList(loggedInMember, result.Id, convertDate, result.Fields.GetValueString("PatronName"), result.Fields.GetValueString("DeliveryLibrary"), result.Fields.GetValueString("Status"), 
                    result.Fields.GetValueString("Type"), result.Fields.GetValueString("Reference"), EditedBy(result.Id));
            }
        }
    }
</div><!-- /order-list -->

@* Debug for Search or List *@
@if (Request.Params["debug"] == "1" && debugMessage != "")
{
    <div class="debug-message">
        <strong>debugMessage:</strong> @debugMessage
    </div>
}


@helper ParseStatusPrevalue(string prevalue)
{
    @prevalue.Split(':').Last()
{'fraction_non_alphanumeric': 0.1471830985915493, 'fraction_numerical': 0.01317907444668008, 'mean_word_length': 3.236309396974217, 'pattern_counts': {'":': 0, '<': 247, '<?xml version=': 0, '>': 254, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6195999', 'n_tokens_mistral': 6090, 'n_tokens_neox': 5819, 'n_words': 1164}