// Validate Signup Attempt
        public JsonResult SingupValidate(string firstname, string lastname, string email, string password)
        {
            string response = "";
            using (MyDatabaseEntities ctx = new MyDatabaseEntities())
            {
                User u = ctx.Users.FirstOrDefault(x => x.Email == email);
                response = "failed";
                if (u == null && email != "admin")
                {
                    u = new User();

                    u.Email = email;
                    u.FirstName = firstname;
                    u.LastName = lastname;
                    u.Password = password;

                    ctx.Users.Add(u);
                    ctx.SaveChanges();

                    Session["id"] = u.UserId;
                    Session["type"] = "user";
                    Session["firstname"] = u.FirstName;
                    Session["lastname"] = u.LastName;
                    response = "success";
                }
            }
            return this.Json(response, JsonRequestBehavior.AllowGet);
        }
 public JsonResult AddCatalog(string name)
 {
     MyDatabaseEntities ctx = new MyDatabaseEntities();
     Catalog u = new Catalog();
     u.Name = name;
     ctx.Catalogs.Add(u);
     ctx.SaveChanges();
     string response = "success";
     return this.Json(response, JsonRequestBehavior.AllowGet);
 }
        public ActionResult Catalog()
        {
            if (Session["id"] != null && Session["type"].ToString() == "user")
                return RedirectToAction("Index", "User");
            else if (Session["id"] == null)
                return RedirectToAction("Index", "Login");

            ViewBag.visible = 2;
            MyDatabaseEntities ctx = new MyDatabaseEntities();
            List<Catalog> cat = ctx.Catalogs.ToList();
            return View(cat);
        }
        public ActionResult TotalOrders()
        {
            if (Session["id"] != null && Session["type"].ToString() == "admin")
                return RedirectToAction("Index", "Admin");
            else if (Session["id"] == null)
                return RedirectToAction("Index", "Login");

            ViewBag.visible = 2;
            MyDatabaseEntities ctx = new MyDatabaseEntities();
            int userId = int.Parse(Session["id"].ToString());
            List<Order> u = ctx.Orders.Where(x => x.UserId == userId).ToList();
            return View(u);
        }
        public ActionResult Setting()
        {
            if (Session["id"] != null && Session["type"].ToString() == "admin")
                return RedirectToAction("Index", "Admin");
            else if (Session["id"] == null)
                return RedirectToAction("Index", "Login");

            ViewBag.visible = 3;
            MyDatabaseEntities ctx = new MyDatabaseEntities();
            int userId = int.Parse(Session["id"].ToString());
            User u = ctx.Users.FirstOrDefault(m => m.UserId == userId);
            return View(u);
        }
        public JsonResult DiscardOrder(int id)
        {
            MyDatabaseEntities ctx = new MyDatabaseEntities();
            Order u = ctx.Orders.FirstOrDefault(x => x.OrderId == id);

            string response = "failed";
            if (u != null)
            {
                u.Status = "DISCARD";
                ctx.SaveChanges();
                response = "success";
            }
            return this.Json(response, JsonRequestBehavior.AllowGet);
        }
        public JsonResult DeleteUser(int id)
        {
            MyDatabaseEntities ctx = new MyDatabaseEntities();
            User u = ctx.Users.FirstOrDefault(x => x.UserId == id);

            string response = "failed";
            if (u != null)
            {
                ctx.Users.Remove(u);
                ctx.SaveChanges();
                response = "success";
            }
            return this.Json(response, JsonRequestBehavior.AllowGet);
        }
        public JsonResult AddOrder(string job, string media, string mediaCatalog, string content)
        {
            MyDatabaseEntities ctx = new MyDatabaseEntities();
            Order o = new Order();

            o.JobType = job;
            o.Media = media;
            o.CatalogNumber = int.Parse(mediaCatalog);
            o.Content = content;
            o.UserId = int.Parse(Session["id"].ToString());
            o.Status = "PENDING";
            string response = "failed";
            ctx.Orders.Add(o);
            ctx.SaveChanges();
            response = "success";
            return this.Json(response, JsonRequestBehavior.AllowGet);
        }
        public JsonResult UpdateUser(int id, string firstname, string lastname, string email, string password)
        {
            MyDatabaseEntities ctx = new MyDatabaseEntities();
            User u = ctx.Users.FirstOrDefault(x => x.UserId == id);

            string response = "failed";
            if (u != null && email != "admin")
            {
                u.Email = email;
                u.FirstName = firstname;
                u.LastName = lastname;
                u.Password = password;

                ctx.SaveChanges();
                response = "success";
                Session["firstname"] = firstname;
            }
            return this.Json(response, JsonRequestBehavior.AllowGet);
        }
        public JsonResult AddOrder(string payOption)
        {
            MyDatabaseEntities ctx = new MyDatabaseEntities();
            Order o = new Order();

            o.JobType = Session["job"].ToString();
            o.Media = Session["media"].ToString();
            o.CatalogNumber = int.Parse(Session["mediaCatalog"].ToString());
            o.Content = Session["content"].ToString();
            o.UserId = int.Parse(Session["id"].ToString());
            o.PaymentMethod = payOption;
            o.Status = "PENDING";

            string response = "pass";
            ctx.Orders.Add(o);
            ctx.SaveChanges();

            return this.Json(response, JsonRequestBehavior.AllowGet);
        }
        public ActionResult EditUser(int id)
        {
            MyDatabaseEntities ctx = new MyDatabaseEntities();
            User u = ctx.Users.FirstOrDefault(x => x.UserId == id);

            return View(u);
        }
        public ActionResult Users()
        {
            if (Session["id"] != null && Session["type"].ToString() == "user")
                return RedirectToAction("Index", "User");
            else if (Session["id"] == null)
                return RedirectToAction("Index", "Login");

            ViewBag.visible = 3;
            MyDatabaseEntities ctx = new MyDatabaseEntities();
            List<User> list = ctx.Users.Where(x => x.UserId != 2).ToList();
            return View(list);
        }
        public JsonResult UpdateCatalog(int id, string name)
        {
            MyDatabaseEntities ctx = new MyDatabaseEntities();
            Catalog u = ctx.Catalogs.FirstOrDefault(x => x.CatalogId == id);

            string response = "failed";
            if (u != null)
            {
                u.Name = name;
                ctx.SaveChanges();
                response = "success";
            }
            return this.Json(response, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Orders()
        {
            if (Session["id"] != null && Session["type"].ToString() == "user")
                return RedirectToAction("Index", "User");
            else if (Session["id"] == null)
                return RedirectToAction("Index", "Login");

            ViewBag.visible = 1;
            MyDatabaseEntities ctx = new MyDatabaseEntities();
            List<Order> or = ctx.Orders.Where(x => x.Status != "PENDING" && x.Status != "DISCARD").ToList();
            return View(or);
        }
        // Validate Login Attempt
        public JsonResult Validate(string email, string password)
        {
            User u = null;
            string response = "failed";
            using (MyDatabaseEntities ctx = new MyDatabaseEntities())
            {
                if (email == "admin")
                {
                    u = ctx.Users.FirstOrDefault(x => x.UserId == 2);
                    if (u.Password == password)
                    {
                        response = "admin";
                        Session["id"] = u.UserId;
                        Session["type"] = "admin";
                        Session["firstname"] = u.FirstName;
                        Session["lastname"] = u.LastName;
                    }
                }
                else
                {
                    u = ctx.Users.FirstOrDefault(x => x.Email == email);
                    if (u == null)
                    {
                        try
                        {
                            u = ctx.Users.FirstOrDefault(x => x.UserId == int.Parse(email));
                        }
                        catch
                        {
                            u = null;
                        }
                    }

                    if (u != null && u.Password == password)
                    {
                        Session["id"] = u.UserId;
                        Session["type"] = "user";
                        Session["firstname"] = u.FirstName;
                        Session["lastname"] = u.LastName;
                        response = "user";
                    }
                }

            }

            return this.Json(response, JsonRequestBehavior.AllowGet);
        }