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

        public frmShowStuff()
        {
            InitializeComponent();
        }

        

        private void SearchShowStuff()
        {
            Aram_CRM_Project.BLL.StuffCRUD StuffCrud = new Aram_CRM_Project.BLL.StuffCRUD();
            Aram_CRM_Project.BLL.Entity.Stuff StuffVar = new Aram_CRM_Project.BLL.Entity.Stuff();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            DataTable dtTemp = new DataTable();

            StuffVar.Name = utility.ChangeChar(txtSearchName.Text.Trim());
            if (cboSearchStuffGroup.SelectedValue.ToString() == "0")
                StuffVar.StuffGroupName = "";
            else
                StuffVar.StuffGroupName = cboSearchStuffGroup.Text;

            dtTemp = StuffCrud.SearchStuff(StuffVar);
            dgvStuff.DataSource = dtTemp;
            dgvStuff.Columns[1].Visible = false;
            dgvStuff.Columns[0].Width = 60;
            dgvStuff.Columns[2].Width = 180;

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

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

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

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

        protected override void btnNew_Click(object sender, EventArgs e)
        {
            frmStuff fStuff = new frmStuff();

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

        protected override void btnEdit_Click(object sender, EventArgs e)
        {
            frmStuff fStuff = new frmStuff();

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

        protected override void btnShow_Click(object sender, EventArgs e)
        {
            frmStuff fStuff = new frmStuff();

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

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

            fMessage.DeleteRecordConfirm();
            if (fMessage.ResultQuestion)
            {
                Result = StuffCrud.DeleteStuff(dgvStuff.SelectedCells[1].Value.ToString());
                if (Result > 0)
                {
                    fMessage.DeleteSuccess();
                    frmShowStuff_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();

            string StuffGroup;
            if (cboSearchStuffGroup.SelectedValue.ToString() == "0")
                StuffGroup = "";
            else
                StuffGroup = cboSearchStuffGroup.Text;

            ConditionQuery = " WHERE (dbo.tblStuff.Name LIKE N'%" + utility.ChangeChar(txtSearchName.Text.Trim()) + "%' AND dbo.tblStuffGroup.Name LIKE N'%" + StuffGroup + "%') ORDER BY dbo.tblStuff.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 = "Stuff";
            fPrintReport.ShowDialog();
        }

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

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

        private void frmShowStuff_Load(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.StuffCRUD StuffCrud = new Aram_CRM_Project.BLL.StuffCRUD();

            cboSearchStuffGroup.DataSource = StuffCrud.GetStuffGroupForSearchComboBox();
            cboSearchStuffGroup.ValueMember = "Id";
            cboSearchStuffGroup.DisplayMember = "Name";
            cboSearchStuffGroup.SelectedValue = 0;
        }

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

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