public async Task <IActionResult> Add(CouponViewModel viewModel)
        {
            #region SelectList
            var users = await _db.UserRepository.GetAsync();

            ViewData["UsersId"] = new SelectList(users, "Id", "UserName");

            var products = await _db.ProductRepository.GetAsync();

            ViewData["ProductsId"] = new SelectList(products, "Id", "Name");

            var categories = await _db.CategoryRepository.GetAsync();

            ViewData["CategoriesId"] = new SelectList(categories, "Id", "Name");
            #endregion
            if (ModelState.IsValid)
            {
                viewModel.Title = viewModel.Title.Trim();
                var couponModel = await _db.CouponRepository.GetAsync(c => c.Title == viewModel.Title, string.Empty);

                if (couponModel == null)
                {
                    if (viewModel.EndDateLimit < viewModel.StartDateLimit)
                    {
                        ModelState.AddModelError("EndDateLimit", "تاریخ پایان باید بعد از تاریخ شروع باشد");
                        return(View(viewModel));
                    }

                    if (viewModel.HasUserLimit && viewModel.Users.Count == 0)
                    {
                        ModelState.AddModelError("Users", "کاربران را وارد کنید");
                        return(View(viewModel));
                    }

                    if (viewModel.HasProductOrCategoryLimit)
                    {
                        if (viewModel.ProductOrCategoryLimit == "product" && viewModel.Products.Count == 0)
                        {
                            ModelState.AddModelError("Products", "محصولات را وارد کنید");
                            return(View(viewModel));
                        }

                        if (viewModel.ProductOrCategoryLimit == "category" && viewModel.Categories.Count == 0)
                        {
                            ModelState.AddModelError("Categories", "دسته بندی ها را وارد کنید");
                            return(View(viewModel));
                        }
                    }

                    var coupon = new Coupon();
                    #region Coupon
                    coupon.Title         = viewModel.Title.Trim();
                    coupon.Type          = viewModel.Type == "percent" ? true : false;
                    coupon.Value         = viewModel.Type == "percent" ? viewModel.PercentDiscount : viewModel.ValueDiscount;
                    coupon.HasCountLimit = viewModel.HasCountLimit;
                    if (viewModel.HasCountLimit)
                    {
                        coupon.CountLimit = viewModel.CountLimit;
                    }
                    coupon.HasDateLimit = viewModel.HasDateLimit;
                    if (viewModel.HasDateLimit)
                    {
                        coupon.StartDateLimit = viewModel.StartDateLimit;
                        coupon.EndDateLimit   = viewModel.EndDateLimit;
                    }
                    coupon.HasUserLimit = viewModel.HasUserLimit;
                    if (viewModel.HasProductOrCategoryLimit)
                    {
                        if (viewModel.ProductOrCategoryLimit == "product")
                        {
                            coupon.HasProductLimit  = true;
                            coupon.HasCategoryLimit = false;
                        }
                        else
                        {
                            coupon.HasCategoryLimit = true;
                            coupon.HasProductLimit  = false;
                        }
                    }
                    coupon.Code = await _codeGenerator.GenerateCouponCodeAsync();

                    #endregion

                    await _db.CouponRepository.AddAsync(coupon);

                    await _db.SaveAsync();

                    if (viewModel.HasUserLimit)
                    {
                        foreach (var userId in viewModel.Users)
                        {
                            var couponUser = new CouponUser()
                            {
                                CouponId = coupon.Id,
                                UserId   = userId
                            };
                            await _db.CouponUserRepository.AddAsync(couponUser);
                        }
                        await _db.SaveAsync();
                    }
                    if (viewModel.HasProductOrCategoryLimit)
                    {
                        if (viewModel.ProductOrCategoryLimit == "product")
                        {
                            foreach (var productId in viewModel.Products)
                            {
                                var couponProduct = new CouponProduct()
                                {
                                    CouponId  = coupon.Id,
                                    ProductId = productId
                                };
                                await _db.CouponProductRepository.AddAsync(couponProduct);
                            }
                            await _db.SaveAsync();
                        }
                        else
                        {
                            foreach (var categoryId in viewModel.Categories)
                            {
                                var couponCategory = new CouponCategory()
                                {
                                    CouponId   = coupon.Id,
                                    CategoryId = categoryId
                                };
                                await _db.CouponCategoryRepository.AddAsync(couponCategory);
                            }
                            await _db.SaveAsync();
                        }
                    }

                    return(Redirect("/Admin/Coupon"));
                }
                else
                {
                    ModelState.AddModelError("Title", "کد تخفیف با این نام قبلا ثبت شده است");
                    return(View(viewModel));
                }
            }
            else
            {
                return(View(viewModel));
            }
        }