@model Disco.Web.Areas.Config.Models.Shared.DeviceGroupDocumentTemplateBulkGenerateModel
@if (Model.BulkGenerateDocumentTemplates != null && Model.BulkGenerateDocumentTemplates.Any())
{
    List<AttachmentTypes> allowedTargets = new List<AttachmentTypes>();
    if (Authorization.HasAll(Claims.Device.Actions.GenerateDocuments, Claims.Config.DocumentTemplate.BulkGenerate))
    {
        allowedTargets.Add(AttachmentTypes.Device);
    }
    if (Authorization.HasAll(Claims.Job.Actions.GenerateDocuments, Claims.Config.DocumentTemplate.BulkGenerate))
    {
        allowedTargets.Add(AttachmentTypes.Job);
    }
    if (Authorization.HasAll(Claims.User.Actions.GenerateDocuments, Claims.Config.DocumentTemplate.BulkGenerate))
    {
        allowedTargets.Add(AttachmentTypes.User);
    }
    var allowedTemplates = Model.BulkGenerateDocumentTemplates.Where(m => allowedTargets.Contains(m.AttachmentType)).ToList();
    if (allowedTemplates.Any())
    {
        string targetDescription;
        int targetId = Model.DeviceGroupId;
        Func<ActionResult> urlDelegate;
        if (Model is Disco.Web.Areas.Config.Models.DeviceBatch.ShowModel)
        {
            targetDescription = "Batch";
            urlDelegate = MVC.API.DocumentTemplate.BulkGenerateDeviceBatch;
        }
        else if (Model is Disco.Web.Areas.Config.Models.DeviceModel.ShowModel)
        {
            urlDelegate = MVC.API.DocumentTemplate.BulkGenerateDeviceModel;
            targetDescription = "Model";
        }
        else if (Model is Disco.Web.Areas.Config.Models.DeviceProfile.ShowModel)
        {
            urlDelegate = MVC.API.DocumentTemplate.BulkGenerateDeviceProfile;
            targetDescription = "Profile";
        }
        else
        {
            throw new NotSupportedException("Unsupported Device Group Model");
        }
        <div class="dialog" id="Config_Shared_DeviceGroupDocumentTemplateBulkGenerate_Dialog" title="Document Template Bulk Generate for Device @targetDescription">
            <p>
                Bulk generate documents for devices, users or jobs (based on document template scope) associated with this Device @(targetDescription).
            </p>
            @using (Html.BeginForm(urlDelegate(), FormMethod.Post))
            {
                @Html.AntiForgeryToken()
                @Html.Hidden("deviceGroupId", targetId)
                <select name="id" required>
                    <option value="">-- Choose Document Template --</option>
                    @foreach (var template in allowedTemplates)
                    {
                        <option value="@template.Id">@template.Description (@(template.AttachmentType)s)</option>
                    }
                </select>
            }
        </div>
        <script type="text/javascript">
            $(function () {
                const button = $('<button class="button">Document Bulk Generate</button>').prependTo('.actionBar');
                let dialog = null;
                button.click(e => {
                    if (!dialog) {
                        dialog = $('#Config_Shared_DeviceGroupDocumentTemplateBulkGenerate_Dialog')
                            .dialog({
                                resizable: false,
                                width: 450,
                                modal: true,
                                autoOpen: false,
                                buttons: {
                                    Close: function () {
                                        $(this).dialog("close");
                                    },
                                    'Bulk Generate': function () {
                                        const form = dialog.find('form')[0];
                                        if (form.reportValidity()) {
                                            form.submit();
                                            $(this).dialog("close");
                                        }
                                    }
                                }
                            });
                    }
                    dialog.dialog('open');
                });
            });
        </script>
    }
}