@model NewsletterSubscriptionSearchModel

@using Nop.Services.Stores
@using Nop.Services.Localization

@inject IStoreService storeService

@{
    //page title
    ViewBag.PageTitle = T("Admin.Promotions.newsLetterSubscriptions").Text;
    //active menu item (system name)
    NopHtml.SetActiveMenuItemSystemName("Newsletter subscriptions");
}

@{
    const string hideSearchBlockAttributeName = "NewsletterSubscriptionPage.HideSearchBlock";
    var hideSearchBlock = await genericAttributeService.GetAttributeAsync<bool>(await workContext.GetCurrentCustomerAsync(), hideSearchBlockAttributeName);
}

<form asp-controller="NewsLetterSubscription" asp-action="List" method="post">
    <div class="content-header clearfix">
        <h1 class="float-left">
            @T("Admin.Promotions.NewsLetterSubscriptions")
        </h1>
        <div class="float-right">
            <button asp-action="ExportCSV"type="submit" name="exportcsv" class="btn btn-success">
                <i class="fas fa-download"></i>
                @T("Admin.Common.ExportToCsv")
            </button>
            <button type="button" name="importcsv" class="btn bg-olive" data-toggle="modal" data-target="#importcsv-window">
                <i class="fas fa-upload"></i>
                @T("Admin.Common.ImportFromCsv")
            </button>
            @await Component.InvokeAsync(typeof(AdminWidgetViewComponent), new { widgetZone = AdminWidgetZones.NewsLetterSubscriptionListButtons, additionalData = Model })
        </div>
    </div>

    <section class="content">
    <div class="container-fluid">
        <div class="form-horizontal">
            <div class="cards-group">
                <div class="card card-default card-search">
                    <div class="card-body">
                        <div class="row search-row @(!hideSearchBlock ? "opened" : "")" data-hideAttribute="@hideSearchBlockAttributeName">
                            <div class="search-text">@T("Admin.Common.Search")</div>
                            <div class="icon-search"><i class="fas fa-search" aria-hidden="true"></i></div>
                            <div class="icon-collapse"><i class="far fa-angle-@(!hideSearchBlock ? "up" : "down")" aria-hidden="true"></i></div>
                        </div>

                        <div class="search-body @(hideSearchBlock ? "closed" : "")">
                            <div class="row">
                                <div class="col-md-5">
                                    <div class="form-group row">
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchEmail" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-editor asp-for="SearchEmail" />
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <div class="col-md-4">
                                            <nop-label asp-for="StartDate" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-editor asp-for="StartDate" />
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <div class="col-md-4">
                                            <nop-label asp-for="EndDate" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-editor asp-for="EndDate" />
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-7">
                                    <div class="form-group row">
                                        <div class="col-md-4">
                                            <nop-label asp-for="ActiveId" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-select asp-for="ActiveId" asp-items="Model.ActiveList" />
                                        </div>
                                    </div>
                                    <div class="form-group row" @(Model.HideStoresList ? Html.Raw("style=\"display:none\"") : null)>
                                        <div class="col-md-4">
                                            <nop-label asp-for="StoreId" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-select asp-for="StoreId" asp-items="Model.AvailableStores" />
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <div class="col-md-4">
                                            <nop-label asp-for="CustomerRoleId" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-select asp-for="CustomerRoleId" asp-items="Model.AvailableCustomerRoles" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="text-center col-12">
                                    <button type="button" id="search-subscriptions" class="btn btn-primary btn-search">
                                        <i class="fas fa-search"></i>
                                        @T("Admin.Common.Search")
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card card-default">
                    <div class="card-body">
                        <nop-doc-reference asp-string-resource="@T("Admin.Documentation.Reference.EmailCampaigns", Docs.EmailCampaigns + Utm.OnAdmin)" />

                        @await Html.PartialAsync("Table", new DataTablesModel
                        {
                            Name = "newsletter-subscriptions-grid",
                            UrlRead = new DataUrl("SubscriptionList", "NewsLetterSubscription", null),
                            UrlDelete = new DataUrl("SubscriptionDelete", "NewsLetterSubscription", null),
                            UrlUpdate = new DataUrl("SubscriptionUpdate", "NewsLetterSubscription", null),
                            SearchButtonId = "search-subscriptions",
                            Length = Model.PageSize,
                            LengthMenu = Model.AvailablePageSizes,
                            Filters = new List<FilterParameter>
                            {
                                new FilterParameter(nameof(Model.StartDate), typeof(DateTime?)),
                                new FilterParameter(nameof(Model.EndDate), typeof(DateTime?)),
                                new FilterParameter(nameof(Model.SearchEmail)),
                                new FilterParameter(nameof(Model.ActiveId)),
                                new FilterParameter(nameof(Model.StoreId)),
                                new FilterParameter(nameof(Model.CustomerRoleId))
                            },
                            ColumnCollection = new List<ColumnProperty>
                                {
                                    new ColumnProperty(nameof(NewsletterSubscriptionModel.Email))
                                    {
                                        Title = T("Admin.Promotions.NewsLetterSubscriptions.Fields.Email").Text,
                                        Width = "300",
                                        Editable = true,
                                        EditType = EditType.String
                                    },
                                    new ColumnProperty(nameof(NewsletterSubscriptionModel.Active))
                                    {
                                        Title = T("Admin.Promotions.NewsLetterSubscriptions.Fields.Active").Text,
                                        Width = "100",
                                        ClassName = NopColumnClassDefaults.CenterAll,
                                        Render = new RenderBoolean(),
                                        Editable = true,
                                        EditType = EditType.Checkbox
                                    },
                                    new ColumnProperty(nameof(NewsletterSubscriptionModel.StoreName))
                                    {
                                        Title = T("Admin.Promotions.NewsLetterSubscriptions.Fields.Store").Text,
                                        Width = "150",
                                        Visible = (await storeService.GetAllStoresAsync()).Count > 1
                                    },
                                    new ColumnProperty(nameof(NewsletterSubscriptionModel.LanguageName))
                                    {
                                        Title = T("Admin.Promotions.NewsLetterSubscriptions.Fields.Language").Text,
                                        Width = "150"
                                    },
                                    new ColumnProperty(nameof(NewsletterSubscriptionModel.CreatedOn))
                                    {
                                        Title = T("Admin.Promotions.NewsLetterSubscriptions.Fields.CreatedOn").Text,
                                        Width = "200"
                                    },
                                    new ColumnProperty(nameof(NewsletterSubscriptionModel.Id))
                                    {
                                        Title = T("Admin.Common.Edit").Text,
                                        Width = "200",
                                        ClassName =  NopColumnClassDefaults.Button,
                                        Render = new RenderButtonsInlineEdit()
                                    },
                                    new ColumnProperty(nameof(NewsletterSubscriptionModel.Id))
                                    {
                                        Title = T("Admin.Common.Delete").Text,
                                        Width = "100",
                                        Render = new RenderButtonRemove(T("Admin.Common.Delete").Text),
                                        ClassName = NopColumnClassDefaults.Button
                                    }
                                }
                        })
                    </div>
                </div>
            </div>
        </div>
    </div>
    </section>
</form>

@*import emails form*@
<div id="importcsv-window" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="importcsv-window-title">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="importcsv-window-title">@T("Admin.Common.ImportFromCsv")</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>                
            </div>
            <form asp-controller="NewsLetterSubscription" asp-action="ImportCsv" method="post" enctype="multipart/form-data">
                <div class="form-horizontal">
                    <div class="modal-body">
                        <div class="form-group row">
                            <div class="col-md-2">
                                <div class="label-wrapper">
                                    <label class="col-form-label">
                                        @T("Admin.Common.CsvFile")
                                    </label>
                                </div>
                            </div>
                            <div class="col-md-10">
                                <input type="file" id="importcsvfile" name="importcsvfile" class="form-control" />
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">
                            @T("Admin.Common.ImportFromCsv")
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>