public FrmDealerEntry(DealerModel model, Form frmForm)
 {
     InitializeComponent();
     _frmForm = frmForm;
     _dealerService = new DealerService();
     LoadRegions();
     if (model != null)
     {
         _isNewMode = false;
         _dealerId = model.DealerId;
         txtDealerName.Text = model.DealerName;
         txtAddress.Text = model.DealerAddress;
         txtPhoneNo.Text = model.PhoneNo;
         txtMobileNo.Text = model.MobileNo;
         txtEmailId.Text = model.Email;
         cmbRegion.SelectedValue = model.RegionId;
         txtIncharge.Text = model.DelearIncharge;
         btnAdd.Text = @"Update";
     }
     else
     {
         _isNewMode = true;
         btnAdd.Text = @"Save";
     }
 }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtDealerName.Text)||string.IsNullOrWhiteSpace(txtAddress.Text)||string.IsNullOrWhiteSpace(txtMobileNo.Text)||string.IsNullOrWhiteSpace(cmbRegion.Text))
            {
                ClsCommon.ShowErrorToolTip(txtDealerName,"Please Enter The Dealer Name");
            }
            else
            {
                var model = new DealerModel
                {
                    DealerId = _dealerId,
                    DealerName = txtDealerName.Text,
                    DealerAddress = txtAddress.Text,
                    PhoneNo = txtPhoneNo.Text,
                    MobileNo = txtMobileNo.Text,
                    Email = txtEmailId.Text,
                    RegionId = (int)cmbRegion.SelectedValue,
                    DelearIncharge = txtIncharge.Text
                };

                if (_isNewMode)
                {

                    _dealerId = _dealerService.Save(model).DealerId;
                    if (_dealerId<=0) return;
                    MessageBox.Show(@"Data Saved Successfully", @"Save", MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                    var frm = (FrmDealer)_frmForm;
                    frm.grdData.Rows.Add(_dealerId, model.DealerName,model.DealerAddress,model.PhoneNo,model.MobileNo,model.Email,model.RegionId,cmbRegion.Text, model.DelearIncharge);
                    frm.grdData.Rows[frm.grdData.Rows.Count - 1].IsSelected = true;
                    txtDealerName.Focus();
                    txtDealerName.Text = "";
                    txtIncharge.Text = "";
                    Notify();
                }
                else
                {
                    var success = _dealerService.Update(model);
                    if (!success) return;
                    MessageBox.Show(@"Data Updated Successfully", @"Update", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                    var frm = (FrmDealer)_frmForm;
                    frm.grdData.CurrentRow.Cells["DealerName"].Value = model.DealerName;
                    frm.grdData.CurrentRow.Cells["DealerAddress"].Value = model.DealerAddress;
                    frm.grdData.CurrentRow.Cells["PhoneNo"].Value = model.PhoneNo;
                    frm.grdData.CurrentRow.Cells["MobileNo"].Value = model.MobileNo;
                    frm.grdData.CurrentRow.Cells["Email"].Value = model.Email;
                    frm.grdData.CurrentRow.Cells["RegionId"].Value = model.RegionId;
                    frm.grdData.CurrentRow.Cells["RegionName"].Value = cmbRegion.Text;
                    frm.grdData.CurrentRow.Cells["DelearIncharge"].Value = model.DelearIncharge;
                    Close();
                    Notify();
                }

            }
        }
        public DealerModel Save(DealerModel model)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var addModel = new Dealer()
                    {
                        DealerName = model.DealerName,
                       DealerAddress = model.DealerAddress,
                       PhoneNo = model.PhoneNo,
                       MobileNo = model.MobileNo,
                       Email = model.Email,
                       DelearIncharge = model.DealerAddress,
                       RegionId = model.RegionId
                    };
                    _context.Entry(addModel).State = EntityState.Added;
                    _context.SaveChanges();
                    model.DealerId = addModel.DealerId;

                    return model;
                }
            }
            catch (Exception e)
            {
                //Console.WriteLine(e);
                return new DealerModel();
            }
        }
        public bool Update(DealerModel model)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var editModel = _context.Dealers.FirstOrDefault(a => a.DealerId == model.DealerId);

                    if (editModel != null)
                    {
                        editModel.DealerName = model.DealerName;
                        editModel.DealerAddress = model.DealerAddress;
                        editModel.DelearIncharge = model.DelearIncharge;
                        editModel.PhoneNo = model.PhoneNo;
                        editModel.MobileNo = model.MobileNo;
                        editModel.Email = model.Email;
                        editModel.RegionId = model.RegionId;
                    }

                    _context.Entry(editModel).State = EntityState.Modified;
                    _context.SaveChanges();

                    return true;

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return false;
            }
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(lblID.Text) <= 0) return;
            var model = new DealerModel()
            {
                DealerId = Convert.ToInt32(lblID.Text),
                DealerName = lblName.Text,
                DealerAddress = lblAddress.Text,
                PhoneNo = lblPhone.Text,
                MobileNo = lblMobile.Text,
                Email = lblEmail.Text,
                RegionId = _regionId,
                DelearIncharge = lblIncharge.Text
            };

            var frm = new FrmDealerEntry(model, this);
            frm.ShowDialog(this);
        }