using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using DevExpress.XtraReports.UI;
using ABC.KetNoi;
using DevExpress.XtraSplashScreen;

using ABC.Report;
using ABC.File;

namespace ABC.CongNo
{
    public partial class ChiTietMuonCat : DevExpress.XtraEditors.XtraForm
    {
        Class_KetNoi db = new Class_KetNoi();
        public int ngay_tinh;
        public string select_khachhang_name;
        public ChiTietMuonCat()
        {
            
            InitializeComponent();
            look_khachhang.Focus();
            this.Load += new EventHandler(TienTra_Load);
            simpleButton1.Click += new EventHandler(simpleButton1_Click);
            simpleButton2.Click += new EventHandler(simpleButton2_Click);         
           
            danhmuc();
            
            look_khachhang.SelectAll();
            look_khachhang.Select();
            look_khachhang.Text = "";
            look_khachhang.EditValue = null;
            look_khachhang.ClosePopup();
            look_khachhang.Focus();
         

        }
        void TienTra_Load(object sender, EventArgs e)
        {
            #region
            #endregion
            
        }

        #region danhmuc
        public void danhmuc()
        {
            look_khachhang.Properties.DataSource = DuLieu.List_KhachHang.Tables[0];            
            look_khachhang.Enter += new EventHandler(look_khachhang_Enter);
            look_khachhang.Leave += new EventHandler(look_khachhang_Leave);
            look_khachhang.KeyPress += new KeyPressEventHandler(look_khachhang_KeyPress);
           
        }

        void look_khachhang_KeyPress(object sender, KeyPressEventArgs e)
        {
            

        }

        #region
        void look_khachhang_Leave(object sender, EventArgs e)
        {
            look_khachhang.BackColor = Color.White;
        }

        void look_khachhang_Enter(object sender, EventArgs e)
        {
            look_khachhang.BackColor = Color.NavajoWhite;
        }
        #endregion

        #endregion

        

      

        #region button
        void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
       
        void simpleButton1_Click(object sender, EventArgs e)
        {
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
            if (look_khachhang.EditValue == "" || look_khachhang.EditValue == null)
            {
                msgbox.dialog_OK_info("Vui lòng chọn khách hàng.");
            }
            else
            {
                tailai();
                print();
            }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
        }
        #endregion
        int ng, ngayden;
        int nodau, conlai;
        int tongmuon, tongtra;
        int tongtienhang, tongthanhtoan;
        public void tailai()
        {
            #region
            ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
             ngayden = (int)(Convert.ToDateTime(txtngayden.Text) - new DateTime(1900, 1, 1)).TotalDays;
           String filter_string = "XemNo_1Ngay_Cat " + (ng-1) + ",";
           filter_string += "Null,";
           filter_string += "Null,";
           filter_string += "'" + look_khachhang.EditValue.ToString().ToUpper() + "'";
           dt_main = db.T_opendataset(filter_string);
           dt_main.Tables.Remove("Table");
            #endregion
            //
            #region cong no ket chuyen
            //lay ds khach
            DataTable dtKhach = db.T_opendataset("sp_KhachHang_TheoID '" + look_khachhang.EditValue.ToString().Trim() + "'").Tables[0];
            for (int i = 0; i < dtKhach.Rows.Count; i++)
            {
                DataRow row = dt_main.Tables[0].NewRow();
                DataView viewKH = dt_main.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;
                    dt_main.Tables[0].Rows.Add(row);
                }
            }
            DataTable dtCongNo_SP_old = db.T_opendataset("sp_CongNoSP_TheoID '" + look_khachhang.EditValue.ToString().Trim() + "'").Tables[0];
            for (int i = 0; i < dt_main.Tables[0].Rows.Count; i++)
            {
                string _IDKhachHang = dt_main.Tables[0].Rows[i]["ID_KhachHang"].ToString().Trim();
                double _NoCu = (dt_main.Tables[0].Rows[i]["NoCu"].ToString().Trim() == "") ? 0 : double.Parse(dt_main.Tables[0].Rows[i]["NoCu"].ToString().Trim());
                double _TongNo = (dt_main.Tables[0].Rows[i]["TongNo"].ToString().Trim() == "") ? 0 : double.Parse(dt_main.Tables[0].Rows[i]["TongNo"].ToString().Trim());
                double _ConLai = (dt_main.Tables[0].Rows[i]["Ton"].ToString().Trim() == "") ? 0 : double.Parse(dt_main.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_ket"].ToString() == "") ? 0 : double.Parse(view2.ToTable().Rows[0]["Ton_ket"].ToString());
                    dt_main.Tables[0].Rows[i]["NoCu"] = _NoCu + _TonChot;
                    dt_main.Tables[0].Rows[i]["TongNo"] = _TongNo + _TonChot;
                    dt_main.Tables[0].Rows[i]["Ton"] = _ConLai + _TonChot;
                }
            }
            if (dt_main.Tables[0].Rows.Count == 0)
            {
                for (int i = 0; i < dtCongNo_SP_old.Rows.Count; i++)
                {
                    DataRow row = dt_main.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_ket"].ToString();
                    row["TongNo"] = dtCongNo_SP_old.Rows[i]["Ton_ket"].ToString();
                    row["Ton"] = dtCongNo_SP_old.Rows[i]["Ton_ket"].ToString();
                    dt_main.Tables[0].Rows.Add(row);
                }

            }
            #endregion
            //
            #region tao bang mau
            dt_mau = new DataTable();
           dt_mau.Columns.Add("Ngay");
           dt_mau.Columns.Add("Ngay_Num");
           dt_mau.Columns.Add("No", typeof(double));
           dt_mau.Columns.Add("Tra", typeof(double));
           dt_mau.Columns.Add("ConLai", typeof(double));

