public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindAsync(model.UserName, model.Password);
                if (user != null)
                {
                    Session["UserID"] = user.Id;
                    
                    UserBasicInfo = new UserInformationController().GetUserByAspNetUserId(user.Id);
                    Session["UserTypeID"] = UserBasicInfo.UserTypeID;
                    ViewBag.UserType = UserBasicInfo.UserTypeID;
                    Session["UserTypeID"] = UserBasicInfo.UserTypeID;

                    var userCookie = new HttpCookie("UserTypeID", UserBasicInfo.UserTypeID.ToString());
                    //userCookie.Expires.AddDays(365);
                    HttpContext.Response.Cookies.Add(userCookie);

                    //User.IsInRole(UserBasicInfo.UserTypeID.ToString());
                    await SignInAsync(user, model.RememberMe);
                    return RedirectToLocal(returnUrl);
                }
                else
                {
                    ModelState.AddModelError("", "Invalid username or password.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public ActionResult Create(UserInformation userinformation)
        {
            if (ModelState.IsValid)
            {
                db.UserInformations.Add(userinformation);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.AspNetRoleID = new SelectList(db.AspNetRoles, "Id", "Name", userinformation.AspNetRoleID);
            ViewBag.AspNetUserId = new SelectList(db.AspNetUsers, "Id", "UserName", userinformation.AspNetUserId);
            return View(userinformation);
        }
        public ActionResult Create(PoleImage poleimage, FormCollection collection)
        {
            foreach (string fileName in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[fileName];
            }
            //, IEnumerable files
            //var userbasic = new UserInformationController().GetUserByAspNetUserId(User.Identity.GetUserId());
            userInfo = TempData["userInfo"] as UserInformation;
            if (ModelState.IsValid)
            {
                poleimage.UserID = userInfo.Id;
                //db.PoleImages.Add(poleimage);
                //db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.PoleInfoID = new SelectList(GetPoleIDList(), "ID", "PoleID", poleimage.PoleInfoID);
            ViewBag.UserID = new SelectList(db.UserInformations, "Id", "UserFullName", poleimage.UserID);
            return View(poleimage);
        }
 /////Method
 public int Save(UserInformation userinformation)
 {
     int id = 0;
     if (ModelState.IsValid)
     {
         db.UserInformations.Add(userinformation);
         id=db.SaveChanges();
     }
     return id;
 }
 private IEnumerable GetSearchData(UserInformation User)
 {
     var data = (from p in objE.PoleInfoes
                 join t in objE.PoleTypes on p.TypeID equals t.ID into typegroup
                 join u in objE.UserInformations on p.TaskAssainUserID equals u.Id
                 from type in typegroup.DefaultIfEmpty()
                 where (userInfo.UserTypeID == 1 || p.TaskAssainUserID==userInfo.Id)
                 select new PoleInfoViewModel
                 {
                     ID = p.ID,
                     PoleID = p.PoleID,
                     TypeID = p.TypeID,
                     TypeName = type.TypeName,
                     TaskAssainUserID=p.TaskAssainUserID,
                     UserFullName = u.UserFullName,
                     TaskAddeddate = p.TaskAddeddate,
                     TaskPerformeddate = p.TaskPerformeddate,
                     ImageMapPath = p.ImageMapPath,
                     AdjacentPoleHeight = p.AdjacentPoleHeight,
                     TransFormerLoading = p.TransFormerLoading,
                     Notes = p.Notes,
                     ImagesTakenpath = p.ImagesTakenpath
                 }).ToList().OrderByDescending(p => p.ID);
     //PoleInfoViewModel
     return data;
 }
        private IEnumerable GetPoleIDList()
        {
            userInfo = new UserInformationController().GetUserByAspNetUserId(User.Identity.GetUserId());
            var data = (from p in objE.PoleInfoes
                        where (userInfo.UserTypeID == 1 || p.UserID == userInfo.Id)
                        select new PoleImageViewModel
                        {
                            ID = p.ID,
                            PoleID = p.PoleID

                        }).ToList();

            return data;
        }
        //
        // GET: /PoleInfo/
        public ActionResult Index()
        {
            userInfo = new UserInformationController().GetUserByAspNetUserId(User.Identity.GetUserId());
            var data = GetSearchData(userInfo);

            return View(data);
        }
        public ActionResult Import(PoleInfoViewModel PoleInfo,HttpPostedFileBase file)
        {
            try
            {
                //return RedirectToAction("Index");
                // TODO: Add insert logic here
                //ViewBag.TaskAssainUser = new SelectList(new UserInformationController().GetDropdownData(), "ID", "UserFullName");
                DataSet ds = new DataSet();
                if (Request.Files["file"].ContentLength > 0)
                {

                    string fileExtension =System.IO.Path.GetExtension(Request.Files["file"].FileName);

                    if (fileExtension == ".xls" || fileExtension == ".xlsx")
                    {

                        string fileLocation = Server.MapPath("~/Content/") + Request.Files["file"].FileName;

                        if (System.IO.File.Exists(fileLocation))
                        {

                            System.IO.File.Delete(fileLocation);
                        }

                        Request.Files["file"].SaveAs(fileLocation);

                        //string _UserID = User.Identity.GetUserId();
                        userInfo = new UserInformationController().GetUserByAspNetUserId(User.Identity.GetUserId());

                        DataTable tbl1 = SQLHelper.GetExcelData(fileLocation, "PoleData");
                        var drs = tbl1.Select("isnull(PoleID,'')=''");

                        foreach (DataRow row in drs)
                        {
                            tbl1.Rows.Remove(row);
                        }

                        foreach (DataRow dr in tbl1.Rows)
                        {

                            PoleInfo obj = new PoleInfo();
                            obj.PoleID = dr["PoleID"].ToString();
                            obj.TypeID = GetPolTypeID(dr["PoleType"].ToString());

                            DateTime _outdatetime;
                            DateTime? TaskAddeddate = null;
                            if (DateTime.TryParse(dr["TaskAddeddate"].ToString(), out _outdatetime))
                            {
                                TaskAddeddate = Convert.ToDateTime(dr["TaskAddeddate"].ToString());
                            }

                            DateTime? TaskPerformeddate = null;
                            if (DateTime.TryParse(dr["TaskPerformeddate"].ToString(), out _outdatetime))
                            {
                                TaskPerformeddate = Convert.ToDateTime(dr["TaskPerformeddate"].ToString());
                            }

                            obj.TaskAddeddate = TaskAddeddate;
                            obj.TaskPerformeddate = TaskPerformeddate;

                            obj.ImageMapPath = dr["ImageMapPath"].ToString();

                            double _outval;
                            double AdjacentPoleHeight = (double.TryParse(dr["AdjacentPoleHeight"].ToString(), out _outval) == false ? default(double) : Convert.ToDouble(dr["AdjacentPoleHeight"].ToString()));

                            obj.AdjacentPoleHeight = AdjacentPoleHeight;
                            obj.TransFormerLoading = dr["TransFormerLoading"].ToString();
                            obj.Notes = dr["Notes"].ToString();
                            obj.ImagesTakenpath = dr["ImagesTakenpath"].ToString();
                            obj.TaskAssainUserID = PoleInfo.TaskAssainUserID;
                            obj.UserID = userInfo.Id;
                            obj.IsActive = true;

                            try
                            {
                                int Id = Save(obj);
                            }
                            catch (Exception ex)
                            {
                                string errormsg = ex.Message;
                            }
                        }

                    }

                }

                return RedirectToAction("Index");
            }
            catch(Exception ex)
            {
                string dd = ex.Message;
                return View(ex.Message);
            }
        }
        private List<PoleInfo> GetPoleIDList()
        {
            userInfo = new UserInformationController().GetUserByAspNetUserId(User.Identity.GetUserId());
            var data = (from p in db.PoleInfoes
                        where (userInfo.UserTypeID == 1 || p.TaskAssainUserID == userInfo.Id)
                        select  p).ToList();

            return data;
        }
        public ActionResult UploadFiles(IEnumerable files, PoleImage poleimage)
        {
            string TempPath = Server.MapPath("~/Content/TempFile/");
            userInfo = new UserInformationController().GetUserByAspNetUserId(User.Identity.GetUserId());
            //List<PoleImage> objlist = new List<PoleImage>();
            foreach (HttpPostedFileBase file in files)
            {
                string filePath = Path.Combine(TempPath, file.FileName);
                //System.IO.File.WriteAllBytes(filePath, ReadData(file.InputStream));

                //PoleImage obj = new PoleImage();
                poleimage.UserID = userInfo.Id;
                //db.PoleImages.Add(poleimage);
                //db.SaveChanges();
            }

            return Json("All files have been successfully stored.");
        }
        public ActionResult SaveUploadedPoleImage(List<PoleImageList> obj)
        {
            try
            {
                if(obj==null)
                {
                    return Json(new { Success = 3, ID = 0, ex = "" });
                }

                string PoleID="";
                PoleInfoEntities objE = new PoleInfoEntities();
                var FileSavePathList = objE.FileSavePaths.ToList();
                string _savepath = FileSavePathList.SingleOrDefault(p => p.FileFor == "PoleImageMapPath").FilePath;
                userInfo = new UserInformationController().GetUserByAspNetUserId(User.Identity.GetUserId());
                foreach (var itm in obj)
                {
                    var id = itm.ID;
                    PoleID = itm.PoleID;
                    var Imgae64 = itm.Image64;

                    Image image = PoleClass.Base64ToImage(Imgae64);

                    string ImageMapPath = _savepath + itm.PoleID.ToString().Trim() + "_MapImage"+itm.ID.ToString() + ".jpg";

                    //string imgname = PoleID+"_img_"+id.ToString()+".jpg";
                    image.Save(ImageMapPath, System.Drawing.Imaging.ImageFormat.Jpeg);

                    PoleImage poleimage = new PoleImage();
                    poleimage.UserID = userInfo.Id;
                    poleimage.PoleInfoID = itm.PoleInfoID;
                    poleimage.ImageMapPath = ImageMapPath;
                    poleimage.CreateDate = DateTime.Now;

                    db.PoleImages.Add(poleimage);
                    db.SaveChanges();

                }
                //var msg = "Saved Successfully Completed";
                //return Content(msg, "text/html");
                return Json(new { Success = 1, ID = PoleID, ex = "" });

            }
            catch (Exception ex)
            {
                return Json(new { Success = 0, ex = ex.Message.ToString() });
                //throw new HttpException(500, ex.Message);
            }

            //return Json(new { Success = 0, ex = new Exception("Unable to save").Message.ToString() });
        }
        // GET: /PoleImage/
        public ActionResult Index()
        {
            //TempData["userInfo"] = new UserInformationController().GetUserByAspNetUserId(User.Identity.GetUserId());
            //userInfo = TempData["userInfo"] as UserInformation ;
            userInfo = new UserInformationController().GetUserByAspNetUserId(User.Identity.GetUserId());
            //var poleimages = db.PoleImages.Include(p => p.PoleInfo).Include(p => p.UserInformation);
            List<PoleImageViewModel> poleimglist = new List<PoleImageViewModel>();
            //foreach(var itm in poleimages)
            //{
            //    PoleImageViewModel obj = new PoleImageViewModel();

            //}

            var data = (from p in db.PoleImages
                        join pt in db.PoleInfoes on p.PoleInfoID equals pt.ID
                        where (userInfo.UserTypeID == 1 || p.UserID == userInfo.Id)
                        select new PoleImageViewModel
                        {
                            ID = p.ID,
                            PoleInfoID=(int)p.PoleInfoID,
                            ImageMapPath=p.ImageMapPath,
                            //ImageBase64=PoleClass.ImageToBase64(p.ImageMapPath),
                            Notes=p.Notes,
                            PoleID = pt.PoleID

                        }).ToList().OrderByDescending(p => p.ID);

            return View(data);
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser() { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    #region Userinformation
                    ////Userinformation
                    UserInformationController objController = new UserInformationController();
                    UserInformation objuser = new UserInformation();
                    objuser.AspNetUserId = user.Id;
                    objuser.AspNetRoleID = model.AspNetRoleID;
                    objuser.UserFullName = model.UserFullName;
                    objuser.UPassword = model.Password;
                    objuser.Email = model.Email;
                    objuser.CreateDate = DateTime.Now;
                    objuser.UserTypeID = 2;
                    objController.Save(objuser);
                    #endregion
                   // await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("RegisterSuccess", "Account");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }