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.GiaoDich;
using ABC.Report;
using ABC.KetNoi;
using DevExpress.XtraGrid.Columns;
using DevExpress.Data.Filtering;
using ABC.BaoCao;
using DevExpress.XtraReports.UI;
using ABC.File;

namespace ABC.CongNo
{
    public partial class CongNo_SoThuSot : DevExpress.XtraEditors.XtraUserControl
    {
        
        public CongNo_SoThuSot()
        {
            InitializeComponent();
            Get_CauHinh.NgayTinhNo = Get_CauHinh.Ngay_DN;
           // tailai();
            event_click();
            Look_khachhang.Properties.DataSource = DuLieu.List_KhachHang.Tables[0];
            
            gv_daluu.CellValueChanged+=new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv_daluu_CellValueChanged);
           gv_daluu.RowCellClick += new DevExpress.XtraGrid.Views.Grid.RowCellClickEventHandler(gv_daluu_RowCellClick);
            txtngay.Text = Get_CauHinh.ngay_text;

            this.Load += new EventHandler(CongNo_SoThuSot_Load);
           // btn_qlmuon.Click += new EventHandler(btn_qlmuon_Click);
           // btn_chitiet.Click += new EventHandler(btn_chitiet_Click);
            //khai bai report truoc
           
        }

        void btn_chitiet_Click(object sender, EventArgs e)
        {
            ChiTietMuonSot _ChiTietMuonSot = new ChiTietMuonSot();
            _ChiTietMuonSot.ShowDialog();
        }
        T_KhaiBaoCongNo_SotM _T_KhaiBaoCongNo_SotM;
        void btn_qlmuon_Click(object sender, EventArgs e)
        {
            QL_Muon();
        }
        public void QL_Muon()
        {
            _T_KhaiBaoCongNo_SotM = new T_KhaiBaoCongNo_SotM();
            _T_KhaiBaoCongNo_SotM.FormClosed += new FormClosedEventHandler(_T_KhaiBaoCongNo_SotM_FormClosed);
            if (check_ngay.Checked == true)
            {
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                _T_KhaiBaoCongNo_SotM.ngayxem = ng;
            }
            else
            {
                _T_KhaiBaoCongNo_SotM.ngayxem = Get_CauHinh.Ngay_DN;
            }
            _T_KhaiBaoCongNo_SotM.ShowDialog();
        }

        void _T_KhaiBaoCongNo_SotM_FormClosed(object sender, FormClosedEventArgs e)
        {
            tailai();
        }
        void CongNo_SoThuSot_Load(object sender, EventArgs e)
        {
            tailai();
            simpleButton1_Click_1(sender, e);
        }


