{if $unread_notices}
    <script>

        $('#lms-userpanel-menu-item-notices .warning-icon').addClass('lms-userpanel-icon-warning');

    </script>
{/if}

{if $urgent_notice}
    <style>

        #urgent-notice-body {
            margin-top: 0.5em;
            margin-bottom: 1em;
        }

        #urgent-notice-attachments {
            border-top: 1px dotted black;
            margin-top: 0.25em;
            padding-top: 0.5em;
        }

        #urgent-notice-attachments ul {
            margin-block-start: 0;
            margin-block-end: 0;
            list-style-type: none;
            padding-inline-start: 0;
            margin-top: 0.5em;
        }

        #urgent-notice-attachments ul li {
            padding-top: 0.25em;
            padding-bottom: 0.25em;
        }

    </style>

    <script>

        $(function () {
            $("#urgent-notice-dialog").dialog({
                resizable: false,
                height: "auto",
                width: 400,
                modal: true,
                position: {
                    my: 'center center',
                    at: 'center top'
                },
                show: {
                    effect: "fade",
                    duration: 350
                },
                hide: {
                    effect: "fade",
                    duration: 350
                },
                open: function(){
                    $('.ui-widget-overlay').bind('click', function() {
                        $("#urgent-notice-dialog").dialog('close');
                    })
                },
                buttons: [{
                    text: "{trans("I confirm reading")}",
                    class: "lms-ui-button",
                    click: function () {
                        $.ajax(
                            '?m=notices&ajax=1&confirm_urgent=' + $("#urgent-notice-dialog").attr('data-id'),
                            {
                                dataType: 'json',
                                success: function (data) {
                                    $('#urgent-notice-dialog').dialog('close');
                                    if (!parseInt(data["unread_notices"])) {
                                        $('#lms-userpanel-menu-item-notices .warning-icon').removeClass('lms-userpanel-icon-warning');
                                    }
                                    if (typeof (data["urgent_notice"]) == 'object') {
                                        $('#urgent-notice-dialog').one('dialogclose', function () {
                                            $('#urgent-notice-dialog').attr('data-id', data["urgent_notice"].id);
                                            cdate = new Date(parseInt(data["urgent_notice"].cdate) * 1000);
                                            $('#urgent-notice-dialog').attr('title', sprintf("%04d/%02d/%02d %02d:%02d / %s",
                                                cdate.getFullYear(),
                                                cdate.getMonth() + 1,
                                                cdate.getDate(),
                                                cdate.getHours(),
                                                cdate.getMinutes(),
                                                data["urgent_notice"].subject
                                            ));
                                            $('#urgent-notice-body').html(data["urgent_notice"].body);
                                            $('#urgent-notice-dialog').dialog('open');
                                        });
                                    }
                                }
                            }
                        );
                    }
                }]
            });
        });

    </script>
    {assign var=title_date value=$urgent_notice.cdate|date_format:"Y-m-d H:i"}
    {assign var=title_subject value=$urgent_notice.subject}
    <div id="urgent-notice-dialog" data-id="{$urgent_notice.id}" title="{$title_date} / {$title_subject}">
        <div id="urgent-notice-body">{$urgent_notice.body}</div>
        {if !empty($urgent_notice.attachments)}
            <div id="urgent-notice-attachments">
                {icon name="attachment"}
                <strong>{trans("Attachments")}</strong>
                <ul>
                    {foreach $urgent_notice.attachments as $attachmentid => $attachment}
                        <li>
                            <a href="?m=notices&f=attachmentview&id={$attachmentid}">
                                {$attachment.filename|escape}
                                {if preg_match('/pdf/', $attachment.contenttype)}
                                    {$icon = "pdf"}
                                {elseif preg_match('/csv|excel|spreadsheet/', $attachment.contenttype)}
                                    {$icon = "xls"}
                                {/if}
                                {icon name="lms-ui-icon-view" class=$icon}
                            </a>
                        </li>
                    {/foreach}
                </ul>
            </div>
        {/if}
    </div>
{/if}
