/// <summary>
 /// Updates the specified user.
 /// </summary>
 /// <param name="user">The user.</param>
 public void Update(PrescriptionDetail user)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// Updates the specified prescription.
        /// </summary>
        /// <param name="prescription">The prescription.</param>
        public void Update(Prescription prescription)
        {
            try
            {
                
                var originalPres = this.Context.Prescription.FirstOrDefault(x => x.Id == prescription.Id);
                if (originalPres == null) throw new Exception("Không tồn tại dữ liệu trong CSDL.");

                var prescriptionList = this.Context.PrescriptionDetails.Where(x => x.PrescriptionId == prescription.Id).ToList();
                
                originalPres.RecheckDate = prescription.RecheckDate;
                originalPres.Note = prescription.Note;
                originalPres.DoctorId = prescription.DoctorId;
                originalPres.Doctor = null;
                originalPres.FigureId = prescription.FigureId;
                originalPres.Version++;


                foreach (var orginItem in prescriptionList)
                {
                    var item = prescription.PrescriptionDetails.FirstOrDefault(x => x.Id == orginItem.Id);
                    if (item == null)
                    {
                        this.Context.PrescriptionDetails.Remove(orginItem);
                    }
                    else
                    {
                        orginItem.MedicineId = item.MedicineId;
                        orginItem.Day = item.Day;
                        orginItem.Description = item.Description;
                        orginItem.Amount = item.Amount;
                        orginItem.FigureDetailId = item.FigureDetailId;
                        orginItem.VolumnPerDay = item.VolumnPerDay;
                    }

                }

                foreach (var orginItem in prescription.PrescriptionDetails)
                {
                    var item = originalPres.PrescriptionDetails.FirstOrDefault(x => x.Id == orginItem.Id);
                    if (item != null) continue;
                    var newItem = new PrescriptionDetail
                                      {
                                          Amount = orginItem.Amount,
                                          Day = orginItem.Day,
                                          Description = orginItem.Description,
                                          FigureDetailId = orginItem.FigureDetailId,
                                          MedicineId = orginItem.MedicineId,
                                          PrescriptionId = orginItem.PrescriptionId,
                                          Version = 0
                                      };
                    originalPres.PrescriptionDetails.Add(newItem);
                }

                originalPres.LastUpdatedDate = DateTime.Now;
                

                this.Context.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// Handles the SelectedIndexChanged event of the cboFigure control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cboFigure_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_isSkipUpdatingFigure) return;

            var removeList = _prescriptionDetailList.Where(x => x.FigureDetailId != null).ToList();
            foreach (var item in removeList) _prescriptionDetailList.Remove(item);


            var comboboxEx = (ComboBoxEx)sender;
            var figureId = (int)comboboxEx.SelectedValue;
            var figureDetails = this._figureDetailRepo.GetByFigure(figureId);
            foreach (var figureDetail in figureDetails)
            {
                var prescriptionDetail = new PrescriptionDetail()
                {
                    FigureDetailId = figureDetail.Id,
                    MedicineId = figureDetail.MedicineId,
                    //Medicine = figureDetail.Medicine,
                    VolumnPerDay = figureDetail.Volumn,
                    Day = this.Day,
                    Amount = DefaultVolumn * figureDetail.Volumn,
                    Version = 0


                };
                try
                {
                    prescriptionDetail.InventoryVolumn = _medicineRepo.GetInventoryVolumeWareHouseByMedicineId(AppContext.CurrentClinic.Id, prescriptionDetail.MedicineId);
                }
                catch (Exception ex)
                {
                    prescriptionDetail.InventoryVolumn = 0;
                }
                _prescriptionDetailList.Insert(0, prescriptionDetail);
            }

            this.bdsPrescriptionDetail.DataSource = _prescriptionDetailList;
            // this.bdsPrescriptionDetail.EndEdit();
            ReupdateNo();
        }
        /// <summary>
        /// Handles the SelectedIndexChanged event of the cboFigure control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CboFigureSelectedIndexChanged(object sender, EventArgs e)
        {
            if (_isSkipUpdatingFigure) return;

            var removeList = _prescriptionDetailList.Where(x => x.FigureDetailId != null).ToList();
            foreach (var item in removeList) _prescriptionDetailList.Remove(item);

            var comboboxEx = (ComboBoxEx)sender;
            var figureId = (int)comboboxEx.SelectedValue;
            var figureDetails = this._figureDetailRepo.GetByFigure(figureId);
            foreach (var figureDetail in figureDetails)
            {
                var prescriptionDetail = new PrescriptionDetail()
                {
                    FigureDetailId = figureDetail.Id,
                    MedicineId = figureDetail.MedicineId,
                    VolumnPerDay = figureDetail.Volumn,
                    TradeName = figureDetail.Medicine.TradeName,
                    MedicineName = figureDetail.Medicine.Name,
                    UnitName = figureDetail.Medicine.Define.Name,
                    Day = this.Day,
                    Amount = DefaultVolumn * figureDetail.Volumn,
                    Version = 0
                };
                prescriptionDetail.InventoryVolumn = _medicineRepo.GetInventoryVolumeWareHouseByMedicineId(AppContext.CurrentClinic.Id, prescriptionDetail.MedicineId);
                _prescriptionDetailList.Insert(0, prescriptionDetail);
            }

            this.bdsPrescriptionDetail.DataSource = _prescriptionDetailList;
            this.bdsPrescriptionDetail.ResetBindings(false);
            this.dataGridViewX1.Update();
        }