@model Disco.Models.Services.Jobs.JobLists.JobTableModel
@using Disco.Models.Services.Jobs.JobLists;
@{
    Html.BundleDeferred("~/ClientScripts/Modules/Disco-DataTableHelpers");
}
<div class="jobTable">
    @if (Model != null && Model.Items.Count() > 0)
    { 
        <table class="jobTable@(Model.IsSmallTable ? " smallTable" : string.Empty)@(Model.HideClosedJobs ? " hideStatusClosed" : string.Empty)@(Model.EnablePaging ? " enablePaging" : string.Empty)@(Model.EnableFilter ? " enableFilter" : string.Empty)">
            <thead>
                <tr>
                    @if (Model.ShowId)
                    { <th class="id">Ref
                    </th> }
                    @if (Model.ShowStatus)
                    { <th class="status">Status
                    </th> }
                    @if (Model.ShowLastActivityDate)
                    { <th class="lastActive">Last Active
                    </th> }
                    @if (Model.ShowDates)
                    { <th class="dates">When
                    </th> }
                    @if (Model.ShowType)
                    { <th class="type">Type
                    </th> }
                    @if (Model.ShowDevice)
                    {<th class="device">Device
                    </th> }
                    @if (Model.ShowUser)
                    { <th class="user">User
                    </th> }
                    @if (Model.ShowTechnician)
                    { <th class="technician">Technician
                    </th> }
                    @if (Model.ShowLocation)
                    { <th class="location">Location
                    </th> }
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.Items)
                {
                    string statusSlaClass = null;
                    if (Model.ShowStatus)
                    {
                        var statusItem = (JobTableStatusItemModel)item;
                        if (statusItem.ActiveJobQueues != null)
                        {
                            var slaRemaining = statusItem.ActiveJobQueues.UsersQueueItems(Authorization).SlaPrecentageRemaining();
                            if (slaRemaining.HasValue && (slaRemaining <= 0))
                            {
                                statusSlaClass = "statusSlaExpired";
                            }
                            else if (slaRemaining.HasValue && (slaRemaining < .3))
                            {
                                statusSlaClass = "statusSlaWarning";
                            }
                        }
                    }
                    <tr data-id="@(item.Id)" data-status="@(Model.ShowStatus ? item.StatusId : null)" class="@(statusSlaClass)">
                        @if (Model.ShowId)
                        {<td class="id">
                            @if (Authorization.Has(Claims.Job.Show))
                            {@Html.ActionLink(item.JobId.ToString(), MVC.Job.Show(item.JobId))}
                            else
                            {@item.JobId.ToString()}</td>}
                        @if (Model.ShowStatus)
                        {
                            var statusItem = (JobTableStatusItemModel)item;
                            <td class="status"><i class="fa fa-square jobStatus @(item.StatusId)"></i>&nbsp;@item.StatusDescription
                                @if (statusItem.ActiveJobQueues != null)
                                {<div class="queues">
                                    @foreach (var jqToken in ((JobTableStatusItemModel)item).ActiveJobQueues.Select(jqj => new Tuple<JobTableStatusQueueItemModel, Disco.Services.Jobs.JobQueues.JobQueueToken>(jqj, Disco.Services.Jobs.JobQueues.JobQueueService.GetQueue(jqj.QueueId))))
                                    {
                                        <i class="fa fa-@(jqToken.Item2.JobQueue.Icon) fa-fw d-@(jqToken.Item2.JobQueue.IconColour)" title="@(jqToken.Item2.JobQueue.Name) [@(jqToken.Item1.Priority)]"></i>
                                    }
                                </div>}
                            </td>}
                        @if (Model.ShowLastActivityDate)
                        {<td class="lastActive">@CommonHelpers.FriendlyDate(item.LastActivityDate)</td>}
                        @if (Model.ShowDates)
                        {<td class="dates">@if (item.ClosedDate.HasValue)
                                           {@CommonHelpers.FriendlyDate(item.ClosedDate.Value)}
                                           else
                                           {@CommonHelpers.FriendlyDate(item.OpenedDate)}</td>}
                        @if (Model.ShowType)
                        {<td class="type"><span title="@item.JobTypeDescription">@item.JobTypeId</span></td>}
                        @if (Model.ShowDevice)
                        {<td class="device">
                            @if (item.DeviceSerialNumber != null)
                            {
                                if (Authorization.Has(Claims.Device.Show))
                                {@Html.ActionLink(item.DeviceSerialNumber, MVC.Device.Show(item.DeviceSerialNumber), new { Title = item.DeviceModelDescription })}
                                else
                                {<span title="@item.DeviceModelDescription">@item.DeviceSerialNumber</span>}
                            }
                            else
                            {<span class="smallMessage">N/A</span>}</td>}
                        @if (Model.ShowUser)
                        {<td class="user">
                            @if (item.UserId != null)
                            {
                                if (Authorization.Has(Claims.User.Show))
                                {@Html.ActionLink(string.Format("{0} ({1})", item.UserDisplayName, item.UserFriendlyId), MVC.User.Show(item.UserId))}
                                else
                                {@(string.Format("{0} ({1})", item.UserDisplayName, item.UserFriendlyId))}
                            }
                            else
                            {<span class="smallMessage">N/A</span>}</td>}
                        @if (Model.ShowTechnician)
                        {<td class="technician"><span title="@item.OpenedTechUserDisplayName">@item.OpenedTechUserFriendlyId</span></td>}
                        @if (Model.ShowLocation)
                        {<td class="location"><span>@(item.DeviceHeldLocation ?? "Unknown")</span></td>}
                    </tr>}
            </tbody>
        </table>
                if (Model.HideClosedJobs && Model.Items.All(j => j.ClosedDate.HasValue))
                {
        <div class="allClosed_container">
            <h3>No open jobs</h3>
            <a href="#" class="dataTables_showStatusClosed button small">Show Closed Jobs (@(Model.Items.Count()))</a>
        </div>
                }
    }
    else
    { 
        <h3>No jobs to display</h3>
    }
</div>
