@model Disco.Web.Areas.Config.Models.DocumentTemplate.BulkGenerateModel
@using Disco.Services.Interop.ActiveDirectory;
@{
    Authorization.RequireAll(Claims.Config.DocumentTemplate.BulkGenerate, Claims.User.Actions.GenerateDocuments);

    ViewBag.Title = Html.ToBreadcrumb("Configuration", MVC.Config.Config.Index(), "Document Templates", MVC.Config.DocumentTemplate.Index(null), Model.DocumentTemplate.Description, MVC.Config.DocumentTemplate.Index(Model.DocumentTemplate.Id), "Bulk Generate");
    Html.BundleDeferred("~/ClientScripts/Modules/Disco-DocumentBulkGenerate");
}
<div id="DocumentTemplate_BulkGenerate">
    <div class="actions">
        @using (Html.BeginForm(MVC.API.DocumentTemplate.BulkGenerate(Model.DocumentTemplate.Id), FormMethod.Post))
        {
            if (Model.TemplatePageCount > 1 && Model.TemplatePageCount % 2 != 0)
            {
                <input id="DocumentTemplate_BulkGenerate_InsertBlankPage" type="checkbox" name="InsertBlankPage" value="True" checked /><label for="DocumentTemplate_BulkGenerate_InsertBlankPage">Insert Blank Pages for Double-Sided Printing</label>
            }
            <input id="DocumentTemplate_BulkGenerate_DataIds" name="DataIds" type="hidden" />
            <button id="BulkGenerate" class="button" disabled>Bulk Generate</button>
            @Html.AntiForgeryToken()
        }
        <br />
        <button id="AddUsers" class="button small">Add Users</button>
        <button id="AddGroupMembers" class="button small">Add Group Members</button>
        @if (Model.UserFlags.Any(f => f.Count > 0))
        {
            <button id="AddUserFlag" class="button small">Add With User Flag</button>
        }
        @if (Model.DeviceProfiles.Any(b => b.Count > 0))
        {
            <button id="AddDeviceProfile" class="button small">Add With Device Profile</button>
        }
        @if (Model.DeviceBatches.Any(b => b.Count > 0))
        {
            <button id="AddDeviceBatch" class="button small">Add With Device Batch</button>
        }
        @if (Model.DocumentTemplates.Any(b => b.Count > 0))
        {
            <button id="AddDocumentAttachment" class="button small">Add With Document Attachment</button>
        }
        @if (Model.UserDetails.Any(b => b.Count > 0))
        {
            <button id="AddUserDetail" class="button small">Add With User Detail</button>
        }
    </div>
    <table class="genericData">
        <thead>
            <tr>
                <th>&nbsp;</th>
                <th>Username</th>
                <th>Name</th>
                <th>Scope</th>
            </tr>
        </thead>
        <tbody>
            <tr class="when-none">
                <td colspan="4">Add Users</td>
            </tr>
        </tbody>
    </table>
</div>

<div id="DocumentTemplate_BulkGenerate_Dialog_AddUsers" class="dialog dialog-bulk-generate" title="@(Model.DocumentTemplate.Description): Add Users">
    <div class="brief">
        <div>
            Enter multiple <span class="scopeDescBulkGenerate">User Ids</span> separated by <code>&lt;new line&gt;</code>, commas (<code>,</code>) or semicolons (<code>;</code>).
        </div>
        <div>
            Security Groups can also be included. Members will be resolved and added.
        </div>
        <div class="examples clearfix">
            <h4>Examples:</h4>
            <div class="example1 code">
                user6<br />
                smi0099<br />
                @(ActiveDirectory.Context.PrimaryDomain.NetBiosName)\rsmith<br />
                Domain Admins
            </div>
            <div class="example2 code">user6,smi0099,@(ActiveDirectory.Context.PrimaryDomain.NetBiosName)\rsmith,Domain Admins</div>
            <div class="example3 code">user6;smi0099;@(ActiveDirectory.Context.PrimaryDomain.NetBiosName)\rsmith;Domain Admins</div>
        </div>
    </div>
    @using (Html.BeginForm(MVC.API.DocumentTemplate.BulkGenerateAddUsers(), FormMethod.Post))
    {
        <div class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="userIds"></div>
        <textarea id="inputBulkGenerateDataIds" name="userIds" data-val="true" data-val-required="Identifiers are required" required></textarea>
        @Html.AntiForgeryToken()
    }
