using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aram_CRM_Project.BLL.Entity;

namespace Aram_CRM_Project.UIL
{
    public partial class frmCustomerGroup : Form
    {
        public frmCustomerGroup()
        {
            InitializeComponent();
        }

        private string _Mode = string.Empty;
        public string Mode
        {
            set { _Mode = value; }
            get { return _Mode; }
        }

        public void IdentifyMode(string Id)
        {
            Aram_CRM_Project.BLL.CustomerGroupCRUD CustomerGroupCrud = new Aram_CRM_Project.BLL.CustomerGroupCRUD();
            CustomerGroup CustomerGroupVar = new CustomerGroup();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();

            switch (Mode)
            {
                case "New":
                    {
                        this.Text = "  تعریف گروه مشتریان جدید";
                        this.ShowDialog();
                        break;
                    }
                case "Edit":
                    {
                        CustomerGroupVar = CustomerGroupCrud.ReadCustomerGroup(Id);
                        if (CustomerGroupVar != null)
                        {
                            lblId.Text = CustomerGroupVar.Id;
                            txtName.Text = CustomerGroupVar.Name;
                            txtComment.Text = CustomerGroupVar.Comment;

                            this.Text = "  ویرایش گروه مشتریان";
                            this.ShowDialog();
                        }
                        else
                        {
                            frmMessage fMessage = new frmMessage();
                            fMessage.ErrorOnDatabase();
                            return;
                        }
                        break;
                    }
                case "Show":
                    {
                        CustomerGroupVar = CustomerGroupCrud.ReadCustomerGroup(Id);
                        if (CustomerGroupVar != null)
                        {
                            lblId.Text = CustomerGroupVar.Id;
                            txtName.Text = CustomerGroupVar.Name;
                            txtComment.Text = CustomerGroupVar.Comment;

                            this.Text = "  نمایش گروه مشتریان";
                            this.ActiveControl = this.btnExit;
                            btnEnter.Enabled = false;
                            this.ShowDialog();
                        }
                        else
                        {
                            frmMessage fMessage = new frmMessage();
                            fMessage.ErrorOnDatabase();
                            return;
                        }
                        break;
                    }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.CustomerGroupCRUD CustomerGroupCrud = new Aram_CRM_Project.BLL.CustomerGroupCRUD();
            CustomerGroup CustomerGroupVar = new CustomerGroup();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            frmMessage fMessage = new frmMessage();

            int Result;
            bool ErrorFlag = false;

            erpCustomerGroup.Clear();
            if (txtName.Text.Trim() == "")
            {
                erpCustomerGroup.SetIconAlignment(this.txtName, ErrorIconAlignment.MiddleLeft);
                erpCustomerGroup.SetError(this.txtName, Properties.Resources.ResourceManager.GetString("ErrEmptyName"));
                ErrorFlag = true;
            }
            if (ErrorFlag)
            {
                return;
            }

            CustomerGroupVar.Id = lblId.Text;
            CustomerGroupVar.Name = utility.ChangeChar(txtName.Text.Trim());
            CustomerGroupVar.Comment = utility.ChangeChar(txtComment.Text.Trim());

            switch (Mode)
            {
                case "New":
                    {
                        Result = CustomerGroupCrud.InsertCustomerGroup(CustomerGroupVar);
                        if (Result == 0)
                        {
                            fMessage.RegUnSuccess();
                            return;
                        }
                        else if (Result == -1)
                        {
                            fMessage.RepeatingRecord();
                            return;
                        }
                        break;
                    }
                case "Edit":
                    {
                        Result = CustomerGroupCrud.UpdateCustomerGroup(CustomerGroupVar);
                        if (Result == 0)
                        {
                            fMessage.RegUnSuccess();
                            return;
                        }
                        else if (Result == -1)
                        {
                            fMessage.RepeatingRecord();
                            return;
                        }
                        break;
                    }
            }
            this.Dispose();
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (Mode == "Show")
            {
                if (!(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
                {
                    e.SuppressKeyPress = true;
                }
            }
        }

        private void txtComment_KeyDown(object sender, KeyEventArgs e)
        {
            if (Mode == "Show")
            {
                if (!(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
                {
                    e.SuppressKeyPress = true;
                }
            }
        }
    }
}