        DataTable dtGrid;
        void gv_daluu_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            #region
            string col = e.Column.Caption;
            string colname = e.Column.FieldName;
            if (colname == "NoCu")
            {
                 kh_ten_choos = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["Tbl_KhachHang_Name"].ToString();
                 kh_id_choos = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang"].ToString();
               // ngay =int.Parse(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["NgayPhatSinhNo"].ToString());                
                xemnocu();
            }            
            if (colname == "NoMoi")
            {
                 kh_ten_choos = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["Tbl_KhachHang_Name"].ToString();
                 kh_id_choos = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["ID_KhachHang"].ToString();
               //  ngay = int.Parse(gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle)["NgayPhatSinhNo"].ToString());                
                xemnomoi();
            }
            #endregion
        }
        string kh_ten_choos = "";
        string kh_id_choos = "";        
        int ngay = 0;
        public void xemnomoi()
        {
            ngay = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;

            frmBaoCao_SoBanHang_ChiTiet_KH ct = new frmBaoCao_SoBanHang_ChiTiet_KH();
            ct.xemtuformcongno = true;
            ct.hinhthucxem = "NoMoi";
            ct.ngayxemcongno = txtngay.Value;
            ct.kh_ten_congno = kh_ten_choos;
            ct.kh_id_congno = kh_id_choos; ;            
            ct.ShowDialog();
        }
      
        public void xemnocu()
        {
            #region
            frmBaoCao_SoBanHang_ChiTiet_KH ct = new frmBaoCao_SoBanHang_ChiTiet_KH();
            ct.xemtuformcongno = true;
            ngay = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;            
            ct.ngayxemcongno = new DateTime(1900, 1, 1).AddDays(ngay-1);
            ct.hinhthucxem = "NoCu";
            // get ngay TRA
            string sql = "sp_ngay_tra_sau_cung "+ ngay+",'" + kh_id_choos + "'";
            DataTable DT_NGAYTRA = db.T_opendataset(sql).Tables[0];
            if (DT_NGAYTRA.Rows.Count > 0)
            {
                #region kh co ngay tra 
                int ngay_tra =int.Parse(DT_NGAYTRA.Rows[0]["NGAYTRASAUCUNG"].ToString());
                ct.ngaytrasaucung = new DateTime(1900, 1, 1).AddDays(ngay_tra);
                ct.kh_ten_congno = kh_ten_choos;
                ct.kh_id_congno = kh_id_choos; ;
                ct.ShowDialog();
                #endregion
            }
            else
            {
                #region kh khong co ngay tra
                //lay ngay mua dau tien
                ct.ngaytrasaucung = new DateTime(1900, 1, 1);
                ct.kh_ten_congno = kh_ten_choos;
                ct.kh_id_congno = kh_id_choos; ;
                ct.ShowDialog();
                #endregion
            }
            #endregion
        }
      
        #region gv_daluu
        void gv_daluu_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (Pow.kiemtra("CongNo_SoThuSot", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                DataRow dt_update = gv_daluu.GetDataRow(gv_daluu.FocusedRowHandle);
                decimal Tong_No = decimal.Parse(dt_update["TongNo"].ToString());
                decimal TogTra = decimal.Parse(e.Value.ToString());
                if (TogTra > Tong_No)
                {
                    msgbox.dialog_OK_warning("trả vược mức nợ.");
                    gv_daluu.CancelUpdateCurrentRow();
                }
                else
                {
                    #region

                    // xoa chi tiet
                    //DataView dtv = new DataView(DuLieu.ChiTiet_TraNo.Tables[0]);
                    //dtv.RowFilter = "ID_KhachHang = '" + dt_update["ID_KhachHang"].ToString().ToUpper() + "' and SuDung = 1 and NgayPhatSinhNo = " + Get_CauHinh.NgayTinhNo + "";

                    //for (int i = 0; i < dtv.Count; i++)
                    //{
                    //    string sql1 = "Tbl_ChiTietTraNo_XoaChiTiet ";
                    //    sql1 += "N'" + dtv[i]["ID_KhachHang"].ToString().ToUpper() + "',";
                    //    sql1 += "N'" + dtv[i]["Luc"].ToString() + "',";
                    //    sql1 += "N'" + dtv[i]["NgayPhatSinhNo"].ToString() + "'";
                    //    db.T_exc(sql1);
                    //}
                    // them chi tiet moi
                    string sql = "Tbl_TraSot_insert ";
                    //@ID_KhachHang uniqueidentifier,
                    sql += "N'" + dt_update["ID_KhachHang"].ToString().ToUpper() + "',";
                    //@Luc nvarchar(200),
                   // sql += "N'" + DateTime.Now.TimeOfDay.ToString() + "',";
                    //@NgayPhatSinhNo int,
                    sql += "N'" + Get_CauHinh.NgayTinhNo_sot + "',";
                    //@Tien decimal
                    sql += "N'" + e.Value + "'";
                    db.T_exc(sql);
                    //
                    tailai();
                    #endregion
                }
            }
            else
            {
                gv_daluu.CancelUpdateCurrentRow();
            }

        }
        #endregion

        #region lệnh dùng chung
        public void excel()
        {
        }

        #region cap nhat tien tra
        TienTra _TienTra;
        public void btn_capnhat()
        {
            if (Pow.kiemtra("CongNo_SoThuSot", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                //_TienTra = new TienTra();
                //_TienTra.FormClosed += new FormClosedEventHandler(_TienTra_FormClosed);
                //_TienTra.TextChanged += new EventHandler(_TienTra_TextChanged);
                //_TienTra.ShowDialog();
            }
            
        }
        string select_khachhang_name;
        
        void _TienTra_TextChanged(object sender, EventArgs e)
        {
            select_khachhang_name = _TienTra.select_khachhang_name;
           // msgbox.dialog_OK_info(select_khachhang_name);
           // gv_daluu.Columns["gridColumn1"].FilterInfo = new ColumnFilterInfo("[gridColumn1] LIKE '" + select_khachhang_name + "'");
           gv_daluu.ActiveFilterString = "[Tbl_KhachHang_Name] = '" + select_khachhang_name + "'";

      

            
        }

        void _TienTra_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_TienTra.DialogResult == DialogResult.OK)
            {
                tailai();
               
            }
            gv_daluu.ClearColumnsFilter();
            //CriteriaOperator expr0 = null;
            //gv_daluu.ActiveFilterCriteria = GroupOperator.And(new CriteriaOperator[] { expr0 });
            _TienTra = new TienTra();
        }
