using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Aram_CRM_Project.UIL
{
    public partial class frmSelectStuff : Form
    {
        public frmSelectStuff()
        {
            InitializeComponent();
        }

        

        private void frmSelectStuff_Activated(object sender, EventArgs e)
        {
            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();

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

            
            if (lblSearchName.Text == "")
            {
                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)
                    btnEnter.Enabled = false;
                else
                    btnEnter.Enabled = true;
                dtTemp.Dispose();
            }
            else
            {
                txtSearchName.Text = lblSearchName.Text;
            }
        }

        private void txtSearchName_TextChanged(object sender, EventArgs e)
        {
            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)
                btnEnter.Enabled = false;
            else
                btnEnter.Enabled = true;
            dtTemp.Dispose();
        }

        private void cboSearchStuffGroup_SelectionChangeCommitted(object sender, EventArgs e)
        {
            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)
                btnEnter.Enabled = false;
            else
                btnEnter.Enabled = true;
            dtTemp.Dispose();
        }

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

        private void btnEnter_Click(object sender, EventArgs e)
        {
            lblEvent.Text = "Enter";
            if (txtStuffQuantity.Text.Trim() == "")
                txtStuffQuantity.Text = "1";
            if (txtUnitPrice.Text.Trim() == "")
                txtUnitPrice.Text = "0";
            if (txtDiscount.Text.Trim() == "")
                txtDiscount.Text = "0";
            this.Close();
        }

        private void txtStuffQuantity_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) && !(e.KeyCode == Keys.Delete) && !(e.KeyCode == Keys.Back) && !(e.KeyCode == Keys.Down) && !(e.KeyCode == Keys.Up) && !(e.KeyCode == Keys.Left) && !(e.KeyCode == Keys.Right))
            {
                e.SuppressKeyPress = true;
            }
        }

        private void txtUnitPrice_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) && !(e.KeyCode == Keys.Delete) && !(e.KeyCode == Keys.Back) && !(e.KeyCode == Keys.Down) && !(e.KeyCode == Keys.Up) && !(e.KeyCode == Keys.Left) && !(e.KeyCode == Keys.Right))
            {
                e.SuppressKeyPress = true;
            }
        }

        private void txtUnitPrice_TextChanged(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            txtUnitPrice.Text = utility.ThousandSeparator(txtUnitPrice.Text.Trim());
            txtUnitPrice.SelectionStart = txtUnitPrice.Text.Length;
        }

        private void txtDiscount_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) && !(e.KeyCode == Keys.Delete) && !(e.KeyCode == Keys.Back) && !(e.KeyCode == Keys.Down) && !(e.KeyCode == Keys.Up) && !(e.KeyCode == Keys.Left) && !(e.KeyCode == Keys.Right))
            {
                e.SuppressKeyPress = true;
            }
        }

        private void txtDiscount_TextChanged(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            txtDiscount.Text = utility.ThousandSeparator(txtDiscount.Text.Trim());
            txtDiscount.SelectionStart = txtDiscount.Text.Length;
        }
    }
}