           DataTable dt_muon = db.T_opendataset("sp_ChiTiet_muonCat " + ng +"," + ngayden +",'"+ look_khachhang.EditValue.ToString().ToUpper() + "'").Tables[0];
           DataTable dt_tra = db.T_opendataset("sp_ChiTiet_traCat  " + ng + "," + ngayden + ",'" + look_khachhang.EditValue.ToString().ToUpper() + "'").Tables[0];



           if (dt_main.Tables[0].Rows.Count > 0)
           {
               nodau = int.Parse(dt_main.Tables[0].Rows[0]["Ton"].ToString());
           }
           else
           {
               nodau = 0;
           }
           conlai = nodau;
           tongtienhang = 0;
           tongthanhtoan = 0;
           for (int i = ng; i <= ngayden; i++)
           {
               DataRow[] dr_no = dt_muon.Select("NgayPhatSinhNo = "+i+"");
               DataRow[] dr_tra = dt_tra.Select("NgayPhatSinhNo = " + i + "");
               if(dr_no.Length>0 && dr_tra.Length>0)
               {
                   DataRow dr_new = dt_mau.NewRow();
                   dr_new["Ngay"] = Convert.ToDateTime(dr_no[0]["TO_TEXT"].ToString()).ToShortDateString();
                   int ngay_num = (int)(Convert.ToDateTime(dr_no[0]["TO_TEXT"].ToString()) - new DateTime(1900, 1, 1)).TotalDays;
                   dr_new["Ngay_Num"] = ngay_num;
                   tongmuon = int.Parse(dr_no[0]["TongMuon"].ToString());
                   tongtra = int.Parse(dr_tra[0]["TongTra"].ToString());
                   conlai = (conlai + tongmuon) - tongtra;
                   dr_new["No"] = tongmuon;
                   dr_new["Tra"] = tongtra;
                   dr_new["ConLai"] = conlai;
                   dt_mau.Rows.Add(dr_new);
                   dt_mau.AcceptChanges();
                   tongtienhang += tongmuon;
                   tongthanhtoan += tongtra;
               }
               else
               {
                   if (dr_no.Length > 0 && dr_tra.Length == 0)
                   {
                       DataRow dr_new = dt_mau.NewRow();
                       dr_new["Ngay"] =Convert.ToDateTime(dr_no[0]["TO_TEXT"].ToString()).ToShortDateString();
                       int ngay_num = (int)(Convert.ToDateTime(dr_no[0]["TO_TEXT"].ToString()) - new DateTime(1900, 1, 1)).TotalDays;
                       dr_new["Ngay_Num"] = ngay_num;
                       tongmuon = int.Parse(dr_no[0]["TongMuon"].ToString());
                       tongtra = 0;
                       conlai = (conlai + tongmuon) - tongtra;
                       dr_new["No"] = tongmuon;
                       dr_new["Tra"] = tongtra;
                       dr_new["ConLai"] = conlai;
                       dt_mau.Rows.Add(dr_new);
                       dt_mau.AcceptChanges();
                       tongtienhang += tongmuon;
                       
                   }
                   else
                   {
                       if (dr_no.Length == 0 && dr_tra.Length > 0)
                       {
                           DataRow dr_new = dt_mau.NewRow();
                           dr_new["Ngay"] = Convert.ToDateTime(dr_tra[0]["TO_TEXT"].ToString()).ToShortDateString();
                           int ngay_num = (int)(Convert.ToDateTime(dr_tra[0]["TO_TEXT"].ToString()) - new DateTime(1900, 1, 1)).TotalDays;
                           dr_new["Ngay_Num"] = ngay_num;
                           tongmuon = 0;
                           tongtra = int.Parse(dr_tra[0]["TongTra"].ToString());
                           conlai = (conlai + tongmuon) - tongtra;
                           dr_new["No"] = tongmuon;
                           dr_new["Tra"] = tongtra;
                           dr_new["ConLai"] = conlai;
                           dt_mau.Rows.Add(dr_new);
                           dt_mau.AcceptChanges();
                           
                           tongthanhtoan += tongtra;
                       }
                       else
                       {
                           //khong lam gi het
                       }
                   }
               }





              
           }
            #endregion
        }
        DataSet dt_main;
        DataTable dt_mau;


        public void print()
        {
            #region


            Get_CauHinh.ch1kh_ngay = "Từ Ngày : " + txtngay.Value.ToShortDateString() + "  Đến Ngày : " + txtngayden.Value.ToShortDateString();
            Get_CauHinh.ch1kh_nocu = nodau.ToString() + " (Cắt)";
            // chitietreport.gc = "Từ Ngày " + dateTimePicker1.Value.ToShortDateString();
            Get_CauHinh.ch1kh_tongtienhang = tongtienhang.ToString() + " (Cắt)";
            Get_CauHinh.ch1kh_thanhtoan = tongthanhtoan.ToString() + " (Cắt)";
            Get_CauHinh.ch1kh_conlai = conlai.ToString() + " (Cắt)";
            Get_CauHinh.ch1kh_kh = look_khachhang.Text;

            T_ct_kh1_cat rapport = new T_ct_kh1_cat();
            dt_mau.DefaultView.Sort = "Ngay_Num ASC";
            dt_mau.AcceptChanges();
            
            rapport.DataSource = dt_mau;
            rapport.ShowRibbonPreview();
            #endregion
        }
    }
}