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

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

        public frmShowCityState()
        {
            InitializeComponent();
        }

        private void SearchCityState()
        {
            Aram_CRM_Project.BLL.CityStateCRUD CityStateCrud = new Aram_CRM_Project.BLL.CityStateCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            DataTable dtTemp = new DataTable();

            dtTemp = CityStateCrud.SearchCityState(utility.ChangeChar(txtSearchName.Text.Trim()));
            dgvCityState.DataSource = dtTemp;
            dgvCityState.Columns[1].Visible = false;
            dgvCityState.Columns[0].Width = 60;

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

        public void frmShowCityState_Activated(object sender, EventArgs e)
        {
            SearchCityState();
        }

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

        protected override void btnNew_Click(object sender, EventArgs e)
        {
            frmCityState fCityState = new frmCityState();

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

        protected override void btnEdit_Click(object sender, EventArgs e)
        {
            frmCityState fCityState = new frmCityState();

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

        protected override void btnShow_Click(object sender, EventArgs e)
        {
            frmCityState fCityState = new frmCityState();

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

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

            fMessage.DeleteRecordConfirm();
            if (fMessage.ResultQuestion)
            {
                Result = CityStateCrud.DeleteCityState(dgvCityState.SelectedCells[1].Value.ToString());
                if (Result > 0)
                {
                    fMessage.DeleteSuccess();
                    frmShowCityState_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.tblCityState.Name LIKE N'%" + utility.ChangeChar(txtSearchName.Text.Trim()) + "%') ORDER BY dbo.tblCityState.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 = "CityState";
            fPrintReport.ShowDialog();
        }


        private void btnExit_Click(object sender, EventArgs e)
        {
            StaticVariable.FormCityState_Shown = false;
            this.Dispose();
        }

        private void frmShowCityState_FormClosed(object sender, FormClosedEventArgs e)
        {
            StaticVariable.FormCityState_Shown = false;
        }

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

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

       
      


    }
}
