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

namespace Grocery
{
    public partial class FrmListKala : Form
    {
        List<Kala> KalasList = new List<Kala>();
        List<kalaGroup> GroupList = new List<kalaGroup>();
        DataView objview;
        public FrmListKala()
        {
            InitializeComponent();
        }

        private void Bind()
        {
            KalasList = Kala.GetAllKalas();
            DataTable tbl = new DataTable("f");
            tbl.Columns.Add("kalaid");
            tbl.Columns.Add("kalaname");
            tbl.Columns.Add("groupname");
            object[] kala = new object[3];
            for (int i = 0; i < KalasList.Count; i++)
            {
                kala[0] = KalasList[i].kalaid;
                kala[1] = KalasList[i].kalaname;
                kala[2] = KalasList[i].GroupName;
                tbl.Rows.Add(kala);
            }
            objview = new DataView(tbl);
            dataGridViewX1.DataSource = objview;
            dataGridViewX1.Columns["kalaid"].HeaderText = "کد کالا";
            dataGridViewX1.Columns["kalaname"].HeaderText = "نام کالا";
            dataGridViewX1.Columns["groupname"].Visible = false;

            GroupList = kalaGroup.GetAllGroups();
            cmbgroups.Enabled = false;
            cmbgroups.DataSource = GroupList;
            cmbgroups.DisplayMember = "groupname";
            cmbgroups.ValueMember = "groupname";
            cmbgroups.Enabled = true;
        }

        private void FrmListKala_Load(object sender, EventArgs e)
        {
            Bind();
            FrmFaktor.selectedkalaid = FrmAnbar.selectedkalaid = "";
            objview.RowFilter = "";
            cmbgroups.SelectedIndex = -1;
        }

        private void txtkalaid_TextChanged(object sender, EventArgs e)
        {
            if (txtkalaid.Text.Trim() != string.Empty)
            {
                objview.RowFilter = "kalaid LIKE '" + txtkalaid.Text + "*'";
            }
            else objview.RowFilter = "";
        }

        private void dataGridViewX1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridViewX1.Rows[e.RowIndex].Cells["kalaid"].Value != null)
            {
                FrmFaktor.selectedkalaid =FrmAnbar.selectedkalaid= dataGridViewX1.Rows[e.RowIndex].Cells["kalaid"].Value.ToString();
                this.Close();
            }
        }

        private void dataGridViewX1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                if (dataGridViewX1.CurrentRow.Cells["kalaid"].Value != null)
                {
                    FrmFaktor.selectedkalaid =FrmAnbar.selectedkalaid= dataGridViewX1.CurrentRow.Cells["kalaid"].Value.ToString();
                    this.Close();
                }
            }
        }

        private void cmbgroups_TextChanged(object sender, EventArgs e)
        {
            if (cmbgroups.Text.Trim() != string.Empty && cmbgroups.Text != "BLL.kalaGroup")
            {
                objview.RowFilter = "groupname='" + cmbgroups.Text + "'";
            }
            else objview.RowFilter = "";
        }

        private void FrmListKala_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void btnnewkala_Click(object sender, EventArgs e)
        {
            FrmKala objform = new FrmKala(true);
            objform.ShowDialog();
            FrmListKala_Load(null, null);
            FrmFaktor.kalachanged = true;
        }
    }
}
