@model DiscountModel.AddVendorToDiscountModel
@inject AdminAreaSettings adminAreaSettings
@{
    Layout = "";
    //page title
    ViewBag.Title = Loc["admin.marketing.Discounts.AppliedToVendors.AddNew"];
}

<form id="VendorAddPopup" asp-area="@Constants.AreaAdmin" asp-controller="Discount" asp-action="VendorAddPopup" method="post"
      asp-route-DiscountId="@Context.Request.Query["DiscountId"]"
      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">
                <div class="x_title">
                    <div class="caption">
                        <i class="fa fa-cubes"></i>
                        @Loc["admin.marketing.Discounts.AppliedToVendors.AddNew"]
                    </div>
                </div>
                <div class="x_content form">
                    <div class="form-horizontal">
                        <div class="form-body">
                            <div class="form-group">
                                <admin-label asp-for="SearchVendorName" class="col-sm-3 control-label" />
                                <div class="col-md-9 col-sm-9">
                                    <admin-input asp-for="SearchVendorName" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchVendorEmail" class="col-sm-3 control-label" />
                                <div class="col-md-9 col-sm-9">
                                    <admin-input asp-for="SearchVendorEmail" />
                                </div>
                            </div>
                            <div class="form-actions">
                                <div class="row">
                                    <div class="offset-md-1 col-md-11 col-sm-11 btn-group btn-group-devided">
                                        <button class="btn btn-success filter-submit" id="search-vendors">
                                            <i class="fa fa-search"></i> @Loc["Admin.Common.Search"]
                                        </button>
                                        <button class="btn btn-success" type="submit" name="save"><i class="fa fa-check"></i> @Loc["Admin.Common.Save"] </button>
                                    </div>
                                </div>
                            </div>
                            <div id="vendors-grid"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
            var mfp = $.magnificPopup.instance;
            $("#VendorAddPopup").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();
                            $('#btnRefreshVendors').click();
                        } else {
                            $.magnificPopup.open({
                                items: {
                                    src: data,
                                    type: 'inline'
                                },
                                callbacks: {
                                    open: function () {
                                        $('.mfp-wrap').removeAttr('tabindex');
                                    }
                                }
                            });
                        }
                    }
                });
            });
            $("#vendors-grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("VendorAddPopupList", "Discount", 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='SelectedVendorIds' value='#=Id#' class='checkboxGroups' />",
                    attributes: { style: "text-align:center" },
                    width: 50
                }, {
                    field: "Name",
                    title: "@Loc["Admin.Catalog.Vendors.Fields.Name"]"
                }
                , {
                    field: "Email",
                    title: "@Loc["Admin.Catalog.Vendors.Fields.Email"]"
                }
                ]
            });
            $('#search-vendors').click(function() {
                var grid = $('#vendors-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.SearchVendorName)").keydown(function(event) {
                if (event.keyCode == 13) {
                    $("#search-vendors").click();
                    return false;
                }
            });

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

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

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

        function additionalData() {
            var data = {
                SearchVendorName: $('#@Html.IdFor(model => model.SearchVendorName)').val(),
                SearchVendorEmail: $('#@Html.IdFor(model => model.SearchVendorEmail)').val()
            };
            addAntiForgeryToken(data);
            return data;
        }

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

    </script>

</form>