private async Task <CreateEditHopDongLaoDongVM> FeedSomeDataToCreateEditVM(CreateEditHopDongLaoDongVM model)
        {
            var mauHopDongs = await mauHopDongRepository.FindAll();

            model.MauHopDongs = mauHopDongs.Select(q => new SelectListItem
            {
                Text  = q.TenMauHopDong,
                Value = q.MaMauHopDong
            });

            return(model);
        }
        // GET: HopDongLaoDongController/Create
        public async Task <ActionResult> Create(string employeeId)
        {
            var model = new CreateEditHopDongLaoDongVM();

            model = await FeedSomeDataToCreateEditVM(model);

            model.NgayKyKet               = DateTime.Now;
            model.NgayBatDau              = DateTime.Now;
            model.NgayKetThuc             = DateTime.Now;
            model.MaNhanVienChuTheHopDong = employeeId;
            model.NhanVienChuTheHopDong   = mapper.Map <EmployeeVM>(await userManager.FindByIdAsync(employeeId));

            return(View(model));
        }
        private string UploadHopDongLaoDong(CreateEditHopDongLaoDongVM model)
        {
            string uniqueFileName = null;

            if (model.FileHopDongScan != null)
            {
                string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "hopDongLaoDongs");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + model.FileHopDongScan.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    model.FileHopDongScan.CopyTo(fileStream);
                }
            }
            return(uniqueFileName);
        }
        public async Task <ActionResult> Create(CreateEditHopDongLaoDongVM model)
        {
            try
            {
                model = await FeedSomeDataToCreateEditVM(model);

                model.NgayKyKet             = DateTime.Now;
                model.NgayBatDau            = DateTime.Now;
                model.NgayKetThuc           = DateTime.Now;
                model.NhanVienChuTheHopDong = mapper.Map <EmployeeVM>(await userManager.FindByIdAsync(model.MaNhanVienChuTheHopDong));
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                var hopDongLaoDong = mapper.Map <HopDongLaoDong>(model);
                hopDongLaoDong.NgayGuiBanScan        = DateTime.Now;
                hopDongLaoDong.MaNhanVienGuiBanScan  = (await userManager.GetUserAsync(User)).Id;
                hopDongLaoDong.MaHopDong             = Guid.NewGuid().ToString();
                hopDongLaoDong.NhanVienChuTheHopDong = null;
                hopDongLaoDong.ViTriLuuBanScan       = UploadHopDongLaoDong(model);

                if (hopDongLaoDong.ViTriLuuBanScan == null)
                {
                    ModelState.AddModelError("", "File Scan Hợp đồng lao động cần được chọn");
                    return(View(model));
                }

                var isSuccess = await hopDongLaoDongRepository.Create(hopDongLaoDong);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Error while creating record on database");
                    return(View(model));
                }



                return(RedirectToAction(nameof(Index), new { id = model.MaNhanVienChuTheHopDong }));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Something went wrong");
                return(View(model));
            }
        }
        public async Task <ActionResult> Edit(CreateEditHopDongLaoDongVM model)
        {
            try
            {
                model = await FeedSomeDataToCreateEditVM(model);

                model.NhanVienChuTheHopDong = mapper.Map <EmployeeVM>(await userManager.FindByIdAsync(model.MaNhanVienChuTheHopDong));

                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                var hopDong = mapper.Map <HopDongLaoDong>(model);

                var uriMauHopDongFile = UploadHopDongLaoDong(model);
                if (uriMauHopDongFile != null)
                {
                    var oldMauHopDongName = hopDong.ViTriLuuBanScan;
                    hopDong.ViTriLuuBanScan = uriMauHopDongFile;
                    DeleteFileHopDong(oldMauHopDongName);
                }
                hopDong.MaNhanVienChinhSuaLanCuoi = userManager.GetUserAsync(HttpContext.User).Result.Id;
                hopDong.ThoiGianChinhSuaLanCuoi   = DateTime.Now;
                hopDong.NhanVienChuTheHopDong     = null;

                var isSuccess = await hopDongLaoDongRepository.Update(hopDong);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something Went Wrong...");
                    return(View(model));
                }

                return(RedirectToAction(nameof(Index), new { id = model.MaNhanVienChuTheHopDong }));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Something Went Wrong...");
                return(View(model));
            }
        }