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 frmStuffGroup : Form
    {
        public frmStuffGroup()
        {
            InitializeComponent();
        }

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

        public void IdentifyMode(string Id)
        {
            Aram_CRM_Project.BLL.StuffGroupCRUD StuffGroupCrud = new Aram_CRM_Project.BLL.StuffGroupCRUD();
            StuffGroup StuffGroupVar = new StuffGroup();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();

            switch (Mode)
            {
                case "New":
                    {
                        this.Text = "  تعریف گروه کالای جدید";
                        this.ShowDialog();
                        break;
                    }
                case "Edit":
                    {
                        StuffGroupVar = StuffGroupCrud.ReadStuffGroup(Id);
                        if (StuffGroupVar != null)
                        {
                            lblId.Text = StuffGroupVar.Id;
                            txtName.Text = StuffGroupVar.Name;
                            txtComment.Text = StuffGroupVar.Comment;

                            this.Text = "  ویرایش گروه کالا";
                            this.ShowDialog();
                        }
                        else
                        {
                            frmMessage fMessage = new frmMessage();
                            fMessage.ErrorOnDatabase();
                            return;
                        }
                        break;
                    }
                case "Show":
                    {
                        StuffGroupVar = StuffGroupCrud.ReadStuffGroup(Id);
                        if (StuffGroupVar != null)
                        {
                            lblId.Text = StuffGroupVar.Id;
                            txtName.Text = StuffGroupVar.Name;
                            txtComment.Text = StuffGroupVar.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.StuffGroupCRUD StuffGroupCrud = new Aram_CRM_Project.BLL.StuffGroupCRUD();
            StuffGroup StuffGroupVar = new StuffGroup();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            frmMessage fMessage = new frmMessage();

            int Result;
            bool ErrorFlag = false;

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

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

            switch (Mode)
            {
                case "New":
                    {
                        Result = StuffGroupCrud.InsertStuffGroup(StuffGroupVar);
                        if (Result == 0)
                        {
                            fMessage.RegUnSuccess();
                            return;
                        }
                        else if (Result == -1)
                        {
                            fMessage.RepeatingRecord();
                            return;
                        }
                        break;
                    }
                case "Edit":
                    {
                        Result = StuffGroupCrud.UpdateStuffGroup(StuffGroupVar);
                        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;
                }
            }
        }
    }
}
