using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using ABC.DanhMuc.NhomNCC;
using ABC.NhomNCC;
using System.Xml;
using System.IO;
using ABC.GiaoDich;
using ABC.DanhMuc.KhachHang;
using System.Data;
using ABC.KetNoi;

namespace ABC.BanLe
{
    
    public partial class ThanhToan_Le : DevExpress.XtraEditors.XtraForm {

        public string ID_KH, Ten_KH;
        public decimal TienMat;
        public ThanhToan_Le()
        {          

            InitializeComponent();
            danhmuc();
            simpleButton1.Click += new EventHandler(simpleButton1_Click);    
            simpleButton2.Click += new EventHandler(simpleButton2_Click);
        }
        #region danhmuc
        public void danhmuc()
        {
            Class_KetNoi db = new Class_KetNoi();
            string sql = @"Select Tbl_KhachHang.*,Tbl_NhomKhachHang.Name as _ID1_Name,Tbl_KhachHang.ID as ID_KhachHang ,  
                    Tbl_KhachHang.Name as Tbl_KhachHang_Name  
                    from Tbl_KhachHang 
                    INNER JOIN Tbl_NhomKhachHang on Tbl_KhachHang._ID1 = Tbl_NhomKhachHang.ID 
             where    Tbl_KhachHang.IS_User=1 
                     order by Tbl_NhomKhachHang.Name asc, Tbl_KhachHang.Name ASC";
            DataTable dt = db.T_opendataset(sql).Tables[0];
            //look_khachhang.Properties.DataSource = DuLieu.List_KhachHang.Tables[0];
            look_khachhang.Properties.DataSource = dt;
            btn_ThemKH.Click += new EventHandler(btn_ThemKH_Click);
            look_khachhang.Enter += new EventHandler(look_khachhang_Enter);
            look_khachhang.Leave += new EventHandler(look_khachhang_Leave);
            look_khachhang.KeyPress += new KeyPressEventHandler(look_khachhang_KeyPress);
            txttien.KeyPress += new KeyPressEventHandler(txttien_KeyPress);
            txttien.Enter += new EventHandler(txttien_Enter);
            txttien.Leave += new EventHandler(txttien_Leave);
        }
        void txttien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (look_khachhang.EditValue != null)
                {
                    ID_KH = look_khachhang.EditValue.ToString().ToUpper();
                    Ten_KH = look_khachhang.Text;
                    TienMat = check_tienmat();
                    if (TienMat <= DuLieu.Le_TongTien)
                    {
                        if (TienMat == -1)
                        {
                            msgbox.dialog_OK_error("Nhập sai định dạng số");
                        }
                        else
                        {
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                    }
                    else
                    {
                        msgbox.dialog_OK_error("Tiền trả vược mức tiền mua hàng \n chức năng bán lẽ chỉ ghi nợ khi tiền trả trong mức tiền hàng");
                    }

                }
                else
                {
                    msgbox.dialog_OK_warning("Vui lòng chọn khách hàng");
                }
            }
            else
            {
                if (e.KeyChar == 27)
                {
                    this.DialogResult = DialogResult.Cancel;
                    this.Close();
                }
            }
        }
        void txttien_Leave(object sender, EventArgs e)
        {
            txttien.BackColor = Color.White;
        }

        void txttien_Enter(object sender, EventArgs e)
        {
            txttien.BackColor = Color.NavajoWhite;

        }
        void look_khachhang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {                
                if (look_khachhang.EditValue != null && look_khachhang.EditValue!="")
                {
                    txttien.Focus();
                    double Tien = 0;
                    Class_KetNoi db = new Class_KetNoi();
                    DataTable dtCongNo_old = db.T_opendataset("sp_CongNo_TheoIDKhachHang '" + look_khachhang.EditValue.ToString() + "'").Tables[0];
                    if (dtCongNo_old.Rows.Count > 0)
                        Tien = double.Parse(dtCongNo_old.Rows[0]["ChotTien"].ToString());
                    //load no
                    DataRow[] dr = DuLieu.CongNo_1Ngay.Tables[0].Select("ID_KhachHang = '"+look_khachhang.EditValue.ToString().ToUpper()+"'");
                    if (dr.Length > 0)
                    {
                        //du lieu ket chuyen
                        double nocus = double.Parse(dr[0]["NoCu"].ToString())+ Tien;
                        double nomois = double.Parse(dr[0]["NoMoi"].ToString());
                        double tongnos = double.Parse(dr[0]["TongNo"].ToString())+ Tien;

                        nocu.Text = String.Format("{0:#,#}", nocus) + " (vnđ)";
                        
                        nomoi.Text = String.Format("{0:#,#}", nomois) + " (vnđ)";
                        tongno.Text = String.Format("{0:#,#}", tongnos) + " (vnđ)";
                    }
                    else
                    {
                        nocu.Text = String.Format("{0:#,#}", Tien) + " (vnđ)";

                        nomoi.Text = String.Format("{0:#,#}", 0) + " (vnđ)";
                        tongno.Text = String.Format("{0:#,#}", Tien) + " (vnđ)";
                    }
                }
                else
                {
                    msgbox.dialog_OK_warning("Vui lòng chọn khách hàng");
                }
            }
            else
            {
                if (e.KeyChar == 27)
                {
                    txttien.Focus();
                    look_khachhang.EditValue = null;
                    look_khachhang.Text = "";
                    look_khachhang.ClosePopup();
                }
            }
        }
        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;
        }
        KhachHang_Forms _KhachHang_Forms;
        KhachHang_Class _KhachHang_Class = new KhachHang_Class();
        void btn_ThemKH_Click(object sender, EventArgs e)
        {
            KhachHang_Forms frm = new KhachHang_Forms();
           // _KhachHang_Forms.FormClosed += new FormClosedEventHandler(_KhachHang_Forms_FormClosed);
            frm.ShowDialog();
            _KhachHang_Class.get_store("sp_KhachHang_Load");
            look_khachhang.Properties.DataSource = DuLieu.List_KhachHang.Tables[0];
        }
        void _KhachHang_Forms_FormClosed(object sender, FormClosedEventArgs e)
        {
            _KhachHang_Class.get_store("sp_KhachHang_Load");
            look_khachhang.Properties.DataSource = DuLieu.List_KhachHang.Tables[0];
            look_khachhang.Focus();
        }
        #endregion

        public decimal check_tienmat()
        {
            #region
            if (txttien.Text == "")
            {
                return 0;
            }
            else
            {
                try
                {
                    decimal tm = decimal.Parse(txttien.Text);
                    return tm;
                }
                catch
                {

                    return -1;
                }
            }
            #endregion
        }
        void simpleButton1_Click(object sender, EventArgs e)
        {
            if (look_khachhang.EditValue != null)
            {
                ID_KH = look_khachhang.EditValue.ToString().ToUpper();
                Ten_KH = look_khachhang.Text;
                TienMat = check_tienmat();
                if (TienMat <= DuLieu.Le_TongTien)
                {
                    if (TienMat == -1)
                    {
                        msgbox.dialog_OK_error("Nhập sai định dạng số");
                    }
                    else
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                }
                else
                {
                    msgbox.dialog_OK_error("Tiền trả vược mức tiền mua hàng \n chức năng bán lẽ chỉ ghi nợ khi tiền trả trong mức tiền hàng");
                }
            }
            else
            {
                msgbox.dialog_OK_warning("Vui lòng chọn khách hàng");
            }
        }
        

      

        void simpleButton2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

   
      
     


     

    }
}