</div>

<div id="DocumentTemplate_BulkGenerate_Dialog_AddGroupMembers" class="dialog dialog-bulk-generate" title="@(Model.DocumentTemplate.Description): Add Group Members">
    <div class="brief">
        <div>
            Add all members of a group (including recursive members) to the bulk generation.
        </div>
    </div>
    @using (Html.BeginForm(MVC.API.DocumentTemplate.BulkGenerateAddGroupMembers(), FormMethod.Post))
    {
        <table class="input">
            <tbody>
                <tr>
                    <th>
                        <label for="DocumentTemplate_BulkGenerate_Dialog_AddGroupMembers_Group">Group:</label>
                    </th>
                    <td>
                        <input id="DocumentTemplate_BulkGenerate_Dialog_AddGroupMembers_Group" type="text" name="groupId" data-autocomplete-src="@(Url.Action(MVC.API.System.SearchGroupSubjects()))" required />
                    </td>
                </tr>
            </tbody>
        </table>
        @Html.AntiForgeryToken()
    }
</div>

@if (Model.UserFlags.Any(f => f.Count > 0))
{
    <div id="DocumentTemplate_BulkGenerate_Dialog_AddUserFlag" class="dialog dialog-bulk-generate" title="@(Model.DocumentTemplate.Description): Add User Flag Assignments">
        <div class="brief">
            <div>
                Add all users associated with the flag to the bulk generation.
            </div>
        </div>
        @using (Html.BeginForm(MVC.API.DocumentTemplate.BulkGenerateAddUserFlag(), FormMethod.Post))
        {
            <input name="flagId" type="hidden" required />
            <div class="dialog-item-picker">
                @foreach (var flag in Model.UserFlags)
                {
                    <div class="item @(flag.Count == 0 ? "disabled" : null)" data-userflagid="@flag.Item.Id">
                        <i class="fa fa-@(flag.Item.Icon) fa-fw fa-lg d-@(flag.Item.IconColour)"></i>@flag.Item.Name <small>(@flag.Count.ToString("N0") user@(flag.Count == 1 ? null : "s"))</small>
                    </div>

                }
            </div>
            @Html.AntiForgeryToken()
        }
    </div>
}

@if (Model.DeviceProfiles.Any(b => b.Count > 0))
{
    <div id="DocumentTemplate_BulkGenerate_Dialog_AddDeviceProfile" class="dialog dialog-bulk-generate" title="@(Model.DocumentTemplate.Description): Add User by Assigned Device Profile">
        <div class="brief">
            <div>
                Add all users associated with a device in the selected profile to the bulk generation.
            </div>
        </div>
        @using (Html.BeginForm(MVC.API.DocumentTemplate.BulkGenerateAddDeviceProfile(), FormMethod.Post))
        {
            <input name="deviceProfileId" type="hidden" required />
            <div class="dialog-item-picker">
                @foreach (var profile in Model.DeviceProfiles)
                {
                    <div class="item @(profile.Count == 0 ? "disabled" : null)" data-id="@profile.Item.Id">
                        <i class="fa fa-cog fa-fw fa-lg"></i>@profile.Item.Name <small>(@profile.Count.ToString("N0") user@(profile.Count == 1 ? null : "s"))</small>
                    </div>
                }
            </div>
            @Html.AntiForgeryToken()
        }
    </div>
}

