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

            if (Session["CurrentPatient"] == null)
            {
                return RedirectToAction("Index", "Patient");

            }
            var patient = (Patient)Session["CurrentPatient"];

            var model = new SubscriptionViewModel();
            model.CurrentPatient= patient;
            model.SubscriptionTypes = new SubscriptionTypeAppService().GetAllSubscriptionType();


            if (id != -1)
            {
                var svc = new SubscriptionAppService();
                var o = svc.GetSubscription(id);
                model.SubscriptionId = o.SubscriptionId;
                model.SubscriptionTypeId = o.SubscriptionTypeId;
                model.PatientId = o.PatientId;
                model.IsActive = o.IsActive;
      
            

            }
            else
            {
                model.Action = "-1";
                model.SubscriptionId = -1;
                model.SubscriptionTypeId = -1;
                model.PatientId =patient.PatientId;
                model.IsActive = 1;
                
            }



            return View(model);
        }
        public DataTablesResult<SubscriptionViewModel> GetAllRecords(DataTablesParam dataTableParam)
        {
              if (Session["CurrentPatient"] == null)
            {
                RedirectToAction("Index", "Patient");

            }
         
             var patient = (Patient)Session["CurrentPatient"];

            var svc = new SubscriptionAppService();
          List<Subscription> ats = svc.GetAllSubscriptionByPatientId(patient.PatientId);
            var atVm = new List<SubscriptionViewModel>();


            foreach (var itm in ats)
            {

                var itmVm = new SubscriptionViewModel
                {

               
                    SubscriptionId= itm.SubscriptionId,
                    SubscriptionTypeName=itm.SubscriptionType.Name,
                    IsActive=itm.IsActive,
                    IsActiveText=itm.IsActive==1 ? "Activa":"Inactiva"

                };

              




                var sb = new StringBuilder();

                string editUrl = Url.Action("Edit", "Subscription");
                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.SubscriptionId+ "\"><i class=\"fa fa-edit\"></i>&nbsp;Editar Subscripción</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(SubscriptionViewModel model)
        {
            if (Session["CurrentPatient"] == null)
            {
                return RedirectToAction("Index", "Patient");

            }
            var patient = (Patient)Session["CurrentPatient"];

    
            model.CurrentPatient = patient;
            model.SubscriptionTypes = new SubscriptionTypeAppService().GetAllSubscriptionType();


            try
            {
                var svc = new SubscriptionAppService();

                var o = new Subscription
                {
                    SubscriptionId= model.SubscriptionId,
                    PatientId = patient.PatientId,
                    SubscriptionTypeId =  model.SubscriptionTypeId,
          IsActive=model.IsActive
                    

                };

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

                        svc.SaveSubscription(o);
                    }
                    else
                    {
                        svc.RemoveSubscription(model.SubscriptionId);
                    }
                    ViewBag.Feed = 0;
                }
            }
            catch (Exception)
            {
                ViewBag.Feed = 1;

            }

            return View(model);
        }