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

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

        public void IdentifyMode(string Id)
        {
            Aram_CRM_Project.BLL.RelationshipWayCRUD RelationshipWayCrud = new Aram_CRM_Project.BLL.RelationshipWayCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            RelationshipWay RelationshipWayVar = new RelationshipWay();

            switch (Mode)
            {
                case "New":
                    {
                        this.Text = "  تعریف نحوه آشنایی با مشتریان جدید";
                        this.ShowDialog();
                        break;
                    }
                case "Edit":
                    {
                        RelationshipWayVar = RelationshipWayCrud.ReadRelationshipWay(Id);
                        if (RelationshipWayVar != null)
                        {
                            lblId.Text = RelationshipWayVar.Id;
                            txtName.Text = RelationshipWayVar.Name;
                            txtAddress.Text = RelationshipWayVar.Address;
                            txtComment.Text = RelationshipWayVar.Comment;
                            string[] StartDate = RelationshipWayVar.StartDate.Split('/');
                            string[] EndDate = RelationshipWayVar.EndDate.Split('/');
                            cboStartDay.Text = StartDate[2];
                            cboStartMonth.Text = StartDate[1];
                            txtStartYear.Text = StartDate[0];
                            cboEndDay.Text = EndDate[2];
                            cboEndMonth.Text = EndDate[1];
                            txtEndYear.Text = EndDate[0];

                            this.Text = "  ویرایش نحوه آشنایی با مشتریان";
                            this.ShowDialog();
                        }
                        else
                        {
                            frmMessage fMessage = new frmMessage();
                            fMessage.ErrorOnDatabase();
                            return;
                        }
                        break;
                    }
                case "Show":
                    {
                        RelationshipWayVar = RelationshipWayCrud.ReadRelationshipWay(Id);
                        if (RelationshipWayVar != null)
                        {
                            lblId.Text = RelationshipWayVar.Id;
                            txtName.Text = RelationshipWayVar.Name;
                            txtAddress.Text = RelationshipWayVar.Address;
                            txtComment.Text = RelationshipWayVar.Comment;
                            string[] StartDate = RelationshipWayVar.StartDate.Split('/');
                            string[] EndDate = RelationshipWayVar.EndDate.Split('/');
                            cboStartDay.Text = StartDate[2];
                            cboStartMonth.Text = StartDate[1];
                            txtStartYear.Text = StartDate[0];
                            cboEndDay.Text = EndDate[2];
                            cboEndMonth.Text = EndDate[1];
                            txtEndYear.Text = EndDate[0];

                            cboStartDay.DropDownStyle = ComboBoxStyle.Simple;
                            cboStartMonth.DropDownStyle = ComboBoxStyle.Simple;
                            cboEndDay.DropDownStyle = ComboBoxStyle.Simple;
                            cboEndMonth.DropDownStyle = ComboBoxStyle.Simple;
                            this.ActiveControl = this.btnExit;
                            btnEnter.Enabled = false;
                            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.RelationshipWayCRUD RelationshipWayCrud = new Aram_CRM_Project.BLL.RelationshipWayCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            RelationshipWay RelationshipWayVar = new RelationshipWay();
            frmMessage fMessage = new frmMessage();

            int Result;
            bool ErrorFlag = false;

            erpRelationshipWay.Clear();
            if (txtName.Text.Trim() == "")
            {
                erpRelationshipWay.SetIconAlignment(this.txtName, ErrorIconAlignment.MiddleLeft);
                erpRelationshipWay.SetError(this.txtName, Properties.Resources.ResourceManager.GetString("ErrEmptyName"));
                ErrorFlag = true;
            }
            if (cboStartDay.SelectedIndex == 0 || cboStartMonth.SelectedIndex == 0 || txtStartYear.Text.Trim() == "" || txtStartYear.Text.Trim().Length != 4)
            {
                erpRelationshipWay.SetIconAlignment(this.txtStartYear, ErrorIconAlignment.MiddleLeft);
                erpRelationshipWay.SetError(this.txtStartYear, Properties.Resources.ResourceManager.GetString("ErrStartDate"));
                ErrorFlag = true;
            }
            if (cboEndDay.SelectedIndex == 0 || cboEndMonth.SelectedIndex == 0 || txtEndYear.Text.Trim() == "" || txtEndYear.Text.Trim().Length != 4)
            {
                erpRelationshipWay.SetIconAlignment(this.txtEndYear, ErrorIconAlignment.MiddleLeft);
                erpRelationshipWay.SetError(this.txtEndYear, Properties.Resources.ResourceManager.GetString("ErrEndDate"));
                ErrorFlag = true;
            }
            if (txtAddress.Text.Trim() == "")
            {
                erpRelationshipWay.SetIconAlignment(this.txtAddress, ErrorIconAlignment.TopLeft);
                erpRelationshipWay.SetError(this.txtAddress, Properties.Resources.ResourceManager.GetString("ErrEmptyAddress"));
                ErrorFlag = true;
            }
            if (ErrorFlag)
            {
                return;
            }

            RelationshipWayVar.Id = lblId.Text;
            RelationshipWayVar.Name = utility.ChangeChar(txtName.Text.Trim());
            RelationshipWayVar.StartDate = txtStartYear.Text.Trim() + "/" + cboStartMonth.SelectedItem.ToString() + "/" + cboStartDay.SelectedItem.ToString();
            RelationshipWayVar.EndDate = txtEndYear.Text.Trim() + "/" + cboEndMonth.SelectedItem.ToString() + "/" + cboEndDay.SelectedItem.ToString();
            RelationshipWayVar.Address = utility.ChangeChar(txtAddress.Text.Trim());
            RelationshipWayVar.Comment = utility.ChangeChar(txtComment.Text.Trim());

            switch (Mode)
            {
                case "New":
                    {
                        Result = RelationshipWayCrud.InsertRelationshipWay(RelationshipWayVar);
                        if (Result == 0)
                        {
                            fMessage.RegUnSuccess();
                            return;
                        }
                        else if (Result == -1)
                        {
                            fMessage.RepeatingRecord();
                            return;
                        }
                        break;
                    }
                case "Edit":
                    {
                        Result = RelationshipWayCrud.UpdateRelationshipWay(RelationshipWayVar);
                        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 txtStartYear_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 txtEndYear_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 txtAddress_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;
                }
            }
        }

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

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

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

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