private void btnAddReq_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (btnAddReq.Content.ToString() != "Add")
                {
                    if (lblReqName.Content == "?" || lblReqDesc.Content == "?" || String.IsNullOrWhiteSpace(txtReqName.Text) || String.IsNullOrWhiteSpace(txtReqDesc.Text))
                    {
                        System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }
                }

                if (btnAddReq.Content.ToString() == "Add")
                {
                    grpReq.Visibility = Visibility.Visible;
                    btnAddReq.Content = "Save";
                    btnEdtReq.Content = "Cancel";
                    btnEdtReq.IsEnabled = true;
                    btnDelReq.Visibility = Visibility.Hidden;

                    tbDed.IsEnabled = false;
                    tbInfo.IsEnabled = false;
                    tbPen.IsEnabled = false;
                    tbReq.IsEnabled = false;
                }
                else if (btnAddReq.Content.ToString() == "Save")
                {
                    if (txtReqName.Text == "" || txtReqDesc.Text == "")
                    {
                        System.Windows.MessageBox.Show("Please complete the required information", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    //for view
                    if (status == "View")
                    {
                        using (var ctx = new finalContext())
                        {
                            var ctr = ctx.Requirements.Where(x => x.ServiceID == sId).Count() + 1;
                            Requirement tr = new Requirement { ServiceID = sId, RequirementNum = ctr, Name = txtReqName.Text, Description = txtReqDesc.Text };
                            ctx.Requirements.Add(tr);
                            ctx.SaveChanges();
                            var reqs = from rq in ctx.Requirements
                                       where rq.ServiceID == sId
                                       select new { ReqNumber = rq.RequirementNum, Name = rq.Name, Description = rq.Description };
                            dgReq.ItemsSource = reqs.ToList();
                        }
                        reset();
                        return;
                    }

                    using (var ctx = new finalContext())
                    {
                        var ctr = ctx.TempoRequirements.Count() + 1;
                        TempoRequirement tr = new TempoRequirement { RequirementNum = ctr, Name = txtReqName.Text, Description = txtReqDesc.Text };
                        ctx.TempoRequirements.Add(tr);
                        ctx.SaveChanges();

                        var rqs = from rq in ctx.TempoRequirements
                                  select new { ReqNumber = rq.RequirementNum, Name = rq.Name, Description = rq.Description };
                        dgReq.ItemsSource = rqs.ToList();
                        reset();

                    }

                    reset();
                }
                else //for update
                {
                    //for view
                    if (status == "View")
                    {
                        using (var ctx = new finalContext())
                        {
                            int num = Convert.ToInt32(getRow(dgReq, 0));
                            var tr = ctx.Requirements.Where(x => x.RequirementNum == num && x.ServiceID == sId).First();
                            tr.Name = txtReqName.Text;
                            tr.Description = txtReqDesc.Text;
                            ctx.SaveChanges();

                            var reqs = from rq in ctx.Requirements
                                       where rq.ServiceID == sId
                                       select new { ReqNumber = rq.RequirementNum, Name = rq.Name, Description = rq.Description };
                            dgReq.ItemsSource = reqs.ToList();
                        }
                        reset();
                        return;
                    }


                    using (var ctx = new finalContext())
                    {
                        int num = Convert.ToInt32(getRow(dgReq, 0));
                        var tr = ctx.TempoRequirements.Where(x => x.RequirementNum == num).First();
                        tr.Name = txtReqName.Text;
                        tr.Description = txtReqDesc.Text;
                        ctx.SaveChanges();
                        var rqs = from rq in ctx.TempoRequirements
                                  select new { ReqNumber = rq.RequirementNum, Name = rq.Name, Description = rq.Description };
                        dgReq.ItemsSource = rqs.ToList();
                        reset();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Runtime Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (cmbDept.Text == "Financing")
                {
                    if (lblName.Content == "?" || lblMinTerm.Content == "?" || lblMaxTerm.Content == "?" || lblMinVal.Content == "?" || lblMaxVal.Content == "?" || lblInterest.Content == "?" || lblDesc.Content == "?" || lblCom.Content == "?" || lblPenAdj.Content == "?" || lblPenCA.Content == "?" || lblPenDaif.Content == "?" || lblPenHolding.Content == "?" || lblPenIn.Content == "?"
                        || String.IsNullOrWhiteSpace(txtCom.Text) || String.IsNullOrWhiteSpace(txtHolding.Text) || String.IsNullOrWhiteSpace(txtDaif.Text) || String.IsNullOrWhiteSpace(txtClosed.Text) || String.IsNullOrWhiteSpace(txtResFee.Text) || String.IsNullOrWhiteSpace(txtResInt.Text) 
                        || String.IsNullOrWhiteSpace(txtName.Text) || String.IsNullOrWhiteSpace(txtMinTerm.Text) || String.IsNullOrWhiteSpace(txtMaxTerm.Text) || String.IsNullOrWhiteSpace(txtMinVal.Text) || String.IsNullOrWhiteSpace(txtMaxVal.Text) || String.IsNullOrWhiteSpace(txtInterest.Text) || String.IsNullOrWhiteSpace(txtDesc.Text))
                    {
                        System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }
                }
                else
                {
                    if (lblName.Content == "?" || lblMinTerm.Content == "?" || lblMaxTerm.Content == "?" || lblMinVal.Content == "?" || lblMaxVal.Content == "?" || lblInterest.Content == "?" || lblDesc.Content == "?" || lblCom.Content == "?" || lblPenLate.Content == "?"
                        || String.IsNullOrWhiteSpace(txtLtPen.Text) || String.IsNullOrWhiteSpace(txtName.Text) || String.IsNullOrWhiteSpace(txtMinTerm.Text) || String.IsNullOrWhiteSpace(txtMaxTerm.Text) || String.IsNullOrWhiteSpace(txtMinVal.Text) || String.IsNullOrWhiteSpace(txtMaxVal.Text) || String.IsNullOrWhiteSpace(txtInterest.Text) || String.IsNullOrWhiteSpace(txtDesc.Text))
                    {
                        System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }
                }

                if (txtName.Text == "" || txtDesc.Text == "" || txtInterest.Text == "" || txtMaxTerm.Text == "" || txtMinVal.Text == "" || txtMinTerm.Text == "" || cmbDept.Text == "" || cmbType.Text == "")
                {
                    return;
                }

                if (Convert.ToDouble(txtMaxVal.Text) < 1 || Convert.ToDouble(txtMinVal.Text) < 1 || Convert.ToDouble(txtMaxTerm.Text) < 1 || Convert.ToDouble(txtMinTerm.Text) < 0)
                {
                    System.Windows.MessageBox.Show("Terms and Values must be greater thant one(1)", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }


                if (status == "Add")
                {


                    using (var ctx = new finalContext())
                    {
                        Service ser = null;
                        if (cmbDept.Text == "Financing")
                        {
                            ser = new Service { Name = txtName.Text, Department = cmbDept.Text, Description = txtDesc.Text, Type = cmbType.Text, Active = true, Interest = Convert.ToDouble(txtInterest.Text), MinTerm = Convert.ToInt32(txtMinTerm.Text), MaxTerm = Convert.ToInt32(txtMaxTerm.Text), MinValue = Convert.ToDouble(txtMinVal.Text), MaxValue = Convert.ToDouble(txtMaxVal.Text), AgentCommission = Convert.ToDouble(txtCom.Text), Holding = Convert.ToDouble(txtHolding.Text), ClosedAccountPenalty = Convert.ToDouble(txtClosed.Text), DaifPenalty = Convert.ToDouble(txtDaif.Text), RestructureFee = Convert.ToDouble(txtResFee.Text), RestructureInterest = Convert.ToDouble(txtResInt.Text), AdjustmentFee = Convert.ToDouble(txtAdjust.Text), LatePaymentPenalty=0 };
                        }
                        else
                        {
                            ser = new Service { Name = txtName.Text, Department = cmbDept.Text, Description = txtDesc.Text, Type = cmbType.Text, Active = true, Interest = Convert.ToDouble(txtInterest.Text), MinTerm = Convert.ToInt32(txtMinTerm.Text), MaxTerm = Convert.ToInt32(txtMaxTerm.Text), MinValue = Convert.ToDouble(txtMinVal.Text), MaxValue = Convert.ToDouble(txtMaxVal.Text), AgentCommission = Convert.ToDouble(txtCom.Text), Holding = 0, ClosedAccountPenalty = 0, DaifPenalty = 0, RestructureFee = 0, RestructureInterest = 0, AdjustmentFee = 0, LatePaymentPenalty=Convert.ToDouble(txtLtPen.Text) };
                        }
                        var deds = from dd in ctx.TempoDeductions
                                   select new { DedNumber = dd.DeductionNum, Name = dd.Name, Percentage = dd.Percentage };
                        var rqs = from rq in ctx.TempoRequirements
                                  select new { ReqNumber = rq.RequirementNum, Name = rq.Name, Description = rq.Description };

                        foreach (var item in deds)
                        {
                            Deduction dd = new Deduction { DeductionNum = item.DedNumber, Name = item.Name, Percentage = item.Percentage };
                            ctx.Deductions.Add(dd);
                        }

                        foreach (var item in rqs)
                        {
                            Requirement rr = new Requirement { RequirementNum = item.ReqNumber, Name = item.Name, Description = item.Description };
                            ctx.Requirements.Add(rr);
                        }

                        ComboBoxItem typeItem = (ComboBoxItem)cmbType.SelectedItem;
                        string value = typeItem.Content.ToString();
                        if (value == "Collateral")
                        {
                            var ci = from c in ctx.TempCollateralInformations
                                     select c;

                            foreach (var itm in ci)
                            {
                                CollateralInformation cl = new CollateralInformation { CollateralInformationNum = itm.TempCollateralInformationNum, Field = itm.Field };
                                ctx.CollateralInformations.Add(cl);
                            }
                        }

                        AuditTrail at = new AuditTrail { EmployeeID = UserID, DateAndTime = DateTime.Now, Action = "Added new Service " + txtName.Text };
                        ctx.AuditTrails.Add(at);

                        ctx.Services.Add(ser);
                        ctx.SaveChanges();
                        MessageBox.Show("Service has been successfully added", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        this.Close();
                    }
                }
                else
                {
                    using (var ctx = new finalContext())
                    {
                        var ser = ctx.Services.Find(sId);
                        ser.Name = txtName.Text;
                        ser.Department = cmbDept.Text;
                        ser.Description = txtDesc.Text;
                        ser.Type = cmbType.Text;
                        ser.Interest = Convert.ToDouble(txtInterest.Text);
                        ser.MinTerm = Convert.ToInt32(txtMinTerm.Text);
                        ser.MaxTerm = Convert.ToInt32(txtMaxTerm.Text);
                        ser.MinValue = Convert.ToDouble(txtMinVal.Text);
                        ser.MaxValue = Convert.ToDouble(txtMaxVal.Text);
                        if (cmbDept.Text == "Financing")
                        {
                            ser.Holding = Convert.ToDouble(txtHolding.Text);
                            ser.DaifPenalty = Convert.ToDouble(txtDaif.Text);
                            ser.ClosedAccountPenalty = Convert.ToDouble(txtClosed.Text);
                            ser.AgentCommission = Convert.ToDouble(txtCom.Text);
                            ser.RestructureFee = Convert.ToDouble(txtResFee.Text);
                            ser.RestructureInterest = Convert.ToDouble(txtResInt.Text);
                            ser.AdjustmentFee = Convert.ToDouble(txtAdjust.Text);
                            ser.LatePaymentPenalty = 0;
                        }
                        else
                        {
                            ser.Holding = 0;
                            ser.DaifPenalty = 0;
                            ser.ClosedAccountPenalty = 0;
                            ser.AgentCommission = 0;
                            ser.RestructureFee = 0;
                            ser.RestructureInterest = 0;
                            ser.AdjustmentFee = 0;
                            ser.LatePaymentPenalty = Convert.ToDouble(txtLtPen.Text);
                        }

                        AuditTrail at = new AuditTrail { EmployeeID = UserID, DateAndTime = DateTime.Now, Action = "Updated Service " + txtName.Text };
                        ctx.AuditTrails.Add(at);
                        ctx.SaveChanges();
                        MessageBox.Show("Service has been successfully updated", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Runtime Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }