<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<XFormSelector>" %>
<%@ Import Namespace="EPiServer.Shell.Web.Mvc.Html" %>
<%@ Import Namespace="EPiServer.Samples.Models.XFormSearch" %>

    <%
        if (Model.CurrentState != SearchState.NotSearching)
        {
            if (Model.CurrentState == SearchState.CountAndResults)
            {
                if (Model.Results.Count() > 0)
                {
    %>
    <table>
        <thead>
            Search Results:
            <tr>
                <%
                    foreach (string name in Model.SelectedFormFields)
                    {
                        Response.Write("<td>");
                        Response.Write(name);
                        Response.Write("</td>");
                    }%>
            </tr>
        </thead>
        <tbody>
            <%
            
                foreach (var result in Model.Results)
                {
                    Response.Write("<tr>");

                    foreach (string key in Model.SelectedFormFields)
                    {
                        Response.Write("<td>");
                        Response.Write(result[key]);
                        Response.Write("</td>");
                    }

                    Response.Write("</tr>");
                }
            %>
        </tbody>
    </table>
    <div class="epi-padding-small">Pages:
        <%
            int pages = Model.ResultCount / Model.ResultsPerPage;

            if (Model.ResultCount % Model.ResultsPerPage > 0)
            {
                pages++;
            }

            for (int page = 1; page <= pages; page++)
            {
                if (page == Model.ResultsPage)
                {
                    Response.Write(page.ToString());
                }
                else
                {
                    // Write a link for this
                    object htmlAttributes = new { @onclick = "epi.xformSearch.subsequentSearch(this);" };
                    Response.Write(Html.ShellLinkButton("#", "", "", page.ToString(), "", htmlAttributes));
                }
            }       
                     %>
    </div>
    <%}
            }%>
    <div class="epi-padding-small">
        <%=Model.ResultCount%>
        match(es) found</div>
    <%} %>

