<div class="modal" tabindex="-1" role="dialog" style="display:none" id="@elementid">
    <div class="modal-container">
        <div class="modal__body">
            <div class="modal__body__header">
                <div class="modal__body__header__info">
                    <img src="img/user.svg" alt="">
                    <p>@Title</p>
                </div>
            </div>
            <div class="modal__body__container modal__body__container_noflex" style="vertical-align:top"> 
                <div class="modal__404__text__box">
                    @Body

                    <button class="btn_orange" style="margin-bottom: 20px" @onclick="SendTestNotification">Отправить тестовое сообщение этому юзеру</button>

                    <table border="1">
                        <tr>
                            <th>Тип</th>
                            <th>Заголовок</th>
                            <th>Сообщение</th>
                            <th>Прочитано</th>
                            <th>Утверждено</th>
                            <th>Отклонено</th>
                        </tr>
                        @foreach (var n in _notifications)
                        {
                            <tr>
                                <td>@n.Value.NotifiType</td>
                                <td>@n.Value.Header</td>
                                <td>@n.Value.Body</td>
                                <td>
                                    <input type="checkbox" @bind="n.Value.IsRead" disabled>
                                    <button @onclick="(() => ReadOnClick(n.Value.ID))">read</button>
                                </td>
                                <td>
                                    <input type="checkbox" @bind="n.Value.IsApproved" disabled>
                                    <button @onclick="(() => ApproveOnClick(n.Value.ID))">approve</button>
                                </td>
                                <td>
                                    <input type="checkbox" @bind="n.Value.IsRejected" disabled>
                                    <button @onclick="(() => RejectOnClick(n.Value.ID))">reject</button>
                                </td>
                            </tr>
                        }
                    </table>
                </div>
            </div>
        </div>
        <a class="modal_close" style="cursor:pointer" @onclick="@Close">&#10006;</a>
    </div>
</div>

@code {
    @using HyperCube.Models;

    [Inject]
    public IJSRuntime JsRuntime { get; set; }

    [Parameter]
    public RenderFragment Title { get; set; }

    [Parameter]
    public RenderFragment Body { get; set; }

    string message = "";
    string elementid = "modal_notifications";

    Dictionary<int, Models.Notification> _notifications = new();
    AccountModel _currentAcc = new();


    public void Open(Dictionary<int, Notification> notifications, AccountModel acc)
    {
        _notifications = notifications;
        _currentAcc = acc;
        JsRuntime.InvokeVoidAsync("OpenModal", elementid);
    }

    public void Close()
    {
        JsRuntime.InvokeVoidAsync("CloseModal", elementid);
    }

    async Task ReadOnClick(int id)
    {
        if (!_notifications[id].IsRead)
        {
            _notifications[id].IsRead = true;
            _notifications[id].DateRead = DateTime.Now;
        }
        else
        {
            _notifications[id].IsRead = false;
            _notifications[id].DateRead = null;
        }

        await _notifications[id].Update();
    }

    async Task ApproveOnClick(int id)
    {
        if (!_notifications[id].IsApproved)
        {
            _notifications[id].IsApproved = true;
            _notifications[id].DateApprove = DateTime.Now;
        }
        else
        {
            _notifications[id].IsApproved = false;
            _notifications[id].DateApprove = null;
        }

        await _notifications[id].Update();
    }

    async Task RejectOnClick(int id)
    {
        if (!_notifications[id].IsRejected)
        {
            _notifications[id].IsRejected = true;
            _notifications[id].DateReject = DateTime.Now;
        }
        else
        {
            _notifications[id].IsRejected = false;
            _notifications[id].DateReject = null;
        }

        await _notifications[id].Update();
    }

    async Task SendTestNotification()
    {
        Notification notification = new(NotificationType.System, "Test from button", "message", _currentAcc.UUID, _currentAcc.UUID);

        int newid = await notification.Send();

        /// adding only in local collection yet
        _notifications.Add(newid, notification);
    }
}