@if (Model.DeviceBatches.Any(b => b.Count > 0))
{
    <div id="DocumentTemplate_BulkGenerate_Dialog_AddDeviceBatch" class="dialog dialog-bulk-generate" title="@(Model.DocumentTemplate.Description): Add User by Assigned Device Batch">
        <div class="brief">
            <div>
                Add all users associated with a device in the selected batch to the bulk generation.
            </div>
        </div>
        @using (Html.BeginForm(MVC.API.DocumentTemplate.BulkGenerateAddDeviceBatch(), FormMethod.Post))
        {
            <input name="deviceBatchId" type="hidden" required />
            <div class="dialog-item-picker">
                @foreach (var batch in Model.DeviceBatches)
                {
                    <div class="item @(batch.Count == 0 ? "disabled" : null)" data-id="@batch.Item.Id">
                        <i class="fa fa-cog fa-fw fa-lg"></i>@batch.Item.Name <small>(@batch.Count.ToString("N0") user@(batch.Count == 1 ? null : "s"))</small>
                    </div>
                }
            </div>
            @Html.AntiForgeryToken()
        }
    </div>
}

@if (Model.DocumentTemplates.Any(b => b.Count > 0))
{
    <div id="DocumentTemplate_BulkGenerate_Dialog_AddDocumentAttachment" class="dialog dialog-bulk-generate" title="@(Model.DocumentTemplate.Description): Add User by Assigned Device Batch">
        <div class="brief">
            <div>
                Add all users associated with an attachment of the selected document template to the bulk generation.
            </div>
        </div>
        @using (Html.BeginForm(MVC.API.DocumentTemplate.BulkGenerateAddDocumentAttachment(), FormMethod.Post))
        {
            <input name="documentTemplateId" type="hidden" required />
            <div class="dialog-item-picker">
                @foreach (var template in Model.DocumentTemplates)
                {
                    <div class="item @(template.Count == 0 ? "disabled" : null)" data-id="@template.Item.Id">
                        <i class="fa fa-file-text-o fa-fw fa-lg"></i>@template.Item.Id: @template.Item.Description <small>(@template.Count.ToString("N0") user@(template.Count == 1 ? null : "s"))</small>
                    </div>
                }
            </div>
            <div class="sub">
                <label for="DocumentTemplate_BulkGenerate_Dialog_AddDocumentAttachment_Threshold">Attachment Added After <small>(optional)</small></label>
                <input id="DocumentTemplate_BulkGenerate_Dialog_AddDocumentAttachment_Threshold" name="threshold" type="date" />
            </div>
            @Html.AntiForgeryToken()
        }
    </div>
}

@if (Model.UserDetails.Any(b => b.Count > 0))
{
    <div id="DocumentTemplate_BulkGenerate_Dialog_AddUserDetail" class="dialog dialog-bulk-generate" title="@(Model.DocumentTemplate.Description): Add User by Detail">
        <div class="brief">
            <div>
                Add all users with a matching user detail to the bulk generation.
            </div>
        </div>
        @using (Html.BeginForm(MVC.API.DocumentTemplate.BulkGenerateAddUserDetail(), FormMethod.Post))
        {
            <input name="key" type="hidden" required />
            <input name="value" type="hidden" />

            <div id="DocumentTemplate_BulkGenerate_Dialog_AddUserDetail_Key" class="dialog-item-picker" style="width: 49%; float: left;">
                @foreach (var key in Model.UserDetails)
                {
                    <div class="item @(key.Count == 0 ? "disabled" : null)" data-id="@key.Item">
                        <i class="fa fa-info fa-fw fa-lg"></i>@key.Item.TrimEnd('*', '&') <small>(@key.Count.ToString("N0") user@(key.Count == 1 ? null : "s"))</small>
                    </div>
                }
            </div>

            @Html.AntiForgeryToken()
        }
        @using (Html.BeginForm(MVC.API.DocumentTemplate.BulkGenerateGetUserDetailValues(), FormMethod.Post))
        {
            <input name="key" type="hidden" required />

            <div id="DocumentTemplate_BulkGenerate_Dialog_AddUserDetail_Value" class="dialog-item-picker" style="width: 49%; float: left; margin-left: 1%;">
            </div>

            @Html.AntiForgeryToken()
        }
    </div>
}