using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;

namespace Aram_CRM_Project.UIL
{
    public partial class frmShowReminder : OfficeManagement.UIL.frmBase
    {
        private string ConditionQuery = string.Empty;

        public frmShowReminder()
        {
            InitializeComponent();
        }

        

        private void frmShowReminder_Load(object sender, EventArgs e)
        {
            //Date
            string[] SystemDate = Aram_CRM_Project.BLL.Entity.StaticVariable.SystemDate.Split('/');

            nupSearchStartDay.Value = decimal.Parse(SystemDate[2]);
            nupSearchStartMonth.Value = decimal.Parse(SystemDate[1]);
            nupSearchStartYear.Value = decimal.Parse(SystemDate[0]) - 1;
            nupSearchEndDay.Value = decimal.Parse(SystemDate[2]);
            nupSearchEndMonth.Value = decimal.Parse(SystemDate[1]);
            nupSearchEndYear.Value = decimal.Parse(SystemDate[0]) + 2;
        }

        private void SearchShowReminder()
        {
            Aram_CRM_Project.BLL.ReminderCRUD ReminderCrud = new Aram_CRM_Project.BLL.ReminderCRUD();
            Aram_CRM_Project.BLL.Entity.Reminder ReminderVar = new Aram_CRM_Project.BLL.Entity.Reminder();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            DataTable dtTemp = new DataTable();

            //Date
            string StartDay, StartMonth, EndDay, EndMonth;
            if (nupSearchStartDay.Value >= 1 && nupSearchStartDay.Value <= 9)
                StartDay = "0" + nupSearchStartDay.Value.ToString();
            else
                StartDay = nupSearchStartDay.Value.ToString();
            if (nupSearchStartMonth.Value >= 1 && nupSearchStartMonth.Value <= 9)
                StartMonth = "0" + nupSearchStartMonth.Value.ToString();
            else
                StartMonth = nupSearchStartMonth.Value.ToString();
            if (nupSearchEndDay.Value >= 1 && nupSearchEndDay.Value <= 9)
                EndDay = "0" + nupSearchEndDay.Value.ToString();
            else
                EndDay = nupSearchEndDay.Value.ToString();
            if (nupSearchEndMonth.Value >= 1 && nupSearchEndMonth.Value <= 9)
                EndMonth = "0" + nupSearchEndMonth.Value.ToString();
            else
                EndMonth = nupSearchEndMonth.Value.ToString();

            ReminderVar.Name = utility.ChangeChar(txtSearchName.Text.Trim());
            ReminderVar.StartDate = nupSearchStartYear.Value.ToString() + "/" + StartMonth + "/" + StartDay;
            ReminderVar.EndDate = nupSearchEndYear.Value.ToString() + "/" + EndMonth + "/" + EndDay;
            ReminderVar.UserId = Aram_CRM_Project.BLL.Entity.StaticVariable.Id;
            if (Aram_CRM_Project.BLL.Entity.StaticVariable.Id == "1")
            {
                dtTemp = ReminderCrud.SearchReminderForAdmin(ReminderVar);
                dgvReminder.DataSource = dtTemp;
                dgvReminder.Columns[1].Visible = false;
                dgvReminder.Columns[0].Width = 50;
                dgvReminder.Columns[2].Width = 200;
                dgvReminder.Columns[3].Width = 70;
            }
            else
            {
                dtTemp = ReminderCrud.SearchReminder(ReminderVar);
                dgvReminder.DataSource = dtTemp;
                dgvReminder.Columns[1].Visible = false;
                dgvReminder.Columns[0].Width = 50;
                dgvReminder.Columns[2].Width = 350;
                dgvReminder.Columns[3].Width = 90;
            }


            if (dtTemp.Rows.Count == 0)
            {
                base.DisableButtonForEmptyDataGrid();
                dgvReminder.Enabled = false;
            }
            else
            {
                base.EnableButtonForFullDataGrid();
                dgvReminder.Enabled = true;
            }
            dtTemp.Dispose();
        }

        private void frmShowReminder_Activated(object sender, EventArgs e)
        {
            SearchShowReminder();
        }

        protected override void btnNew_Click(object sender, EventArgs e)
        {
            frmReminder fReminder = new frmReminder();

            fReminder.Location = new Point(this.Left + 110, this.Top + 100);
            fReminder.Mode = "New";
            fReminder.IdentifyMode("");
            frmShowReminder_Activated(sender, e);
            
            //Reminder Message
            Aram_CRM_Project.BLL.Public PublicClass = new Aram_CRM_Project.BLL.Public();
            Aram_CRM_Project.BLL.Entity.StaticVariable.ReminderCount = PublicClass.ReminderCount(Aram_CRM_Project.BLL.Entity.StaticVariable.Id);
            ((frmParent)(this.MdiParent)).lilReminder.Text = "شما  " + Aram_CRM_Project.BLL.Entity.StaticVariable.ReminderCount + "  یادآوری دارید";
        }

        protected override void btnEdit_Click(object sender, EventArgs e)
        {
            frmReminder fReminder = new frmReminder();

            fReminder.Location = new Point(this.Left + 110, this.Top + 100);
            fReminder.Mode = "Edit";
            fReminder.IdentifyMode(dgvReminder.SelectedCells[1].Value.ToString());
            frmShowReminder_Activated(sender, e);
        }

