// GET: CarLicences/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(RedirectToAction("errorPage", "home"));
            }
            CarLicence carLicence = db.CarLicences.Find(id);

            if (carLicence == null)
            {
                return(RedirectToAction("errorPage", "home"));
            }
            ViewBag.CitizenId = new SelectList(db.Citizens.Where(a => a.citizen_isDeleted != true), "citizen_id", "citizen_national_id", carLicence.CitizenId);

            ViewBag.CarId = new SelectList(db.Cars.Where(a => a.Is_Deleted != true), "Id", "CarName", carLicence.CarId);

            if (Session["lang"] != null)
            {
                if (Session["lang"].ToString().Equals("ar-EG"))
                {
                    ViewBag.CarId = new SelectList(db.Cars.Where(a => a.Is_Deleted != true), "Id", "CarNameArabic", carLicence.CarId);
                }
            }
            return(View(carLicence));
        }
        public IHttpActionResult PutCarLicence(int id, CarLicence carLicence)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != carLicence.Id)
            {
                return BadRequest();
            }

            db.Entry(carLicence).State = System.Data.Entity.EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CarLicenceExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        public ActionResult DeleteConfirmed(int id)
        {
            CarLicence carLicence = db.CarLicences.Find(id);

            carLicence.Is_Deleted = true;
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public IHttpActionResult PostCarLicence(CarLicence carLicence)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.CarLicences.Add(carLicence);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = carLicence.Id }, carLicence);
        }
        public IHttpActionResult DeleteCarLicence(int id)
        {
            CarLicence carLicence = db.CarLicences.Find(id);
            if (carLicence == null)
            {
                return NotFound();
            }

            db.CarLicences.Remove(carLicence);
            db.SaveChanges();

            return Ok(carLicence);
        }
        // GET: CarLicences/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(RedirectToAction("errorPage", "home"));
            }
            CarLicence carLicence = db.CarLicences.Find(id);

            if (carLicence == null)
            {
                return(RedirectToAction("errorPage", "home"));
            }
            return(View(carLicence));
        }
        public ActionResult Create(CarLicence carLicence, string checkEndDate, string arkam, string hroof)
        {
            if (ModelState.IsValid)
            {
                var data = db.CarLicences.Where(a => a.CarCode == arkam + hroof && a.Is_Deleted != true).SingleOrDefault();

                if (data == null)
                {
                    carLicence.StartDate = DateTime.Now;


                    string codeee = arkam + hroof;
                    carLicence.CarCode = codeee;

                    if (checkEndDate == "1")
                    {
                        carLicence.EndDate = DateTime.Now.AddDays(3 * 365);
                    }
                    else
                    {
                        carLicence.EndDate = DateTime.Now.AddDays(365);
                    }


                    db.CarLicences.Add(carLicence);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                else
                {
                    ViewBag.CitizenId    = new SelectList(db.Citizens, "citizen_id", "citizen_national_id", carLicence.CitizenId);
                    ViewBag.errorMessage = Servicely.Languages.Language.thisCodeIsAlreadyTaken;
                    ViewBag.CarId        = new SelectList(db.Cars.Where(a => a.Is_Deleted != true), "Id", "CarName", carLicence.CarId);

                    if (Session["lang"] != null)
                    {
                        if (Session["lang"].ToString().Equals("ar-EG"))
                        {
                            ViewBag.CarId = new SelectList(db.Cars.Where(a => a.Is_Deleted != true), "Id", "CarNameArabic", carLicence.CarId);
                        }
                    }

                    return(View(carLicence));
                }
            }

            ViewBag.CitizenId = new SelectList(db.Citizens, "citizen_id", "citizen_national_id", carLicence.CitizenId);
            return(View(carLicence));
        }
        public ActionResult Edit(CarLicence carLicence, string arkam, string hroof)
        {
            if (ModelState.IsValid)
            {
                var data = db.CarLicences.Where(a => a.Id != carLicence.Id && a.Is_Deleted != true);
                foreach (var item in data)
                {
                    if (item.CarCode == carLicence.CarCode)
                    {
                        ViewBag.CitizenId    = new SelectList(db.Citizens.Where(a => a.citizen_isDeleted != true), "citizen_id", "citizen_national_id", carLicence.CitizenId);
                        ViewBag.errorMessage = Servicely.Languages.Language.thisCodeIsAlreadyTaken;


                        ViewBag.CitizenId = new SelectList(db.Citizens.Where(a => a.citizen_isDeleted != true), "citizen_id", "citizen_national_id");

                        ViewBag.CarId = new SelectList(db.Cars.Where(a => a.Is_Deleted != true), "Id", "CarName");

                        if (Session["lang"] != null)
                        {
                            if (Session["lang"].ToString().Equals("ar-EG"))
                            {
                                ViewBag.CarId = new SelectList(db.Cars.Where(a => a.Is_Deleted != true), "Id", "CarNameArabic");
                            }
                        }



                        return(View(carLicence));
                    }
                }
                string cod = arkam + hroof;
                var    old = db.CarLicences.Find(carLicence.Id);
                old.CarModel      = carLicence.CarModel;
                old.CitizenId     = carLicence.CitizenId;
                old.CarNameArabic = carLicence.CarNameArabic;
                old.CarName       = carLicence.CarName;
                old.CarCode       = cod;

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.CitizenId = new SelectList(db.Citizens.Where(a => a.citizen_isDeleted != true), "citizen_id", "citizen_national_id", carLicence.CitizenId);
            return(View(carLicence));
        }