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 ABC.BanLe
{
    public partial class Setup : Form
    {
        public string nhom;
        public Setup()
        {
            InitializeComponent();
            //load combo
            load_combo();
            //load list ncc da co 
            this.Load += new EventHandler(Setup_Load);
            simpleButton1.Click += new EventHandler(simpleButton1_Click);
            link_xoalai.Click += new EventHandler(link_xoalai_Click);
           
        }
        
  

        void link_xoalai_Click(object sender, EventArgs e)
        {
            if (msgbox.dialog_yes_no("Xóa ?") == DialogResult.Yes)
            {
                DataRow dtr = gv_thuonglai.GetDataRow(gv_thuonglai.FocusedRowHandle);
                _NhapLe_Class.set("update BanLe set SD = 1 where NCC_ID = '" + dtr["NCC_ID"].ToString().ToUpper() + "'");
                _NhapLe_Class.get_NCC_store("sp_NhapSe_NCC_Load");
                filter_theonhom();
            }
        }

        void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void Setup_Load(object sender, EventArgs e)
        {
            filter_theonhom();
            //colSTT.UnboundType = DevExpress.Data.UnboundColumnType.Integer;
           // gv_thuonglai.CustomUnboundColumnData += gridView1_CustomUnboundColumnData;
        }
        void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
                e.Value = e.ListSourceRowIndex + 1;
        }


        #region load-add ncc
        public void load_combo()
        {
            txtthuonglai.Properties.DataSource = DuLieu.List_NCC.Tables[0];
            txtthuonglai.KeyPress += new KeyPressEventHandler(txtthuonglai_KeyPress);
            txtthuonglai.Enter += new EventHandler(txtthuonglai_Enter);
            txtthuonglai.Leave += new EventHandler(txtthuonglai_Leave);
        }

        void txtthuonglai_Leave(object sender, EventArgs e)
        {
            txtthuonglai.BackColor = Color.White;
        }

        void txtthuonglai_Enter(object sender, EventArgs e)
        {
            txtthuonglai.BackColor = Color.NavajoWhite;
        }

        void txtthuonglai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                add_ncc();
            }
            else
            {
            }
        }
        NhapLe_Class _NhapLe_Class = new NhapLe_Class();
        public void add_ncc()
        {
            if (txtthuonglai.EditValue != null && txtthuonglai.EditValue != "")
            {
                if (_NhapLe_Class.them_ncc(txtthuonglai.EditValue.ToString().ToUpper()) == true)
                {
                    _NhapLe_Class.get_NCC_store("sp_NhapSe_NCC_Load");
                    filter_theonhom();
                }

            }
            
            
        }
        #endregion

        #region load grid
        DataView dtv_Thuonglai;
        public void filter_theonhom()
        {
            DataTable dt = new DataTable();
            dt.Columns.Clear();
            dt = DuLieu.NhapLe_List_NCC.Tables[0];
            bool chk = false;
          for(int i=0;i<dt.Columns.Count;i++)
            {
                if (dt.Columns[i].ColumnName=="STT")
                {
                    chk = true;
                    break;
                }    
            }   
          if(!chk)
            dt.Columns.Add("STT", typeof(int));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["STT"] = i + 1;
            }
            dtv_Thuonglai = new DataView(dt);
            #region
            if (nhom == "All")
            {
                
              
                dtv_Thuonglai.RowFilter = "Nhom is not null";
                gc_thuonglai.DataSource = dtv_Thuonglai;               
            }
            else
            {
                try
                {
                    
                    dtv_Thuonglai.RowFilter = "Nhom = '" + nhom.ToUpper() + "'";
                    gc_thuonglai.DataSource = dtv_Thuonglai;       
                }
                catch (Exception)
                {
                    
                   
                }
                       
            }
            colSTT.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            
            #endregion
        }
        #endregion

        private void Setup_Load_1(object sender, EventArgs e)
        {

        }

        private void gv_thuonglai_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            {
                gv_thuonglai.ClearSorting();
                colSTT.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
                if (e.Column.FieldName == "STT")
                {
                    dtv_Thuonglai.Sort = "[STT] asc";
                    DataTable dt = dtv_Thuonglai.ToTable().Copy();
                    DuLieu.NhapLe_List_NCC.Tables[0].Rows.Clear();
                    DuLieu.NhapLe_List_NCC.Tables[0].Merge(dt);
                }
            }
             
        }
    }
}
