@using Disco.Web.Areas.Config.Models.UserFlag;
@model ExportModel
@{
    Authorization.RequireAny(Claims.Config.UserFlag.Export);

    ViewBag.Title = Html.ToBreadcrumb("Configuration", MVC.Config.Config.Index(), "User Flags", MVC.Config.UserFlag.Index(null), "Export");
}
<div id="UserFlag_Export">
    @using (Html.BeginForm(MVC.API.UserFlag.Export(), FormMethod.Post, new { @data_saveaction = Url.Action(MVC.API.UserFlag.SaveExport()) }))
    {
        @Html.AntiForgeryToken()
        <div id="UserFlag_Export_Scope" class="form" style="width: 570px">
            <h2>Export Scope</h2>
            <table>
                <tr>
                    <th style="width: 150px">
                        User Flags:
                    </th>
                    <td>
                        @foreach (var userFlag in Model.UserFlags)
                        {
                            <div>
                                <label>
                                    <input type="checkbox" id="Options_UserFlagIds" name="Options.UserFlagIds" value="@userFlag.Id" @(((bool)Model.Options.UserFlagIds.Contains(userFlag.Id)) ? "checked " : null) />
                                    <i class="fa fa-@(userFlag.Icon) fa-lg d-@(userFlag.IconColour)"></i>
                                    <span>@userFlag.Name</span>
                                </label>
                            </div>
                        }
                    </td>
                </tr>
                <tr>
                    <th>@Html.LabelFor(m => m.Options.CurrentOnly)</th>
                    <td>
                        @Html.CheckBoxFor(m => m.Options.CurrentOnly)
                        <p>Uncheck to include all historical user flag assignments.</p>
                    </td>
                </tr>
                <tr>
                    <th>@Html.LabelFor(m => m.Options.Format)</th>
                    <td>
                        @Html.DropDownListFor(m => m.Options.Format, Enum.GetNames(typeof(Disco.Models.Exporting.ExportFormat)).Select(v => new SelectListItem() { Value = v, Text = v }))
                    </td>
                </tr>
            </table>
        </div>
        @Html.Partial(MVC.Shared.Views._ExportFields, Model.Fields.FieldGroups)
        <script>
            $(function () {
                const $exportFields = $('#Export_Fields');
                const $form = $exportFields.closest('form');
                let $exportingDialog = null;

                $.validator.unobtrusive.parse($form);
                $form.data("validator").settings.submitHandler = function () {
                    const exportFieldCount = $exportFields.find('input:checked').length;

                    if (exportFieldCount > 0) {

                        if ($exportingDialog == null) {
                            $exportingDialog = $('#Export_Exporting').dialog({
                                width: 400,
                                height: 164,
                                resizable: false,
                                modal: true,
                                autoOpen: false
                            });
                        }
                        $exportingDialog.dialog('open');

                        $form[0].submit();
                        return;
                    }

                    alert('Select at least one field to export.');
                };

                $('#UserFlag_Export_Button').on('click', function () {
                    $form.submit();
                });
                $('#UserFlag_Export_Save_Button').on('click', function () {
                    $form.attr('action', $form[0].dataset.saveaction);
                    $form.submit();
                });
            });
        </script>
    }
</div>
@if (Model.ExportId.HasValue)
{
    <div id="Export_Download_Dialog" class="dialog" title="Export User Flags">
        @if (Model.ExportResult.RecordCount == 0)
        {
            <h4>No records matched the filter criteria</h4>
        }
        else
        {
            <h4>@Model.ExportResult.RecordCount record@(Model.ExportResult.RecordCount != 1 ? "s" : null) were successfully exported.</h4>
            <a href="@Url.Action(MVC.API.UserFlag.ExportRetrieve(Model.ExportId.Value))" class="button"><i class="fa fa-download fa-lg"></i>Download User Flag Export</a>
        }
    </div>
    <script>
        $(function () {
            $('#Export_Download_Dialog')
                .dialog({
                    width: 400,
                    height: 164,
                    resizable: false,
                    modal: true,
                    autoOpen: true
                });
        });
    </script>
}
<div id="Export_Exporting" class="dialog" title="Exporting User Flags...">
    <h4><i class="fa fa-lg fa-cog fa-spin" title="Please Wait"></i>Exporting user flags...</h4>
</div>
<div class="actionBar">
    @if (Authorization.Has(Claims.Config.ManageSavedExports))
    {
        <button type="button" id="UserFlag_Export_Save_Button" class="button">Save Export</button>
    }
    else
    {
        <button type="button" class="button" disabled title="Requires Manage Saved Exports Permission">Save Export</button>
    }

    <button type="button" id="UserFlag_Export_Button" class="button">Export Now</button>
</div>
