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

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

        public void IdentifyMode(string Id)
        {
            Aram_CRM_Project.BLL.CityStateCRUD CityStateCrud = new Aram_CRM_Project.BLL.CityStateCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            CityState CityStateVar = new CityState();

            switch (Mode)
            {
                case "New":
                    {
                        this.Text = "  تعریف مناطق شهری جدید";
                        this.ShowDialog();
                        break;
                    }
                case "Edit":
                    {
                        CityStateVar = CityStateCrud.ReadCityState(Id);
                        if (CityStateCrud != null)
                        {
                            lblId.Text = CityStateVar.Id;
                            txtName.Text = CityStateVar.Name;
                            txtComment.Text = CityStateVar.Comment;

                            this.Text = "  ویرایش مناطق شهری";
                            this.ShowDialog();
                        }
                        else
                        {
                            frmMessage fMessage = new frmMessage();
                            fMessage.ErrorOnDatabase();
                            return;
                        }
                        break;
                    }
                case "Show":
                    {
                        CityStateVar = CityStateCrud.ReadCityState(Id);
                        if (CityStateCrud != null)
                        {
                            lblId.Text = CityStateVar.Id;
                            txtName.Text = CityStateVar.Name;
                            txtComment.Text = CityStateVar.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.CityStateCRUD CityStateCrud = new Aram_CRM_Project.BLL.CityStateCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            CityState CityStateVar = new CityState();
            frmMessage fMessage = new frmMessage();

            int Result;
            bool ErrorFlag = false;

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

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

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