public int GetDisplayOrder(int parentId, int display, string cal)
        {
            int res = 0;

            if (cal == "none")
            {
                res = (int)db.CarPartCategories.Where(x => x.ParentID == parentId).Max(x => x.DisplayOrder) + 1;
            }
            if (cal == "minus")
            {
                for (int i = display - 1; i > 0; i--)
                {
                    CarPartCategory entity = db.CarPartCategories.SingleOrDefault(x => x.ParentID == parentId && x.DisplayOrder == i);
                    if (entity == null)
                    {
                        return(i);
                    }
                }
                return(0);
            }
            if (cal == "plus")
            {
                while (true)
                {
                    display++;
                    CarPartCategory entity = db.CarPartCategories.SingleOrDefault(x => x.ParentID == parentId && x.DisplayOrder == display);
                    if (entity == null)
                    {
                        return(display);
                    }
                }
            }
            return(res);
        }
        public bool?ChangeStatus(int Id)
        {
            CarPartCategory entity = db.CarPartCategories.Find(Id);

            entity.Status = !entity.Status;
            db.SaveChanges();
            return(entity.Status);
        }
        public JsonResult LoadCarPartCategoryDetail(int Id)
        {
            var             dao    = new CarPartCategoryDAO();
            CarPartCategory entity = dao.GetDetailByID(Id);

            return(Json(new
            {
                data = entity
            }, JsonRequestBehavior.AllowGet));
        }
        public int CreateEditCarPartCategory(CarPartCategory entity, string username)
        {
            if (entity.ID == 0)
            {
                entity.CreatedBy = username;

                entity.ModifiedBy   = username;
                entity.CreatedDate  = DateTime.Now;
                entity.ModifiedDate = DateTime.Now;
                db.CarPartCategories.Add(entity);
                try
                {
                    db.SaveChanges();
                    return(1);
                }
                catch (Exception)
                {
                    return(0);
                }
            }
            if (entity.ID != 0)
            {
                CarPartCategory car = db.CarPartCategories.Find(entity.ID);
                car.Name             = entity.Name;
                car.MetaTitle        = entity.MetaTitle;
                car.ParentID         = entity.ParentID;
                car.DisplayOrder     = entity.DisplayOrder;
                car.SeoTitle         = entity.SeoTitle;
                car.ModifiedBy       = username;
                car.ModifiedDate     = DateTime.Now;
                car.MetaKeywords     = entity.MetaKeywords;
                car.MetaDescriptions = entity.MetaDescriptions;
                car.Status           = entity.Status;
                if (car.ParentID != 0)
                {
                    IEnumerable <CarPartCategory> listCarPart = db.CarPartCategories.Where(x => x.ParentID == entity.ID);
                    int i = GetDisplayOrder(0, 0, "none") - 1;
                    foreach (var item in listCarPart)
                    {
                        item.ParentID     = 0;
                        item.DisplayOrder = i++;
                    }
                }
                try
                {
                    db.SaveChanges();
                    return(2);
                }
                catch (Exception)
                {
                    return(0);
                }
            }
            return(0);
        }
        public int DeleteCarPartCategory(int Id)
        {
            CarPartCategory entity = db.CarPartCategories.Find(Id);

            db.CarPartCategories.Remove(entity);
            try
            {
                db.SaveChanges();
                return(1);
            }
            catch (Exception)
            {
                return(0);
            }
        }
        public JsonResult CreateEditCarPartCategory(string strCarPartCategory)
        {
            var dao = new CarPartCategoryDAO();
            JavaScriptSerializer seriliazer = new JavaScriptSerializer();
            CarPartCategory      entity     = seriliazer.Deserialize <CarPartCategory>(strCarPartCategory);

            entity.MetaTitle = Common.convertToUnSign.convert(entity.Name);
            string username = (string)Session[Common.CommonConstants.USER_NAME];
            int    res      = dao.CreateEditCarPartCategory(entity, username);

            return(Json(new
            {
                result = res
            }, JsonRequestBehavior.AllowGet));
        }