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;

using ABC.DanhMuc.NhomNCC;
using ABC.DanhMuc.Hang;
using ABC.DanhMuc.KhachHang;
using DevExpress.XtraEditors;
using ABC.File;

namespace ABC.GiaoDich
{
    public partial class CapNhatGiaChiTietBien :XtraUserControl
    {
        string select_nhom_id, select_nhom_name;
        string select_thuonglai_id, select_thuonglai_name;
        string select_ca_id, select_ca_name;
        public CapNhatGiaChiTietBien()
        {
            InitializeComponent();
            data();
            linktlai.Click += new EventHandler(linktlai_Click);
            linkloaica.Click += new EventHandler(linkloaica_Click);
            gv_loaica.CellValueChanged += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv_loaica_CellValueChanged);
            gv_nhapsi.CellValueChanged += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv_nhapsi_CellValueChanged);
            event_click();
            this.Load += new EventHandler(CapNhatGiaChiTietBien_Load);
        }

        void CapNhatGiaChiTietBien_Load(object sender, EventArgs e)
        {
            DataRow[] dr = DuLieu.List_Hang.Tables[0].Select("ID='' or ID is null");
            if (dr.Length == 0)
            {
                DataRow dr_new = DuLieu.List_Hang.Tables[0].NewRow();
                dr_new["Name"] = "        ''        ";
                DuLieu.List_Hang.Tables[0].Rows.Add(dr_new);
            }
        }
        void BaoCao_SoBaoCao_Si_Tra_FormClosed(object sender, FormClosedEventArgs e)
        {
            DataRow[] dr = DuLieu.List_Hang.Tables[0].Select("ID='' or ID is null");
            if (dr.Length > 0)
            {
                for (int d = 0; d < dr.Length; d++)
                {
                    DuLieu.List_Hang.Tables[0].Rows.Remove(dr[d]);
                }
                DuLieu.List_Hang.Tables[0].AcceptChanges();
            }
        }
       
        #region gv_nhapsi valuechanged
        void gv_nhapsi_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {

            if (Pow.kiemtra("CapNhatGiaChiTietBien", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                if (e.Column.FieldName != "Select")
                {
                    #region
                    DataRow dt_update = gv_nhapsi.GetDataRow(gv_nhapsi.FocusedRowHandle);
                    string colum = e.Column.FieldName;
                    string value = e.Value.ToString();

                    DataRowView row_ncc = look_thuonglai_daluu.GetDataSourceRowByKeyValue(dt_update["Tbl_NCC_Name"].ToString()) as DataRowView;
                    string id_ncc = row_ncc["ID"].ToString().ToUpper();
                    DataRowView row_khachhang = look_khachhang_daluu.GetDataSourceRowByKeyValue(dt_update["Tbl_KhachHang_Name"].ToString()) as DataRowView;
                    string id_khachhang = row_khachhang["ID"].ToString().ToUpper();
                   // DataRowView row_hang = look_loaica_daluu.GetDataSourceRowByKeyValue(dt_update["Tbl_Hang_Name"].ToString()) as DataRowView;
                    string id_ca;
                    if (e.Column.FieldName == "CA_View")
                    {
                        DataRowView row_hang = look_loaica_daluu.GetDataSourceRowByKeyValue(dt_update["CA_View"].ToString()) as DataRowView;
                        id_ca = row_hang["ID"].ToString().ToUpper();
                        if (id_ca == null || id_ca == "")
                        {

                            for (int i = gv_nhapsi.FocusedRowHandle; i >= 0; i--)
                            {
                                if (gv_nhapsi.GetDataRow(i)["CA_View"] != "        ''        ")
                                {
                                    id_ca = gv_nhapsi.GetDataRow(i)["ID_Hang"].ToString();
                                    break;
                                }
                            }
                        }

                    }
                    else
                    {
                        id_ca = dt_update["ID_Hang"].ToString();
                    }
                    //update id referent
                    #region
                    dt_update["ID_NCC"] = id_ncc;
                    dt_update["ID_KhachHang"] = id_khachhang;
                    dt_update["ID_Hang"] = id_ca;
                    #endregion
                    
                  

                    #region save to database
                    #region
                    //save and add vao muc du lieu da luu
                    string sql = "NhapSi ";
                    //  @STT
                    sql += "N'" + dt_update["STT"].ToString().ToUpper() + "',";
                    //,@ID
                    sql += "N'" + dt_update["ID"].ToString().ToUpper() + "',";
                    //,@ID_Hang
                    sql += "N'" + dt_update["ID_Hang"].ToString().ToUpper() + "',";
                    //,@ID_KhachHang
                    sql += "N'" + dt_update["ID_KhachHang"].ToString().ToUpper() + "',";
                    //,@ID_NCC
                    sql += "N'" + dt_update["ID_NCC"].ToString().ToUpper() + "',";
                    //,@Date_Num_Final_UpDate
                    sql += "N'" + dt_update["Date_Num_Final_UpDate"].ToString().ToUpper() + "',";
                    //,@User_ID_Final_UpDate
                    sql += "N'" + Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper() + "',";
                    //,@IS_User
                    sql += "N'1',";
                    //,@NguonGoc
                    sql += "N'SI',";
                    //,@SL
                    sql += "N'" + dt_update["SL"].ToString() + "',";
                    //,@Gia_0 --------SE THAY DOI VI LA GIA UU TIEN
                    sql += "N'" + dt_update["Gia"].ToString() + "',";
                    //,@Gia_1            
                    sql += "N'" + dt_update["Gia_1"].ToString() + "',";
                    //,@Gia_2
                    sql += "N'" + dt_update["Gia_2"].ToString() + "',";
                    //,@Sot
                    sql += "N'" + dt_update["Sot"].ToString() + "',";
                    //,@Ket
                    sql += "N'" + dt_update["Ket"].ToString() + "',";
                    //,@TienMat
                    sql += "N'0',";

                    //,@Toa_ID
                    sql += "Null,";
                    //,@Toa_SL
                    sql += "Null,";
                    //,@Toa_Gia
                    sql += "Null,";
                    //,@Toa_HHSL
                    sql += "Null,";
                    //,@Toa_HHPC
                    sql += "Null,";
                    //,@Toa_Xoa
                    sql += "Null,";
                    //,@_Text1
                    sql += "Null,";
                    //,@_Text2
                    sql += "Null,";
                    //,@_Text3
                    sql += "Null,";
                    //,@_Text4
                    sql += "Null,";
                    //,@_Text5
                    sql += "Null,";
                    //,@_Num1
                    sql += "Null,";
                    //,@_Num2
                    sql += "Null,";
                    //,@_Num3
                    sql += "Null,";
                    //,@_ID1
                    sql += "Null,";
                    //,@Lu
                    sql += "N'" + dt_update["Lu"].ToString() + "',";
                    //,@Cat
                    sql += "N'" + dt_update["Cat"].ToString() + "',";
                    //,@Bom
                    sql += "N'" + dt_update["Bom"].ToString() + "'";
                    _NhapSi_Class.set(sql);
                    #endregion
                    #endregion
                    #endregion
                    tailai_sauedit();
                }
                else
                {
                    if ((bool)e.Value == true)
                    {
                        DataRow dr = gv_nhapsi.GetDataRow(gv_nhapsi.FocusedRowHandle);
                        dr["Select"] = true;
                        dts_dulieu.Tables[0].AcceptChanges();
                    }
                    else
                    {
                        DataRow dr = gv_nhapsi.GetDataRow(gv_nhapsi.FocusedRowHandle);
                        dr["Select"] = false;
                        dts_dulieu.Tables[0].AcceptChanges();
                    }
                    gv_nhapsi.RefreshData();
                    
                }
                
            }
            else
            {
                gv_nhapsi.CancelUpdateCurrentRow();
            }
        }
        #endregion

        public void tailai_sauedit()
        {
            int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
            string sql = "select * from NhapSi_GiaChiTiet_Thuonglai_bien where Date_Num_Final_UpDate = " + ng + " ORDER BY Tbl_NCC_Name ASC";
            dts_thuonglai = _NhapSi_Class.get_dataset(sql);
            object TL = dts_thuonglai.Tables[0].Compute("Sum(TONGLUONG)", "");
            object TT = dts_thuonglai.Tables[0].Compute("Sum(TONGTIEN)", "");

            Tongluongnhom.Text = String.Format("{0:N1}", TL) + " (kg)";
            tongtiennhom.Text = String.Format("{0:#,#}", TT) + " (vnđ)";

            //-----------------------------------tai lai thuong lai
            DataRow dr = gv_thuonglai.GetDataRow(gv_thuonglai.FocusedRowHandle);
            if (dr["Tbl_NCC_Name"].ToString() == "__Tất Cả__")
            {
                //load tat ca ca
                string sql_loaica = "select * from NhapSi_GiaChiTiet_Loaica where Date_Num_Final_UpDate = " + ng + "  AND _ID1 ='985A1EB2-AA36-47B8-AB09-6FD46BA93903'";
                sql_loaica += " order by Tbl_Hang_Name asc";
                dts_loaica = _NhapSi_Class.get_dataset(sql_loaica);
            }
            else
            {
                string sql_loaica = "select * from NhapSi_GiaChiTiet_Loaica where Date_Num_Final_UpDate = " + ng + " and ID_NCC = '" + select_thuonglai_id + "' AND _ID1 ='985A1EB2-AA36-47B8-AB09-6FD46BA93903'";
                sql_loaica += " order by Tbl_Hang_Name asc";
                dts_loaica = _NhapSi_Class.get_dataset(sql_loaica);
            }

            object TLT = dts_loaica.Tables[0].Compute("Sum(TONGLUONG)", "");
            object TTT = dts_loaica.Tables[0].Compute("Sum(TONGTIEN)", "");

            Tongluonglai.Text = String.Format("{0:N1}", TLT) + " (kg)";
            tongtienlai.Text = String.Format("{0:#,#}", TTT) + " (vnđ)";

            //------------------------------------------------------------tai lai loai ca
            DataRow dr_ca = gv_loaica.GetDataRow(gv_loaica.FocusedRowHandle);
            string filter_string = "select * from NhapSi_DaLuu where Date_Num_Final_UpDate = " + ng + "  AND NHOM_NCC_ID ='985A1EB2-AA36-47B8-AB09-6FD46BA93903'";
            if (select_thuonglai_name != "__Tất Cả__")
            {
                filter_string += " AND ID_NCC = '" + select_thuonglai_id + "'";
                //  
            }
            if (dr_ca["Tbl_Hang_Name"].ToString() != "__Tất Cả__")
            {
                filter_string += " AND ID_Hang = '" + dr_ca["ID_Hang"].ToString().ToUpper() + "'";
            }
            else
            {
                select_ca_name = "Tất Cả";
            }
            filter_string += " order by Tbl_NCC_Name asc,Tbl_KhachHang_Name asc,Tbl_Hang_Name asc,Gia desc";
            dts_dulieu = _NhapSi_Class.get_dataset(filter_string);
            dts_dulieu.Tables[0].Columns.Add("Select", typeof(bool));
            object TLTC = dts_dulieu.Tables[0].Compute("Sum(SL)", "");
            object TTTC = dts_dulieu.Tables[0].Compute("Sum(ThanhTien)", "");

            tongluongca.Text = String.Format("{0:N1}", TLTC) + " (kg)";
            tongtienca.Text = String.Format("{0:#,#}", TTTC) + " (vnđ)";


        }


        #region gv_loaica valuechanged
        void gv_loaica_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (Pow.kiemtra("CapNhatGiaChiTietBien", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                DataRow dr = gv_loaica.GetDataRow(gv_loaica.FocusedRowHandle);
                string sql = "NhapSi_update_Gia_2 ";
                // @ID_Hang uniqueidentifier,
                sql += "N'" + dr["ID_Hang"].ToString().ToUpper() + "',";

                //@ID_NCC uniqueidentifier,
                sql += "N'" + dr["ID_NCC"].ToString().ToUpper() + "',";
                //@Date_Num_Final_UpDate int,
                sql += "N'" + dr["Date_Num_Final_UpDate"].ToString() + "',";
                //@Gia_2 decimal
                sql += "N'" + e.Value.ToString() + "'";
                _NhapSi_Class.set(sql);
                //load lai
                int thamso = 1;
                string[] arr_name = new string[thamso];
                object[] arr_value = new object[thamso];
                arr_name[0] = "@Ngay";
                arr_value[0] = Get_CauHinh.Ngay_DN;
                _NhapSi_Class.get_store("sp_NhapSiDaLuu_TheoNgay", arr_name, arr_value, thamso);

                Filter_theolai(select_thuonglai_id,select_thuonglai_name);

                //load_nhapsi(dr["ID_Hang"].ToString(), id_ncc_selected, dr["Tbl_Hang_Name"].ToString());
                gv_nhapsi.RefreshData();
                //load lai nhom va thuong lai
              
                //dts_thuonglai = _NhapSi_Class.get_dataset("select * from NhapSi_GiaChiTiet_Thuonglai where Date_Num_Final_UpDate = " + Get_CauHinh.Ngay_DN + "");              
                //dts_loaica = _NhapSi_Class.get_dataset("select * from NhapSi_GiaChiTiet_Loaica where Date_Num_Final_UpDate = " + Get_CauHinh.Ngay_DN + "");
               
            
                #endregion
            }
            else
            {
                gv_loaica.CancelUpdateCurrentRow();
            }
        }
        #endregion

        

        #region click link
        void linkloaica_Click(object sender, EventArgs e)
        {
            int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
            DataRow dr = gv_loaica.GetDataRow(gv_loaica.FocusedRowHandle);

            string filter_string = "select * from NhapSi_DaLuu where Date_Num_Final_UpDate = " + ng + "  AND NHOM_NCC_ID ='985A1EB2-AA36-47B8-AB09-6FD46BA93903'";
            if (select_thuonglai_name != "__Tất Cả__")
            {
                filter_string += " AND ID_NCC = '" + select_thuonglai_id + "'";
              //  
            }
            if (dr["Tbl_Hang_Name"].ToString() != "__Tất Cả__")
            {
                filter_string += " AND ID_Hang = '" + dr["ID_Hang"].ToString().ToUpper() + "'";
                select_ca_name = dr["Tbl_Hang_Name"].ToString();
                select_ca_id = dr["ID_Hang"].ToString().ToUpper();
            }
            else
            {
                select_ca_name = "Tất Cả";
            }
          //  filter_string += " order by Tbl_NCC_Name asc,Tbl_Hang_Name asc,Gia desc";
            filter_string += " order by Tbl_NCC_Name asc,Tbl_Hang_Name asc,Tbl_KhachHang_Name asc,Gia desc";
            dts_dulieu = _NhapSi_Class.get_dataset(filter_string);

            dts_dulieu.Tables[0].Columns.Add("CA_View");
            lever1("Tbl_Hang_Name", "CA_View");
           // dts_dulieu.Tables[0].Columns.Add("TL_View");
            dts_dulieu.Tables[0].Columns.Add("Select", typeof(bool));

            gc_nhapsi.DataSource = dts_dulieu.Tables[0];
            look_loaica_daluu.DataSource = DuLieu.List_Hang.Tables[0];
            look_khachhang_daluu.DataSource = DuLieu.List_KhachHang.Tables[0];
            look_thuonglai_daluu.DataSource = DuLieu.List_NCC.Tables[0];

            tencachon.Text = select_ca_name;
            tongluongca.Text = ca_sl.SummaryText;
            tongtienca.Text = ca_thanhtien.SummaryText;
        }
        public void lever1(string filename, string colum)
        {
            if (dts_dulieu.Tables[0].Rows.Count > 0)
            {
                string lever1 = dts_dulieu.Tables[0].Rows[0][filename].ToString();
                dts_dulieu.Tables[0].Rows[0][colum] = lever1;
                dts_dulieu.Tables[0].AcceptChanges();
                for (int i = 1; i < dts_dulieu.Tables[0].Rows.Count; i++)
                {
                    if (lever1 == dts_dulieu.Tables[0].Rows[i][filename].ToString())
                    {
                        //xoa trang                       
                        dts_dulieu.Tables[0].Rows[i][colum] = "        ''        ";
                        dts_dulieu.Tables[0].AcceptChanges();
                    }
                    else
                    {
                        lever1 = dts_dulieu.Tables[0].Rows[i][filename].ToString();
                        dts_dulieu.Tables[0].Rows[i][colum] = lever1;
                        dts_dulieu.Tables[0].AcceptChanges();
                    }
                }
            }
        }
        void linktlai_Click(object sender, EventArgs e)
        {
            int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
            gc_nhapsi.DataSource = null;
            DataRow dr = gv_thuonglai.GetDataRow(gv_thuonglai.FocusedRowHandle);
            if (dr["Tbl_NCC_Name"].ToString() == "__Tất Cả__")
            {
                //load tat ca ca
                string sql_loaica = "select * from NhapSi_GiaChiTiet_Loaica where Date_Num_Final_UpDate = " + ng + "  AND _ID1 ='985A1EB2-AA36-47B8-AB09-6FD46BA93903'";
                sql_loaica += " order by Tbl_Hang_Name asc";
                dts_loaica = _NhapSi_Class.get_dataset(sql_loaica);

                DataRow dr1 = dts_loaica.Tables[0].NewRow();
                dr1["Tbl_Hang_Name"] = "__Tất Cả__";
                dts_loaica.Tables[0].Rows.Add(dr1);

                select_thuonglai_name = "__Tất Cả__";
                gc_loaica.DataSource = dts_loaica.Tables[0];
                Tenlaichon.Text = "Tất Cả";
                Tongluonglai.Text = thuonglai_tongluong.SummaryText;
                tongtienlai.Text = thuonglai_tongtien.SummaryText;
            }
            else
            {
                select_thuonglai_id = dr["ID_NCC"].ToString().ToUpper();
                select_thuonglai_name = dr["Tbl_NCC_Name"].ToString();
                string sql_loaica = "select * from NhapSi_GiaChiTiet_Loaica where Date_Num_Final_UpDate = " + ng + " and ID_NCC = '" + select_thuonglai_id + "' AND _ID1 ='985A1EB2-AA36-47B8-AB09-6FD46BA93903'";
                sql_loaica += " order by Tbl_Hang_Name asc";
                dts_loaica = _NhapSi_Class.get_dataset(sql_loaica);

                DataRow dr1 = dts_loaica.Tables[0].NewRow();
                dr1["Tbl_Hang_Name"] = "__Tất Cả__";
                dts_loaica.Tables[0].Rows.Add(dr1);

                
                gc_loaica.DataSource = dts_loaica.Tables[0];
                Tenlaichon.Text = select_thuonglai_name;
                Tongluonglai.Text = thuonglai_tongluong.SummaryText;
                tongtienlai.Text = thuonglai_tongtien.SummaryText;
            }

        }
        #endregion

        #region lệnh dùng chung

        public void them()
        {
           
        }
      
        public void excel()
        {
        }
        public void xoa()
        {
            if (Pow.kiemtra("CapNhatGiaChiTietBien", "Xoa", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                if (msgbox.dialog_yes_no("Xóa dòng chọn?") == DialogResult.Yes)
                {
                    DataRow[] dr = dts_dulieu.Tables[0].Select("Select = " + true + "");
                    for (int i = 0; i < dr.Length; i++)
                    {
                        string sql = "NhapSi_Xoa '" + dr[i]["ID"].ToString().ToUpper() + "'";
                        _NhapSi_Class.set(sql);
                        dts_dulieu.Tables[0].Rows.Remove(dr[i]);
                    }
                    gv_nhapsi.RefreshData();
                  

                }
            }
        }
        public void xoa_dangnhap()
        {
            
        }
        public void xoa_daluu()
        {
            
        }
        public void chon()
        {
           
            for (int i = 0; i < gv_nhapsi.DataRowCount; i++)
            {
              
                DataRow dtr = gv_nhapsi.GetDataRow(i);
                dtr["Select"] = true;
                dts_dulieu.Tables[0].AcceptChanges();
            }
            
            gv_nhapsi.RefreshData();

        }
        public void bochon()
        {
            for (int i = 0; i < dts_dulieu.Tables[0].Rows.Count; i++)
            {
                dts_dulieu.Tables[0].Rows[i]["Select"] = false;
            }
            dts_dulieu.Tables[0].AcceptChanges();
            gv_nhapsi.RefreshData();
        }
        public void chep()
        {
            
        }
        public void tailai()
        {
            load();
        }

        #endregion

        #region menu click
        public void event_click()
        {
        
            btn_del.Click += new EventHandler(btn_del_Click);
            btn_all.Click += new EventHandler(btn_all_Click);
            btn_none.Click += new EventHandler(btn_none_Click);
           // btn_close.Click += new EventHandler(btn_close_Click);
            btn_load.Click += new EventHandler(btn_load_Click);
        }

        void btn_load_Click(object sender, EventArgs e)
        {
            tailai();
        }

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

        void btn_none_Click(object sender, EventArgs e)
        {
            bochon();
        }

        void btn_all_Click(object sender, EventArgs e)
        {
            chon();
        }

        void btn_copy_Click(object sender, EventArgs e)
        {
            chep();
        }

        void btn_del_Click(object sender, EventArgs e)
        {
            xoa();
        }

        void btn_new_Click(object sender, EventArgs e)
        {
            them();
        }

        void btn_copy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            chep();
        }

        void btn_none_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bochon();
        }

        void btn_all_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            chon();
        }

        void btn_del_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xoa();
        }

        void btn_excel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            excel();
        }
        #endregion
       
        #region load
        NhapSi_Class _NhapSi_Class = new NhapSi_Class();
        NhomNCC_Class _NhomNCC_Class = new NhomNCC_Class();
        ChonNhom_Form _ChonNhom_Form;
       
        public void load()
        {
            int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
            string sql = "select * from NhapSi_GiaChiTiet_Thuonglai_bien where Date_Num_Final_UpDate = " + ng + " ORDER BY Tbl_NCC_Name ASC";
            dts_thuonglai = _NhapSi_Class.get_dataset(sql);
           DataRow dr = dts_thuonglai.Tables[0].NewRow();
           dr["Tbl_NCC_Name"] = "__Tất Cả__";
           dts_thuonglai.Tables[0].Rows.Add(dr);
           gc_thuonglai.DataSource = dts_thuonglai.Tables[0];
           Tennhomchon.Text = "Biển";
           Tongluongnhom.Text = nhom_sl.SummaryText;
           tongtiennhom.Text = nhom_thanhtien.SummaryText;
           //load dlieu nhap si
           string filter_string = "select * from NhapSi_DaLuu where Date_Num_Final_UpDate = " + ng + "  AND NHOM_NCC_ID ='985A1EB2-AA36-47B8-AB09-6FD46BA93903'";


           filter_string += " order by Tbl_NCC_Name asc,Tbl_KhachHang_Name asc,Tbl_Hang_Name asc,Gia desc";
           dts_dulieu = _NhapSi_Class.get_dataset(filter_string);

           dts_dulieu.Tables[0].Columns.Add("KH_View");
           lever1("Tbl_KhachHang_Name", "KH_View");
           dts_dulieu.Tables[0].Columns.Add("TL_View");
           //lever1("Tbl_NCC_Name", "TL_View");
           dts_dulieu.Tables[0].Columns.Add("Select", typeof(bool));
        }
      
        DataSet dts_thuonglai,dts_loaica,dts_dulieu;
      
        #endregion

        #region data
        Hang_Class _Hang_Class = new Hang_Class();
        KhachHang_Class _KhachHang_Class = new KhachHang_Class();
        


        public void data()
        {

            //_KhachHang_Class.get("Select Tbl_KhachHang.*,Tbl_NhomKhachHang.Name as _ID1_Name from Tbl_KhachHang INNER JOIN Tbl_NhomKhachHang on Tbl_KhachHang._ID1 = Tbl_NhomKhachHang.ID order by Tbl_NhomKhachHang.Name asc, Tbl_KhachHang.Name ASC");
            //_Hang_Class.get("Select Tbl_Hang.*,Tbl_NhomHang.Name as _ID1_Name from Tbl_Hang INNER JOIN Tbl_NhomHang on Tbl_Hang._ID1 = Tbl_NhomHang.ID order by Tbl_NhomHang.Name asc, Tbl_Hang.Name ASC");
            


        }

        #endregion

        #region load filter
        
        public void Filter_theolai(string truonghop,string tenlai)
        {
            if (truonghop == "Trong")
            {
                select_thuonglai_id = truonghop;
                select_thuonglai_name = tenlai;
                gc_loaica.DataSource = null;
                //thong ke
                Tenlaichon.Text = tenlai;
                Tongluonglai.Text = "0 (kg)";
                tongtienlai.Text = "0,00 (kg)";
                Filter_theoca("Trong", "Không có");
            }
            else
            {
                if (truonghop == "Auto")//AUTO LA TAT CA
                {
                    DataRow dr_auto = gv_thuonglai.GetDataRow(0);
                    select_thuonglai_id = dr_auto["ID_NCC"].ToString().ToUpper();
                    select_thuonglai_name = dr_auto["Tbl_NCC_Name"].ToString();
                    //bat dau filter
                    DataView dtv = new DataView(dts_loaica.Tables[0]);
                    dtv.RowFilter = "ID_NCC = '" + select_thuonglai_id.ToUpper() + "'";
                    gc_loaica.DataSource = dtv;
                    gv_loaica.RefreshData();
                    //thong ke                  
                    Tenlaichon.Text = select_thuonglai_name;
                    Tongluonglai.Text = thuonglai_tongluong.SummaryText;
                    tongtienlai.Text = thuonglai_tongtien.SummaryText;
                    //
                    if (dtv.Count > 0)
                    {
                        Filter_theoca("Auto", "");
                    }
                    else
                    {
                        Filter_theoca("Trong", "Không có");
                    }
                }
                else // truong hop nay da co chinh xac ID thuong lai
                {
                    select_thuonglai_id = truonghop;
                    select_thuonglai_name = tenlai;
                    DataView dtv = new DataView(dts_loaica.Tables[0]);
                    dtv.RowFilter = "ID_NCC = '" + select_thuonglai_id.ToUpper() + "'";
                   
                    gc_loaica.DataSource = dtv;
                    gv_loaica.RefreshData();
                    //thong ke
                    Tenlaichon.Text = select_thuonglai_name;
                    Tongluonglai.Text = thuonglai_tongluong.SummaryText;
                    tongtienlai.Text = thuonglai_tongtien.SummaryText;
                    if (dtv.Count > 0)
                    {                       
                        if (select_ca_id != "Trong" && select_ca_id != "Auto" && select_ca_id != "")
                        {
                            Filter_theoca(select_ca_id, select_ca_name);
                        }
                        else
                        {
                            Filter_theoca("Auto", "");
                        }
                    }
                    else
                    {
                        Filter_theoca("Trong", "Không có");
                    }
                }
            }

        }
        public void Filter_theoca(string truonghop, string tenca)
        {
            if (truonghop == "Trong")
            {
                select_ca_id = "Trong";
                select_ca_name = "Không có";
                //load
                gc_nhapsi.DataSource = null;
                look_loaica_daluu.DataSource = DuLieu.List_Hang.Tables[0];
                look_khachhang_daluu.DataSource = DuLieu.List_KhachHang.Tables[0];
                tencachon.Text = select_ca_name;
                tongluongca.Text = "0 (kg)";
                tongtienca.Text = "0,00 (vnđ)";
            }
            else
            {
                if (truonghop == "Auto")
                {
                    DataRow dr = gv_loaica.GetDataRow(0);
                    select_ca_id = dr["ID_Hang"].ToString().ToUpper();
                    select_ca_name = dr["Tbl_Hang_Name"].ToString();
                    DataView dtv = new DataView(dts_dulieu.Tables[0]);
                    dtv.RowFilter = "ID_NCC = '" + select_thuonglai_id.ToUpper() + "' AND ID_Hang = '" + select_ca_id.ToUpper() + "'";
                    if (dtv.Count > 0)
                    {
                      
                        gc_nhapsi.DataSource = dtv;
                        gv_nhapsi.RefreshData();
                        gv_nhapsi.ExpandAllGroups();
                        look_loaica_daluu.DataSource = DuLieu.List_Hang.Tables[0];
                        look_khachhang_daluu.DataSource = DuLieu.List_KhachHang.Tables[0];

                        tencachon.Text = select_ca_name;
                        tongluongca.Text = ca_sl.SummaryText;
                        tongtienca.Text = ca_thanhtien.SummaryText;
                    }
                    else
                    {
                        select_ca_id = "Trong";
                        select_ca_name = "Không có";
                        //load
                        gc_nhapsi.DataSource = null;
                        look_loaica_daluu.DataSource = DuLieu.List_Hang.Tables[0];
                        look_khachhang_daluu.DataSource = DuLieu.List_KhachHang.Tables[0];
                        tencachon.Text = select_ca_name;
                        tongluongca.Text = "0 (kg)";
                        tongtienca.Text = "0,00 (vnđ)";
                    }
                }
                else // truong hop nay da co chinh xac ID Ca
                {
                    select_ca_id = truonghop.ToUpper();
                    select_ca_name = tenca;
                    DataView dtv = new DataView(dts_dulieu.Tables[0]);
                    dtv.RowFilter = "ID_NCC = '" + select_thuonglai_id.ToUpper() + "' AND ID_Hang = '" + select_ca_id.ToUpper() + "'";
                    if (dtv.Count > 0)
                    {
                       
                        gc_nhapsi.DataSource = dtv;
                        gv_nhapsi.RefreshData();
                        gv_nhapsi.ExpandAllGroups();
                        look_loaica_daluu.DataSource = DuLieu.List_Hang.Tables[0];
                        look_khachhang_daluu.DataSource = DuLieu.List_KhachHang.Tables[0];

                        tencachon.Text = select_ca_name;
                        tongluongca.Text = ca_sl.SummaryText;
                        tongtienca.Text = ca_thanhtien.SummaryText;
                    }
                    else
                    {
                        Filter_theoca("Auto", "");
                        //select_ca_id = "Trong";
                        //select_ca_name = "Không có";
                        ////load
                        //gc_nhapsi.DataSource = null;
                        //look_loaica_daluu.DataSource = DuLieu.List_Hang.Tables[0];
                        //look_khachhang_daluu.DataSource = DuLieu.List_KhachHang.Tables[0];
                        //tencachon.Text = select_ca_name;
                        //tongluongca.Text = "0 (kg)";
                        //tongtienca.Text = "0,00 (vnđ)";
                    }
                }
            }
            
        }
        #endregion

        private void CapNhatGiaChiTiet_Load(object sender, EventArgs e)
        {
            load();
        }
    }
}
