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

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

        public frmShowCrType()
        {
            InitializeComponent();
        }

        private void SearchCrType()
        {
            Aram_CRM_Project.BLL.CrTypeCRUD CrTypeCrud = new Aram_CRM_Project.BLL.CrTypeCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            DataTable dtTemp = new DataTable();

            dtTemp = CrTypeCrud.SearchCrType(utility.ChangeChar(txtSearchName.Text.Trim()));
            dgvCrType.DataSource = dtTemp;
            dgvCrType.Columns[1].Visible = false;
            dgvCrType.Columns[0].Width = 60;

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

        private void frmShowCrType_Activated(object sender, EventArgs e)
        {
            SearchCrType();
        }

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

        protected override void btnNew_Click(object sender, EventArgs e)
        {
            frmCrType fCrType = new frmCrType();

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

        protected override void btnEdit_Click(object sender, EventArgs e)
        {
            frmCrType fCrType = new frmCrType();

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

        protected override void btnShow_Click(object sender, EventArgs e)
        {
            frmCrType fCrType = new frmCrType();

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

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

            fMessage.DeleteRecordConfirm();
            if (fMessage.ResultQuestion)
            {
                Result = CrTypeCrud.DeleteCrType(dgvCrType.SelectedCells[1].Value.ToString());
                if (Result > 0)
                {
                    fMessage.DeleteSuccess();
                    frmShowCrType_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.tblCrType.Name LIKE N'%" + utility.ChangeChar(txtSearchName.Text.Trim()) + "%') ORDER BY dbo.tblCrType.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 = "CrType";
            fPrintReport.ShowDialog();
        }

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

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

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

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