// public ActionResult Interactuar(int id, string back, int tel, int pSubCanalId, string pSubCanal)
        public ActionResult Interactuar(int id, string mensaje, datos_mensaje datosMensaje)
        {
            // , int pSubCanalId, string pSubCanal
            // lee subcanal
            var datSubC = db.sp_InteraccionProgramada(id).FirstOrDefault();

            ViewBag.ContactoID = id;

            ViewBag.Datosmensaje = datosMensaje;

            int    pSubCanalId = (int)datSubC.SubCanalId;
            string pSubCanal   = datSubC.SubCanal;

            ViewBag.MsgEror = "";

            if (!string.IsNullOrEmpty(mensaje))
            {
                ViewBag.MsgEror = "mensajeError();";
            }

            // pagina de regreso
            // ViewBag.Regreso = back;

            ViewBag.MedioDeContacto = pSubCanal;
            ViewBag.SubCanal_ID     = pSubCanalId;


            ViewBag.DisableWhatsApp = "disabled='disabled'";
            if (pSubCanalId == 1)
            {
                ViewBag.DisableWhatsApp = "";
            }


            ViewBag.SubCanalx = 1; // whatsapp

            //if (tel == 1)
            //    ViewBag.SubCanalx = 2; // Telefono


            // id = InteraccionId
            ViewBag.ID = id;

            ViewBag.botonDisabled = "disabled";

            // subcanals
            ViewBag.SubCanalId = new SelectList(db.SubCanales.Where(w => w.CanalId == 1).OrderBy(w => w.SubCanalNombre), "SubCanalId", "SubCanalNombre", pSubCanalId);



            // Causa no Interesado
            ViewBag.CausaNoInterId = new SelectList(db.CausaNoInter.OrderBy(w => w.CausaNoInterNombre), "CausaNoInterId", "CausaNoInterNombre");



            // causa no interes
            ViewBag.CausaNoInterId = new SelectList(db.CausaNoInter.OrderBy(w => w.CausaNoInterNombre), "CausaNoInterId", "CausaNoInterNombre");



            ViewBag.Telefono = 0;
            //if(tel == 1)
            //    ViewBag.Telefono = 1;



            // Lee datos del CONTACTO
            int Desarrollo_Id = 0;
            int subCanalId    = 0;
            int contactoId    = 0;

            // obtenet el contactoId
            // var contactId = db.InteraccionesContacto.Where(w => w.ContactoId == id).FirstOrDefault();
            var contactId = db.Contactos.Where(w => w.ContactoId == id).FirstOrDefault();

            contactoId = contactId.ContactoId;

            Interaccion interaccion = new Interaccion();

            var DatLead = from x in db.View_Datos_Contacto
                          where x.ContactoId == contactoId
                          select x;
            int EstatusBIDA      = 0;
            int SubEstatusBIDAId = 0;

            foreach (var item in DatLead)
            {
                interaccion.id = id;
                interaccion.NombreContactoCompleto = item.NombreContacto + " " + item.Apellidos;
                EstatusBIDA      = item.EstatusBIDA;
                Desarrollo_Id    = item.DesarrolloId;
                subCanalId       = item.SubCanalId;
                SubEstatusBIDAId = (int)item.SubEstatusBIDAId;
            }

            ViewBag.SubEstatusBIDAId = SubEstatusBIDAId;
            ViewBag.Desarrollo_Id    = Desarrollo_Id;
            ViewBag.Subcanal         = subCanalId;



            if (EstatusBIDA == 5)
            {
                ViewBag.botonDisabled = "";
            }


            ViewBag.ContactoNombre = interaccion.NombreContactoCompleto;
            ViewBag.EstatusBida    = EstatusBIDA;

            // Lee datos reales del Lead
            var datReal = from x in db.DatosReales
                          where x.ContactoId == id
                          select x;

            foreach (var item in datReal)
            {
                interaccion.NombreReal          = item.Nombre;
                interaccion.ApellidoPaternoReal = item.ApellidoPaterno;
                interaccion.ApellidoMaternoReal = item.ApellidoMaterno;
                interaccion.TelefonoReal        = item.Telefono;
                interaccion.CorreoReal          = item.Correo;
            }


            // lee interaccion final
            // var interacc = db.Interacciones.OrderByDescending(w => w.InteraccionId).FirstOrDefault(w => w.ContactoId == contactoId && w.InteraccionId == id);
            var interacc = db.Interacciones.OrderByDescending(w => w.InteraccionId).FirstOrDefault(w => w.ContactoId == contactoId);

            if (interacc != null)
            {
                interaccion.Descripcion         = interacc.Descripcion;
                interaccion.ResumenConversacion = interacc.Comentarios;
            }

            // OBTIENE DATOS REALES
            ViewBag.TieneDatosRales = 0;

            var datos_reales = from x in db.DatosReales
                               where x.ContactoId == id
                               select x;

            DatosReales datReales = new DatosReales();

            foreach (var item in datos_reales)
            {
                datReales.DatosRealesId   = item.DatosRealesId;
                datReales.ContactoId      = Convert.ToInt32(item.ContactoId);
                datReales.Nombre          = item.Nombre;
                datReales.ApellidoPaterno = item.ApellidoPaterno;
                datReales.ApellidoMaterno = item.ApellidoMaterno;
                datReales.Telefono        = item.Telefono;
                datReales.Correo          = item.Correo;

                ViewBag.TieneDatosRales = 1;
            }

            ViewBag.DatosReales = datReales;

            // estatus
            ViewBag.EstatusBIDAId = new SelectList(db.EstatusBIDA.Where(w => w.EstatusBIDAId < 3).OrderBy(w => w.EstatusBidaNombre), "EstatusBIDAId", "EstatusNombre", EstatusBIDA);



            // lee datos del contacto
            var rsContacto       = db.Contactos.Where(w => w.ContactoId == id).FirstOrDefault();
            int DesarrolloID     = rsContacto.DesarrolloId;
            int DesarrolloModelo = rsContacto.DesarrolloId;


            // Desarrollo
            ViewBag.DesarrolloId = new SelectList(db.Desarrollo.OrderBy(w => w.DesarrolloNombre), "DesarrolloId", "DesarrolloNombre", DesarrolloID);

            // Modelo
            ViewBag.DesarrolloModeloId = new SelectList(db.DesarrolloModelo.OrderBy(w => w.DesarrolloModeloNombre), "DesarrolloModeloId", "DesarrolloModeloNombre", DesarrolloModelo);

            return(View(interaccion));
        }
        public ActionResult Interactuar(Interaccion_nueva modelo, string WhatsAppCerrado)
        {
            datos_mensaje datos_mensaje = new datos_mensaje();

            try
            {
                datos_mensaje.Fecha_asignacion = String.Format("{0:dd-MMM-yyyy}", DateTime.Now);
                ViewBag.ContactoID             = modelo.ContactoId;

                // ResumenConversacion = -Nota

                string Usuario_ID = Session["Usuario_ID"].ToString();

                bool bInteres = (modelo.SubEstatusBIDAId == 4) ? true : false;


                Interacciones interacciones = new Interacciones();
                interacciones.ContactoId       = modelo.ContactoId;
                interacciones.SubCanalId       = modelo.SubCanalId;
                interacciones.UsuarioHOST      = Usuario_ID;
                interacciones.EstatusBIDAId    = modelo.EstatusBIDAId;
                interacciones.FechaInteraccion = DateTime.Now;
                interacciones.Descripcion      = string.Empty;
                //interacciones.Comentarios = modelo.ResumenConversacion;
                interacciones.Interes = bInteres;

                if (modelo.CausaNoInterId == 0)
                {
                    interacciones.CausaNoInterId      = null;
                    interacciones.ComentarioNoInteres = null;
                }
                else
                {
                    interacciones.CausaNoInterId      = modelo.CausaNoInterId;
                    interacciones.ComentarioNoInteres = modelo.comentarioNoInteres;
                }
                interacciones.WhatsAppCerrado  = modelo.WhatsAppCerrado;
                interacciones.SubEstatusBIDAId = modelo.SubEstatusBIDAId;

                db.Interacciones.Add(interacciones);


                // agrega nota
                NotasContacto notasContacto = new NotasContacto();
                notasContacto.ContactoId = modelo.ContactoId;
                notasContacto.Fecha      = DateTime.Now;
                notasContacto.Nota       = modelo.ResumenConversacion;
                notasContacto.HOSTId     = Usuario_ID;

                db.NotasContacto.Add(notasContacto);
                db.SaveChanges();



                // actualiza estatus en tabla contactos
                var dsContacto = db.Contactos.Where(w => w.ContactoId == modelo.ContactoId).FirstOrDefault();

                if (dsContacto != null)
                {
                    dsContacto.EstatusBIDA      = modelo.EstatusBIDAId;
                    dsContacto.SubEstatusBIDAId = modelo.SubEstatusBIDAId;

                    db.SaveChanges();
                }

                var sigInter = db.sp_SiguienteInteraccion(modelo.ContactoId, 0);


                // busca si tiene capturado datos reales
                var datosReal = db.DatosReales.Where(w => w.ContactoId == modelo.ContactoId).FirstOrDefault();

                datos_mensaje.Lead = String.Format("{0} {1} {2}", datosReal.Nombre, datosReal.ApellidoPaterno, datosReal.ApellidoMaterno);

                if (datosReal != null)
                {
                    // YA TIENE CAPTURADO DATOS REALES
                    string   conStr = WebConfigurationManager.ConnectionStrings["ConStr"].ToString();
                    ADO_Core adoC   = new ADO_Core(conStr);

                    string        comando = String.Format("Exec sp_carruselAsignacion {0}, '{1}', {2}", modelo.DesarrolloId, Usuario_ID, 0);
                    SqlDataReader dr      = adoC.GetDataReader(comando);

                    if (dr.HasRows)
                    {
                        dr.Read();
                        try
                        {
                            int _EPId = Convert.ToInt32(dr["EPId"].ToString());

                            // obtiene el nombre del host
                            var    dsHostAsigna = db.AspNetUsers.Where(w => w.Id == Usuario_ID).FirstOrDefault();
                            string HostAsigna   = dsHostAsigna.UserName;
                            datos_mensaje.HostAsigna = HostAsigna;

                            // obtiene el ep seleccionado
                            int epID = int.Parse(dr["EPid"].ToString());

                            var DsEpSel = db.AgentesEP.Where(w => w.EPId == epID).FirstOrDefault();

                            string epSeleccinado = DsEpSel.Nombre;
                            datos_mensaje.EP_seleccionado = epSeleccinado;

                            ViewBag.Datosmensaje = datos_mensaje;

                            Session["Datos_Mensaje"] = datos_mensaje;



                            if (modelo.SubEstatusBIDAId == 3)
                            {
                                var Desarrollo    = db.Desarrollo.Where(w => w.DesarrolloId == modelo.DesarrolloId).FirstOrDefault();
                                var datosContacto = db.DatosReales.Where(w => w.ContactoId == modelo.ContactoId).FirstOrDefault();


                                // WEB SERVICE
                                long PartyId    = long.Parse(dr["PartyId"].ToString());
                                long ResourceId = long.Parse(dr["ResourceId"].ToString());

                                hu_website.hu_web_service.SoaClient webSer = new hu_web_service.SoaClient();
                                //string SalesOracleId = webSer.crearContactoRespondersV3(
                                //    "@XZYContaToSales#",
                                //    datosContacto.Nombre,
                                //    datosContacto.ApellidoPaterno,
                                //    datosContacto.ApellidoMaterno,
                                //    "DIGIN",
                                //    "IBWA",
                                //    datosContacto.Telefono.ToString(),
                                //    datosContacto.Correo,
                                //    Desarrollo.DesarrolloNombre,
                                //    PartyId,
                                //    ResourceId,
                                //    true);



                                // GUARDAR LOS DATOS DEL WEB SERVICE
                                long valorl = 0;
                                valorl = 10000000055445;

                                try
                                {
                                    //valorl = long.Parse(SalesOracleId);
                                }
                                catch (Exception ex)
                                {
                                    throw new Exception("'El web service no está funcionando.'");
                                }

                                guardaContactoDatos(modelo.ContactoId, valorl);

                                // guarda en la tabla asignacicion

                                using (rubenzgb_hudevEntities dbasae = new rubenzgb_hudevEntities())
                                {
                                    Asignacion asignacion = new Asignacion()
                                    {
                                        ContactoId      = modelo.ContactoId,
                                        EPId            = _EPId,
                                        UsuarioHOST     = Usuario_ID,
                                        FechaAsignacion = DateTime.Now
                                    };

                                    dbasae.Asignacion.Add(asignacion);
                                    dbasae.SaveChanges();
                                }


                                // cambia el estatus del contacto a 4
                                using (rubenzgb_hudevEntities dbasae = new rubenzgb_hudevEntities())
                                {
                                    var qry1 = dbasae.Contactos.Where(w => w.ContactoId == modelo.ContactoId).First();
                                    qry1.EstatusBIDA      = modelo.EstatusBIDAId;
                                    qry1.SubEstatusBIDAId = modelo.SubEstatusBIDAId;
                                    qry1.Estatus          = 4;
                                    dbasae.SaveChanges();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            throw new Exception(ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(RedirectToAction("Interactuar", "whatsapp", new { id = modelo.ContactoId, mensaje = ex.Message }));
            }

            return(RedirectToAction("Interactuar", "whatsapp", new { id = modelo.ContactoId, datosMensaje = datos_mensaje }));

            // return RedirectToAction("index", "Host", new { id = modelo.ContactoId });
        }