/// <summary>
        /// Edits the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns>ActionResult.</returns>
        public ActionResult Edit(int id)
        {

            if (Session["CurrentSchedule"] == null)
            {
                return RedirectToAction("Index", "Schedule");

            }
            var schedule = (Schedule)Session["CurrentSchedule"];
              int days = DateTime.DaysInMonth(schedule.Year, schedule.Month);
            var model = new TimeSlotViewModel();
            model.CurrentSchedule= schedule;
            model.Offices = new OfficeAppService().GetOfficesByEmployeeId(schedule.EmployeeId);
            model.DaysInMonth = days;

            




            if (id != -1)
            {
                var svc = new TimeSlotAppService();
                var o = svc.GetTimeSlot(id);
                model.TimeSlotId = o.TimeSlotId;
                model.ScheduleId = o.ScheduleId;
                model.StartTime = o.StartTime;
                model.EndTime = o.EndTime;
                model.Day = o.Day;
                model.OfficeId = o.OfficeId;
                model.StartTimeText = o.StartTime.ToShortTimeString();
                model.EndTimeText = o.EndTime.ToShortTimeString();
              




            }
            else
            {
                model.Action = "-1";
                model.TimeSlotId = -1;
                model.ScheduleId = schedule.ScheduleId;
                model.StartTime = DateTime.Now;
                model.EndTime = DateTime.Now;
                model.Day = 1;
                model.OfficeId = -1;
                model.StartTimeText = DateTime.Now.ToShortTimeString();
                model.EndTimeText = DateTime.Now.ToShortTimeString();
            }



            return View(model);
        }
        public ActionResult Index(SearchViewModel model)
        {
            var ci = new CultureInfo("es-CO");

            model.Specialties = new SpecialtyAppService().GetAllSpecialty();
            model.Medics = new EmployeeAppService().GetAllMedics();

            List<TimeSlot> lst = new TimeSlotAppService().GetAllTimeSlotWithoutAppoitment();

            if (model.EmployeeId != -1)
            {
                lst = ApplyFilterEmployee(lst, model.EmployeeId);
            }

                 if (model.SpecialtyId != -1)
            {
                lst = ApplyFilterSpecialty(lst, model.SpecialtyId);
            }


            if (!String.IsNullOrEmpty(model.GeoCityId) || model.GeoCityId!= "-1")
            {
                lst = ApplyFilterCity(lst, model.GeoCityId);
            }

            if (!String.IsNullOrEmpty(model.FromDate) && !String.IsNullOrEmpty(model.ToDate))
            {
               

                var fromDate = DateTime.ParseExact(model.FromDate, "dd/MM/yyyy", ci);
                var toDate = DateTime.ParseExact(model.ToDate, "dd/MM/yyyy", ci);

                lst = ApplyFilterDate(lst, fromDate,toDate);
            }





            model.AvailableSlot = new List<TimeSlotViewModel>();

            System.Globalization.DateTimeFormatInfo mfi = new System.Globalization.DateTimeFormatInfo();
            foreach (var itm in lst)
            {

                var emp = new EmployeeAppService().GetEmployee(itm.Schedule.EmployeeId);

                var itmVm = new TimeSlotViewModel
                {
                    ScheduleId = itm.ScheduleId,
                    MonthName = itm.Schedule.Month,
                    TimeSlotId = itm.TimeSlotId,
                    OfficeId = itm.OfficeId,
                    Day = itm.Day,
                    StartTime = itm.StartTime,
                    EndTime = itm.EndTime,
                    StartTimeText = itm.StartTime.ToShortTimeString(),
                    EndTimeText = itm.EndTime.ToShortTimeString(),
                    OfficeAddress = string.Format("Medico:{0}({1}) {2},{3}", emp.FullName, itm.Office.Address, itm.Office.Number, itm.Office.GeoCity.Name),
                    DayName = string.Format("{0},{1} de {2} de {3}", new DateTime(itm.Schedule.Year, itm.Schedule.Month, itm.Day).DayOfWeek.ToString(ci), itm.Day,mfi.GetMonthName(itm.Schedule.Month).ToString(),itm.Schedule.Year) 

                };

                


                model.AvailableSlot.Add(itmVm);
            }


            Session["AvailableSchedule"] = model.AvailableSlot;
            return View(model);
        }
        public DataTablesResult<TimeSlotViewModel> GetAllRecords(DataTablesParam dataTableParam)
        {
              if (Session["CurrentSchedule"] == null)
            {
                RedirectToAction("Index", "Schedule");

            }
         
             var schedule = (Schedule)Session["CurrentSchedule"];

            var svc = new TimeSlotAppService();
          List<TimeSlot> ats = svc.GetAllTimeSlotByScheduleId(schedule.ScheduleId);
            var atVm = new List<TimeSlotViewModel>();
            var ci = new CultureInfo("es-CO");

            foreach (var itm in ats)
            {

                var itmVm = new TimeSlotViewModel
                {
                    ScheduleId=itm.ScheduleId,
                    MonthName= schedule.Month,
                    TimeSlotId= itm.TimeSlotId,
                    OfficeId= itm.OfficeId,
                    Day= itm.Day,
                    StartTime = itm.StartTime,
                    EndTime = itm.EndTime,
                    StartTimeText = itm.StartTime.ToShortTimeString(),
                    EndTimeText = itm.EndTime.ToShortTimeString(),
                    OfficeAddress = string.Format("{0}({1})", itm.Office.Address, itm.Office.GeoCity.Name),
                    DayName= new DateTime(schedule.Year, schedule.Month, itm.Day).DayOfWeek.ToString(ci) + " " + itm.Day 
                    
                };

              




                var sb = new StringBuilder();

                string editUrl = Url.Action("Edit", "TimeSlot");
                sb.AppendLine("<div class=\"btn-group\">");
                sb.AppendLine(
                    "<button type=\"button\" class=\"btn btn-default dropdown-toggle\" data-toggle=\"dropdown\" aria-expanded=\"false\">");
                sb.AppendLine("Acciones <span class=\"caret\"></span>");
                sb.AppendLine("</button>");
                sb.AppendLine("<ul class=\"dropdown-menu\" role=\"menu\">");
                sb.AppendLine("<li><a href=\"" + editUrl + "?id=-1\"><i class=\"fa fa-plus\"></i>&nbsp;Nuevo Registro</a></li>");
                sb.AppendLine("<li><a href=\"" + editUrl + "?id=" + itmVm.TimeSlotId+ "\"><i class=\"fa fa-edit\"></i>&nbsp;Editar Disponibilidad</a></li>");
                sb.AppendLine("</ul>");
                sb.AppendLine("</div>");






                var actionButton = sb.ToString();

                itmVm.ActionButton = actionButton;
                    atVm.Add(itmVm);

            }

            var atmVmQueryable = atVm.AsQueryable();


            return DataTablesResult.Create(atmVmQueryable, dataTableParam);

        }
        public ActionResult Edit(TimeSlotViewModel model)
        {
            if (Session["CurrentSchedule"] == null)
            {
                return RedirectToAction("Index", "Schedule");

            }
            var schedule = (Schedule)Session["CurrentSchedule"];

            int days = DateTime.DaysInMonth(schedule.Year, schedule.Month);
            model.CurrentSchedule = schedule;
            model.Offices = new OfficeAppService().GetOfficesByEmployeeId(schedule.EmployeeId);
            model.DaysInMonth = days; ;

            DateTime parsedDate1 = DateTime.ParseExact(model.StartTimeText, "H:mm", System.Globalization.CultureInfo.InvariantCulture);
            DateTime parsedDat2 = DateTime.ParseExact(model.EndTimeText, "H:mm", System.Globalization.CultureInfo.InvariantCulture);
                



            try
            {
                var svc = new TimeSlotAppService();

                var o = new TimeSlot
                {
                    TimeSlotId= model.TimeSlotId,
                    ScheduleId = schedule.ScheduleId,
                    Day = model.Day,
                    StartTime = parsedDate1,
                    EndTime = parsedDat2,
                    OfficeId = model.OfficeId,
                    
          
                    

                };

                if (model.Action == "-1")
                {
                    var exist = svc.GetTimeSlot(model.TimeSlotId)!=null;
                    if (!exist)
                    {
                        svc.AddTimeSlot(o);
                        ViewBag.Feed = 0;
                    }
                    else
                    {
                        model.Action = "-1";
                        ViewBag.Feed = 3;
                        return View(model);
                    }
                }
                else
                {
                    o.TimeSlotId= model.TimeSlotId;
                    if (model.IsDeleteAction == 0)
                    {

                        svc.SaveTimeSlot(o);
                    }
                    else
                    {
                        svc.RemoveTimeSlot(model.TimeSlotId);
                    }
                    ViewBag.Feed = 0;
                }
            }
            catch (Exception)
            {
                ViewBag.Feed = 1;

            }

            return View(model);
        }