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

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

        public void IdentifyMode(string Id)
        {
            Aram_CRM_Project.BLL.CrTypeCRUD CrTypeCrud = new Aram_CRM_Project.BLL.CrTypeCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            CrType CrTypeVar=new CrType();

            switch (Mode)
            {
                case "New":
                    {
                        this.Text = "  تعریف نحوه ارتباط با مشتریان";
                        this.ShowDialog();
                        break;
                    }
                case "Edit":
                    {
                        CrTypeVar = CrTypeCrud.ReadCrType(Id);
                        if (CrTypeVar != null)
                        {
                            lblId.Text = CrTypeVar.Id;
                            txtName.Text = CrTypeVar.Name;
                            txtComment.Text = CrTypeVar.Comment;

                            this.Text = "  ویرایش نحوه ارتباط با مشتریان";
                            this.ShowDialog();
                        }
                        else
                        {
                            frmMessage fMessage = new frmMessage();
                            fMessage.ErrorOnDatabase();
                            return;
                        }
                        break;
                    }
                case "Show":
                    {
                        CrTypeVar = CrTypeCrud.ReadCrType(Id);
                        if (CrTypeVar != null)
                        {
                            lblId.Text = CrTypeVar.Id;
                            txtName.Text = CrTypeVar.Name;
                            txtComment.Text = CrTypeVar.Comment;

                            btnEnter.Enabled = false;
                            this.ActiveControl = this.btnExit;
                            this.Text = "  نمایش جزییات";
                            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.CrTypeCRUD CrTypeCrud = new Aram_CRM_Project.BLL.CrTypeCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            CrType CrTypeVar = new CrType();

            frmMessage fMessage = new frmMessage();

            int Result;
            bool ErrorFlag = false;

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

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

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