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 frmShowStuffGroup : OfficeManagement.UIL.frmBase
    {
        private string ConditionQuery = string.Empty;

        public frmShowStuffGroup()
        {
            InitializeComponent();
        }

        

        private void SearchShowStuff()
        {
            Aram_CRM_Project.BLL.StuffGroupCRUD StuffGroupCrud = new Aram_CRM_Project.BLL.StuffGroupCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            DataTable dtTemp = new DataTable();

            dtTemp = StuffGroupCrud.SearchStuffGroup(utility.ChangeChar(txtSearchName.Text.Trim()));
            dgvStuffGroup.DataSource = dtTemp;
            dgvStuffGroup.Columns[1].Visible = false;
            dgvStuffGroup.Columns[0].Width = 60;

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

        private void frmShowStuffGroup_Activated(object sender, EventArgs e)
        {
            SearchShowStuff();
        }

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

        protected override void btnNew_Click(object sender, EventArgs e)
        {
            frmStuffGroup fStuffGroup = new frmStuffGroup();

            fStuffGroup.Location = new Point(this.Left + 110, this.Top + 100);
            fStuffGroup.Mode = "New";
            fStuffGroup.IdentifyMode("");
            frmShowStuffGroup_Activated(sender, e);
        }

        protected override void btnEdit_Click(object sender, EventArgs e)
        {
            frmStuffGroup fStuffGroup = new frmStuffGroup();

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

        protected override void btnShow_Click(object sender, EventArgs e)
        {
            frmStuffGroup fStuffGroup = new frmStuffGroup();

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

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

            fMessage.DeleteRecordConfirm();
            if (fMessage.ResultQuestion)
            {
                Result = StuffGroupCrud.DeleteStuffGroup(dgvStuffGroup.SelectedCells[1].Value.ToString());
                if (Result > 0)
                {
                    fMessage.DeleteSuccess();
                    frmShowStuffGroup_Activated(sender, e);
                }
                else if (Result == -1)
                {
                    fMessage.DeleteConflict();
                }
                else
                {
                    fMessage.DeleteError();
                }
            }
        }

        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();

            ConditionQuery = " WHERE (dbo.tblStuffGroup.Name LIKE N'%" + utility.ChangeChar(txtSearchName.Text.Trim()) + "%') ORDER BY dbo.tblStuffGroup.Id Desc ";

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

            this.Cursor = Cursors.Default;
        }

        private void PrintReport()
        {
            frmPrintReport fPrintReport = new frmPrintReport();
            fPrintReport.ConditionQuery = ConditionQuery;
            fPrintReport.Mode = "StuffGroup";
            fPrintReport.ShowDialog();
        }

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

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

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

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