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 TraNo_CongNo_SoThuLu : DevExpress.XtraEditors.XtraForm
    {

        public TraNo_CongNo_SoThuLu(string _ngay, DataSet dt, string filter_string_Lu)
        {
            InitializeComponent();
            _filter_string_Lu = filter_string_Lu;
            NgayXem = _ngay;
            _dt = dt;
            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
          
        }
        string _filter_string_Lu = "";
        string NgayXem;
        DataSet _dt;
        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);
            int ng = (int)(Convert.ToDateTime(NgayXem) - new DateTime(1900, 1, 1)).TotalDays;
            _T_KhaiBaoCongNo_SotM.ngayxem = ng;
            _T_KhaiBaoCongNo_SotM.ShowDialog();
        }

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


        DataTable dtGrid;
        void gv_daluu_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
           
        }
#pragma warning disable CS0414 // The field 'TraNo_CongNo_SoThuLu.kh_ten_choos' is assigned but its value is never used
        string kh_ten_choos = "";
#pragma warning restore CS0414 // The field 'TraNo_CongNo_SoThuLu.kh_ten_choos' is assigned but its value is never used
#pragma warning disable CS0414 // The field 'TraNo_CongNo_SoThuLu.kh_id_choos' is assigned but its value is never used
        string kh_id_choos = "";        
#pragma warning restore CS0414 // The field 'TraNo_CongNo_SoThuLu.kh_id_choos' is assigned but its value is never used
#pragma warning disable CS0414 // The field 'TraNo_CongNo_SoThuLu.ngay' is assigned but its value is never used
        int ngay = 0;
#pragma warning restore CS0414 // The field 'TraNo_CongNo_SoThuLu.ngay' is assigned but its value is never used
       
        
      
        #region gv_daluu
        void gv_daluu_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (Pow.kiemtra("CongNo_QLSanPham", "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("Lu trả vược mức Lu nợ.");
                    gv_daluu.CancelUpdateCurrentRow();
                }
                else
                {
                    #region
                    // them chi tiet moi
                    string sql = "Tbl_TraLu_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);
                    DuLieu.LuuLuocSu(Main_Form._tk, this.Name, "", dt_update["ID_KhachHang"].ToString().ToUpper() + ": " + e.Value, "Thêm");
                    //
                    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_QLSanPham", "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_QLSanPham", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {               
            }
        }
        
        Class_KetNoi db = new Class_KetNoi();
#pragma warning disable CS0169 // The field 'TraNo_CongNo_SoThuLu.ngaydetru' is never used
        int ngaydetru;
#pragma warning restore CS0169 // The field 'TraNo_CongNo_SoThuLu.ngaydetru' is never used
        public void tailai()
        {
            Get_CauHinh.NgayTinhNo_sot = (int)(Convert.ToDateTime(NgayXem) - new DateTime(1900, 1, 1)).TotalDays;
            DuLieu.CongNo_1Ngay_Lu = db.T_opendataset(_filter_string_Lu);
            data();
        }
#pragma warning disable CS0169 // The field 'TraNo_CongNo_SoThuLu.dts' is never used
        DataSet dts;
#pragma warning restore CS0169 // The field 'TraNo_CongNo_SoThuLu.dts' is never used
       
        #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();
                    _MuonSot.ngaymuon = NgayXem;
            _MuonSot.FormClosed += new FormClosedEventHandler(_MuonSot_FormClosed);
            _MuonSot.ShowDialog();
        }

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

        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_del_Click(object sender, EventArgs e)
        {
            btn_capnhat();
        }

       


        #endregion

        #region data       
#pragma warning disable CS0169 // The field 'TraNo_CongNo_SoThuLu.dtv_songay' is never used
        DataView dtv_songay;
#pragma warning restore CS0169 // The field 'TraNo_CongNo_SoThuLu.dtv_songay' is never used
        public void data()
        {
           // DuLieu.CongNo_1Ngay_Lu.Tables.Remove("Table");
            dtGrid = DuLieu.CongNo_1Ngay_Lu.Tables[0];
            if (dtGrid.Rows.Count > 0)
            {
                DataTable dt_SP_ketChuyen = db.T_opendataset("sp_CongNoSP_Chot").Tables[0];
                DataView view1 = dt_SP_ketChuyen.Copy().DefaultView;
                for (int i = 0; i < dtGrid.Rows.Count; i++)
                {
                    view1.RowFilter = "ID_KhachHang='" + dtGrid.Rows[i]["ID_KhachHang"].ToString() + "'";
                    double TonS = 0, TonK = 0, TonL = 0, TonC = 0, TonB = 0;
                    if (view1.ToTable().Rows.Count > 0)
                    {
                        TonS = double.Parse(view1.ToTable().Rows[0]["Ton_sot"].ToString());
                        TonK = double.Parse(view1.ToTable().Rows[0]["Ton_ket"].ToString());
                        TonL = double.Parse(view1.ToTable().Rows[0]["Ton_lu"].ToString());
                        TonC = double.Parse(view1.ToTable().Rows[0]["Ton_cat"].ToString());
                        TonB = double.Parse(view1.ToTable().Rows[0]["Ton_bom"].ToString());
                    }
                    dtGrid.Rows[i]["Ton"] = double.Parse(dtGrid.Rows[i]["Ton"].ToString()) + TonL;
                    dtGrid.Rows[i]["NoCu"] = double.Parse(dtGrid.Rows[i]["NoCu"].ToString()) + TonL;
                    dtGrid.Rows[i]["TongNo"] = double.Parse(dtGrid.Rows[i]["TongNo"].ToString()) + TonL;
                }
            }
            else
            {
                DataTable dt1 = db.T_opendataset("sp_CongNoSP_Lu").Tables[0];
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    DataRow row = dtGrid.NewRow();
                    row["ID_KhachHang"] = dt1.Rows[i]["ID_KhachHang"].ToString();
                    row["Tbl_KhachHang_Name"] = dt1.Rows[i]["Tbl_KhachHang_Name"].ToString();
                    row["Ton"] = dt1.Rows[i]["Ton_lu"].ToString();
                    row["NoCu"] = dt1.Rows[i]["Ton_lu"].ToString();
                    row["TongNo"] = dt1.Rows[i]["Ton_lu"].ToString();
                    dtGrid.Rows.Add(row);
                }
            }
            gc_daluu.DataSource = dtGrid;
                gv_daluu.ExpandAllGroups();
                gv_daluu.ExpandAllGroups();


        }
        #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)
            //{


            //}
        }

       

    }
}
