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

        public frmShowCustomerGroup()
        {
            InitializeComponent();
        }


        private void SearchCustomerGroup()
        {
            Aram_CRM_Project.BLL.CustomerGroupCRUD CustomerGroupCrud = new Aram_CRM_Project.BLL.CustomerGroupCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            DataTable dtTemp = new DataTable();

            dtTemp = CustomerGroupCrud.SearchCustomerGroup(utility.ChangeChar(txtSearchName.Text.Trim()));
            dgvCustomerGroup.DataSource = dtTemp;
            dgvCustomerGroup.Columns[1].Visible = false;
            dgvCustomerGroup.Columns[0].Width = 60;
            dgvCustomerGroup.Columns[2].Width = 100;

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

        private void frmShowCustomerGroup_Activated(object sender, EventArgs e)
        {
            SearchCustomerGroup();
        }

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

        protected override void btnNew_Click(object sender, EventArgs e)
        {
            frmCustomerGroup fCustomerGroup = new frmCustomerGroup();

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

        protected override void btnEdit_Click(object sender, EventArgs e)
        {
            frmCustomerGroup fCustomerGroup = new frmCustomerGroup();

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

        protected override void btnShow_Click(object sender, EventArgs e)
        {
            frmCustomerGroup fCustomerGroup = new frmCustomerGroup();

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

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

            fMessage.DeleteRecordConfirm();
            if (fMessage.ResultQuestion)
            {
                Result = CustomerGroupCrud.DeleteCustomerGroup(dgvCustomerGroup.SelectedCells[1].Value.ToString());
                if (Result > 0)
                {
                    fMessage.DeleteSuccess();
                    frmShowCustomerGroup_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.tblCustomerGroup.Name LIKE N'%" + utility.ChangeChar(txtSearchName.Text.Trim()) + "%') ORDER BY dbo.tblCustomerGroup.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 = "CustomerGroup";
            fPrintReport.ShowDialog();
        }

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

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

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

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