        protected override void btnShow_Click(object sender, EventArgs e)
        {
            frmReminder fReminder = new frmReminder();

            fReminder.Location = new Point(this.Left + 110, this.Top + 100);
            fReminder.Mode = "Show";
            fReminder.IdentifyMode(dgvReminder.SelectedCells[1].Value.ToString());
        }

        protected override void btnDelete_Click(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.ReminderCRUD ReminderCrud = new Aram_CRM_Project.BLL.ReminderCRUD();
            frmMessage fMessage = new frmMessage();
            int Result;

            fMessage.DeleteRecordConfirm();
            if (fMessage.ResultQuestion)
            {
                Result = ReminderCrud.DeleteReminder(dgvReminder.SelectedCells[1].Value.ToString());
                if (Result > 0)
                {
                    fMessage.DeleteSuccess();
                    frmShowReminder_Activated(sender, e);

                    //Reminder Message
                    Aram_CRM_Project.BLL.Public PublicClass = new Aram_CRM_Project.BLL.Public();
                    string ReminderCount = string.Empty;
                    ReminderCount = PublicClass.ReminderCount(Aram_CRM_Project.BLL.Entity.StaticVariable.Id);
                    ((frmParent)(this.MdiParent)).lilReminder.Text = "شما  " + ReminderCount + "  یادآوری دارید";
                }
                else if (Result == -1)
                {
                    fMessage.DeleteConflict();
                }
                else
                {
                    fMessage.DeleteError();
                }
            }
        }

        private void txtSearchName_TextChanged(object sender, EventArgs e)
        {
            SearchShowReminder();
        }

        

        protected override void btnPrint_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();

            //Date
            string StartDate, EndDate;
            string StartDay, StartMonth, EndDay, EndMonth;
            if (nupSearchStartDay.Value >= 1 && nupSearchStartDay.Value <= 9)
                StartDay = "0" + nupSearchStartDay.Value.ToString();
            else
                StartDay = nupSearchStartDay.Value.ToString();
            if (nupSearchStartMonth.Value >= 1 && nupSearchStartMonth.Value <= 9)
                StartMonth = "0" + nupSearchStartMonth.Value.ToString();
            else
                StartMonth = nupSearchStartMonth.Value.ToString();
            if (nupSearchEndDay.Value >= 1 && nupSearchEndDay.Value <= 9)
                EndDay = "0" + nupSearchEndDay.Value.ToString();
            else
                EndDay = nupSearchEndDay.Value.ToString();
            if (nupSearchEndMonth.Value >= 1 && nupSearchEndMonth.Value <= 9)
                EndMonth = "0" + nupSearchEndMonth.Value.ToString();
            else
                EndMonth = nupSearchEndMonth.Value.ToString();

            StartDate = nupSearchStartYear.Value.ToString() + "/" + StartMonth + "/" + StartDay;
            EndDate = nupSearchEndYear.Value.ToString() + "/" + EndMonth + "/" + EndDay;

            ConditionQuery = " WHERE (dbo.tblReminder.Name LIKE N'%" + utility.ChangeChar(txtSearchName.Text.Trim()) + "%' AND dbo.tblReminder.Date BETWEEN '" + StartDate + "' AND '" + EndDate + "') ORDER BY dbo.tblReminder.Id Desc";

            Thread PrintThread = new Thread(PrintReport);
            PrintThread.Start();

            this.Cursor = Cursors.Default;
        }

        private void PrintReport()
        {
            frmPrintReport fPrintReport = new frmPrintReport();
            fPrintReport.ConditionQuery = ConditionQuery;
            if (Aram_CRM_Project.BLL.Entity.StaticVariable.Id == "1")
                fPrintReport.Mode = "ReminderForAdmin";
            else
                fPrintReport.Mode = "Reminder";
            fPrintReport.ShowDialog();
        }

        private void frmShowReminder_FormClosed(object sender, FormClosedEventArgs e)
        {
            Aram_CRM_Project.BLL.Entity.StaticVariable.FormReminder_Shown = false;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.Entity.StaticVariable.FormReminder_Shown = false;
            this.Dispose();
        }

        private void nupSearchStartDay_ValueChanged(object sender, EventArgs e)
        {
            SearchShowReminder();
        }

        private void nupSearchStartMonth_ValueChanged(object sender, EventArgs e)
        {
            SearchShowReminder();
        }

        private void nupSearchStartYear_ValueChanged(object sender, EventArgs e)
        {
            SearchShowReminder();
        }

        private void nupSearchEndDay_ValueChanged(object sender, EventArgs e)
        {
            SearchShowReminder();
        }

        private void nupSearchEndMonth_ValueChanged(object sender, EventArgs e)
        {
            SearchShowReminder();
        }

        private void nupSearchEndYear_ValueChanged(object sender, EventArgs e)
        {
            SearchShowReminder();
        }

        private void dgvReminder_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnEdit_Click(sender, e);
        }

        private void dgvReminder_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnEdit_Click(sender, e);
        }

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

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

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

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

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

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