public ActionResult SelectedContactsFields(LayoutsViewModel objViewlayouts)
        {
            Account accountObj = (Account)Session["account"];
            User userObj = (User)Session["user"];
            CCFolder folderObj = (CCFolder)Session["folderDetail"];

            var objvalues = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(objViewlayouts.FieldValues);
            var objfield = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(objViewlayouts.FieldNames);

            var allfolderfields = CCFieldRepository.CCFolderFields.Where(aid => aid.FolderID == objViewlayouts.FolderID & aid.AccountGUID == accountObj.AccountGUID);
            var fieldvalues = CCFieldValueRepository.CCFieldValues.Where(cid => cid.ItemID == objViewlayouts.ContactID & cid.AccountGUID == accountObj.AccountGUID);
            var notes = "";
            int i = 0;
            foreach (var item in objvalues)
            {
                string itemValue = item;
                if (allfolderfields.Count() > 0 && fieldvalues.Count() > 0)
                {
                    string fieldname = "";
                    fieldname = objfield[i];
                    if (fieldname == "Start" || fieldname == "End")
                    {
                        string format = "yyyy-MM-dd HH:mm";
                        DateTime startEndTime = DateTimeOffset.Parse(item).UtcDateTime;
                        itemValue = startEndTime.ToString(format);
                    }
                    if(fieldname == "Notes")
                    {
                        notes = itemValue;
                    }
                    if (fieldname != "")
                    {
                        var fieldid = allfolderfields.FirstOrDefault(fname => fname.FieldName == fieldname).FieldID;
                        CCFieldValue objFieldValue = new CCFieldValue();
                        if (fieldvalues.FirstOrDefault(fid => fid.FieldID == fieldid) != null)
                        {
                            var valueid = fieldvalues.FirstOrDefault(fid => fid.FieldID == fieldid).ValueID;
                            objFieldValue.ValueID = valueid;
                        }
                        else
                        {
                            objFieldValue.LastUpdated = DateTime.UtcNow;
                            objFieldValue.ValueID = 0;
                            objFieldValue.FieldID = fieldid;
                            objFieldValue.ItemID = objViewlayouts.ContactID;
                        }
                        objFieldValue.Value = itemValue;
                        objFieldValue.AccountGUID = accountObj.AccountGUID;
                        CCFieldValueRepository.SaveFieldValues(objFieldValue);
                    }

                }

                i++;
            }
            long contactid = objViewlayouts.ContactID;
            if (contactid != 0)
            {
                AddDedupeViewModel dedupe = new AddDedupeViewModel();
                foreach (var field in allfolderfields.ToList())
                {
                    if (folderObj != null)
                    {
                        if (folderObj.Type == 1)
                        {
                            if (field.FieldCaption == "First Name") { dedupe.FirstName = fieldvalues.ToList().Find(fid => fid.FieldID == field.FieldID).Value; }
                            if (field.FieldCaption == "Middle Name") { dedupe.MiddleName = fieldvalues.ToList().Find(fid => fid.FieldID == field.FieldID).Value; }
                            if (field.FieldCaption == "Last Name") { dedupe.LastName = fieldvalues.ToList().Find(fid => fid.FieldID == field.FieldID).Value; }
                            if (field.FieldCaption == "Company") { dedupe.CompanyName = fieldvalues.ToList().Find(fid => fid.FieldID == field.FieldID).Value; }
                            if (field.FieldCaption == "Email Address") { dedupe.Email = fieldvalues.ToList().Find(fid => fid.FieldID == field.FieldID).Value; }
                        }
                        else if (folderObj.Type == 2)
                        {
                            if (field.FieldCaption == "Subject") { dedupe.Subject = fieldvalues.ToList().Find(fid => fid.FieldID == field.FieldID).Value; }
                            if (field.FieldCaption == "Start Time") { dedupe.StartDateTime = fieldvalues.ToList().Find(fid => fid.FieldID == field.FieldID).Value; }
                            if (field.FieldCaption == "End Time") { dedupe.EndDateTime = fieldvalues.ToList().Find(fid => fid.FieldID == field.FieldID).Value; }
                        }
                    }
                }

                CCItems contact = new CCItems();
                contact.ItemID = contactid;
                if (folderObj.Type == 1) { contact.DeDupeValue = dedupe.FirstName + "|" + dedupe.MiddleName + "|" + dedupe.LastName + "|" + dedupe.CompanyName + "|" + dedupe.Email; }
                else if (folderObj.Type == 2)
                {
                    contact.DeDupeValue = dedupe.Subject + "|" + dedupe.StartDateTime + "|" + dedupe.EndDateTime;
                    contact.Notes = notes;
                    contact.TextBody = notes;
                }
                bool res = CCItemRepository.UpdateContact(contact);
            }

            return RedirectToAction("ViewLayout", "Folder", new { id = objViewlayouts.ContactID, fid = objViewlayouts.FolderID, pid = objViewlayouts.PageID });
        }
        public long ImportInputContacts(long fid, string aguid, string path, string fileExtension, long maxItemImportCount)
        {
            if (inputdatas == null)
                inputdatas = new DataInputHelper(path, fileExtension);

            List<CCFolderField> FieldsByFolderID = null;
            FieldsByFolderID = CCFieldRepository.CCFolderFields.Where(id => id.FolderID == fid).ToList();
            DataTable valus = inputdatas.GetImportExcel();
            bool _readheader = true;
            IsFieldValueMatch = false;
            var _availablevalue = new List<Tuple<long, bool>>();

            var totalRowCount = (valus.Rows.Count)-1;

            if (totalRowCount <= maxItemImportCount)
            {
                foreach (DataRow row in valus.Rows)
                {
                    AddDedupeViewModel dedupe = new AddDedupeViewModel();
                    int _fieldcount = 0;
                    long contectID = 0;
                    string notes = "";
                    if (!_readheader && IsFieldValueMatch)
                    {
                        contectID = CCItemRepository.CreateContact(fid, aguid);
                    }
                    foreach (DataColumn col in valus.Columns)
                    {
                        string _colname = row[col].ToString();
                        if (_readheader) // run when it header
                        {
                            // set correct Header to Field name
                            var reponse = FieldsByFolderID.Find(name => name.FieldName == _colname);
                            if (reponse != null)
                            {
                                _availablevalue.Add(new Tuple<long, bool>(reponse.FieldID, true));
                                IsFieldValueMatch = true;
                            }
                            else
                            {
                                _availablevalue.Add(new Tuple<long, bool>(0, false));
                            }

                        }
                        else
                        { // reu when it columns value
                            if (_availablevalue[_fieldcount].Item2)
                            {
                                CCFieldValue objFieldValue = new CCFieldValue();
                                objFieldValue.FieldID = _availablevalue[_fieldcount].Item1;
                                if (_colname == String.Empty) objFieldValue.Value = String.Empty;
                                else objFieldValue.Value = _colname;
                                objFieldValue.ItemID = contectID;
                                objFieldValue.LastUpdated = DateTime.Now.ToUniversalTime();
                                objFieldValue.AccountGUID = aguid;
                                ObjFieldValues.Add(objFieldValue);

                                // update dedupe
                                var fieldName = FieldsByFolderID.Find(id => id.FieldID == _availablevalue[_fieldcount].Item1).FieldCaption;
                                if (fieldName == "First Name") { dedupe.FirstName = _colname; }
                                if (fieldName == "Middle Name") { dedupe.MiddleName = _colname; }
                                if (fieldName == "Last Name") { dedupe.LastName = _colname; }
                                if (fieldName == "Company") { dedupe.CompanyName = _colname; }
                                if (fieldName == "Email Address") { dedupe.Email = _colname; }
                                // End update dedupe

                                if(fieldName == "Notes")
                                    notes = _colname;

                            }

                            _fieldcount++;

                        }
                    }
                    bool res = UpdateContact(dedupe, contectID, 1, notes);
                    _readheader = false;

                }

                var savedfields = CCFieldValueRepository.SaveFieldsObjValues(ObjFieldValues);

                if (IsFieldValueMatch == false) NumberOfContacts = 0;
                else NumberOfContacts = valus.Rows.Count;

                return NumberOfContacts;
            }
            else
            {
                return -2;
            }
        }
        public CCFieldValue RevertChangeToFieldValues(CCFieldValue FieldValueObj, CCHistoryLog HistLogObj)
        {
            CCFieldValue dbEntry = context.CCFieldValues.Find(FieldValueObj.ValueID);
            if (dbEntry != null)
            {
                CCHistoryLog HistoryLog = new CCHistoryLog();
                HistoryLog.AccountGUID = FieldValueObj.AccountGUID;
                HistoryLog.Date = DateTime.Now;
                HistoryLog.FieldID = dbEntry.FieldID;
                HistoryLog.ItemID = dbEntry.ItemID;
                HistoryLog.NewValue = FieldValueObj.Value;
                HistoryLog.OldValue = HistLogObj.NewValue;
                HistoryLog.Source = "Web";
                HistoryLog.Action = "Update";
                HistoryLog.ConnectionID = 0;
                context.CCHistoryLog.Add(HistoryLog);

                dbEntry.Value = FieldValueObj.Value;

                context.SaveChanges();
            }
            return FieldValueObj;
        }
        public bool ImportSingleContact(AddContactViewModel objContact, int type,string accountGUID, string timeZone)
        {
            List<string> fieldValus = objContact.FolderValues.Split('|').ToList();
            List<CCFieldValue> ObjFieldValues = new List<CCFieldValue>();
            List<CCFieldValue> savedfields = new List<CCFieldValue>();
            AddDedupeViewModel dedupe = new AddDedupeViewModel();
            DateTime startTime;
            DateTime endTime;
            string notes = "";

            long contectID = CCItemRepository.CreateContact(objContact.FolderID, accountGUID);
            if (contectID > 0)
            {
                List<CCFolderField> folderFields = new List<CCFolderField>();
                folderFields = CCFieldRepository.CCFolderFields.Where(fid => fid.FolderID == objContact.FolderID & fid.AccountGUID==accountGUID).ToList();

                int i = 0;
                foreach (var field in fieldValus)
                {
                    if (field != "")
                    {
                        var fieldName = folderFields[i].FieldCaption;
                        if (type == 1)
                        {
                            if (fieldName == "First Name") { dedupe.FirstName = field; }
                            if (fieldName == "Middle Name") { dedupe.MiddleName = field; }
                            if (fieldName == "Last Name") { dedupe.LastName = field; }
                            if (fieldName == "Company") { dedupe.CompanyName = field; }
                            if (fieldName == "Email Address") { dedupe.Email = field; }
                        }
                        else
                        {
                            if (fieldName == "Subject") { dedupe.Subject = field; }
                            if (fieldName == "Start Time")
                            {
                                string format = "yyyy-MM-dd HH:mm";
                                startTime = DateTime.Parse(field);
                                dedupe.StartDateTime = ConvertLocaltoUTC(startTime, timeZone).ToString(format);
                            }
                            if (fieldName == "End Time")
                            {
                                string format = "yyyy-MM-dd HH:mm";
                                //endTime = DateTimeOffset.Parse(field).UtcDateTime;
                                //dedupe.EndDateTime = endTime.ToString(format);
                                endTime = DateTime.Parse(field);
                                dedupe.EndDateTime = ConvertLocaltoUTC(endTime, timeZone).ToString(format);
                            }
                        }

                        if (fieldName == "Notes")
                            notes = field;

                        var fieldID = folderFields[i].FieldID;
                        i++;

                        CCFieldValue objFieldValue = new CCFieldValue();
                        objFieldValue.FieldID = fieldID;
                        if (fieldName == "Start Time") { objFieldValue.Value = dedupe.StartDateTime; }
                        else if (fieldName == "End Time") { objFieldValue.Value = dedupe.EndDateTime; }
                        else
                        {
                            if (field == " ") objFieldValue.Value = String.Empty;
                            else objFieldValue.Value = field;
                        }
                        objFieldValue.ItemID = contectID;
                        objFieldValue.LastUpdated = DateTime.UtcNow;
                        objFieldValue.AccountGUID = accountGUID;
                        ObjFieldValues.Add(objFieldValue);
                    }
                }
                savedfields = CCFieldValueRepository.SaveFieldsObjValues(ObjFieldValues);
            }
            if (savedfields.Count() > 0)
            {
                bool res = UpdateContact(dedupe, contectID, type, notes);
                return true;
            }
            else return false;
        }
        public CCFieldValue SaveFieldValues(CCFieldValue FieldValueObj)
        {
            if (FieldValueObj.ValueID == 0)
            {
                context.CCFieldValues.Add(FieldValueObj);
                //context.SaveChanges();

                CCHistoryLog HistoryLog = new CCHistoryLog();
                HistoryLog.AccountGUID = FieldValueObj.AccountGUID;
                HistoryLog.Date = DateTime.Now;
                HistoryLog.FieldID = FieldValueObj.FieldID;
                HistoryLog.ItemID = FieldValueObj.ItemID;
                HistoryLog.NewValue = FieldValueObj.Value;
                HistoryLog.OldValue = "";
                HistoryLog.Source = "Web";
                HistoryLog.Action = "Insert";
                HistoryLog.ConnectionID = 0;
                context.CCHistoryLog.Add(HistoryLog);
                context.SaveChanges();
            }
            else
            {
                CCFieldValue dbEntry = context.CCFieldValues.Find(FieldValueObj.ValueID);
                if (dbEntry != null)
                {
                    if (dbEntry.Value != FieldValueObj.Value)
                    {
                        CCHistoryLog HistoryLog = new CCHistoryLog();
                        HistoryLog.AccountGUID = FieldValueObj.AccountGUID;
                        HistoryLog.Date = DateTime.Now;
                        HistoryLog.FieldID = dbEntry.FieldID;
                        HistoryLog.ItemID = dbEntry.ItemID;
                        HistoryLog.NewValue = FieldValueObj.Value;
                        HistoryLog.OldValue = dbEntry.Value;
                        HistoryLog.Source = "Web";
                        HistoryLog.Action = "Update";
                        HistoryLog.ConnectionID = 0;
                        context.CCHistoryLog.Add(HistoryLog);
                        //context.SaveChanges();

                        dbEntry.LastUpdated = DateTime.UtcNow;
                    }
                    dbEntry.Value = FieldValueObj.Value;
                    context.SaveChanges();
                }

            }
            return FieldValueObj;
        }