@model PantheonRA.BackOffice.Models.InvoicePreviewViewModel
@{
    ViewBag.Title = Loc.Invoice;
    Html.RenderPartial("_Breadcrumb", new PantheonRA.BackOffice.Models.BreadcrumbViewModel
    {
        Links = new List<Tuple<string, string>> {
            new Tuple<string, string>(Loc.CashRegister, Url.Action("Index", "CashRegister")),
            new Tuple<string, string>(Loc.PreviewAndPrint, string.Empty)
        }
    });
}

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row m-b-sm">
        <div class="col-xs-12">
            <a href="#" onclick="redirectBack(); return false;" class="btn btn-success pull-right m-l-xs">@Loc.Back</a>

            @if (Model.DocType == Request.GetCookieClaim<string>(PantheonRA.BackOffice.Helpers.Security.JwtToken.CashierCookieName, "cashRegisterId"))
            {
                <div onclick="invokeCancelInvoice(this);" class="btn btn-outline btn-danger pull-right m-l-xs"><i class="fa fa-ban"></i> @Loc.CancelInvoice</div>
            }

            @if (Model.PreviewOptions != null)
            {
                <button id="printButton" class="btn btn-primary pull-right m-l-xs" onClick="print('@(Model.PreviewOptions.First().PreviewUrl)&preview=false')">@Loc.Print</button>
                <select id="printOptions" class="form-control form-control-medium pull-right @(Model.PreviewOptions.Count == 0 ? "hidden" : string.Empty)">
                    @foreach (var previewOption in Model.PreviewOptions)
                    {
                        <option value="@previewOption.PreviewUrl">@previewOption.DisplayName</option>
                    }
                </select>
            }
        </div>
    </div>

    <div class="row">
        <div class="col-lg-12">
            <div class="ibox-content p-xl">
                <div id="mediaViewerContainer"></div>
            </div>
        </div>
    </div>
</div>

@section pageScripts{
    <script type="text/javascript">
        $("#mediaViewerContainer").mediaViewer({ dataUrl: '@Model.PreviewOptions.First().PreviewUrl&preview=true', dataType: "pdf" });

        $("#printOptions").on("change", function () {
            $("#mediaViewerContainer").mediaViewer({ dataUrl: $(this).val()+"&preview=true", dataType: "pdf" });
        });

        function redirectBack() {
            var url = getUrlParam('returnUrl');
            if (url) {
                window.location.href = decodeURIComponent(url);
            } else {
                window.location.href = '@Url.Action("Index", "CashRegister")';
            }
        }

        function print(url) {
            var myWindow = window.open(url, '_blank');
            myWindow.document.close();

            $(myWindow.window).on("load", function () {
                myWindow.focus();
                myWindow.print();
                //myWindow.close();
            });

            //redirectBack();
        }

        function invokeCancelInvoice(button) {
            if ('@(Model.Clerk == PantheonRA.BackOffice.CurrentUser.Id)' === '@bool.FalseString') {
                swal({
                    title: '@Loc.CancelationOfInvoice' + '?',
                    text: '@(string.Format(Loc.CancelInvoiceWillBecreatedByCurrentUser, PantheonRA.BackOffice.CurrentUser.DisplayName))',
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: '@Loc.Yes',
                    cancelButtonText: '@Loc.No',
                    closeOnConfirm: true
                }, function () {
                    cancelInvoice(button);
                });
            }
            else {
                cancelInvoice(button);
            }
        }

        function cancelInvoice(button) {
             $(button).find('i').removeClass('fa-ban').addClass('fa-spinner fa-spin fa-fw');

            $.get("@Url.Action("CancelInvoice", "CashRegister", new { acKey = Model.AcKey})", function (data) {
                if (data.Success) {
                    window.location.href = '@Url.Action("Index", "CashRegister")' + '?acKey=' + data.AcKey;
                }
                else {
                    $(button).find('i').removeClass('fa-spinner fa-spin fa-fw').addClass('fa-times');
                }
            }).fail(function () {
                $(button).find('i').removeClass('fa-spinner fa-spin fa-fw').addClass('fa-times');
            });
        }

        function getUrlParam (parameterName) {
            var results = new RegExp('[\?&]' + parameterName + '=([^&#]*)').exec(window.location.href);

            if(results == null){
                return null;
            } else {
                return results[1] || 0;
            }
        }
    </script>
}