#endregion

        #region
        public void khaibao_nodau()
        {
            if (Pow.kiemtra("CongNo_SoThuSot", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                _T_KhaiBaoCongNo_Sot = new T_KhaiBaoCongNo_Sot();
                _T_KhaiBaoCongNo_Sot.FormClosed += new FormClosedEventHandler(_T_KhaiBaoCongNo_Sot_FormClosed);
                _T_KhaiBaoCongNo_Sot.ShowDialog();
                #endregion
            }
            else
            {
                msgbox.dialog_OK_warning("Bạn không có quyền cập nhật");
            }
        }
        T_KhaiBaoCongNo_Sot _T_KhaiBaoCongNo_Sot;
        void _T_KhaiBaoCongNo_Sot_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_T_KhaiBaoCongNo_Sot.DialogResult == DialogResult.OK)
            {
                tailai();
            }
        }
        #endregion

        public void sua()
        {
            if (Pow.kiemtra("CongNo_SoThuSot", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {               
            }
        }
        public void print5()
        {
            if (Pow.kiemtra("CongNo_SoThuSot", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                Get_CauHinh.sono1_sot = "Ngày " + txtngay.Value.Day.ToString() + " Tháng " + txtngay.Value.Month.ToString() + " Năm " + txtngay.Value.Year.ToString();
                // Remember_ID_combo_selected.tongno = "Tổng Tiền Nợ : " + String.Format("{0:N0}", tongcong);
                T_Tien_Sot_Moi rapport = new T_Tien_Sot_Moi();
                if (checksongay.Checked == false)
                {
                    
                    DataTable dt_in = DuLieu.CongNo_1Ngay_Sot.Tables[0];

                    DataRow[] drr = dt_in.Select("NoMoi = 0");
                    for (int i = 0; i < drr.Length; i++)
                        dt_in.Rows.Remove(drr[i]);
                    dt_in.AcceptChanges();
                    try
                    {
                        rapport.DataSource = dt_in;
                    }
                    catch
                    {
                        rapport.DataSource = dt_in;
                    }
                }
                else
                {
                   // rapport.DataSource = dtv_songay.ToTable();
                    DataTable dt_in = dtv_songay.ToTable();

                    DataRow[] drr = dt_in.Select("NoMoi = 0");
                    for (int i = 0; i < drr.Length; i++)
                        dt_in.Rows.Remove(drr[i]);
                    dt_in.AcceptChanges();
                    try
                    {
                        rapport.DataSource = dt_in;
                    }
                    catch
                    {
                        rapport.DataSource = dt_in;
                    }
                }
                rapport.ShowRibbonPreview();
            }
        }
        
        public void print1()
        {
            if (Pow.kiemtra("CongNo_SoThuSot", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                Get_CauHinh.sono1_sot = "Ngày " + txtngay.Value.Day.ToString() + " Tháng " + txtngay.Value.Month.ToString() + " Năm " + txtngay.Value.Year.ToString();
                // Remember_ID_combo_selected.tongno = "Tổng Tiền Nợ : " + String.Format("{0:N0}", tongcong);
                T_Tien1_Sot rapport = new T_Tien1_Sot();
                if (checksongay.Checked == false)
                {
                    rapport.DataSource = DuLieu.CongNo_1Ngay_Sot;
                }
                else
                {
                    rapport.DataSource = dtv_songay.ToTable();
                }
                rapport.ShowRibbonPreview();
            }
        }
        public void print2()
        {
            if (Pow.kiemtra("CongNo_SoThuSot", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                Get_CauHinh.sono1_sot = "Ngày " + txtngay.Value.Day.ToString() + " Tháng " + txtngay.Value.Month.ToString() + " Năm " + txtngay.Value.Year.ToString();
                // Remember_ID_combo_selected.tongno = "Tổng Tiền Nợ : " + String.Format("{0:N0}", tongcong);
                T_Tien_Sot rapport = new T_Tien_Sot();
                if (checksongay.Checked == false)
                {
                    rapport.DataSource = DuLieu.CongNo_1Ngay_Sot;
                }
                else
                {
                    rapport.DataSource = dtv_songay.ToTable();
                }
                rapport.ShowRibbonPreview();
            }
        }
        Class_KetNoi db = new Class_KetNoi();
#pragma warning disable CS0169 // The field 'CongNo_SoThuSot.ngaydetru' is never used
        int ngaydetru;
#pragma warning restore CS0169 // The field 'CongNo_SoThuSot.ngaydetru' is never used
        public void tailai()
        {

            string filter_string = "";

            if (check_ngay.Checked == true)
            {
                int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                //ngaydetru = ng;

                Get_CauHinh.NgayTinhNo_sot = ng;
                //Get_CauHinh.NgayTinhNo_text = txtngay.Text;



                filter_string = "XemNo_1Ngay_Sot " + ng + ",";
                //filter_string = "XemNo_1Ngay "+ng+",Null,Null,Null";
                if (checksongay.Checked == true && txtsongay.Text != null && txtsongay.Text != "")
                {
                    filter_string += txtsongay.Text + ",";

                }
                else
                {
                    filter_string += "Null,";
                }
                //------
                if (check_kytu.Checked == true && txtkhachhang.Text != null && txtkhachhang.Text != "")
                {
                    filter_string += "'" + txtkhachhang.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten.Checked == true && Look_khachhang.EditValue != null && Look_khachhang.EditValue != "")
                {
                    filter_string += "'" + Look_khachhang.EditValue.ToString().ToUpper() + "'";
                }
                else
                {
                    filter_string += "Null";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast

            }
            else
            {
                Get_CauHinh.NgayTinhNo_sot = Get_CauHinh.Ngay_DN;
                //Get_CauHinh.NgayTinhNo_text = Get_CauHinh.ngay_text;
               // ngaydetru = Get_CauHinh.Ngay_DN;


                filter_string = "XemNo_1Ngay_Sot " + Get_CauHinh.Ngay_DN + ",";
                if (checksongay.Checked == true && txtsongay.Text != null && txtsongay.Text != "")
                {
                    filter_string += "'" + txtsongay.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }
                //------
                if (check_kytu.Checked == true && txtkhachhang.Text != null && txtkhachhang.Text != "")
                {
                    filter_string += "'" + txtkhachhang.Text + "',";
                }
                else
                {
                    filter_string += "Null,";
                }

#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (check_ten.Checked == true && Look_khachhang.EditValue != null && Look_khachhang.EditValue != "")
                {
                    filter_string += "'" + Look_khachhang.EditValue.ToString().ToUpper() + "'";
                }
                else
                {
                    filter_string += "Null";
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }
            DuLieu.CongNo_1Ngay_Sot = db.T_opendataset(filter_string);

            // DuLieu.CongNo_1Ngay.Constraints.Remove(myTable.Constraints[i]);
            DuLieu.CongNo_1Ngay_Sot.Tables.Remove("Table");
            //lay ds khach
            DataTable dtKhach = Main_Form._dtKH;
            for (int i = 0; i < dtKhach.Rows.Count; i++)
            {
                DataRow row = DuLieu.CongNo_1Ngay_Sot.Tables[0].NewRow();
                DataView viewKH = DuLieu.CongNo_1Ngay_Sot.Tables[0].Copy().DefaultView;
                viewKH.RowFilter = "ID_KhachHang='" + dtKhach.Rows[i]["ID_KhachHang"].ToString() + "'";
                if (viewKH.ToTable().Rows.Count == 0)
                {
                    row["ID_KhachHang"] = dtKhach.Rows[i]["ID_KhachHang"].ToString();
                    row["Tbl_KhachHang_Name"] = dtKhach.Rows[i]["Tbl_KhachHang_Name"].ToString();
                    row["NoCu"] = 0;
                    row["NoMoi"] = 0;
                    row["MoiTra"] = 0;
                    row["TongNo"] = 0;
                    row["Ton"] = 0;
                    DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows.Add(row);
                }
            }
            DataTable dtCongNo_SP_old = db.T_opendataset("sp_CongNoSP_Chot").Tables[0];
            for (int i = 0; i < DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows.Count; i++)
            {
                string _IDKhachHang = DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows[i]["ID_KhachHang"].ToString().Trim();
                double _NoCu = (DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows[i]["NoCu"].ToString().Trim() == "") ? 0 : double.Parse(DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows[i]["NoCu"].ToString().Trim());
                double _TongNo = (DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows[i]["TongNo"].ToString().Trim() == "") ? 0 : double.Parse(DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows[i]["TongNo"].ToString().Trim());
                double _ConLai = (DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows[i]["Ton"].ToString().Trim() == "") ? 0 : double.Parse(DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows[i]["Ton"].ToString().Trim());
                DataView view2 = dtCongNo_SP_old.DefaultView;
                view2.RowFilter = "ID_KhachHang='" + _IDKhachHang + "'";
                if (view2.ToTable().Rows.Count > 0)
                {
                    double _TonChot = (view2.ToTable().Rows[0]["Ton_sot"].ToString() == "") ? 0 : double.Parse(view2.ToTable().Rows[0]["Ton_sot"].ToString());
                    DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows[i]["NoCu"] = _NoCu + _TonChot;
                    DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows[i]["TongNo"] = _TongNo + _TonChot;
                    DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows[i]["Ton"] = _ConLai + _TonChot;
                }
            }
            if (DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows.Count == 0)
            {
                for (int i = 0; i < dtCongNo_SP_old.Rows.Count; i++)
                {
                    DataRow row = DuLieu.CongNo_1Ngay_Sot.Tables[0].NewRow();
                    row["ID_KhachHang"] = dtCongNo_SP_old.Rows[i]["ID_KhachHang"].ToString();
                    row["Tbl_KhachHang_Name"] = dtCongNo_SP_old.Rows[i]["Tbl_KhachHang_Name"].ToString();
                    row["NoCu"] = dtCongNo_SP_old.Rows[i]["Ton_sot"].ToString();
                    row["TongNo"] = dtCongNo_SP_old.Rows[i]["Ton_sot"].ToString();
                    row["Ton"] = dtCongNo_SP_old.Rows[i]["Ton_sot"].ToString();
                    DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows.Add(row);
                }

            }
            DataView viewkq = DuLieu.CongNo_1Ngay_Sot.Tables[0].Copy().DefaultView;
            viewkq.RowFilter = "Ton>0 or TongNo>0";
            DuLieu.CongNo_1Ngay_Sot.Tables[0].Rows.Clear();
            DuLieu.CongNo_1Ngay_Sot.Tables[0].Merge(viewkq.ToTable());
            data();

        }
#pragma warning disable CS0169 // The field 'CongNo_SoThuSot.dts' is never used
        DataSet dts;
#pragma warning restore CS0169 // The field 'CongNo_SoThuSot.dts' is never used
        public void print1111()
        {
           
            if (Pow.kiemtra("CongNo_SoThuSot", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                //DataSet dsSelectData = new DataSet();

                //dsSelectData = DuLieu.BaoCao_SoBaoCao_Si_Bien;
                //Get_CauHinh.report_ngay = "BÁO CÁO NHẬP SĨ (BIỂN) NGÀY " + txtngay.Value.ToShortDateString();
                //T_Baocaotonghop rapport = new T_Baocaotonghop();
                //rapport.DataSource = dsSelectData;
                //rapport.LocationFloat = new DevExpress.Utils.PointFloat(10, 10);
                //rapport.ShowPreview();

                #endregion
            }
        }
        public void print_bill()
        {
            if (Pow.kiemtra("CongNo_SoThuSot", "Inan", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                Get_CauHinh.bill_no = "Ngày " + txtngay.Value.Day.ToString() + " Tháng " + txtngay.Value.Month.ToString() + " Năm " + txtngay.Value.Year.ToString();
               // Remember_ID_combo_selected.tongno = "Tổng Tiền Nợ : " + String.Format("{0:N0}", tongcong);
                T_bill_sot rapport = new T_bill_sot();
                if (checksongay.Checked == false)
                {
                    rapport.DataSource = DuLieu.CongNo_1Ngay_Sot;
                }
                else
                {
                    rapport.DataSource = dtv_songay.ToTable();
                }
                rapport.ShowRibbonPreview();
                #endregion
            }
        }
        #endregion

        #region menu click
        public void event_click()
        {
           
          //  //btn_tratien.Click += new EventHandler(btn_del_Click);          
          //  btn_print1.Click += new EventHandler(btn_all_Click);
          //  btn_print2.Click += new EventHandler(btn_none_Click);
          ////  btn_close.Click += new EventHandler(btn_close_Click);
            btn_load.Click += new EventHandler(btn_load_Click);
          //  btn_nodau.Click += new EventHandler(btn_in_Click);
          //  btn_bill.Click += new EventHandler(btn_sua_Click);
          //  btnxemghichu.Click += new EventHandler(btn_ghichu_Click);
          //  btn_muon.Click += new EventHandler(btn_muon_Click);
        }
        MuonSot _MuonSot;
        void btn_muon_Click(object sender, EventArgs e)
        {
            muon();
        }
        public void muon()
        {
            _MuonSot = new MuonSot();
            if(check_ngay.Checked==true)
                {
                _MuonSot.ngaymuon = txtngay.Text;
                }
            else
                {
                _MuonSot.ngaymuon = Get_CauHinh.ngay_text;
                }
            _MuonSot.FormClosed += new FormClosedEventHandler(_MuonSot_FormClosed);
            _MuonSot.ShowDialog();
        }

        void _MuonSot_FormClosed(object sender, FormClosedEventArgs e)
        {
            tailai();
        }
        void btn_ghichu_Click(object sender, EventArgs e)
        {

            print5();
            tailai();
        }
       

        void btn_sua_Click(object sender, EventArgs e)
        {
            print_bill();
        }

   

        void btn_in_Click(object sender, EventArgs e)
        {
            khaibao_nodau();
        }

        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)
        {
            print2();
        }

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

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

       


        #endregion

        #region data       
#pragma warning disable CS0649 // Field 'CongNo_SoThuSot.dtv_songay' is never assigned to, and will always have its default value null
        DataView dtv_songay;
#pragma warning restore CS0649 // Field 'CongNo_SoThuSot.dtv_songay' is never assigned to, and will always have its default value null
        public void data()
        {

            dtGrid = DuLieu.CongNo_1Ngay_Sot.Tables[0];
            gc_daluu.DataSource = dtGrid;
                gv_daluu.ExpandAllGroups();
                gv_daluu.ExpandAllGroups();


                //lbnocu.Text = cnocu.SummaryText;
                //lbnomoi.Text = cnomoi.SummaryText;
                //lbtongno.Text = ctongno.SummaryText;
                //lbtongtra.Text = ctongtra.SummaryText;
                //lbton.Text = cton.SummaryText;
                //if (lbnomoi.Text == "")
                //{
                //    lbnomoi.Text = "0";
                //}
                //if (lbnocu.Text == "")
                //{
                //    lbnocu.Text = "0";
                //}
                //if (lbtongno.Text == "")
                //{
                //    lbtongno.Text = "0";
                //}
                //if (lbtongtra.Text == "")
                //{
                //    lbtongtra.Text = "0";
                //}
                //if (lbton.Text == "")
                //{
                //    lbton.Text = "0";
                //}

        }
        #endregion

        private void gv_daluu_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            //try
            //{
            //    double _hanmuc = double.Parse(gv_daluu.GetRowCellValue(e.RowHandle, "HanMuc").ToString().Trim());
            //    double _ton = double.Parse(gv_daluu.GetRowCellValue(e.RowHandle, "Ton").ToString().Trim());
            //    if (_hanmuc < _ton && _hanmuc > 0)
            //    {
            //        e.Appearance.BackColor = Color.Red;
            //        e.Appearance.ForeColor = Color.White;
            //        //gv_daluu.SetRowCellValue(e.RowHandle, "VuotHanMuc", "x");
            //    }
            //}
            //catch (Exception)
            //{


            //}
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //DataView dv = dtGrid.Copy().DefaultView;
            //dv.RowFilter = "VuotHanMuc='x'";
            //frmVuot_DM_CongNo_SoThuSot frm = new frmVuot_DM_CongNo_SoThuSot(dv.ToTable());
            //frm.ShowDialog();
        }

        private void simpleButton1_Click_1(object sender, EventArgs e)
        {
            if (simpleButton1.Text.ToLower().Trim() == "mở điều kiện lọc")
            {
                panelControl2.Visible = true;
                simpleButton1.Text = "Tắt điều kiện lọc";
            }
            else
            {
                panelControl2.Visible = false;
                simpleButton1.Text = "Mở điều kiện lọc";
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_ghichu_Click(sender, e);
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_all_Click(sender, e);
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_none_Click(sender, e);
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_in_Click(sender, e);
        }

        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_sua_Click(sender, e);
        }

        private void barButtonItem6_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_chitiet_Click(sender, e);
        }

        private void barButtonItem7_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_muon_Click(sender, e);
        }

        private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_qlmuon_Click(sender, e);
        }
    }
}
