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 FrmListCustomer : Form
    {
        DataView objview;
        List<Customer> ListCustomers = new List<Customer>();
        public FrmListCustomer()
        {
            InitializeComponent();
        }

        private void Bind()
        {
            ListCustomers = Customer.GetAllCustomers();
            DataTable tbl = new DataTable("f");
            tbl.Columns.Add("cid");
            tbl.Columns.Add("fname");
            tbl.Columns.Add("lname");
            object[] cs = new object[3];
            for (int i = 0; i < ListCustomers.Count; i++)
            {
                cs[0] = ListCustomers[i].cid;
                cs[1] = ListCustomers[i].fname;
                cs[2] = ListCustomers[i].lname;
                tbl.Rows.Add(cs);
            }
            objview = new DataView(tbl);
            dataGridViewX1.DataSource = objview;
            dataGridViewX1.Columns["cid"].HeaderText = "کد";
            dataGridViewX1.Columns["fname"].HeaderText = "نام";
            dataGridViewX1.Columns["lname"].HeaderText = "نام خانوادگی";           
        }

        private void FrmListCustomer_Load(object sender, EventArgs e)
        {
            Bind();
            FrmFaktor.SelectedCid = "";
            objview.RowFilter = "";
        }

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

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

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

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

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

        private void btnnewcustomer_Click(object sender, EventArgs e)
        {
            FrmCustomers cs = new FrmCustomers(true);
            cs.ShowDialog();
            FrmFaktor.CustomerChanged = true;
        }

    }
}
