public async Task <IActionResult> AddEdit(int?id)
        {
            TourcostDTO tourcost = new TourcostDTO();

            if (id > 0)
            {
                tourcost.Tourcost = await _tourcostCrudService.GetAsync(id);

                tourcost.TourcostDetail = _tourcostDetailCrudService.GetAll(p => p.TourcostId == id).ToList();
            }

            ViewBag.Guide     = new SelectList(await _guideCrudService.GetAllAsync(), "Id", "Name");
            ViewBag.Sector    = new SelectList(await _sectorCrudService.GetAllAsync(), "Id", "Name");
            ViewBag.HotelCatA = new SelectList(await _hotelCrudService.GetAllAsync(p => p.Category == 'A'), "Id", "Code");
            ViewBag.HotelCatB = new SelectList(await _hotelCrudService.GetAllAsync(p => p.Category == 'B'), "Id", "Code");
            ViewBag.HotelCatC = new SelectList(await _hotelCrudService.GetAllAsync(p => p.Category == 'C'), "Id", "Code");

            return(View(tourcost));
        }
        public async Task <IActionResult> AddEdit(TourcostDTO tourcostDTO)
        {
            try
            {
                _unitOfWork.BeginTransaction();

                var user = _userManager.GetUserAsync(HttpContext.User).Result;
                if (tourcostDTO.Tourcost.Id > 0)
                {
                    await EditTourcost(tourcostDTO, user);
                }
                else
                {
                    tourcostDTO.Tourcost.MealType  = tourcostDTO.Tourcost.MealType ?? "None";
                    tourcostDTO.Tourcost.CreatedBy = user.Id;
                    var tourCostId = await _tourcostCrudService.InsertAsync(tourcostDTO.Tourcost);

                    foreach (var detail in tourcostDTO.TourcostDetail)
                    {
                        detail.TourcostId = tourCostId;
                        detail.CreatedBy  = user.Id;

                        await _tourcostDetailCrudService.InsertAsync(detail);
                    }
                }

                _unitOfWork.Commit();
            }
            catch (Exception exception)
            {
                _unitOfWork.Rollback();
                throw exception;
            }

            return(RedirectToAction(nameof(Index)));
        }
        private async Task EditTourcost(TourcostDTO tourcostDTO, IdentityUser user)
        {
            var tourcost = await _tourcostCrudService.GetAsync(tourcostDTO.Tourcost.Id);

            tourcost.Days                   = tourcostDTO.Tourcost.Days;
            tourcost.ClientName             = tourcostDTO.Tourcost.ClientName;
            tourcost.MinPAX                 = tourcostDTO.Tourcost.MinPAX;
            tourcost.MaxPAX                 = tourcostDTO.Tourcost.MaxPAX;
            tourcost.Guide                  = tourcostDTO.Tourcost.Guide;
            tourcost.IsLuxury               = tourcostDTO.Tourcost.IsLuxury;
            tourcost.MealType               = tourcostDTO.Tourcost.MealType;
            tourcost.Category1              = tourcostDTO.Tourcost.Category1;
            tourcost.Category2              = tourcostDTO.Tourcost.Category2;
            tourcost.Category3              = tourcostDTO.Tourcost.Category3;
            tourcost.Category4              = tourcostDTO.Tourcost.Category4;
            tourcost.GuideType              = tourcostDTO.Tourcost.GuideType;
            tourcost.GuideDays              = tourcostDTO.Tourcost.GuideDays;
            tourcost.DiscountAccomodation   = tourcostDTO.Tourcost.DiscountAccomodation;
            tourcost.DiscountTransportation = tourcostDTO.Tourcost.DiscountTransportation;
            tourcost.Comment                = tourcostDTO.Tourcost.Comment;
            tourcost.ModifiedBy             = user.Id;
            tourcost.ModifiedDate           = DateTime.Now;

            _tourcostCrudService.Update(tourcost);

            var tourcostDetails = _tourcostDetailCrudService.GetAll(p => p.TourcostId == tourcost.Id);

            foreach (TourcostDetail tourcostDetail in tourcostDetails)
            {
                if (!tourcostDTO.TourcostDetail.Any(x => x.Id == tourcostDetail.Id))
                {
                    _tourcostDetailCrudService.Delete(tourcostDetail);
                }
            }

            foreach (TourcostDetail tourcostDetail in tourcostDTO.TourcostDetail)
            {
                if (tourcostDetail.Id > 0)
                {
                    var record = _tourcostDetailCrudService.Get(tourcostDetail.Id);

                    record.Day           = tourcostDetail.Day;
                    record.Sector1Id     = tourcostDetail.Sector1Id;
                    record.Sector2Id     = tourcostDetail.Sector2Id;
                    record.Sector3Id     = tourcostDetail.Sector3Id;
                    record.HotelAId      = tourcostDetail.HotelAId;
                    record.HotelBId      = tourcostDetail.HotelBId;
                    record.HotelCId      = tourcostDetail.HotelCId;
                    record.Category1Cost = tourcostDetail.Category1Cost;
                    record.Category2Cost = tourcostDetail.Category2Cost;
                    record.Category3Cost = tourcostDetail.Category3Cost;
                    record.Category4Cost = tourcostDetail.Category4Cost;
                    record.ModifiedBy    = user.Id;
                    record.ModifiedDate  = DateTime.Now;

                    _tourcostDetailCrudService.Update(record);
                }
                else
                {
                    tourcostDetail.TourcostId = tourcostDTO.Tourcost.Id;
                    tourcostDetail.CreatedBy  = user.Id;

                    await _tourcostDetailCrudService.InsertAsync(tourcostDetail);
                }
            }
        }