public async Task <IActionResult> Index()
        {
            try
            {
                var pastDaysAppoints = new List <DayAppointsStatistics>();

                IQueryable <Appointment> _appointments = _dbContext.Set <Appointment>();

                _appointments = _dbContext.Appointments.Where(a => a.ServiceSupply.ShiftCenter.ClinicId == CurrentClinic.Id && a.Paymentstatus != PaymentStatus.NotPayed);

                var canceledAppoints = await _appointments.CountAsync(a => a.Status == AppointmentStatus.Canceled);

                _appointments = _appointments.Where(a => !a.IsDeleted);

                var allAppoints = await _appointments.CountAsync();

                var pendingAppoints = await _appointments.CountAsync(a => a.Status == AppointmentStatus.Pending);

                var doneAppoints = await _appointments.CountAsync(a => a.Status == AppointmentStatus.Done);

                var pendingPercent  = 0;
                var donePercent     = 0;
                var canceledPercent = 0;
                if (allAppoints > 0)
                {
                    pendingPercent  = (pendingAppoints * 100) / allAppoints;
                    donePercent     = (doneAppoints * 100) / allAppoints;
                    canceledPercent = (canceledAppoints * 100) / allAppoints;
                }

                for (var date = DateTime.Now.AddDays(-7); date <= DateTime.Now; date = date.AddDays(1))
                {
                    var _date = date.ToShortDateString();
                    if (date.Date == DateTime.Now.Date)
                    {
                        _date = Core.Resources.Global.Global.Today;
                    }
                    else
                    {
                        _date = Utils.ConvertDayOfWeek(date.DayOfWeek.ToString());
                    }

                    var count = await _appointments.CountAsync(a => a.Start_DateTime.Date == date.Date);

                    pastDaysAppoints.Add(new DayAppointsStatistics
                    {
                        Date   = _date,
                        Counts = count
                    });
                }
                var model = new CMIndexViewModel
                {
                    PoliClinicsCount                = await _dbContext.ShiftCenters.CountAsync(p => p.ClinicId == CurrentClinic.Id),
                    DoctorsCount                    = await _dbContext.ServiceSupplies.CountAsync(x => x.ShiftCenter.ClinicId == CurrentClinic.Id && x.IsAvailable),
                    TodayAppointsCount              = await _appointments.CountAsync(a => a.Start_DateTime.Date == DateTime.Now.Date),
                    AppointmentsCount               = allAppoints,
                    PendingAppointsCount            = pendingAppoints,
                    DoneAppointsCount               = doneAppoints,
                    CanceledAppointsCount           = canceledAppoints,
                    PendingAppointsPercent          = pendingPercent,
                    DoneAppointsPercent             = donePercent,
                    CanceledAppointsPercent         = canceledPercent,
                    DaysAppoints                    = pastDaysAppoints,
                    KioskAppointmentsCount          = await _appointments.CountAsync(a => a.ReservationChannel == ReservationChannel.Kiosk),
                    WebsiteAppointmentsCount        = await _appointments.CountAsync(a => a.ReservationChannel == ReservationChannel.Website),
                    AndroidAppAppointmentsCount     = await _appointments.CountAsync(a => a.ReservationChannel == ReservationChannel.MobileApplication),
                    ReserveOutsideAppointmentsCount = await _appointments.CountAsync(a => a.ReservationChannel == ReservationChannel.ClinicManagerSection),
                    SMSAppointmentsCount            = await _appointments.CountAsync(a => a.ReservationChannel == ReservationChannel.SMS),
                };

                return(View(model));
            }
            catch (Exception)
            {
                return(View());
            }
        }
        public async Task <OffersDataDTO> GetOffersDataAsync(Lang lang, string hostAddress, int?cityId = null)
        {
            IQueryable <Offer> query = _dbContext.Set <Offer>().Include(x => x.ShiftCenterService).ThenInclude(x => x.ShiftCenter);

            query = query.Where(x => x.Status == OfferStatus.APPROVED);

            query = query.Where(x => DateTime.Now >= x.StartAt && DateTime.Now <= x.ExpiredAt);

            query = query.Where(x => x.Type != OfferType.FREE_APPOINTMENTS || x.RemainedCount > 0);

            query = query.Where(x => x.ShiftCenterService != null && x.ShiftCenterService.Service != null);

            if (cityId != null)
            {
                query = query.Where(x => x.ShiftCenterService.ShiftCenter.CityId == cityId);
            }

            var offersGroups = await query.GroupBy(x => x.ShiftCenterService.Service.ServiceCategoryId).Select(c => new
            {
                CategoryId  = c.Key,
                OffersCount = c.Count(),
                VisitsCount = c.Sum(v => v.VisitsCount)
            }).ToListAsync();

            var trendingCategory = offersGroups.OrderByDescending(o => o.OffersCount).ThenByDescending(o => o.VisitsCount).FirstOrDefault();

            var categories = await _dbContext.ServiceCategories.Where(x => x.Services.Any(s => s.ShiftCenterServices.Any(h => (cityId == null || h.ShiftCenter.CityId == cityId) && h.Offers.Any(o => o.Status == OfferStatus.APPROVED && (DateTime.Now >= o.StartAt && DateTime.Now <= o.ExpiredAt) && (o.Type != OfferType.FREE_APPOINTMENTS || o.RemainedCount > 0))))).Select(c => new OfferCategoryDTO
            {
                Id         = c.Id,
                Title      = lang == Lang.KU ? c.Name_Ku : lang == Lang.AR ? c.Name_Ar : c.Name,
                Photo      = $"{hostAddress}{c.RealPhoto}",
                IsTrending = trendingCategory != null && trendingCategory.CategoryId == c.Id,
                Services   = c.Services.Select(s => new MySelectListItem
                {
                    Text  = lang == Lang.KU ? s.Name_Ku : lang == Lang.AR ? s.Name_Ar : s.Name,
                    Value = s.Id.ToString()
                }).ToList()
            }).ToListAsync();

            var offers = await query.Select(x => new OfferItemDTO
            {
                Id = x.Id,
                ShiftCenterServiceId = x.ShiftCenterServiceId ?? 0,
                ServiceSupplyId      = x.ServiceSupplyId,
                CurrencyType         = x.CurrencyType == CurrencyType.USD ? "USD" : "IQD",
                OldPrice             = x.OldPrice,
                CurrentPrice         = x.CurrentPrice,
                DiscountPercentange  = x.DiscountPercentange,
                ShowDiscountBanner   = x.ShowDiscountBanner,
                Summary = lang == Lang.KU ? x.Summary_Ku : lang == Lang.AR ? x.Summary_Ar : x.Summary,
                Title   = lang == Lang.KU ? x.Title_Ku : lang == Lang.AR ? x.Title_Ar : x.Title,
                Photos  = new List <string>
                {
                    lang == Lang.KU ? $"{hostAddress}{x.Photo_Ku}" : lang == Lang.AR ?$"{hostAddress}{x.Photo_Ar}" : $"{hostAddress}{x.Photo}",
                },
                Photo             = lang == Lang.KU ? $"{hostAddress}{x.Photo_Ku}" : lang == Lang.AR ? $"{hostAddress}{x.Photo_Ar}" : $"{hostAddress}{x.Photo}",
                AppointmentsCount = x.Appointments.Count,
                VisitsCount       = x.VisitsCount,
                CreatedAt         = x.CreatedAt
            }).ToListAsync();

            foreach (var item in offers)
            {
                var serviceSupply = await _dbContext.ServiceSupplies.FindAsync(item.ServiceSupplyId);

                if (serviceSupply == null)
                {
                    throw new AwroNoreException("Offer Doctor Not Found");
                }

                var x = serviceSupply;

                var shiftCenterService = x.ShiftCenter.PolyclinicHealthServices.FirstOrDefault(ss => ss.Id == item.ShiftCenterServiceId);

                item.ServiceSupply = new AN.Core.DTO.Doctors.DoctorListItemDTO
                {
                    Id                = x.Id,
                    FullName          = lang == Lang.KU ? x.Person.FullName_Ku : lang == Lang.AR ? x.Person.FullName_Ar : x.Person.FullName,
                    Avatar            = x.Person.RealAvatar,
                    ExpertiseCategory = x.DoctorExpertises.FirstOrDefault() != null ? lang == Lang.AR ? x.DoctorExpertises.FirstOrDefault().Expertise.ExpertiseCategory.Name_Ar : x.DoctorExpertises.FirstOrDefault().Expertise.ExpertiseCategory.Name_Ku : "",
                    Address           = lang == Lang.KU ? x.ShiftCenter.Address_Ku : x.ShiftCenter.Address_Ar,
                    HasEmptyTurn      = _doctorServiceManager.FindFirstEmptyTimePeriodFromNow(serviceSupply, shiftCenterService) != null,
                    AverageRating     = x.AverageRating != null ? (double)Math.Round((decimal)x.AverageRating, 2) : 5,
                    CenterServiceId   = item.ShiftCenterServiceId,
                    Service           = shiftCenterService == null ? "" : lang == Lang.KU ? shiftCenterService.Service.Name_Ku : lang == Lang.AR ? shiftCenterService.Service.Name_Ar : shiftCenterService.Service.Name,
                    ReservationType   = x.ReservationType,
                    CenterType        = x.ShiftCenter.Type
                };
            }

            var sortByData = new List <MySelectListItem>
            {
                new MySelectListItem {
                    Text = OfferSort.Booking.GetLocalizedDisplayName(), Value = "0"
                },
                new MySelectListItem {
                    Text = OfferSort.Visits.GetLocalizedDisplayName(), Value = "1"
                },
                new MySelectListItem {
                    Text = OfferSort.Price.GetLocalizedDisplayName(), Value = "2"
                }
            };

            var sortDirData = new List <MySelectListItem>
            {
                new MySelectListItem {
                    Text = SortDir.ASC.GetLocalizedDisplayName(), Value = "0"
                },
                new MySelectListItem {
                    Text = SortDir.DESC.GetLocalizedDisplayName(), Value = "1"
                },
            };

            var minPrice = offers.Any() ? (offers.OrderBy(o => o.CurrentPrice).Select(o => o.CurrentPrice).Min() ?? 0) : 0;
            var maxPrice = offers.Any() ? (offers.OrderBy(o => o.CurrentPrice).Select(o => o.CurrentPrice).Max() ?? 1000000) : 1000000;

            var result = new OffersDataDTO
            {
                SlideShow   = offers,
                Categories  = categories,
                TrendingNow = offers.OrderByDescending(o => o.CreatedAt).ThenByDescending(o => o.VisitsCount).Take(20).ToList(),
                All         = offers.OrderByDescending(o => o.AppointmentsCount).ToList(),
                FiltersData = new OffersFilterDataDTO
                {
                    SortBy   = sortByData,
                    SortDir  = sortDirData,
                    MinPrice = minPrice,
                    MaxPrice = maxPrice
                }
            };

            return(result);
        }