@using PantheonRA.BackOffice.Helpers;
@model List<PantheonRA.WebApi.Contracts.PaFile>

@{ 
    var attachmentsId = "Attachments[{0}].{1}";
}

    @Html.Hidden("AttachmentsPrefix", string.Format(attachmentsId, "OrdinalNumberNo", "Property"))

<div class="attachmentsContainer dropzone">
    @Html.Partial("_AttachmentsCreate")
    <div class="attachments-grid" id="attachments-grid">
        @if (Model != null)
        {
            <table id="AttachmentsTable" class="table table-hover table-striped attachment-table">
                @for (var i = 0; i < Model.Count; i++)
                {
                    if (Model[i] != null)
                    {
                        <tr id="attachmentRow-@Model[i].Id" class="attachments-grid-file" data-fileId="@Model[i].Id">
                            @Html.HiddenFor(model => model[i].Id, new { id = "id" + Model[i].Id })
                            @Html.HiddenFor(model => model[i].Name, new { id = "attachmentName" + Model[i].Id })
                            @Html.HiddenFor(model => model[i].DocumentKey, new { id = "documentKey" + Model[i].Id })
                            @Html.HiddenFor(model => model[i].AttachmentId, new { id = "attachmentId" + Model[i].Id })
                            @Html.HiddenFor(model => model[i].AttachmentGuid, new { id = "attachmentGuid" + Model[i].Id })
                            @Html.Hidden("Id", Model[i].DocumentKey)

                            <td class="text-center attachment-item">
                                @if (ExtensionHelper.GetMimeType(Model.ElementAt(i).Name).Contains("image"))
                                {
                                    <a href="@Url.Action("DisplayFile", "Documents", 
                                            new { DocumentKey = Model.ElementAt(i).DocumentKey, 
                                                AttachmentId = Model.ElementAt(i).AttachmentId, 
                                                AttachmentName = Model.ElementAt(i).Name, 
                                                Download = false })" target="_blank">
                                        <img class="img-thumbnail"
                                             src="@Url.Action("DisplayFile", "Documents", 
                                                 new { DocumentKey = Model.ElementAt(i).DocumentKey, 
                                                     AttachmentId = Model.ElementAt(i).AttachmentId, 
                                                     AttachmentName = Model.ElementAt(i).Name, 
                                                     Download = false })" style="max-height:100px;" />
                                    </a>
                                    
                                }
                                else
                                {
                                    if (ExtensionHelper.GetMimeType(Model.ElementAt(i).Name).Contains("pdf"))
                                    {
                                        <iframe src="@Url.Action("DisplayFile", "Documents", 
                                                    new { DocumentKey = Model.ElementAt(i).DocumentKey, 
                                                        AttachmentId = Model.ElementAt(i).AttachmentId, 
                                                        AttachmentName = Model.ElementAt(i).Name, 
                                                        Download = false })#view=Fit&toolbar=0"
                                                style="border:none;width:100px;height:100%"></iframe>
                                    }
                                    else
                                    {
                                        <a href="@Url.Action("DisplayFile", "Documents", 
                                                new { DocumentKey = Model.ElementAt(i).DocumentKey, 
                                                    AttachmentId = Model.ElementAt(i).AttachmentId, 
                                                    AttachmentName = Model.ElementAt(i).Name, 
                                                    Download = true })" target="_blank">
                                            <i class="fa fa-file fa-4x"></i>
                                        </a>
                                    }
                                }
                            </td>
                            <td style="vertical-align: middle" class="attachment-item">
                                <div class="m-b-sm" style="min-width:95px">
                                    <a href="@Url.Action("Edit", "Documents", new { id = Model.ElementAt(i).DocumentAcKey })" target="_self">
                                        @Model.ElementAt(i).DocumentAcKeyDisplay
                                    </a>
                                </div>
                                
                                @if (ExtensionHelper.GetMimeType(Model.ElementAt(i).Name).Contains("image"))
                                {
                                    <a href="@Url.Action("DisplayFile", "Documents",
                                                new { DocumentKey = Model.ElementAt(i).DocumentKey,
                                                    AttachmentId = Model.ElementAt(i).AttachmentId,
                                                    AttachmentName = Model.ElementAt(i).Name,
                                                    Download = false })" target="_blank">
                                        <i class="@ExtensionHelper.GetExtensionIcon(Path.GetExtension(@Model[i].Name).TrimStart('.')) attachment-icon-margin"></i>
                                        @Model[i].Name
                                    </a>
                                }
                                else
                                {
                                    <a href="@Url.Action("DisplayFile", "Documents",
                                                new { DocumentKey = Model.ElementAt(i).DocumentKey,
                                                    AttachmentId = Model.ElementAt(i).AttachmentId,
                                                    AttachmentName = Model.ElementAt(i).Name,
                                                    Download = false })" target="_blank">
                                        <i class="@ExtensionHelper.GetExtensionIcon(Path.GetExtension(@Model[i].Name).TrimStart('.')) attachment-icon-margin"></i>
                                        @Model[i].Name
                                    </a>
                                }
                            </td>
                            <td style="vertical-align: middle">
                                <button onclick='confirmDeleteAttachment(@Model[i].AttachmentId)'
                                        class="btn btn-outline" type="button">
                                    <span class="far fa-trash-alt"></span>
                                </button>
                            </td>
                        </tr>
                    }
                }
            </table>
        }
    </div>
</div>

