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 TaoDuAn()
 {
     if (Request.Cookies["ftid"] != null)
     {
         ViewBag.DanhMuc = db.EntityDanhMucs.Where(g => g.IdRoot == 1).ToList();
         DuAnModel daModel = new DuAnModel();
         return View(daModel);
     }
     else
     {
         return RedirectToAction("Login","Account", new { trolai = "tao-du-an"});
     }
 }
        public ActionResult TaoDuAn(DuAnModel daModel)
        {
            try
            {
                ViewBag.DanhMuc = db.EntityDanhMucs.Where(g => g.IdRoot == 1).ToList();
                if (ModelState.IsValid)
                {
                    int idlogin = Convert.ToInt16(Request.Cookies["ftid"].Value);

                    EntityThongTinTaiKhoan thongtin = new EntityThongTinTaiKhoan();
                    db.EntityThongTinTaiKhoans.InsertOnSubmit(thongtin);
                    db.SubmitChanges();

                    EntityDuAn duan = new EntityDuAn();
                    duan.LoaiHinhGoiVon = daModel.Loaihinhkeugoivon;
                    duan.TenDuAn = daModel.TenDuAn;
                    duan.IdDanhMuc = daModel.IdDanhMuc;
                    duan.TienDauTuMucTieu = daModel.TienDauTuMucTieu;
                    duan.IdUser = idlogin;
                    duan.TrangThai = -1;
                    duan.ThongTinWhichPerson = daModel.whichpreson;
                    duan.IdThongTinTaiKhoan = thongtin.Id;
                    db.EntityDuAns.InsertOnSubmit(duan);
                    db.SubmitChanges();
                    HoatDongModel.GetListFriend_Sent_HoatDong(idlogin, duan.Id, 3);// tạo dự án
                    return RedirectToAction("TaoDuAn_Buoc2", "DuAn", new { Id = duan.Id });
                }
            }
            catch
            {

                //return RedirectToAction("Index", "Error", new { id = 500 });
            }
            return View(daModel);
        }
        // phần tạo tạo tài khoản
        public static DuAnModel LayModel(int Id)
        {
            DuAnModel Ban = new DuAnModel();
            dbFirstStepDataContext db = new dbFirstStepDataContext();
            EntityDuAn tnew = db.EntityDuAns.Where(p => p.Id == Id).First();

            var thongtin = db.EntityUsers.Where(p => p.Id == Convert.ToInt16(HttpContext.Current.Request.Cookies["ftid"].Value)).First();

            Ban.flag = tnew.Flag == null ? "ThongTin": tnew.Flag;

            Ban.TrangThai = tnew.TrangThai == null ? 0 : tnew.TrangThai.Value;
            Ban.Id = tnew == null ? 0 : tnew.Id;

            #region Thông Tin
            Ban.IdDanhMuc = tnew.IdDanhMuc == null ? 0 : tnew.IdDanhMuc;
            Ban.Loaihinhkeugoivon = tnew.LoaiHinhGoiVon == null ? 0 : tnew.LoaiHinhGoiVon;
            Ban.TenDuAn = tnew.TenDuAn == null ? string.Empty : tnew.TenDuAn;
            Ban.HinhAnh = tnew.HinhAnh == null ? string.Empty : tnew.HinhAnh;
            Ban.ThongTinNgan = tnew.ThongTinNgan == null ? string.Empty : tnew.ThongTinNgan;
            Ban.TienDauTuMucTieu = tnew.TienDauTuMucTieu == null ? 0 : tnew.TienDauTuMucTieu;
            Ban.ThoiGianKetThuc = tnew.ThoiGianKetThuc == null ? DateTime.Now : tnew.ThoiGianKetThuc.Value;
            Ban.SoNgay = tnew.SoNgay == null ? 0 : tnew.SoNgay;
            Ban.CheckGioiThieu = false;

            if (Ban.IdDanhMuc != 0 && Ban.TenDuAn != string.Empty && Ban.HinhAnh != string.Empty && Ban.ThongTinNgan != string.Empty && Ban.TienDauTuMucTieu != 0 && (Ban.SoNgay != 0 || (Ban.ThoiGianKetThuc > DateTime.Now)))
            {
                Ban.CheckThongTin = true;
            }
            #endregion

            #region Phần thưởng
            var phanthuong = db.EntityPhanThuongs.Where(g=>g.IdDuAn== tnew.Id).ToList();
            Ban.vt_pt_edit = phanthuong.Count();
            Ban.CheckPhanThuong = true;

            foreach(var item_phanthuong in phanthuong)
            {
                if (item_phanthuong.TienHoTro == 0 || item_phanthuong.NoiDung == "" || item_phanthuong.NgayGiao == "" || item_phanthuong.HinhThucGiao == "")
                {
                    Ban.CheckPhanThuong = false;
                    break;
                }
            }
            #endregion

            #region Mô Tả
            Ban.LinkVideo = tnew.LinkVideo == null ? string.Empty : tnew.LinkVideo;
            Ban.NoiDung = tnew.NoiDung == null ? string.Empty : tnew.NoiDung;
            Ban.RuiRo = tnew.RuiRo == null ? string.Empty : tnew.RuiRo;
            Ban.CheckMoTa = false;

            if (Ban.NoiDung != string.Empty && Ban.RuiRo != string.Empty)
            {
                Ban.CheckMoTa = true;
            }
            #endregion

            #region Giới Thiệu Bản Thân
            Ban.ChoO = thongtin.DiaDiem == null ? string.Empty : thongtin.DiaDiem;
            Ban.WebCaNhan = thongtin.Website == null ? string.Empty : thongtin.Website;
            Ban.GioiThieu = thongtin.GioiThieu == null ? string.Empty : thongtin.GioiThieu;
            Ban.CheckGioiThieu = false;

            if (Ban.ChoO != string.Empty && Ban.WebCaNhan != string.Empty && Ban.GioiThieu != string.Empty)
            {
                Ban.CheckGioiThieu = true;
            }
            #endregion

            #region Tài Khoản
            Ban.TKPayoo = thongtin.TKPayoo == null ? string.Empty : thongtin.TKPayoo;
            Ban.whichpreson = tnew.ThongTinWhichPerson == null ? string.Empty : tnew.ThongTinWhichPerson;
            if (tnew.IdThongTinTaiKhoan > 0)
            {
                EntityThongTinTaiKhoan thongtintaikhoan = db.EntityThongTinTaiKhoans.Where(g=>g.Id== tnew.IdThongTinTaiKhoan).FirstOrDefault();
                if (thongtintaikhoan != null && Ban.whichpreson == "Cá nhân")
                {
                    Ban.cnName = thongtintaikhoan.Col1;
                    Ban.cnYear = thongtintaikhoan.Col2;
                    Ban.cnMonth = thongtintaikhoan.Col3;
                    Ban.cnDay = thongtintaikhoan.Col4;
                    Ban.cnHomeaddress1 = thongtintaikhoan.Col5;
                    Ban.cnHomeaddress2 = thongtintaikhoan.Col6;
                    Ban.cnHinhAnh = thongtintaikhoan.Col7;
                }
                if (thongtintaikhoan != null && Ban.whichpreson == "Doanh nghiệp")
                {
                    Ban.ctTenCongTy = thongtintaikhoan.Col1;
                    Ban.ctDiaChi = thongtintaikhoan.Col2;
                    Ban.ctMST = thongtintaikhoan.Col3;
                    Ban.ctSDTBan = thongtintaikhoan.Col4;
                    Ban.ctTenNguoiDaiDien = thongtintaikhoan.Col5;
                    Ban.ctChucVu = thongtintaikhoan.Col6;
                    Ban.ctHinhAnh = thongtintaikhoan.Col7;
                }
            }
            if (Ban.TKPayoo != String.Empty)
            {
                Ban.CheckTaiKhoan = true;
            }
            #endregion

            return Ban;
        }