@using PantheonRA.Contracts.DataContracts;
@model PantheonRA.BackOffice.Models.WoExDocument

@{
    var _isMobile = Request.Browser.IsMobileDevice;
}

@Html.Hidden("UploadLoc", Loc.Upload)
@Html.Hidden("DeleteWTAttachmentsUrl", Url.Action("DeleteAttachment", "Manufacturing"))
@Html.Hidden("PrevAcKey", Model.PrevAcKey)
@Html.Hidden("NextAcKey", Model.NextAcKey)

<div class="wrapper wrapper-content">
    <div class="tabs-container">
        @Html.Partial("_ProcessingTabNavigation")
        <div class="tab-content">
            <div class="row document-head @(_isMobile ? "d-head-m-b-0" : "")">

                @* AcKey *@
                <div class="col-xs-6 document-head-disabled-input">
                    <div class="form-group">
                        @Html.LabelFor(model => model.AcKeyView, Loc.Key, new { @class = "control-label m-t-xs" })
                        <div class="control-label @(string.IsNullOrEmpty(Model.AcKeyView) ? "hidden" : string.Empty)">

                            <h4 class="control-label">
                                <div class="prev-next-key-display">
                                    <i class="fa fa-chevron-left" onclick="prevWorkOrder('@Model.PrevAcKey')"></i>
                                    <span>@Model.AcKeyView</span>
                                    <i class="fa fa-chevron-right" onclick="nextWorkOrder('@Model.NextAcKey')"></i>
                                </div>
                            </h4>
                        </div>
                    </div>
                </div>

                @* Ident *@
                <div class="col-xs-6 document-head-disabled-input">
                    <div class="form-group">
                        @Html.LabelFor(model => model.Ident, Loc.Ident, new { @class = "control-label m-t-xs" })
                        <input type="text" disabled="disabled" class="form-control" style="overflow:hidden;text-overflow:ellipsis" id="ident" value="@Model.Ident" />
                    </div>
                </div>

            </div>

            @if (!ViewBag.HasPersonalFolder)
            {
                <div class="alert alert-warning alert-dismissible" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    @Loc.NoPersonalFolderWarning
                </div>
            }

            <div class="scrollable-wrapper">
                <div id="work-order-documents" style="max-width:800px">
                    <form action="@Url.Action("UploadDocument", "Manufacturing", new { key = Model.AcKey })"
                          method="post"
                          enctype="multipart/form-data"
                          id="work-order-document-form">

                        @Html.HiddenFor(model => model.AcKey)
                        @Html.HiddenFor(model => model.Subject)
                        @Html.HiddenFor(model => model.ClassificationId)
                        @Html.HiddenFor(model => model.DocType)

                        @Html.Partial("~/Views/Documents/_FileDisplayPartial.cshtml", Model.Files ?? new List<FileData>(),
                            new ViewDataDictionary(ViewData) { { "uploadFiles", true } })
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="~/Scripts/custom/global-helpers.js"></script>
<script src="~/Scripts/custom/manufacturing/helpers.js"></script>
@Scripts.Render("~/bundles/fileDisplay")

<script>
    $(document).ready(function () {
        disableChangeAcKey();
        dropifyUpload();
        initializeFileDisplay('', '@(Model.Files != null && Model.Files.Any() ? Model.Files[0].Id.ToString() : string.Empty)');
    });

    function dropifyUpload() {
        $('#uploadFiles').attr('data-height', 100);
        $('#DeleteAttachmentUrl').val($('#DeleteWTAttachmentsUrl').val())

        var dr = $('.dropify').dropify({
            messages: {
                'default': $("#UploadLoc").val(),
                'replace': $("#UploadLoc").val(),
                'remove': $("#Remove").val(),
                'error': $("#GeneralErrorMessage").val()
            },
            error: {
                'imageFormat': $("#WrongImageFormat").val(),
                'fileExtension': $("#WrongImageFormat").val(),
                'fileSize': $("#FileSizeMessage").val()
            }
        });

        $('.dropify').on('change', function () {
            setTimeout(function () {
                if ($('.dropify-error').is(':hidden')) {
                    $('.dropify-loader').css('display', 'block');
                    $('#work-order-document-form').submit();
                }
            }, 600);
        });

        var cc = paw.com.CameraCaptureComponent.create($('#camera-capture'), function (captured) {
            paw.com.CameraCaptureComponent.uploadAsForm($('#work-order-document-form')[0], 'uploadFiles', captured, cc).then(function () {
                window.location.reload();
            });
        });
    }
</script>
