// GET: /SalesMan/Create
 public ActionResult Create()
 {
     using (FFSecurityDBEntities db2 = new FFSecurityDBEntities())
     {
         ViewBag.UserName = new SelectList(db2.AspNetUsers, "UserName", "UserName").ToList();
     }
     return View();
 }
        // GET: /SalesMan/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            SalesMan salesman = await db.SalesMan.FindAsync(id);
            ViewBag.salesManName = salesman.UserName;

            using (FFSecurityDBEntities db2 = new FFSecurityDBEntities())
            {
                ViewBag.UserName = new SelectList(db2.AspNetUsers, "UserName", "UserName").ToList();
            }

            if (salesman == null)
            {
                return HttpNotFound();
            }
            return View(salesman);
        }
        public ActionResult GetUserCredintionals()
        {
            using (FFSecurityDBEntities ff = new FFSecurityDBEntities())
            {
                var user = ff.AspNetUsers.FirstOrDefault(s => s.UserName == HttpContext.User.Identity.Name);
                if (user != null)
                {
                    var uJson = new
                    {
                        UserName = user.UserName,
                        IsAdministrator = user.IsAdministrator,
                        IsCustomerService = user.IsCustomerService,
                        IsOperation = user.IsOperation,
                        IsSales = user.IsSales,
                        IsAccounting = user.IsAccounting,
                        IsCustomerClearance = user.IsCustomClearance,
                    };

                    return Json(uJson, JsonRequestBehavior.AllowGet);
                }
                return Json("0", JsonRequestBehavior.AllowGet);
            }
        }
        public async Task<ActionResult> Edit([Bind(Include="SalesManID,Updated,Updator,SalesManName,SalesManPhone,UserName")] SalesMan salesman)
        {
           //if (HttpContext.User.IsInRole("sales"))
           // {
           //     salesman.UserName = HttpContext.User.Identity.Name;
           // }
           using (FFSecurityDBEntities db2 = new FFSecurityDBEntities())
           {
               ViewBag.UserName = new SelectList(db2.AspNetUsers, "UserName", "UserName" , salesman.UserName).ToList();
           }

            if (ModelState.IsValid)
            {
                db.Entry(salesman).State = EntityState.Modified;
                await db.SaveChangesAsync();
                if (HttpContext.User.IsInRole("sales"))
                {
                    return RedirectToAction("Details", "SalesMan");
                }
                return RedirectToAction("Index");
            }
            return View(salesman);
        }