public async Task<ActionResult> Edit(ToChucChinhSachCNTT toChucChinhSachCNTT)
        {
            var user = S4T_HaTinhBase.GetUserSession();
            if (user == null) return RedirectToAction("Login", "Account", new { returnUrl = Request.Url.PathAndQuery });
            var per = S4T_HaTinhBase.CheckPermission(Request.RequestContext.RouteData.GetRequiredString("controller"));
            if (per != PermissionType.Write) return Content(ExceptionViewer.GetMessage("UPDATE_NOT_PERMISSION"));

            if (ModelState.IsValid)
            {
                var mess = CheckReportStatus(user, TrangThaiNhapLieu.Sua);
                if (String.IsNullOrEmpty(mess))
                {
                    using (var context = new S4T_HaTinhEntities())
                    {
                        toChucChinhSachCNTT.Success = (byte)TrangThaiNhapLieu.DaGui;
                        //haTangKyThuatCNTT.TruongNhapLai = string.Empty; // Xóa hết các yêu cầu nhập lại dữ liệu
                        context.Entry(toChucChinhSachCNTT).State = EntityState.Modified;
                        await context.SaveChangesAsync();
                    }

                    // Đổi trạng thái nhập liệu trong bảng lịch nhập liệu
                    var objLichNhap = db.Ht_LichNhapLieu.FirstOrDefault(o => o.DonVi_ID == toChucChinhSachCNTT.DonVi_ID
                                                                          && o.PhanHe_ID == objChucNang.PhanHeChucNang_ID
                                                                          && o.ChucNang_ID != TrangThaiNhapLieu.PheDuyet);
                    objLichNhap.ChucNang_ID = TrangThaiNhapLieu.DaGui;
                    db.Entry(objLichNhap).State = EntityState.Modified;
                    await db.SaveChangesAsync();

                    return RedirectToAction("Index");
                }

                return Content(mess);
            }

            return View(toChucChinhSachCNTT);
        }
        // GET: ToChucChinhSachCNTTs/Create
        public ActionResult Create()
        {
            var user = S4T_HaTinhBase.GetUserSession();
            if (user == null) return RedirectToAction("Login", "Account", new { returnUrl = Request.Url.PathAndQuery });
            var per = S4T_HaTinhBase.CheckPermission(Request.RequestContext.RouteData.GetRequiredString("controller"));
            if (per != PermissionType.Write) return Content(ExceptionViewer.GetMessage("UPDATE_NOT_PERMISSION"));

            GetViewBag(user.DonVi_ID);
            
            var mess = CheckReportStatus(user, TrangThaiNhapLieu.ThemMoi);
            if (String.IsNullOrEmpty(mess))
            {
                ToChucChinhSachCNTT obj = new ToChucChinhSachCNTT();
                obj.Success = (byte)TrangThaiNhapLieu.ThemMoi;
                obj.DonVi_ID = user.DonVi_ID;
                TempData[objChucNang.ControllerName + "_" + User.Identity.GetUserId()] = null;

                return View(obj);
            }

            return Content(mess);
        }
        //[MultipleButton(Name = "action", Argument = "Create")]
        public async Task<ActionResult> Create(ToChucChinhSachCNTT toChucChinhSachCNTT)
        {
            var user = S4T_HaTinhBase.GetUserSession();
            if (user == null) return RedirectToAction("Login", "Account", new { returnUrl = Request.Url.PathAndQuery });
            var per = S4T_HaTinhBase.CheckPermission(Request.RequestContext.RouteData.GetRequiredString("controller"));
            if (per != PermissionType.Write) return Content(ExceptionViewer.GetMessage("UPDATE_NOT_PERMISSION"));

            if (ModelState.IsValid)
            {
                var mess = CheckReportStatus(user, TrangThaiNhapLieu.ThemMoi);
                if (String.IsNullOrEmpty(mess))
                {
                    try
                    {
                        var objLichNhap = db.Ht_LichNhapLieu.FirstOrDefault(o => o.DonVi_ID == toChucChinhSachCNTT.DonVi_ID
                                                                          && o.PhanHe_ID == objChucNang.PhanHeChucNang_ID
                                                                          && o.ChucNang_ID != TrangThaiNhapLieu.PheDuyet);

                        // Đổi trạng thái nhập liệu
                        toChucChinhSachCNTT.Success = (byte)TrangThaiNhapLieu.DaGui;
                        toChucChinhSachCNTT.LichNhap_ID = objLichNhap.LichNhap_ID; // Add LichNhap_ID vào báo cáo
                        db.ToChucChinhSachCNTT.Add(toChucChinhSachCNTT);
                        await db.SaveChangesAsync();

                        // Lưu file vào thư mục dữ liệu chính
                        if (TempData[objChucNang.ControllerName + "_" + User.Identity.GetUserId()] != null)
                        {
                            var listObjFile = (List<Ht_FileDinhKem>)TempData[objChucNang.ControllerName + "_" + User.Identity.GetUserId()];

                            var pathSource = ConfigurationManager.AppSettings["FolderUploadPath"];
                            if (string.IsNullOrEmpty(pathSource)) return JavaScript("Không tìm thấy đường dẫn file");

                            var duongDan = pathSource + "/ToChucChinhSachCNTT/NewUpload/" + User.Identity.GetUserId();
                            var duongDanVatLy = pathSource + "/ToChucChinhSachCNTT";
                            string[] filesPath = System.IO.Directory.GetFiles(duongDan);

                            if (filesPath.Any())
                            {
                                foreach (var item in listObjFile)
                                {
                                    item.BaoCao_ID = toChucChinhSachCNTT.ToChucChinhSachCNTT_ID;
                                    item.PhanHeChucNang_ID = objChucNang.PhanHeChucNang_ID;
                                    item.DonVi_ID = toChucChinhSachCNTT.DonVi_ID;
                                    item.HieuLuc = TrangThai.HoatDong;

                                    var fileName = toChucChinhSachCNTT.ToChucChinhSachCNTT_ID + "_" + item.LoaiVanBan_ID + "_" + User.Identity.GetUserId() + "_" +  item.TenHienThi;
                                    var pathFile = duongDanVatLy + "/" + fileName;

                                    //copy file vào thư mục chính
                                    System.IO.File.Copy(item.DuongDan, pathFile, true);

                                    item.DuongDan = pathFile;
                                    item.TenFile = fileName;
                                    item.TrangThai = TrangThai.HoatDong;
                                    db.Ht_FileDinhKem.Add(item);
                                }
                            }
                            //await db.SaveChangesAsync();
                        }
                        else
                        {
                            //return JavaScript("Không tìm thấy file");
                        }

                        // Đổi trạng thái nhập liệu trong bảng lịch nhập liệu
                        objLichNhap.ChucNang_ID = TrangThaiNhapLieu.DaGui;
                        db.Entry(objLichNhap).State = EntityState.Modified;
                        await db.SaveChangesAsync();

                        // Xóa dữ liệu trong thư mục tạm
                        DeleteTempUploadFile();
                        return RedirectToAction("Index");
                    }
                    catch (Exception ex)
                    {
                        var exv = new ExceptionViewer(ex);
                        return Json(new { msg = exv.GetErrorMessage(ex.Message) });
                    }                 
                }

                return Content(mess);
            }

            return View(toChucChinhSachCNTT);
        }
        public async Task<ActionResult> Details(int? id, int? donVi_ID, int? lichNhap_ID)
        {
            var returnUrl = Request.UrlReferrer;
            if (returnUrl != null)
                ViewBag.returnUrl = returnUrl.ToString();

            var user = S4T_HaTinhBase.GetUserSession();
            if (user == null) if (user == null) return RedirectToAction("Login", "Account", new { returnUrl = Request.Url.PathAndQuery });
            var per = S4T_HaTinhBase.CheckPermission(Request.RequestContext.RouteData.GetRequiredString("controller"));
            if (per == PermissionType.Deny)
            {
                // Kiểm tra thêm quyền là Thẩm định báo cáo ?
                per = S4T_HaTinhBase.CheckPermissionAdmin(LoaiBaoCao.ChinhSach);
                if (per == PermissionType.Deny) return Content(ExceptionViewer.GetMessage("VIEW_NOT_PERMISSION"));
            }

            ToChucChinhSachCNTT toChucChinhSachCNTT = new ToChucChinhSachCNTT();
            if (id == null)
            {
                if (lichNhap_ID == null)
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                else
                    toChucChinhSachCNTT = await db.ToChucChinhSachCNTT.FirstOrDefaultAsync(o => o.LichNhap_ID == lichNhap_ID
                                                                                                && o.DonVi_ID == donVi_ID);
            }
            else
                toChucChinhSachCNTT = await db.ToChucChinhSachCNTT.FindAsync(id);

            if (toChucChinhSachCNTT == null)
                return HttpNotFound();
                
            // Lấy danh sách file đã đc up lên
            TempData[objChucNang.ControllerName + "_" + User.Identity.GetUserId()] = db.Ht_FileDinhKem.Where(o => o.PhanHeChucNang_ID == objChucNang.PhanHeChucNang_ID && o.BaoCao_ID == toChucChinhSachCNTT.ToChucChinhSachCNTT_ID).ToList();
            
            return View(toChucChinhSachCNTT);
        }