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.File;

namespace ABC.CongNo
{
    public partial class MuonKet : DevExpress.XtraEditors.XtraForm
    {
        Class_KetNoi db = new Class_KetNoi();
        public int ngay_tinh;
        public string select_khachhang_name;
        public string ngaymuon;
        public MuonKet()
        {
            
            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();
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
            if (look_khachhang.EditValue != null && look_khachhang.EditValue != "")
            {
                select_khachhang_name = look_khachhang.Text;
                if (this.Text.ToString().Substring(0, 1) == "+")
                {
                    this.Text = "-" + "Cập nhật kết mượn ngày "+Get_CauHinh.NgayTinhNo_text+"";
                }
                else
                {
                    this.Text = "+" + "Cập nhật kết mượn ngày " + Get_CauHinh.NgayTinhNo_text + "";
                }
            }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast

        }

      
       
      

        void TienTra_Load(object sender, EventArgs e)
        {
            #region
            txttien.Text = "";
            txtngay.Text = ngaymuon;
            
            
            #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);
            txttien.KeyPress += new KeyPressEventHandler(txttien_KeyPress);
            txttien.Enter += new EventHandler(txttien_Enter);
            txttien.Leave += new EventHandler(txttien_Leave);
        }
        string ID_KH;
        string Ten_KH;
#pragma warning disable CS0169 // The field 'MuonKet.TongNo' is never used
        int TienMat,TongNo;
#pragma warning restore CS0169 // The field 'MuonKet.TongNo' is never used
        public bool kientra_tongtra_va_tongno(decimal _tongtra,decimal _Tongno)
        {
            #region
            //if (_tongtra > _Tongno)
            //{
            //    msgbox.dialog_OK_error("Tiền trả vược mức tiền nợ .");
            //    return false;
            //}
            //else
            //{
            //    return true;
            //}
            return true;
            #endregion
        }
        public int check_tienmat()
        {
            #region
            if (txttien.Text == "")
            {
                return -2;
            }
            else
            {
                try
                {
                    int tm = int.Parse(txttien.Text);
                    return tm;
                }
                catch
                {

                    return -1;
                }
            }
            #endregion
        }
        void txttien_KeyPress(object sender, KeyPressEventArgs e)
        {
            #region
            if (e.KeyChar == 13)
            {
                save();
         
            }
            else
            {
                if (e.KeyChar == 27)
                {
                    this.DialogResult = DialogResult.Cancel;
                    this.Close();
                }
            }
            #endregion

        }

        #region lever
        void txttien_Leave(object sender, EventArgs e)
        {
            txttien.BackColor = Color.White;
        }

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

        }
        #endregion

        public void load_chitiet(string id_kh)
        {
            #region
           
            #endregion

        }
        void look_khachhang_KeyPress(object sender, KeyPressEventArgs e)
        {
            #region
            if (e.KeyChar == 13)
            {
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
                if (look_khachhang.EditValue != null && look_khachhang.EditValue != "")
                {
                    load_chitiet(look_khachhang.EditValue.ToString());
                    txttien.Focus();
                    select_khachhang_name = look_khachhang.Text;
                    if (this.Text.ToString().Substring(0, 1) == "+")
                    {
                        this.Text = "-" + "Cập nhật Kết mượn ngày " + Get_CauHinh.NgayTinhNo_text + "";
                    }
                    else
                    {
                        this.Text = "+" + "Cập nhật Kết mượn ngày " + Get_CauHinh.NgayTinhNo_text + "";
                    }
                }
                else
                {
                    msgbox.dialog_OK_warning("Vui lòng chọn khách hàng");
                }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            }
            else
            {
                if (e.KeyChar == 27)
                {
                    txttien.Focus();
                    look_khachhang.EditValue = null;
                    look_khachhang.Text = "";
                    look_khachhang.ClosePopup();
                }
            }
            #endregion

        }

        #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

        public void save()
        {
            #region
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
            if (look_khachhang.EditValue != null&&look_khachhang.EditValue != "")
            {
                ID_KH = look_khachhang.EditValue.ToString().ToUpper();
                Ten_KH = look_khachhang.Text;
                TienMat = check_tienmat();
               
                    if (TienMat == -1)
                    {
                        msgbox.dialog_OK_error("Nhập sai định dạng số");
                        txttien.SelectAll();
                        txttien.Focus();
                    }
                    else
                    {
                        if (TienMat == -2)
                        {
                            msgbox.dialog_OK_error("vui lòng nhập số Kết mượn.");
                            txttien.SelectAll();
                            txttien.Focus();
                        }
                        else
                        {
                           //save database
                             //EXEC Tbl_SOTNO_insert
                             //      @ID_KhachHang
                             //     ,@ID
                             //     ,'SI'
                             //     ,@Date_Num_Final_UpDate
                             //     ,@Sot
                            int ng = (int)(Convert.ToDateTime(txtngay.Text) - new DateTime(1900, 1, 1)).TotalDays;
                            string sql = "Tbl_KETNO_insert '" + ID_KH.ToUpper() + "','" + Guid.NewGuid().ToString().ToUpper() + "','Muon'," + ng + ",'" + TienMat + "'";
                            db.T_exc(sql);
                        DuLieu.LuuLuocSu(Main_Form._tk, this.Name, "", Ten_KH + " " + TienMat.ToString(), "Thêm");
                        this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                    }
            }
            else
            {
                msgbox.dialog_OK_warning("Vui lòng chọn khách hàng");
            }
#pragma warning restore CS0252 // Possible unintended reference comparison; left hand side needs cast
            #endregion
        }

      

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

        void simpleButton1_Click(object sender, EventArgs e)
        {
            if (txttien.Text != "")
            {
                save();
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
       
        }
#endregion

     
    


      
    }
}