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

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

        private string ShowModeDate = string.Empty;

        public void IdentifyMode(string Id)
        {
            Aram_CRM_Project.BLL.ReminderCRUD ReminderCrud = new Aram_CRM_Project.BLL.ReminderCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            Reminder ReminderVar = new Reminder();

            switch (Mode)
            {
                case "New":
                    {
                        //Date
                        string[] SystemDate = Aram_CRM_Project.BLL.Entity.StaticVariable.SystemDate.Split('/');
                        nudDay.Value = decimal.Parse(SystemDate[2]);
                        nudMonth.Value = decimal.Parse(SystemDate[1]);
                        nudYear.Value = decimal.Parse(SystemDate[0]);

                        this.Text = "  ثبت یادآوری جدید";
                        this.ShowDialog();
                        break;
                    }
                case "Edit":
                    {
                        ReminderVar = ReminderCrud.ReadReminder(Id);
                        if (ReminderVar != null)
                        {
                            lblId.Text = ReminderVar.Id;
                            lblUserId.Text = ReminderVar.UserId;
                            txtName.Text = ReminderVar.Name;
                            txtComment.Text = ReminderVar.Comment;
                            string[] Date = ReminderVar.Date.Split('/');
                            nudDay.Value = decimal.Parse(Date[2]);
                            nudMonth.Value = decimal.Parse(Date[1]);
                            nudYear.Value = decimal.Parse(Date[0]);

                            if (ReminderVar.Alarm == "1")
                                chbAlarm.Checked = true;

                            this.Text = "  ویرایش یادآوری";
                            this.ShowDialog();
                        }
                        else
                        {
                            frmMessage fMessage = new frmMessage();
                            fMessage.ErrorOnDatabase();
                            return;
                        }
                        break;
                    }
                case "Show":
                    {
                        ReminderVar = ReminderCrud.ReadReminder(Id);
                        if (ReminderVar != null)
                        {
                            lblId.Text = ReminderVar.Id;
                            txtName.Text = ReminderVar.Name;
                            txtComment.Text = ReminderVar.Comment;
                            ShowModeDate = ReminderVar.Date;
                            string[] Date = ReminderVar.Date.Split('/');
                            nudDay.Value = decimal.Parse(Date[2]);
                            nudMonth.Value = decimal.Parse(Date[1]);
                            nudYear.Value = decimal.Parse(Date[0]);

                            if (ReminderVar.Alarm == "1")
                                chbAlarm.Checked = true;

                            chbAlarm.Enabled = false;
                            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.ReminderCRUD ReminderCrud = new Aram_CRM_Project.BLL.ReminderCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            Reminder ReminderVar = new Reminder();
            frmMessage fMessage = new frmMessage();

            int Result;
            bool ErrorFlag = false;

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

            ReminderVar.Id = lblId.Text;
            ReminderVar.UserId = Aram_CRM_Project.BLL.Entity.StaticVariable.Id;
            ReminderVar.Name = utility.ChangeChar(txtName.Text.Trim());
            string StartDay, StartMonth;
            if (nudDay.Value >= 1 && nudDay.Value <= 9)
                StartDay = "0" + nudDay.Value.ToString();
            else
                StartDay = nudDay.Value.ToString();
            if (nudMonth.Value >= 1 && nudMonth.Value <= 9)
                StartMonth = "0" + nudMonth.Value.ToString();
            else
                StartMonth = nudMonth.Value.ToString();

            ReminderVar.Date = nudYear.Value.ToString() + "/" + StartMonth + "/" + StartDay;
            ReminderVar.Comment = utility.ChangeChar(txtComment.Text.Trim());
            if (chbAlarm.Checked == true)
                ReminderVar.Alarm = "1";
            else
                ReminderVar.Alarm = "0";

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

        private void nudDay_ValueChanged(object sender, EventArgs e)
        {
            if (Mode == "Show")
            {
                string[] Date = ShowModeDate.Split('/');
                nudDay.Value = decimal.Parse(Date[2]);
            }
        }

        private void nudMonth_ValueChanged(object sender, EventArgs e)
        {
            if (Mode == "Show")
            {
                string[] Date = ShowModeDate.Split('/');
                nudMonth.Value = decimal.Parse(Date[1]);
            }
        }

        private void nudYear_ValueChanged(object sender, EventArgs e)
        {
            if (Mode == "Show")
            {
                string[] Date = ShowModeDate.Split('/');
                nudYear.Value = decimal.Parse(Date[0]);
            }
        }

        private void nudDay_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;
        }

        private void nudMonth_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;
        }

        private void nudYear_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;
        }


       

        

        
    }
}
