public JsonResult UpdateTempItem(TempFloorTable table)
        {
            ViewBag.IsTemp = true;

            try
            {
                var tbl = Db.tabTempFloorTables.Find(table.FloorTableId);
                tbl.TableName = table.TableName;

                FloorTable copyTable = new FloorTable();
                CopyHelper.Copy(typeof(TempFloorTable), table, typeof(FloorTable), copyTable);

                tbl.TableDesign = this.RenderPartialViewToString("~/Views/Floor/GetFloorItemTemplate.cshtml", copyTable);
                tbl.Angle = table.Angle;
                //tbl.MinCover = table.MinCover;
                //tbl.HtmlId = table.HtmlId;
                //tbl.Shape = table.Shape;
                //tbl.Size = table.Size;
                //tbl.MaxCover = table.MaxCover;
                tbl.TTop = table.TTop;
                tbl.TBottom = table.TBottom;
                tbl.TLeft = table.TLeft;
                tbl.TRight = table.TRight;
                tbl.UpdatedOn = DateTime.UtcNow.ToDefaultTimeZone(User.Identity.GetDatabaseName());

                Db.Entry(tbl).State = EntityState.Modified;
                Db.SaveChanges();

                var totalTables = 0;
                var totalMaxCovers = 0;
                var totalMinCovers = 0;

                Db.tabTempFloorTables.GetFloorItemCount(tbl.FloorPlanId, out totalTables, out totalMinCovers, out totalMaxCovers);

                return Json(new
                {
                    Status = ResponseStatus.Success,
                    ItemId = tbl.FloorTableId,
                    totalTables = totalTables,
                    totalMaxCovers = totalMaxCovers,
                    totalMinCovers = totalMinCovers
                });
            }
            catch (Exception)
            {
                return Json(new { Status = ResponseStatus.Fail, ItemId = 0 });
            }
        }
        public JsonResult AddTempItem(TempFloorTable table)
        {
            ViewBag.IsTemp = true;

            try
            {
                table.HtmlId = "table" + Guid.NewGuid().ToString("N");
                table.TableName = "T-0";
                table.Angle = table.Angle;
                table.MinCover = table.MinCover;
                table.MaxCover = table.MaxCover;
                table.TTop = "150px";
                table.TLeft = "36px";
                table.TableDesign = " ";
                table.UpdatedOn = DateTime.UtcNow.ToDefaultTimeZone(User.Identity.GetDatabaseName());
                table.CreatedOn = DateTime.UtcNow.ToDefaultTimeZone(User.Identity.GetDatabaseName());

                Db.tabTempFloorTables.Add(table);
                Db.SaveChanges();

                var tblDB = Db.tabTempFloorTables.Include("FloorPlan").Where(t => t.FloorTableId == table.FloorTableId).Single();

                tblDB.TableName = this.AssignItemName(tblDB.FloorTableId, tblDB.Shape);

                FloorTable copyTable = new FloorTable();
                CopyHelper.Copy(typeof(TempFloorTable), table, typeof(FloorTable), copyTable);

                tblDB.TableDesign = this.RenderPartialViewToString("~/Views/Floor/GetFloorItemTemplate.cshtml", copyTable);

                Db.Entry(tblDB).State = EntityState.Modified;
                Db.SaveChanges();

                var totalTables = 0;
                var totalMaxCovers = 0;
                var totalMinCovers = 0;

                Db.tabTempFloorTables.GetFloorItemCount(tblDB.FloorPlanId, out totalTables, out totalMinCovers, out totalMaxCovers);

                return Json(new
                {
                    Status = ResponseStatus.Success,
                    ItemId = tblDB.FloorTableId,
                    totalTables = totalTables,
                    totalMaxCovers = totalMaxCovers,
                    totalMinCovers = totalMinCovers,
                    Template = tblDB.TableDesign,
                    HtmlId = table.HtmlId
                });
            }
            catch (Exception)
            {
                return Json(new { Status = ResponseStatus.Fail, ItemId = 0 });
            }
        }
        public JsonResult ChangeTempItem(TempFloorTable table, bool SaveChanges = false, bool CancelChanges = false)
        {
            ModelState.Clear();

            ViewBag.IsSelected = true;
            ViewBag.IsTemp = true;

            var totalTables = 0;
            var totalMaxCovers = 0;
            var totalMinCovers = 0;

            try
            {
                if (!CancelChanges)
                {
                    FloorTable copyTable = new FloorTable();
                    CopyHelper.Copy(typeof(TempFloorTable), table, typeof(FloorTable), copyTable);

                    if (SaveChanges)
                    {
                        ViewBag.IsSelected = false;

                        table.TableDesign = this.RenderPartialViewToString("~/Views/Floor/GetFloorItemTemplate.cshtml", copyTable);

                        var tblDB = Db.tabTempFloorTables.Find(table.FloorTableId);

                        tblDB.TableName = table.TableName;
                        tblDB.Shape = table.Shape;
                        tblDB.Size = table.Size;
                        tblDB.Angle = table.Angle;
                        tblDB.TableDesign = table.TableDesign;
                        tblDB.MinCover = table.MinCover;
                        tblDB.MaxCover = table.MaxCover;
                        tblDB.UpdatedOn = DateTime.UtcNow.ToDefaultTimeZone(User.Identity.GetDatabaseName());

                        Db.Entry(table).State = EntityState.Modified;
                        Db.SaveChanges();
                    }
                    else
                    {
                        table.TableDesign = this.RenderPartialViewToString("~/Views/Floor/GetFloorItemTemplate.cshtml", copyTable);
                    }

                    Db.tabTempFloorTables.GetFloorItemCount(table.FloorPlanId, out totalTables, out totalMinCovers, out totalMaxCovers);

                    var response = new
                    {
                        Status = ResponseStatus.Success,
                        HtmlId = table.HtmlId,
                        Template = table.TableDesign,
                        totalTables = totalTables,
                        totalMaxCovers = totalMaxCovers,
                        totalMinCovers = totalMinCovers,
                        IsUpdated = SaveChanges
                    };

                    return Json(response, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    ViewBag.IsSelected = false;

                    var origionalTable = Db.tabTempFloorTables.Find(table.FloorTableId);

                    FloorTable copyOrigionalTable = new FloorTable();
                    CopyHelper.Copy(typeof(TempFloorTable), origionalTable, typeof(FloorTable), copyOrigionalTable);

                    origionalTable.TableDesign = this.RenderPartialViewToString("~/Views/Floor/GetFloorItemTemplate.cshtml", copyOrigionalTable);

                    Db.tabTempFloorTables.GetFloorItemCount(table.FloorPlanId, out totalTables, out totalMinCovers, out totalMaxCovers);

                    var response = new
                    {
                        Status = ResponseStatus.Success,
                        HtmlId = origionalTable.HtmlId,
                        Template = origionalTable.TableDesign,
                        totalTables = totalTables,
                        totalMaxCovers = totalMaxCovers,
                        totalMinCovers = totalMinCovers,
                        IsUpdated = SaveChanges
                    };

                    return Json(response, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception)
            {
                Db.Dispose();

                Db = new UsersContext();

                ViewBag.IsSelected = false;

                var origionalTable = Db.tabTempFloorTables.Find(table.FloorTableId);

                FloorTable copyOrigionalTable = new FloorTable();
                CopyHelper.Copy(typeof(TempFloorTable), origionalTable, typeof(FloorTable), copyOrigionalTable);

                origionalTable.TableDesign = this.RenderPartialViewToString("~/Views/Floor/GetFloorItemTemplate.cshtml", copyOrigionalTable);

                Db.tabTempFloorTables.GetFloorItemCount(table.FloorPlanId, out totalTables, out totalMinCovers, out totalMaxCovers);

                var response = new
                {
                    Status = ResponseStatus.Fail,
                    HtmlId = origionalTable.HtmlId,
                    Template = origionalTable.TableDesign,
                    totalTables = totalTables,
                    totalMaxCovers = totalMaxCovers,
                    totalMinCovers = totalMinCovers,
                    IsUpdated = SaveChanges
                };

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