public ActionResult TaoDuAn_Buoc2(DuAnModel DA)
        {
            try
            {
                var duan = db.EntityDuAns.Where(p => p.Id == DA.Id).First();
                ViewBag.DanhMuc = db.EntityDanhMucs.Where(p => p.IdRoot == 1).ToList();

                #region Chỉnh sửa hình ảnh (chung cho toàn bộ)
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    HttpPostedFileBase hpf = Request.Files[i];

                    #region phan sua khi chay du an  // hình ảnh - phần thưởng - tiếu sử

                    if (hpf.FileName != "" && i == 0)
                    {
                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(hpf);
                        duan.HinhAnh = encodestring;
                        db.SubmitChanges();
                        var path = Path.Combine(Server.MapPath("~/Content/Images/DuAn"), encodestring);
                        hpf.SaveAs(path);
                    }

                    #endregion

                    if (duan.TrangThai < 1)
                    {
                        if (hpf.FileName != "" && i == 1)
                        {
                            var ava = db.EntityUsers.Where(p => p.Email == Request.Cookies["ftusername"].Value).First();

                            if (ava.Avatar != null && ava.Avatar.IndexOf("ava0.jpg") == -1)
                            {
                                string[] link = ava.Avatar.Split('/');
                                string fileToDelete = Path.Combine(Server.MapPath("~/Content/Images/Avatar"), link[link.Count() - 1]); // file hinh cu
                                System.IO.File.Delete(fileToDelete);
                            }
                            ImageHelper imgHelper = new ImageHelper();
                            string encodestring = imgHelper.encodeImageFile(hpf);
                            string[] mang = Request.Url.AbsoluteUri.ToString().Split('/');
                            string url = mang[0] + "//" + mang[2];
                            ava.Avatar = url + "/Content/Images/Avatar/" + encodestring;
                            imgHelper.ResizeStream(180, hpf.InputStream, Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Content\\Images\\Avatar\\", encodestring));
                            //  ava.Avatar = "http://localhost:41372/Content/Images/Avatar/" + encodestring;
                            db.SubmitChanges();
                            //var path = Path.Combine(Server.MapPath("~/Content/Images/Avatar"), encodestring);
                            //hpf.SaveAs(path);
                            Response.Cookies["ftavatar"].Value = ava.Avatar;
                        }
                        if (hpf.FileName != "" && i == 2)
                        {
                            ImageHelper imgHelper = new ImageHelper();
                            string encodestring = imgHelper.encodeImageFile(hpf);
                            imgHelper.ResizeStream(180, hpf.InputStream, Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Content\\Images\\TaiKhoan\\", encodestring));
                            //var path = Path.Combine(Server.MapPath("~/Content/Images/TaiKhoan"), encodestring);
                            //hpf.SaveAs(path);
                            DA.cnHinhAnh = encodestring;
                        }
                        if (hpf.FileName != "" && i == 3)
                        {
                            ImageHelper imgHelper = new ImageHelper();
                            string encodestring = imgHelper.encodeImageFile(hpf);
                            imgHelper.ResizeStream(180, hpf.InputStream, Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Content\\Images\\TaiKhoan\\", encodestring));
                            //var path = Path.Combine(Server.MapPath("~/Content/Images/TaiKhoan"), encodestring);
                            //hpf.SaveAs(path);
                            DA.ctHinhAnh = encodestring;
                        }
                    }
                }
                #endregion

                try
                {
                    if(DA.flag != "XemTruoc")
                        duan.Flag = DA.flag;
                    #region Cập nhật dữ liệu TAB MÔ TẢ
                    if (DA.flag == "MoTa")
                    {
                        duan.NoiDung = DA.NoiDung;
                        duan.LinkVideo = DA.LinkVideo;
                        duan.RuiRo = DA.RuiRo;
                    }

                    #endregion

                    #region xử lý trạng thái XEM TRƯỚC
                    if (DA.flag == "XemTruoc")
                    {

                    }
                    #endregion

                    //if (duan.TrangThai < 1)
                    //{
                    #region Cập nhật dữ liệu TAB THÔNG TIN
                    if (DA.flag == "ThongTin")
                    {
                        duan.LoaiHinhGoiVon = DA.Loaihinhkeugoivon;
                        duan.TenDuAn = DA.TenDuAn;
                        duan.TienDauTuMucTieu = DA.TienDauTuMucTieu;
                        duan.ThongTinNgan = DA.ThongTinNgan;

                        duan.IdDanhMuc = DA.IdDanhMuc;
                        duan.TienDauTuHienTai = 0;

                        duan.ThongTinWhichPerson = DA.whichpreson;
                        if (DA.SoNgay != null && DA.SoNgay > 0)
                        {
                            duan.SoNgay = DA.SoNgay;
                            int i = Convert.ToInt16(DA.SoNgay);
                            duan.ThoiGianKetThuc = DateTime.Now.AddDays(i);
                        }
                        else
                        {
                            if (DA.ThoiGianKetThuc != null)
                            {
                                duan.ThoiGianKetThuc = Convert.ToDateTime(DA.ThoiGianKetThuc);
                                DateTime i = Convert.ToDateTime(DA.ThoiGianKetThuc);
                                // duan.SoNgay = Math.Abs(i.Subtract(DateTime.Now).Days);
                                duan.SoNgay = null;
                            }
                        }

                        //#region  // Phần thêm mới danh mục, hiện tại chưa dùng
                        //try
                        //{
                        //    var dm = db.EntityChiTietDanhMucs.Where(p => p.IdDuAn == DA.Id).ToList();
                        //    if (dm.Count() > 0)
                        //    {
                        //        dm.First().IdDanhMuc = Convert.ToInt16(DA.IdDanhMuc);
                        //        db.SubmitChanges();
                        //    }
                        //    else
                        //    {
                        //        EntityChiTietDanhMuc ct = new EntityChiTietDanhMuc();
                        //        ct.IdDanhMuc = Convert.ToInt16(DA.IdDanhMuc);
                        //        ct.IdDuAn = DA.Id;
                        //        db.EntityChiTietDanhMucs.InsertOnSubmit(ct);
                        //        db.SubmitChanges();
                        //    }
                        //}
                        //catch { }
                        //#endregion
                     }
                    //}
                    db.SubmitChanges();
                    #endregion

                    #region Cập nhật dữ liệu TAB PHẦN THƯỞNG
                    try
                    {
                        if (DA.flag == "PhanThuong")
                        {
                            if (DA.PhanThuong != null)
                            {
                                string[] arr1 = DA.PhanThuong.Split('#');
                                var phanthuong = db.EntityPhanThuongs.Where(g => g.IdDuAn == DA.Id).ToList();
                                for (int i = 0; i < DA.vt_pt_edit; i++)
                                {
                                    string[] arr2 = arr1[i].Split('^');
                                    for (int j = 0; j < arr2.Count() / 4; j = j + 4)
                                    {
                                        EntityPhanThuong pt = phanthuong.ElementAt(i);
                                        if (db.EntityDauTus.Any(g => g.IdPhanthuong == pt.Id) == true)
                                            continue;
                                        pt.IdDuAn = DA.Id;
                                        int tienhotro = 0;
                                        int.TryParse(arr2[j], out tienhotro);
                                        pt.TienHoTro = tienhotro;
                                        pt.HinhThucGiao = arr2[2];
                                        pt.NgayGiao = arr2[1];
                                        if (arr2[3] != "")
                                        {
                                            int SoLuong = 0;
                                            int.TryParse(arr2[3], out SoLuong);
                                            pt.SoLuong = SoLuong;
                                        }
                                        pt.NoiDung = arr2[4];

                                        //db.EntityPhanThuongs.InsertOnSubmit(pt);
                                        db.SubmitChanges();

                                    }
                                }
                                for (int i = DA.vt_pt_edit; i < arr1.Count() - 1; i++)
                                {
                                    string[] arr2 = arr1[i].Split('^');
                                    for (int j = 0; j < arr2.Count() / 4; j = j + 4)
                                    {
                                        EntityPhanThuong pt = new EntityPhanThuong();
                                        pt.IdDuAn = DA.Id;
                                        int tienhotro = 0;
                                        int.TryParse(arr2[j], out tienhotro);
                                        pt.TienHoTro = tienhotro;
                                        pt.HinhThucGiao = arr2[2];
                                        pt.NgayGiao = arr2[1];
                                        if (arr2[3] != "")
                                        {
                                            int SoLuong = 0;
                                            int.TryParse(arr2[3], out SoLuong);
                                            pt.SoLuong = SoLuong;
                                        }
                                        pt.NoiDung = arr2[4];

                                        db.EntityPhanThuongs.InsertOnSubmit(pt);
                                    }
                                }
                            }
                            db.SubmitChanges();
                        }
                    }
                    catch { }
                    #endregion

                    #region Cập nhật dữ liệu TAB GIỚI THIỆU
                    try
                    {
                        if (DA.flag == "GioiThieu")
                        {
                            var user = db.EntityUsers.Where(p => p.Id == Convert.ToInt16(Request.Cookies["ftid"].Value)).First();
                            user.HoTen = DA.HoTen;
                            user.GioiThieu = DA.GioiThieu;
                            user.DiaDiem = DA.ChoO;
                            user.Website = DA.WebCaNhan;
                            db.SubmitChanges();
                        }
                    }
                    catch { }
                    #endregion

                    #region Cập nhật dữ liệu TAB TÀI KHOẢN
                    try
                    {
                        if (DA.flag == "TaiKhoan")
                        {
                            //if (duan.TrangThai < 1 )
                            //{
                            EntityThongTinTaiKhoan thongtintaikhoan = db.EntityThongTinTaiKhoans.Where(g => g.Id == duan.IdThongTinTaiKhoan).FirstOrDefault();
                            if (thongtintaikhoan != null)
                            {
                                if (DA.whichpreson == "Cá nhân")
                                {
                                    thongtintaikhoan.Col1 = DA.cnName;
                                    thongtintaikhoan.Col2 = DA.cnYear;
                                    thongtintaikhoan.Col3 = DA.cnMonth;
                                    thongtintaikhoan.Col4 = DA.cnDay;
                                    thongtintaikhoan.Col5 = DA.cnHomeaddress1;
                                    thongtintaikhoan.Col6 = DA.cnHomeaddress2;
                                    thongtintaikhoan.Col7 = DA.cnHinhAnh != null && DA.cnHinhAnh != "" ? DA.cnHinhAnh : thongtintaikhoan.Col7;
                                }
                                else if (DA.whichpreson == "Doanh nghiệp")
                                {
                                    thongtintaikhoan.Col1 = DA.ctTenCongTy;
                                    thongtintaikhoan.Col2 = DA.ctDiaChi;
                                    thongtintaikhoan.Col3 = DA.ctMST;
                                    thongtintaikhoan.Col4 = DA.ctSDTBan;
                                    thongtintaikhoan.Col5 = DA.ctTenNguoiDaiDien;
                                    thongtintaikhoan.Col6 = DA.ctChucVu;
                                    thongtintaikhoan.Col7 = DA.ctHinhAnh != null && DA.ctHinhAnh != "" ? DA.ctHinhAnh : thongtintaikhoan.Col7;
                                }
                                db.SubmitChanges();
                            }
                            //}
                        }
                    }
                    catch { }
                    #endregion
                }
                catch { }

                #region Xuất Bản
                try
                {
                    if (DA.TrangThai == 1 && DA.CheckGioiThieu && DA.CheckMoTa && DA.CheckPhanThuong && DA.CheckThongTin && DA.CheckTaiKhoan)
                    {
                        duan.Flag = "XemTruoc";
                        //duan.ThoiGianBatDau =
                        duan.TrangThai = DA.TrangThai;
                        duan.ThoigianSummit = DateTime.Now;
                        string[] mang = Request.Url.AbsoluteUri.ToString().Split('/');
                        string url = mang[0] + "//" + mang[2];
                        string linkdanhmucduan = url + "/du-an?option=-category=" + duan.IdDanhMuc + "-blend=0-";
                        MailHelper.SendMail_DangKyDuAn(duan.EntityUser.HoTen, duan.EntityUser.Email, linkdanhmucduan);
                        db.SubmitChanges();
                    }
                }
                catch { }
                #endregion

                // return View(DuAnModel.LayModel(duan.Id));
                return RedirectToAction("ChiTietDuAn", "DuAn", new { Title = Utilities.Paste_Int64(Utilities.Encode(DA.TenDuAn), DA.Id.Value) });
            }
            catch
            { return View(DA); }
        }
        public ActionResult SuaThongTin(AccountModel Mtnew)
        {
            try
            {
                //if (ModelState.IsValid)
                //{
                dbFirstStepDataContext db = new dbFirstStepDataContext();
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    HttpPostedFileBase hpf = Request.Files[i];
                    if (hpf.FileName != "")
                    {
                        var ava = db.EntityUsers.Where(p => p.Email == Request.Cookies["ftusername"].Value).First();

                        if (ava.Avatar != null && ava.Avatar.IndexOf("ava0.jpg") == -1)
                        {
                            string[] link = ava.Avatar.Split('/');
                            string fileToDelete = Path.Combine(Server.MapPath("~/Content/Images/Avatar"), link[link.Count() - 1]); // file hinh cu
                            System.IO.File.Delete(fileToDelete);
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(hpf);
                        string[] mang = Request.Url.AbsoluteUri.ToString().Split('/');
                        string url = mang[0] + "//" + mang[2];
                        ava.Avatar = url + "/Content/Images/Avatar/" + encodestring;
                        imgHelper.ResizeStream(180, hpf.InputStream, Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Content\\Images\\Avatar\\", encodestring));
                        //  ava.Avatar = "http://localhost:41372/Content/Images/Avatar/" + encodestring;
                        db.SubmitChanges();
                        //var path = Path.Combine(Server.MapPath("~/Content/Images/Avatar"), encodestring);
                        //hpf.SaveAs(path);
                        Response.Cookies["ftavatar"].Value = ava.Avatar;

                        //ImageHelper imgHelper = new ImageHelper();
                        //string encodestring = imgHelper.encodeImageFile(hpf);
                        //ava.Avatar = "/Content/Images/Avatar/" + encodestring;
                        //db.SubmitChanges();
                        //if (encodestring == "!")
                        //    return RedirectToAction("Error", "Home", new { errorMsg = "Can't upload Images" });
                        //var path = Path.Combine(Server.MapPath("~/Content/Images/Avatar"), encodestring);
                        //hpf.SaveAs(path);
                        //Response.Cookies["ftavatar"].Value = ava.Avatar;
                    }
                }

                var query = from p in db.EntityUsers
                            where p.Email == Request.Cookies["ftusername"].Value
                            select p;
                if (query.Count() == 0)
                {
                    return RedirectToAction("Logout", "Account");
                }
                else
                {
                    query.First().HoTen = Mtnew.HoTen;
                    query.First().GioiThieu = Mtnew.GioiThieu;
                    query.First().DiaDiem = Mtnew.DiaDiem;
                    query.First().Website = Mtnew.Website;
                    string[] mang = Mtnew.Profile.Split('/');
                    string kt = mang[mang.Count() - 1].ToString().Replace("user/", "");
                    kt = Utilities.Encode(kt);
                    if (db.EntityUsers.Any(g => g.VanityURL == kt) == false)
                        query.First().VanityURL = kt;

                    db.SubmitChanges();
                    return RedirectToAction("SuaThongTin", "Account");
                    //  View(Mtnew);
                }
                //}
            }
            catch
            {
                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });
            }

            //string[] mang1 = Request.Url.AbsoluteUri.ToString().Split('/');
            //string url1 = mang1[0] + "//" + mang1[2];
            //ViewBag.Link = url1 + "/user/";

            //return View(Mtnew);
        }