//*************************************************************************************************************************************
        // you had to pass in AUConsignorList model and populate it with the additionaldata function
        // in the cshtml to get the entered search data. ToTest: do you need DataSourceRequest?? Note, when just doing a total list,
        // you didn;t need to pass in anything
        public ActionResult ListConsignors(DataSourceRequest command, AUConsignorList model)
        {
            //********************************Original*******See Consignments for M-M *********************************************************
            //var consignors = _consignorRepo.Table.ToList();

            //var gridModel = new DataSourceResult
            //{
            //    Data = consignors.Select(x => new
            //    {
            //        AUConsignorID = x.AUConsignorID,
            //        FirstName = x.FirstName,
            //        MiddleName = x.MiddleName,
            //        LastName = x.LastName,
            //        CreateDate = x.CreateDate,
            //        BuyerFee = x.BuyerFee,
            //        SellerFee = x.SellerFee,
            //    }),
            //    Total = consignors.Count()
            //};
            //*****************************Original*******************************************************************

            int consignorid = 0;
            string email = "";
            //string firstname = "Test";
            string lastname = "";

            string fn = model.searchFirstName;

            var consignors = _consignorService.GetAllConsignors(
                consignorId: consignorid,
                WEmail: email,
                firstName: model.searchFirstName,
                lastName: lastname,
                pageIndex: 0,
                pageSize: 500);

            //pageIndex: command.Page - 1,
            //pageSize: command.PageSize);


            var gridModel = new DataSourceResult
            {
                Data = consignors.Select(x => new
                {
                    Id = x.Id,
                    AUConsignorID = x.AUConsignorID,
                    CustomerID = x.CustomerID,
                    FirstName = x.FirstName,
                    MiddleName = x.MiddleName,
                    LastName = x.LastName
                }),
                Total = consignors.TotalCount
            };


            return Json(gridModel);
        }
        public ActionResult ConsignmentConsignorAddPopup(int consignmentId)
        {
            //TODO: permissions to associate consignor to consignment
            //if (!_permissionService.Authorize(StandardPermissionProvider.ManageProducts))
            //    return View("~/Administration/Views/Security/AccessDenied.cshtml");

            var model = new AUConsignorList();  //using same search model as ManageConsignors

            //a vendor should not have access assign consignors to consignments
            //model.IsLoggedInAsVendor = _workContext.CurrentVendor != null;
            model.consignmentId = consignmentId;

            return View("~/Views/AUConsignor/ConsignmentConsignorsPopup.cshtml", model);
        }
        public ActionResult ConsignmentConsignorAddPopup(string selectedIds, int consignmentId, string btnIdToRefresh, string frmIdToRefresh, AUConsignorList model)
        {
            //TODO: Implement permissions
            //if (!_permissionService.Authorize(StandardPermissionProvider.ManageProducts))
            //    return AccessDeniedView();


            var consignment = _consignorService.GetConsignmentById(consignmentId);
            if (consignment == null)
            {
                ErrorNotification("A major error occurred -consignment id from ConsignmentConsignorAddPopup not found. Please contact System Support");
                return RedirectToAction("ManageConsignors");
            }

            //TODO: Finish this - Need to get the lot info to ensure not already sold and unpublished
            var consignors = new List<AUConsignorRecord>();
            if (selectedIds != null)
            {
                consignors.AddRange(_consignorService.GetConsignorsByConsignorIds(selectedIds));
            }

            foreach (var c in consignors)
            {
                var cc = c.AUConsignmentRecords.FirstOrDefault(x => x.AUConsignmentID == consignment.AUConsignmentID);
                if (cc == null) //Consignment exists but the junction AUConsignorConsignment record doesn't
                {
                    var consignorconsignment = new AUConsignorConsignmentRecord
                    {
                        AUConsignmentID = consignment.AUConsignmentID,
                        AUConsignorID = c.AUConsignorID,
                        Term = "RemoveField"
                    };
                    _consignorService.InsertConsignorConsignment(consignorconsignment);
                }
                //else                                            //Sale exists and the junction AUSaleLot record does as well, so update it
                //{
                //    sl.AUSaleID = sale.AUSaleID;
                //    sl.AULotID = lot.AULotID;
                //    sl.LotNbr = LotNbr;
                //    sl.SaleIsActive = sale.SaleIsActive;
                //    sl.DisplayOrder = DisplayOrder;
                //    _consignorService.UpdateSaleLot(sl);
                //}
            }


            ////a vendor should have access only to his products
            //if (_workContext.CurrentVendor != null)
            //{
            //    products = products.Where(p => p.VendorId == _workContext.CurrentVendor.Id).ToList();
            //}


            //_consignorService.PublishSelectedProducts(products);


            SuccessNotification("Lots were associated to sale!!");  //seems to be nop admin convention

            ViewBag.RefreshPage = true;
            ViewBag.btnId = btnIdToRefresh;
            ViewBag.formId = frmIdToRefresh;
            return View("~/Views/AUConsignor/ConsignmentConsignorsPopup.cshtml", model);
            //return RedirectToAction("ListLots"); //TODO FIX THIS
        }
        public ActionResult ListConsignmentConsignors(DataSourceRequest command, AUConsignorList model, int excludeConsignmentId, bool excludeConsignment)
        {
            //TODO: permission to search consignors
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageProducts))
                return View("~/Administration/Views/Security/AccessDenied.cshtml");


            //0 - all (according to "ShowHidden" parameter)
            //1 - published only
            //2 - unpublished only
            //bool? overridePublished = false; //show published and unpublished lots associated to the sale the sale

            //should not happen
            //if (excludeConsignmentId != 0 && excludeConsignment == true)
            //{
            //    model.SearchSaleId = excludeSaleId;
            //}


            //TODO: only show lots in the store
            var consignors = _consignorService.GetAllConsignors(
                firstName: model.searchFirstName,
                excludeConsignment: excludeConsignment,
                excludeConsignmentId: excludeConsignmentId,
                pageIndex: command.Page - 1,
                pageSize: command.PageSize
            );

            //overridePublished: overridePublished,



            var gridModel = new DataSourceResult();
            gridModel.Data = consignors.Select(x => new
            {
                //var lotsModel = x.ToModel();  //ToModel uses EF mapper and is implemented as an Extension. Map definition is in Infrastructure\AutomapperStartupTask
                AUConsignorID = x.AUConsignorID,
                CustomerID = x.CustomerID,
                FirstName = x.FirstName,
                MiddleName = x.MiddleName,
                LastName = x.LastName

            });
            gridModel.Total = consignors.TotalCount;

            return Json(gridModel);
        }