@model ShipmentSearchModel

@using Nop.Web.Areas.Admin.Models.Orders

@inject Nop.Services.Common.IGenericAttributeService genericAttributeService
@inject Nop.Core.IWorkContext workContext

@{
    Layout = "_AdminPopupLayout";
    ViewBag.PageTitle = T("Plugins.Shipping.EasyPost.Batch.Shipments.Add").Text;
    const string hideSearchBlockAttributeName = "ShipmentsPage.HideSearchBlock";
    var hideSearchBlock = await genericAttributeService.GetAttributeAsync<bool>(await workContext.GetCurrentCustomerAsync(), hideSearchBlockAttributeName);
}

<form asp-controller="EasyPost" asp-action="BatchShipmentAdd"
      asp-route-batchId="@Context.Request.Query["batchId"]"
      asp-route-btnId="@Context.Request.Query["btnId"]"
      asp-route-formId="@Context.Request.Query["formId"]">
    <div class="content-header clearfix">
        <h1 class="float-left">
            @T("Plugins.Shipping.EasyPost.Batch.Shipments.Add")
        </h1>
        <div class="float-right">
            <button type="button" id="select-shipments" class="btn btn-primary">
                <i class="far fa-save"></i>
                @T("Plugins.Shipping.EasyPost.Batch.Shipments.Add.Save")
            </button>
        </div>
    </div>
    <section class="content">
        <div class="container-fluid">
            <div class="form-horizontal">
                <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="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 class="form-group row">
                                                <div class="col-md-4">
                                                    <nop-label asp-for="TrackingNumber" />
                                                </div>
                                                <div class="col-md-8">
                                                    <nop-editor asp-for="TrackingNumber" />
                                                </div>
                                            </div>
                                            <div class="form-group row">
                                                <div class="col-md-4">
                                                    <nop-label asp-for="WarehouseId" />
                                                </div>
                                                <div class="col-md-8">
                                                    <nop-select asp-for="WarehouseId" asp-items="Model.AvailableWarehouses" />
                                                </div>
                                            </div>
                                            <div class="form-group row">
                                                <div class="col-md-4">
                                                    <nop-label asp-for="LoadNotDelivered" />
                                                </div>
                                                <div class="col-md-8">
                                                    <nop-editor asp-for="LoadNotDelivered" />
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-md-7">
                                            <div class="form-group row">
                                                <div class="col-md-4">
                                                    <nop-label asp-for="CountryId" />
                                                </div>
                                                <div class="col-md-8">
                                                    <nop-select asp-for="CountryId" asp-items="Model.AvailableCountries" />
                                                </div>
                                            </div>
                                            <div class="form-group row">
                                                <div class="col-md-4">
                                                    <nop-label asp-for="StateProvinceId" />
                                                </div>
                                                <div class="col-md-8">
                                                    <nop-select asp-for="StateProvinceId" asp-items="Model.AvailableStates" />
                                                </div>
                                            </div>
                                            <div class="form-group row">
                                                <div class="col-md-4">
                                                    <nop-label asp-for="County" />
                                                </div>
                                                <div class="col-md-8">
                                                    <nop-editor asp-for="County" />
                                                </div>
                                            </div>
                                            <div class="form-group row">
                                                <div class="col-md-4">
                                                    <nop-label asp-for="City" />
                                                </div>
                                                <div class="col-md-8">
                                                    <nop-editor asp-for="City" />
                                                </div>
                                            </div>
                                            <div class="form-group row">
                                                <div class="col-md-4">
                                                    <nop-label asp-for="LoadNotShipped" />
                                                </div>
                                                <div class="col-md-8">
                                                    <nop-editor asp-for="LoadNotShipped" />
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="text-center col-12">
                                            <button type="button" id="search-shipments" 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">
                                @await Html.PartialAsync("Table", new DataTablesModel
                                {
                                    Name = "shipments-grid",
                                    UrlRead = new DataUrl("ShipmentListSelect", "Order", null),
                                    SearchButtonId = "search-shipments",
                                    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.TrackingNumber)),
                                        new FilterParameter(nameof(Model.CountryId)),
                                        new FilterParameter(nameof(Model.StateProvinceId)),
                                        new FilterParameter(nameof(Model.County)),
                                        new FilterParameter(nameof(Model.City)),
                                        new FilterParameter(nameof(Model.WarehouseId)),
                                        new FilterParameter(nameof(Model.LoadNotShipped), typeof(bool)),
                                        new FilterParameter(nameof(Model.LoadNotDelivered),typeof(bool))
                                    },
                                    ColumnCollection = new List<ColumnProperty>
                                    {
                                        new ColumnProperty(nameof(ShipmentModel.Id))
                                        {
                                            IsMasterCheckBox = true,
                                            Render = new RenderCheckBox("checkbox_shipments"),
                                            Searchable = false,
                                            ClassName =  NopColumnClassDefaults.CenterAll,
                                            Width = "50"
                                        },
                                        new ColumnProperty(nameof(ShipmentModel.Id))
                                        {
                                            Title = T("Admin.Orders.Shipments.ID").Text,
                                            Width = "80"
                                        },
                                        new ColumnProperty(nameof(ShipmentModel.CustomOrderNumber))
                                        {
                                            Title = T("Admin.Orders.Shipments.CustomOrderNumber").Text,
                                            Width = "100"
                                        },
                                        new ColumnProperty(nameof(ShipmentModel.TotalWeight))
                                        {
                                            Title = T("Admin.Orders.Shipments.TotalWeight").Text,
                                            Width = "100"
                                        },
                                    }
                                })
                            </div>
                        </div>
                        <script>
                            $(document).ready(function () {
                                $('#select-shipments').click(function (e) {
                                    e.preventDefault();
                                    var postData = {
                                        batchId: '@Context.Request.Query["batchId"]',
                                        selectedIds: selectedIds
                                    };
                                    addAntiForgeryToken(postData);
                                    $.ajax({
                                        cache: false,
                                        type: "POST",
                                        url: "@(Url.Action("SelectShipments", "EasyPost"))",
                                        data: postData,
                                        traditional: true,
                                        error: function (jqXHR, textStatus, errorThrown) {
                                            $('#select-shipments-failed-info').text(errorThrown);
                                            $("#select-shipments-failed").click();
                                        },
                                        complete: function (jqXHR, textStatus) {
                                            try {
                                                window.opener.document.getElementById('@(Context.Request.Query["btnId"])').click();
                                            }catch (e){}
                                            window.close();
                                        }
                                    });
                                    return false;
                                });
                            })
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </section>
</form>
<nop-alert asp-alert-id="select-shipments-failed" />