@using PantheonRA.BackOffice.Helpers;
@model List<PantheonRA.Contracts.DataContracts.FileData>

<div class="attachmentsContainer dropzone">
    <div id="attachments" class="clearfix attachments-btns-container">
        @if (ViewData["uploadFiles"] as bool? == true)
        {
            <table class="full-width">
                <tr>
                    <td>
                        <input type="file"
                               name="uploadFiles"
                               id="uploadFiles"
                               class="dropify"
                               data-height="30"
                               data-show-loader="true"
                               data-max-file-size="10M"
                               title="@Loc.ClickOrDragAndDropFile"
                               data-allowed-file-extensions='["jpg", "jpeg", "png", "xlsx", "xls", "pdf", "doc", "docx", "odt", "txt", "csv"]'
                               multiple />
                    </td>
                    <td class="attachments-list-btn hidden">
                        <button class="btn btn-link btn-bg-default" onclick="showAttachmentsClick()" type="button"><i class="fas fa-list text-primary"></i> @Loc.List</button>
                    </td>
                </tr>
            </table>
        }
    </div>
    <div id="mediaViewerContainer" class="attachments-media-viewer-container hidden"></div>
    <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].DocumentAcKey, new { id = "documentAcKey" + 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 })

                            <td class="text-center attachment-item">
                                @if (ExtensionHelper.GetMimeType(Model.ElementAt(i).Name).Contains("image"))
                                {
                                    <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;" />
                                }
                                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">
                                @if (!string.IsNullOrEmpty(Model[i].DocumentAcKey))
                                {
                                    <div class="m-b-sm">
                                        <a href="@Url.Action("Edit", "Documents", new { id = Model[i].DocumentAcKey })" target="_self">
                                            @Model[i].DocumentAcKeyDisplay
                                        </a>
                                    </div>
                                }
                                <i class="@ExtensionHelper.GetExtensionIcon(Path.GetExtension(@Model[i].Name).TrimStart('.')) attachment-icon-margin"></i>
                                @Model[i].Name
                            </td>
                            <td style="vertical-align: middle">
                                <button onclick='confirmDeleteAttachment(@Model[i].DocumentKey, @Model[i].AttachmentId)'
                                        class="btn btn-outline" type="button">
                                    <span class="far fa-trash-alt"></span>
                                </button>
                            </td>
                        </tr>
                    }
                }
            </table>
        }
    </div>
</div>

@Html.Hidden("AddAttachment", Loc.AddAttachment)
@Html.Hidden("DragAndDropOrClick", Loc.DragAndDropAFileHereOrClick)
@Html.Hidden("DragAndDropOrClickToReplace", Loc.DragAndDropOrClickToReplace)
@Html.Hidden("Remove", Loc.Remove)
@Html.Hidden("GeneralErrorLoc", Loc.GeneralError)
@Html.Hidden("ConfirmLoc", Loc.Confirm)
@Html.Hidden("YesLoc", Loc.Yes)
@Html.Hidden("NoLoc", Loc.No)
@Html.Hidden("ConfirmDeleteMessageLoc", Loc.ConfirmDeleteMessage)
@Html.Hidden("FileSizeMessage", string.Format(Loc.FileSizeMessage, "10MB"))
@Html.Hidden("FileNotAllowedMessage", Loc.FileNotAllowed)
@Html.Hidden("AddConnectionUrl", Url.Action("AddDocumentConnection", "Documents"))
@Html.Hidden("DeleteConnectionUrl", Url.Action("DeleteDocumentConnection", "Documents"))
@Html.Hidden("DeleteAttachmentUrl", Url.Action("DeleteAttachment", "Documents"))