@using System.Drawing
@using MrCMS.Entities.Documents.Media
@using MrCMS.Helpers
@using MrCMS.Media
@using MrCMS.Paging
@using MrCMS.Services.FileMigration
@using MrCMS.Web.Areas.Admin.Helpers
@using MrCMS.Web.Areas.Admin.Models.Search
@model MrCMS.Web.Areas.Admin.Models.Search.AdminSearchQuery
@{
    var results = ViewData["results"] as IPagedList<AdminSearchResult>;
    List<SelectListItem> typeOptions = UniversalSearchHelper.GetOptions();
}
@using (Html.BeginForm("Index", "Search", FormMethod.Get))
{
    <fieldset>
        <legend>
            Search @(!string.IsNullOrWhiteSpace(Model.Term) ? " - " + Model.Term : "")
            <span class="btn-group  display-inline  pull-right">
                <input type="submit" value="Search" class="btn btn-primary" />
            </span>
        </legend>
        <div class="row">
            <div class="col-lg-6">
                <div class="form-group">
                    @Html.LabelFor(x => x.Term)
                    @Html.TextBoxFor(x => x.Term, new {@class = "form-control"})
                    @Html.ValidationMessageFor(x => x.Term)
                </div>
            </div>
            <div class="col-lg-6">
                <div class="form-group">
                    @Html.LabelFor(x => x.CreatedOnFrom)
                    @Html.TextBoxFor(x => x.CreatedOnFrom, new {@class = "date-time-picker form-control"})
                    @Html.ValidationMessageFor(x => x.CreatedOnFrom)
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-6">
                <div class="form-group">
                    @Html.LabelFor(x => x.Type)
                    @Html.DropDownListFor(x => x.Type, typeOptions, new {@class = "form-control"})
                    @Html.ValidationMessageFor(x => x.Type)
                </div>

            </div>
            <div class="col-lg-6">
                <div class="form-group">
                    @Html.LabelFor(x => x.CreatedOnTo)
                    @Html.TextBoxFor(x => x.CreatedOnTo, new {@class = "date-time-picker form-control"})
                    @Html.ValidationMessageFor(x => x.CreatedOnTo)
                </div>
            </div>
        </div>
        @Html.ValidationSummary()

    </fieldset>
}

<div id="search-results">
    @if (results == null || !results.Any())
    {
        <p>No documents could be found matching your search term</p>
    }
    else
    {
        <table class="table table-striped">
            <tr>
                <th class="col-lg-8">Page</th>
                <th class="col-lg-4">Type</th>
            </tr>
            @foreach (AdminSearchResult result in results)
            {
                <tr>
                    <td>
                        <a href="@result.ActionUrl" class="">
                            @if (result.Entity is MediaFile)
                            {
                                var file = result.Entity as MediaFile;
                                if (file.IsImage())
                                {
                                    @Html.RenderImage(file.FileUrl, new Size(100, 100))
                                }
                                else
                                {
                                    <i class="@file.FileExtension.GetIconClass()"> </i>
                                }
                                <br />
                            }
                            <strong>@result.DisplayName</strong>
                        </a>
                        
                        <br />
                        <p>Last updated on @result.Entity.UpdatedOn</p>
                    </td>
                    <td>@result.Type</td>
                </tr>
            }
        </table>
        @Html.Pager(results, "Index", page => new {page})
    }
</div>