@page "/fstaprl"
@inject AuthenticationStateProvider AuthenticationStateProvider
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using NPetrovich
@using Otchinslator.Services
@attribute [Authorize]
@inject IStatementGenerator StatementGenerator

<PageTitle>Заяление отправлено | 1 апреля</PageTitle>

@code {

    protected override async Task OnInitializedAsync()
    {
        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        var userEmail = authState.User.Identity.Name;
        var userFIO = authState.User.Claims.FirstOrDefault(x => x.Type == "name")?.Value;

        var petrovich = new Petrovich()
        {
            FirstName = userFIO.Split(' ')[0],
            LastName = userFIO.Split(' ')[1],
            MiddleName = userFIO.Split(' ').Length > 2 ? userFIO.Split(' ')[2] : "",
            Gender = Gender.Male
        };

        userFIO = petrovich.InflectFirstNameTo(Case.Genitive) + " " + petrovich.InflectLastNameTo(Case.Genitive) + " " + petrovich.InflectMiddleNameTo(Case.Genitive);

        var userData = new UserData
        {
            reason = "Прошу отчислить меня по собственному желанию в связи с не желанием продолжать обучение в данном учебном заведении, планирую перевестись в ДГТУ.",
            email = userEmail,
            phone = "",
            fio = userFIO,
            kurs = 1,
            isFreeEducation = true,
            isOchno = true,
            speciality = SpecialityType.Bakalavriat,
            direction = ""
        };
        var statement = await StatementGenerator.GenerateStatementAsync(userData, "frstaprl.docx");
        var pdf = await StatementGenerator.ConvertToPDFAsync(statement);

        await using var fileStream = new FileStream("./PDFCache/" + userEmail.Split('@')[0] + ".pdf", FileMode.Create, FileAccess.Write);
        await pdf.CopyToAsync(fileStream);
    }

}

<dialog id="denyModal" class="modal modal-bottom sm:modal-middle">
    <div class="modal-box">
        <h3 class="text-lg font-bold text-center">С 1 апреля!</h3>
        @* <p class="py-4 font-bold text-center"></p> *@
        <img src="https://risovach.ru/upload/2013/04/mem/moe-lico_15228723_orig_.jpeg" class="h-80 my-2 mx-auto"
             alt=""/>
        @* <p class="text-center">.</p> *@
        <div class="modal-action justify-center">
            <form method="dialog">
                <button class="btn btn-primary">Понял, принял</button>
            </form>
        </div>
    </div>
</dialog>

<div class="relative">
    <div
        class="text-center font-bold text-3xl md:text-5xl w-max absolute left-1/2 -top-1/4 transform -translate-x-1/2 italic">
        <br>Заявление на отчисление<p class="text-2xl font-normal">отправлено директору</p>
    </div>
    <div id="pdfrenderer">
        <object data="/getStatement" type="application/pdf" class="w-96 sm:w-[32rem] h-[30rem] rounded-2xl text-center">
            <p>Не удалось отобразить заявление</p>
            <button id="alterRender" class="btn btn-primary my-2">использовать альтернативную отрисовку</button>
            <p>или откойте в браузере на основе Firefox</p>
        </object>
    </div>
    <div class="join w-full mt-4 flex gap-2">
        <button onclick="denyModal.showModal()" class="btn btn-error rounded-full flex-grow w-30">
            Отозвать
        </button>
        <a id="downloadPDF" target="_blank" href="/getStatement"
           class="btn btn-primary bg-base-200 border-base-200 rounded-full flex-grow-0 w-[3rem]">
            @* Скачать PDF *@
            <div class="absolute rounded-full w-12 h-12">
                <img class="p-3" src="img/pdf.svg" alt=""/>
            </div>
        </a>
    </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.10.377/pdf.min.js"></script>
<script>
    const alterRenderButton = document.getElementById('alterRender');
    alterRenderButton.addEventListener('click', alterRender);

    function alterRender() {
        const pdfrenderer = document.getElementById('pdfrenderer');
        const url = '/getStatement';
        pdfrenderer.innerHTML = '<canvas id="pdf-canvas" class="w-96 rounded-2xl"></canvas>';
        pdfjsLib.getDocument(url).promise.then(function (pdf) {
            pdf.getPage(1).then(function (page) {
                const viewport = page.getViewport({scale: 1});

                const canvas = document.getElementById('pdf-canvas');
                const context = canvas.getContext('2d');
                canvas.width = viewport.width;
                canvas.height = viewport.height;

                const renderContext = {
                    canvasContext: context,
                    viewport: viewport
                };
                page.render(renderContext);
            });
        });
    }
</script>