public ActionResult DeleteConfirmed(int id)
        {
            try
            {


                Option option = db.Option.Find(id);
                var opv = db.OptionValue.Where(x => x.OptionID == option.OptionID);
                foreach (var s in opv)
                {
                    var set = db.SetValue.Where(x => x.OptionValueID == s.OptionValueID);
                    foreach (var item in set)
                    {
                        var rev = new RevisionHistory();
                        rev.Action = "Deleted";
                        rev.CreatedOn = item.ModifiedOn;
                        rev.ModifiedOn = DateTime.Now;
                        rev.ModifiedBy = User.Identity.Name;
                        rev.SystemName = item.OptionValue.Option.Lsystem.LsystemName;
                        rev.Option = item.OptionValue.Option.OptionName;
                        rev.Optionvalue = item.OptionValue.OptionVal;
                        rev.TCSetName = item.TcSet.SetName;
                        rev.SetValueID = item.SetValueID;
                        rev.InitialValue = item.Value;
                        db.RevisionHistory.Add(rev);
                        db.SetValue.Remove(item);
                    }
                    var cv = db.Config_OptionVal.Where(x => x.OptionValueID == s.OptionValueID);
                    foreach (var cvl in cv)
                        db.Config_OptionVal.Remove(cvl);
                    db.OptionValue.Remove(s);
                }
                db.Option.Remove(option);
                db.SaveChanges();
                return RedirectToAction("ViewOptions", new { id = option.LsystemID });
            }
            catch
            {
                return View("Error");
            }
        }
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {


                LsystemFamily lsystemFamily = db.LsystemFamily.Find(id);
                var sys = db.Lsystem.Where(x => x.LsystemFamilyID == lsystemFamily.LsystemFamilyID);
                foreach (var sy in sys)
                {
                    var cc = db.ConfigurationCollection.Where(x => x.LsystemID == sy.LsystemID);
                    foreach (var c in cc)
                    {
                        var cv = db.Config_OptionVal.Where(x => x.ConfigurationCollectionID == c.ConfigurationCollectionID);
                        foreach (var item in cv)
                            db.Config_OptionVal.Remove(item);
                        db.ConfigurationCollection.Remove(c);
                    }
                    var op = db.Option.Where(x => x.LsystemID == sy.LsystemID);
                    foreach (var o in op)
                    {
                        var opv = db.OptionValue.Where(x => x.OptionID == o.OptionID);
                        foreach (var ov in opv)
                        {
                            var set = db.SetValue.Where(x => x.OptionValueID == ov.OptionValueID);
                            foreach (var item in set)
                            {
                                var rev = new RevisionHistory();
                                rev.Action = "Deleted";
                                rev.CreatedOn = item.ModifiedOn;
                                rev.ModifiedOn = DateTime.Now;
                                rev.ModifiedBy = User.Identity.Name;
                                rev.SystemName = item.OptionValue.Option.Lsystem.LsystemName;
                                rev.Option = item.OptionValue.Option.OptionName;
                                rev.Optionvalue = item.OptionValue.OptionVal;
                                rev.TCSetName = item.TcSet.SetName;
                                rev.SetValueID = item.SetValueID;
                                rev.InitialValue = item.Value;
                                db.RevisionHistory.Add(rev);
                                db.SetValue.Remove(item);
                            }
                            db.OptionValue.Remove(ov);
                        }
                        db.Option.Remove(o);
                    }
                    
                        
                    db.Lsystem.Remove(sy);
                }

                db.LsystemFamily.Remove(lsystemFamily);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch(Exception e)
            {
                ViewBag.Error = e.Message;
                return View("Error");
            }
        }
        public string Edit(SetValue setValue, IPrincipal user=null)
        {
            try
            {

                var opv = db.OptionValue.FirstOrDefault(x => x.OptionValueID == setValue.OptionValueID);
                var q1 = db.TcSet.FirstOrDefault(x => x.TcSetID == setValue.TcSetID);
                if (ModelState.IsValid)
                {
                    var q = db.TcSet.FirstOrDefault(x => x.TcSetID == setValue.TcSetID);
                    var data = q.DataFormat.FormatType;
                    var precision = q.DataFormat.PrecisionDigits;
                    var scaling = q.DataFormat.ScalingDigits;
                    double val = 0;
                    double value;
                    string valuestring = setValue.Value;
                    bool flag = double.TryParse(setValue.Value, out value);
                    if (setValue.Value != "#" && data != "String" && setValue.Value != "?")
                    {
                        if (!flag)
                        {
                            return "Error";

                        }
                        if (precision != null && value >= 0)
                        {
                            int precision1 = precision ?? default(int);
                            if (precision != 0)
                                valuestring = Significant(value, precision1);
                            else
                                valuestring = setValue.Value;
                        }
                        else if (scaling != null && value >= 0)
                        {
                            int sca = scaling ?? default(int);
                            valuestring = Scaling(value, sca);
                        }

                    }
                    setValue.Value = valuestring;
                    var original = db.SetValue.Find(setValue.SetValueID);
                    bool modified = original.Value != setValue.Value;
                    if (modified)
                    {
                        //var old = db.RevisionHistory.FirstOrDefault(x => x.SetValueID == setValue.SetValueID);
                        var rev = new RevisionHistory();
                        rev.CreatedOn = original.ModifiedOn;
                        rev.ModifiedOn = DateTime.Now;
                        rev.ModifiedBy = user.Identity.Name;
                        rev.InitialValue = original.Value; ;
                        rev.Optionvalue = original.OptionValue.OptionVal;
                        rev.TCSetName = original.TcSet.SetName;
                        rev.Option = original.OptionValue.Option.OptionName;
                        rev.SystemName = original.OptionValue.Option.Lsystem.LsystemName;
                        rev.SetValueID = original.SetValueID;
                        rev.ModifiedValue = setValue.Value;
                        rev.Action = "Modified";
                        db.Entry(original).CurrentValues.SetValues(setValue);
                        db.RevisionHistory.Add(rev);
                    }
                    original.ModifiedOn = DateTime.Now;
                    original.ModifiedBy = user.Identity.Name;
                    db.Entry(original).State = EntityState.Modified;
                    db.Entry(original).Property("CreatedOn").IsModified = false;
                    db.Entry(original).Property("CreatedBy").IsModified = false;
                    db.Entry(original).Property("OptionValueID").IsModified = false;
                    db.Entry(original).Property("TcSetID").IsModified = false;

                    db.SaveChanges();
                    return "Success";
                }
                return "Error";
            }
            catch (Exception e)
            {
                ViewBag.Error = e.Message;
                return "Error";
            }

        }
        public ActionResult EditAll([Bind(Include = "SetValueID,Value,Status,TcSetID,OptionValueID")] List<SetValue> setValue)
        {
            try
            {


                int ovid1 = setValue[0].OptionValueID;
                var opv = db.OptionValue.FirstOrDefault(x => x.OptionValueID == ovid1);
                ViewBag.OptionValue = opv.OptionVal;
                ViewBag.Option = opv.Option.OptionName;
                ViewBag.Lsystem = opv.Option.Lsystem.LsystemName;
                ViewBag.FamilyName = opv.Option.Lsystem.LsystemFamily.FamilyName;
                ViewBag.id = opv.OptionValueID;
                int ovid = 0;
                bool error = false;
                double val;
                if (ModelState.IsValid)
                {
                    for (int i = 0; i < setValue.Count(); i++)
                    {
                        int setid = setValue[i].TcSetID;
                        if (db.TcSet.FirstOrDefault(x => x.TcSetID == setid).DataFormat.FormatType == "Number" && setValue[i].Value != "#" && setValue[i].Value!="?")
                        {
                            if (!double.TryParse(setValue[i].Value, out val))
                            {
                                error = true;
                                Expression<Func<List<SetValue>, string>> exp = x => x[i].Value;
                                string key = ExpressionHelper.GetExpressionText(exp);
                                ModelState.AddModelError(key, "Value can only be a number or #");
                                //int sd = setValue[i].OptionValueID;
                                //var ov = db.OptionValue.Include(x => x.Option).FirstOrDefault(x => x.OptionValueID == sd);
                                //var opid = ov.OptionID;
                                //var op = db.Option.Include(x => x.TechnicalCharacteristic).FirstOrDefault(x => x.OptionID == opid);
                                //var tcid = op.TechnicalCharacteristicID;
                                //var tc = db.TechnicalCharacteristic.Include(x => x.TcSets).FirstOrDefault(x => x.TechnicalCharacteristicID == tcid);
                                //var tcset = tc.TcSets;
                                //ViewBag.OptionValueID = new SelectList(db.OptionValue, "OptionValueID", "OptionVal", setValue.OptionValueID);
                            }
                        }
                     }

                    
                    if (error)
                    {
                        int count = setValue.Count();
                        for (int j = 0; j < count; j++)
                        {
                            int setid = setValue[j].TcSetID;
                            setValue[j].TcSet = db.TcSet.FirstOrDefault(x => x.TcSetID == setid);
                        }
                        return View(setValue);
                    }

                    foreach(var item in setValue)
                    {
                        var original = db.SetValue.Find(item.SetValueID);
                        if(original.Value!=item.Value)
                        {
                            var tcsetid = item.TcSetID;
                            var tcset = db.TcSet.FirstOrDefault(x => x.TcSetID == tcsetid);
                            var dataformat = tcset.DataFormat;
                            var precision = dataformat.PrecisionDigits;
                            var scaling = dataformat.ScalingDigits;
                            string valuestring = item.Value.ToString();
                            double value;
                            if(dataformat.FormatType!="String"&&item.Value!="#" && item.Value!="?")
                            {
                                bool flag = double.TryParse(item.Value, out value);
                                if (precision != null && value>=0)
                                {
                                    int precision1 = precision ?? default(int);
                                    if (precision != 0)
                                        valuestring = Significant(value, precision1);
                                    else
                                        valuestring = item.Value;
                                }
                                else if (scaling != null && value>=0)
                                {
                                    int sca = scaling ?? default(int);
                                    valuestring = Scaling(value, sca);
                                }
                            }

                            item.Value = valuestring;
                            
                        }
                        bool modified = item.Value != original.Value;
                        if (modified)
                        {
                            //var old = db.RevisionHistory.FirstOrDefault(x => x.SetValueID == setValue.SetValueID);
                            var rev = new RevisionHistory();
                            rev.CreatedOn = original.ModifiedOn;
                            rev.ModifiedOn = DateTime.Now;
                            rev.ModifiedBy = User.Identity.Name;
                            rev.InitialValue = original.Value; ;
                            rev.Optionvalue = original.OptionValue.OptionVal;
                            rev.TCSetName = original.TcSet.SetName;
                            rev.Option = original.OptionValue.Option.OptionName;
                            rev.SystemName = original.OptionValue.Option.Lsystem.LsystemName;
                            rev.SetValueID = original.SetValueID;
                            rev.ModifiedValue = item.Value;
                            rev.Action = "Modified";
                            db.Entry(original).CurrentValues.SetValues(item);
                            db.RevisionHistory.Add(rev);
                            original.ModifiedOn = DateTime.Now;
                            original.ModifiedBy = User.Identity.Name;
                            db.Entry(original).State = EntityState.Modified;
                            db.Entry(original).Property("CreatedOn").IsModified = false;
                            db.Entry(original).Property("CreatedBy").IsModified = false;
                            db.Entry(original).Property("OptionValueID").IsModified = false;
                            db.Entry(original).Property("TcSetID").IsModified = false;
                        }
                        

                        
                    }
                    db.SaveChanges();
                    return RedirectToAction("Index", "SetValues", new { id = ovid1 });

                }
                return View(setValue);
             }
            catch (Exception e)
            {
                ViewBag.Error = e.Message;
                return View("Error");
            }
            
           
        }
        public ActionResult Edit([Bind(Include = "SetValueID,Value,Status,TcSetID,OptionValueID,CreatedOn,CreatedBy,ModifiedOn,ModifiedBy")] SetValue setValue)
        {
            try
            {
                
                var opv = db.OptionValue.FirstOrDefault(x => x.OptionValueID == setValue.OptionValueID);
                ViewBag.OptionValue = opv.OptionVal;
                ViewBag.Option = opv.Option.OptionName;
                ViewBag.Lsystem = opv.Option.Lsystem.LsystemName;
                ViewBag.FamilyName = opv.Option.Lsystem.LsystemFamily.FamilyName;
                ViewBag.id = setValue.OptionValueID;
                var q1 = db.TcSet.FirstOrDefault(x => x.TcSetID == setValue.TcSetID);
                ViewBag.TcSet = q1.SetName;
                if (ModelState.IsValid)
                {
                    //if (db.SetValue.Any(x => x.TcSetID == setValue.TcSetID && x.OptionValueID == setValue.OptionValueID && x.SetValueID != setValue.SetValueID))
                    //{
                    //    ModelState.AddModelError("TcSetID", "Property already exists");
                    //    var ov = db.OptionValue.Include(x => x.Option).FirstOrDefault(x => x.OptionValueID == setValue.OptionValueID);
                    //    var opid = ov.OptionID;
                    //    var op = db.Option.Include(x => x.TechnicalCharacteristic).FirstOrDefault(x => x.OptionID == opid);
                    //    var tcid = op.TechnicalCharacteristicID;
                    //    var tc = db.TechnicalCharacteristic.Include(x => x.TcSets).FirstOrDefault(x => x.TechnicalCharacteristicID == tcid);
                    //    var tcset = tc.TcSets;
                    //    //ViewBag.OptionValueID = new SelectList(db.OptionValue, "OptionValueID", "OptionVal", setValue.OptionValueID);
                    //    ViewBag.TcSetID = new SelectList(tcset, "TcSetID", "SetName");
                    //    return View();
                    //}
                    var q = db.TcSet.FirstOrDefault(x => x.TcSetID == setValue.TcSetID);
                    var data = q.DataFormat.FormatType;
                    var precision = q.DataFormat.PrecisionDigits;
                    var scaling = q.DataFormat.ScalingDigits;
                    double val = 0;

                    ViewBag.val = val;

                    double value;
                    string valuestring =setValue.Value;
                    bool flag= double.TryParse(setValue.Value,out value);
                    if (setValue.Value != "#" && data != "String" && setValue.Value != "?")
                    {
                        if(!flag)
                        {
                            ModelState.AddModelError("Value", "Value can only be a number, # or ?");
                            return View(setValue);

                        }
                        if(precision!=null && value>=0)
                        {
                            int precision1 = precision ?? default(int);
                            if (precision != 0)
                                valuestring = Significant(value, precision1);
                            else
                                valuestring = setValue.Value;
                        }
                        else if(scaling!=null&& value>=0)
                        {
                            int sca = scaling ?? default(int);
                            valuestring = Scaling(value, sca);
                        }
                        
                    }
                    setValue.Value = valuestring;
                    var original = db.SetValue.Find(setValue.SetValueID);
                    bool modified = original.Value != setValue.Value;
                    if (modified)
                    {
                        //var old = db.RevisionHistory.FirstOrDefault(x => x.SetValueID == setValue.SetValueID);
                        var rev = new RevisionHistory();
                        rev.CreatedOn = original.ModifiedOn;
                        rev.ModifiedOn = DateTime.Now;
                        rev.ModifiedBy = User.Identity.Name;
                        rev.InitialValue = original.Value; ;
                        rev.Optionvalue = original.OptionValue.OptionVal;
                        rev.TCSetName = original.TcSet.SetName;
                        rev.Option = original.OptionValue.Option.OptionName;
                        rev.SystemName = original.OptionValue.Option.Lsystem.LsystemName;
                        rev.SetValueID = original.SetValueID;
                        rev.ModifiedValue = setValue.Value;
                        rev.Action = "Modified";
                        db.Entry(original).CurrentValues.SetValues(setValue);
                        db.RevisionHistory.Add(rev);
                    }
                    original.ModifiedOn = DateTime.Now;
                    
                    original.ModifiedBy = User.Identity.Name;
                    db.Entry(original).State = EntityState.Modified;
                    db.Entry(original).Property("CreatedOn").IsModified = false;
                    db.Entry(original).Property("CreatedBy").IsModified = false;
                    db.Entry(original).Property("OptionValueID").IsModified = false;
                    db.Entry(original).Property("TcSetID").IsModified = false;

                    db.SaveChanges();
                    return RedirectToAction("Index", "SetValues", new { id = setValue.OptionValueID });
                }
                ViewBag.OptionValueID = new SelectList(db.OptionValue.OrderBy(x=>x.OptionVal), "OptionValueID", "OptionVal", setValue.OptionValueID);
                return View(setValue);
            }
            catch(Exception e)
            {
                ViewBag.Error = e.Message;
                return View("Error");
            }
        }
        public ActionResult copy(int CopyID,int OptionValueID)
        {
            try
            {


                if (db.UserRight.FirstOrDefault(x => x.UserCode == User.Identity.Name).IsAdmin || db.UserRight.FirstOrDefault(x => x.UserCode == User.Identity.Name).IsEditor)
                {
                    //if (CopyID == null)
                    //{
                    //    ViewBag.Error = "A null parameter was passed to the function";
                    //    return View("Error");
                    //}
                    if (CopyID == OptionValueID)
                    {
                        ViewBag.id = OptionValueID;
                        ViewBag.Message = "Cannot copy Option value to itself. Select another Option value to copy";
                        return View("Empty");
                    }
                    var ovtobecopied = db.OptionValue.FirstOrDefault(x => x.OptionValueID == CopyID);
                    var ovtobecreated = db.OptionValue.FirstOrDefault(x => x.OptionValueID == OptionValueID);
                    var oldsetval = ovtobecopied.SetValue;
                    
                    
                    if(db.SetValue.Where(x=>x.OptionValueID==OptionValueID).Count()!=0)
                    {
                        ViewBag.id = OptionValueID;
                        ViewBag.Message = "Copy cannot be completed. There are a existing Property values for the Option value you tried to create";
                        return View("Empty");
                    }
                    
                    if (!db.SetValue.Any(x => x.OptionValueID == OptionValueID))
                    {


                        foreach (var item in ovtobecopied.SetValue)
                        {

                            var setVal = new SetValue
                            {
                                OptionValueID = OptionValueID,
                                Value = item.Value,
                                TcSet = item.TcSet,
                                TcSetID = item.TcSetID,
                                OptionValue = db.OptionValue.FirstOrDefault(x => x.OptionValueID == OptionValueID),
                                CreatedBy = User.Identity.Name,
                                ModifiedBy = User.Identity.Name,
                                CreatedOn = DateTime.Now,
                                ModifiedOn = DateTime.Now

                            };
                            db.SetValue.Add(setVal);
                            var rev = new RevisionHistory();
                            rev.CreatedOn = setVal.CreatedOn;
                            rev.ModifiedOn = setVal.ModifiedOn;
                            rev.ModifiedBy = setVal.ModifiedBy;
                            rev.Optionvalue = setVal.OptionValue.OptionVal;
                            rev.Option = setVal.OptionValue.Option.OptionName;
                            rev.SystemName = setVal.OptionValue.Option.Lsystem.LsystemName;
                            var p = db.TcSet.FirstOrDefault(x => x.TcSetID == setVal.TcSetID);
                            rev.TCSetName = p.SetName;
                            rev.InitialValue = setVal.Value;
                            rev.ModifiedValue = setVal.Value; ;
                            rev.SetValueID = setVal.SetValueID;
                            rev.Action = "Created";
                            db.RevisionHistory.Add(rev);
                            db.SaveChanges();
                        }
                    }

                    return RedirectToAction("Index", "SetValues", new { id = OptionValueID });
                }



                else
                    return View("AuthorizationError");
            }
            catch(Exception e)
            {
                ViewBag.Error = e.Message;
                return View("Error");
            }
        }
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {


                SetValue setValue = db.SetValue.Find(id);
                var rev = new RevisionHistory();
                rev.Action = "Deleted";
                rev.CreatedOn = setValue.ModifiedOn;
                rev.ModifiedOn = DateTime.Now;
                rev.ModifiedBy = User.Identity.Name;
                rev.SystemName = setValue.OptionValue.Option.Lsystem.LsystemName;
                rev.Option = setValue.OptionValue.Option.OptionName;
                rev.Optionvalue = setValue.OptionValue.OptionVal;
                rev.TCSetName = setValue.TcSet.SetName;
                rev.SetValueID = setValue.SetValueID;
                rev.InitialValue = setValue.Value;
                db.RevisionHistory.Add(rev);
                db.SetValue.Remove(setValue);
                db.SaveChanges();
                return RedirectToAction("Index", "SetValues", new { id = setValue.OptionValueID });
            }
            catch(Exception e)
            {
                ViewBag.Error = e.Message;
                return View("Error");
            }
        }
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {


                DataFormat dataFormat = db.DataFormat.Find(id);
                var tset = db.TcSet.Where(x => x.DataFormatID == dataFormat.DataFormatID);
                foreach (var s in tset)
                {
                    var set = db.SetValue.Where(x => x.TcSetID == s.TcSetID);
                    foreach (var item in set)
                    {
                        var rev = new RevisionHistory();
                        rev.Action = "Deleted";
                        rev.CreatedOn = item.ModifiedOn;
                        rev.ModifiedOn = DateTime.Now;
                        rev.ModifiedBy = User.Identity.Name;
                        rev.SystemName = item.OptionValue.Option.Lsystem.LsystemName;
                        rev.Option = item.OptionValue.Option.OptionName;
                        rev.Optionvalue = item.OptionValue.OptionVal;
                        rev.TCSetName = item.TcSet.SetName;
                        rev.SetValueID = item.SetValueID;
                        rev.InitialValue = item.Value;
                        db.RevisionHistory.Add(rev);
                        db.SetValue.Remove(item);
                    }
                    db.TcSet.Remove(s);
                }

                db.DataFormat.Remove(dataFormat);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch(Exception e)
            {
                ViewBag.Error = e.Message;
                return View("Error");
            }
        }
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {


                TechnicalCharacteristic technicalCharacteristic = db.TechnicalCharacteristic.Find(id);
                var tset = db.TcSet.Where(x => x.TechnicalCharacteristicID == technicalCharacteristic.TechnicalCharacteristicID);

                var op = db.Option.Where(x => x.TechnicalCharacteristicID == technicalCharacteristic.TechnicalCharacteristicID);
                foreach (var o in op)
                {
                    var opv = db.OptionValue.Where(x => x.OptionID == o.OptionID);
                    foreach (var ov in opv)
                    {
                        var set = db.SetValue.Where(x => x.OptionValueID == ov.OptionValueID);
                        foreach (var item in set)
                        {
                            var rev = new RevisionHistory();
                            rev.Action = "Deleted";
                            rev.CreatedOn = item.ModifiedOn;
                            rev.ModifiedOn = DateTime.Now;
                            rev.ModifiedBy = User.Identity.Name;
                            rev.SystemName = item.OptionValue.Option.Lsystem.LsystemName;
                            rev.Option = item.OptionValue.Option.OptionName;
                            rev.Optionvalue = item.OptionValue.OptionVal;
                            rev.TCSetName = item.TcSet.SetName;
                            rev.SetValueID = item.SetValueID;
                            rev.InitialValue = item.Value;
                            db.RevisionHistory.Add(rev);
                            db.SetValue.Remove(item);
                        }
                        var con = db.Config_OptionVal.Where(x => x.OptionValueID == ov.OptionValueID).ToList();
                        foreach (var c in con)
                            db.Config_OptionVal.Remove(c);
                        db.OptionValue.Remove(ov);
                    }
                    db.Option.Remove(o);
                }
                foreach (var s in tset)
                {
                    var viewprop = db.ViewsProperty.Where(x => x.TcSetID == s.TcSetID);
                    foreach (var vp in viewprop)
                        db.ViewsProperty.Remove(vp);
                    db.TcSet.Remove(s);
                }
                    
                db.TechnicalCharacteristic.Remove(technicalCharacteristic);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch(Exception e)
            {
                ViewBag.Error = e.Message;
                return View("Error");
            }
        }