// GET: Kodas/Edit/5
        public ActionResult Edit(int?id)
        {
            //To prevent back browser button
            myLibrary.PreventBackButtonBrowser(Response);
            if (Session["curchID"] == null)
            {
                return(RedirectToAction("signIn", "Logins"));
            }

            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Koda koda = db.Kodas.Find(id);

            //In order to prevent user to edite old Date
            if (koda.Date < DateTime.Now)
            {
                return(RedirectToAction("Index"));
            }

            // To check url security id
            if (koda.ChurchesID != Convert.ToInt32(Session["curchID"]))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
            }
            if (koda == null)
            {
                return(HttpNotFound());
            }
            ViewBag.ChurchesID = new SelectList(db.Churches, "Id", "churchName", koda.ChurchesID);
            ViewBag.fatherID   = new SelectList(db.Logins, "Id", "Phone", koda.fatherID);
            return(View(koda));
        }
        public ActionResult Edit([Bind(Include = "Id,Date,Time,PeopleCount")] Koda koda)
        {
            if (ModelState.IsValid)
            {
                Koda k1 = db.Kodas.Find(koda.Id);
                //In order to prevent user to edite old Date
                if (koda.Date < DateTime.Now)
                {
                    return(RedirectToAction("Index"));
                }


                k1.Date        = koda.Date;
                k1.Time        = koda.Time;
                k1.PeopleCount = koda.PeopleCount;
                if (k1.PeopleCount < k1.PeopleDeposited)
                {
                    ModelState.AddModelError("PeopleCount", "يجب ان يكون عدد الأشخاص فى القداس أكبر من" + k1.PeopleDeposited);
                    koda.PeopleDeposited = k1.PeopleDeposited;
                    return(View(koda));
                }
                db.Entry(k1).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.ChurchesID = new SelectList(db.Churches, "Id", "churchName", koda.ChurchesID);
            ViewBag.fatherID   = new SelectList(db.Logins, "Id", "Phone", koda.fatherID);
            return(View(koda));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            Deposited deposited = db.Depositeds.Find(id);
            Koda      koda      = db.Kodas.Find(deposited.kodasID);

            --koda.PeopleDeposited;
            db.Entry(koda).State = EntityState.Modified;
            db.Depositeds.Remove(deposited);
            db.SaveChanges();
            return(RedirectToAction("Index/" + deposited.kodasID));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            Koda koda = db.Kodas.Find(id);

            //In order to prevent user to edite old Date
            if (koda.Date >= DateTime.Now)
            {
                db.Kodas.Remove(koda);
                db.SaveChanges();
            }


            return(RedirectToAction("Index"));
        }
        public ActionResult Create([Bind(Include = "Id,Date,Time,PeopleCount")] Koda koda)
        {
            if (ModelState.IsValid)
            {
                koda.PeopleDeposited = 0;
                koda.ChurchesID      = Convert.ToInt32(Session["curchID"]);
                koda.fatherID        = Convert.ToInt32(Session["id"]);
                db.Kodas.Add(koda);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ChurchesID = new SelectList(db.Churches, "Id", "churchName", koda.ChurchesID);
            ViewBag.fatherID   = new SelectList(db.Logins, "Id", "Phone", koda.fatherID);
            return(View(koda));
        }
        // GET: Kodas/Details/5
        public ActionResult Details(int?id)
        {
            //To prevent back browser button
            myLibrary.PreventBackButtonBrowser(Response);
            if (Session["id"] == null)
            {
                return(RedirectToAction("signIn", "Logins"));
            }

            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Koda koda = db.Kodas.Find(id);

            if (koda == null)
            {
                return(HttpNotFound());
            }
            return(View(koda));
        }
        // GET: Depositeds
        public ActionResult Index(int?id)
        {
            //To prevent back browser button
            myLibrary.PreventBackButtonBrowser(Response);
            if (Session["curchID"] == null)
            {
                return(RedirectToAction("signIn", "Logins"));
            }

            int church     = Convert.ToInt32(Session["curchID"]);
            var depositeds = db.Depositeds.Where(m => m.kodasID == id && m.churchesID == church).Include(d => d.Church).Include(d => d.Login).Include(d => d.Koda);
            // To check url security id
            Koda koda = db.Kodas.Find(id);

            if (koda.ChurchesID != Convert.ToInt32(Session["curchID"]))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            Session["kodasId"] = id;
            return(View(depositeds.ToList()));
        }
        public ActionResult DetailsPost([Bind(Include = "kodasID")] Deposited deposited)
        {
            Session["flag"] = 0;
            if (TempData["natID"] == null)
            {
                return(Redirect("index"));
            }
            Koda koda = db.Kodas.Find(deposited.kodasID);

            deposited.NationalID = TempData["natID"] + "";
            deposited.fullName   = TempData["full"] + "";
            deposited.churchesID = koda.ChurchesID;
            deposited.AttendedOn = koda.Date;
            deposited.FatherID   = koda.fatherID;
            Koda k = db.Kodas.Where(m => m.Id == deposited.kodasID).FirstOrDefault();

            k.PeopleDeposited++;
            db.Entry(koda).State = EntityState.Modified;
            db.Depositeds.Add(deposited);
            db.SaveChanges();
            TempData["natID"] = null;
            TempData["dep"]   = "تم التسجيل بنجاح";
            return(Redirect("Create"));
        }