@model ProductModel.AddBundleProductModel
@inject AdminAreaSettings adminAreaSettings
@{
    Layout = "";

    //page title
    ViewBag.Title = Loc["Admin.Catalog.Products.BundleProducts.AddNew"];
}
<form id="BundleProductAddPopup" asp-area="@Constants.AreaAdmin" asp-controller="Product" asp-action="BundleProductAddPopup"
      asp-route-ProductId="@Context.Request.Query["ProductId"]"
      asp-route-btnId="@Context.Request.Query["btnId"]"
      asp-route-formId="@Context.Request.Query["formId"]">

    <div class="row">
        <div class="col-md-12">
            <div class="x_panel light form-fit popup-window">
                <div class="x_title">
                    <div class="caption">
                        <i class="fa fa-cubes"></i>
                        @Loc["Admin.Catalog.Products.BundleProducts.AddNew"]
                    </div>
                </div>
                <div class="x_content form">
                    <div class="form-horizontal">
                        <div class="form-body">
                            <div class="main-header col-12 px-0">
                                <div class="row align-items-end">
                                    <div class="col-md col-ms-12 col-12">
                                        <div class="form-group mb-0">
                                            <admin-label asp-for="SearchProductName" class="control-label" />
                                            <admin-input asp-for="SearchProductName" />
                                        </div>
                                    </div>
                                    <div class="col-md-auto col-sm-12 col-12">
                                        <div class="form-actions">
                                            <div class="btn-group btn-group-devided">
                                                <button class="btn btn-success filter-submit" id="search-products">
                                                    <i class="fa fa-search"></i>&nbsp; @Loc["Admin.Common.Search"]
                                                </button>
                                                <button class="btn btn-info" type="button" data-toggle="collapse" data-target="#filterCollapse" aria-expanded="false" aria-controls="filterCollapse">
                                                    <i class="fa fa-filter"></i>&nbsp; @Loc["Admin.Common.Filters"]
                                                </button>
                                            </div>
                                            <button class="btn btn-success" type="submit" name="save"><i class="fa fa-check"></i> &nbsp; @Loc["Admin.Common.Save"] </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="collapse" id="filterCollapse">
                                <div class="drop-filters-container">
                                    <div class="form-group">
                                        <admin-label asp-for="SearchCategoryId" class="col-sm-3 control-label" />
                                        <div class="col-md-9 col-sm-9">
                                            <admin-input asp-for="SearchCategoryId" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <admin-label asp-for="SearchBrandId" class="col-sm-3 control-label" />
                                        <div class="col-md-9  col-sm-9">
                                            <admin-input asp-for="SearchBrandId" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <admin-label asp-for="SearchCollectionId" class="col-sm-3 control-label" />
                                        <div class="col-md-9  col-sm-9">
                                            <admin-input asp-for="SearchCollectionId" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <admin-label asp-for="SearchStoreId" class="col-sm-3 control-label" />
                                        <div class="col-md-9 col-sm-9">
                                            <admin-select asp-for="SearchStoreId" asp-items="Model.AvailableStores" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <admin-label asp-for="SearchVendorId" class="col-sm-3 control-label" />
                                        <div class="col-md-9 col-sm-9">
                                            <admin-select asp-for="SearchVendorId" asp-items="Model.AvailableVendors" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div id="products-grid"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
            var mfp = $.magnificPopup.instance;
            $("#BundleProductAddPopup").submit(function (e) {
                e.preventDefault();
                var form = $(this);
                var url = form.attr('action');
                $.ajax({
                    type: "POST",
                    url: url,
                    data: form.serialize(),
                    success: function (data) {
                        if (data == "") {
                            mfp.close();
                            $('#btnRefreshBundleProducts').click();
                        } else {
                            $.magnificPopup.open({
                                items: {
                                    src: data,
                                    type: 'inline'
                                },
                                callbacks: {
                                    open: function () {
                                        $('.mfp-wrap').removeAttr('tabindex');
                                    }
                                }
                            });
                        }
                    }
                });
            });

            $("#products-grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("BundleProductAddPopupList", "Product", new { area = Constants.AreaAdmin }))",
                            type: "POST",
                            dataType: "json",
                            data: additionalData
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors"
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    pageSize: @(adminAreaSettings.DefaultGridPageSize),
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                pageable: {
                    refresh: true,
                    pageSizes: [@(adminAreaSettings.GridPageSizes)]
                },
                editable: {
                    confirmation: false,
                    mode: "inline"
                },
                scrollable: false,
                columns: [{
                    field: "Id",
                    headerTemplate: "<input id='mastercheckbox' type='checkbox'/>",
                    headerAttributes: { style: "text-align:center" },
                    template: "<input type='checkbox' name='SelectedProductIds' value='#=Id#' class='checkboxGroups' />",
                    attributes: { style: "text-align:center" },
                    width: 50
                }, {
                    field: "Name",
                    title: "@Loc["Admin.Catalog.Products.Fields.Name"]"
                }, {
                    field: "Published",
                    title: "@Loc["Admin.Catalog.Products.Fields.Published"]",
                    width: 100,
                    headerAttributes: { style: "text-align:center" },
                    attributes: { style: "text-align:center" },
                    template: '# if(Published) {# <i class="fa fa-check" aria-hidden="true" style="color:green"></i> #} else {# <i class="fa fa-times" aria-hidden="true" style="color:red"></i> #} #'
                }]
            });


            $('#search-products').click(function () {
                var grid = $('#products-grid').data('kendoGrid');
                grid.dataSource.page(1); //new search. Set page size to 1
                //grid.dataSource.read(); we already loaded the grid above using "page" function
                //clear selected checkboxes
                $('.checkboxGroups').prop('checked', false).change();
                return false;
            });

            $("#@Html.IdFor(model => model.SearchProductName)").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#search-products").click();
                    return false;
                }
            });

            //wire up checkboxes
            $('#mastercheckbox').click(function() {
                $('.checkboxGroups').prop('checked', $(this).is(':checked')).change();
            });

            $('#products-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function(e) {
                updateMasterCheckbox();
            });

        function additionalData() {
            var data = {
                SearchProductName: $('#@Html.IdFor(model => model.SearchProductName)').val(),
                SearchCategoryId: $('#SearchCategoryId').val(),
                SearchBrandId: $('#SearchBrandId').val(),
                SearchCollectionId: $('#SearchCollectionId').val(),
                SearchStoreId: $('#SearchStoreId').val(),
                SearchVendorId: $('#SearchVendorId').val()
            };
            addAntiForgeryToken(data);
            return data;
        }

        function updateMasterCheckbox() {
            var numChkBoxes = $('#products-grid input[type=checkbox][id!=mastercheckbox]').length;
            var numChkBoxesChecked = $('#products-grid input[type=checkbox][id!=mastercheckbox]:checked').length;
            $('#mastercheckbox').prop('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
        }
    </script>
</form>
