using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using System.Xml;
using System.IO;
using System.Data;
using ABC.NhanVien;
using ABC.DanhMuc.NhomNCC;
using DevExpress.XtraEditors;
using DevExpress.XtraLayout;
using ABC.File;
using ABC.DanhMuc;

namespace ABC.NhomNCC
{
    
    public partial class NhomToaLai_Form : DevExpress.XtraEditors.XtraForm {
        NhomNCC_Class _NhomNCC_Class = new NhomNCC_Class();
        public DataRow dr; 
        #region hàm dựng
        public NhomToaLai_Form()
        {           
            InitializeComponent();           
            this.Text = Get_CauHinh.Dt_DAT.Rows[0]["Name_Soft"].ToString();      
            simpleButton_ok.Click += new EventHandler(simpleButton_ok_Click);
        }
        #endregion
        private bool KiemtraNhapLieu(string chuoi)
        {
            bool isTrung = false;
            foreach (char item in chuoi)
            {
                if (char.IsLetterOrDigit(item) == false && char.IsWhiteSpace(item) == false)
                {
                    isTrung = true;
                    break;
                }
            }
            return isTrung;
        }
        #region ham save
        public void save()
        {
            #region
            bool Next = true;
            string ms = "";

            if (txtName.Text == "")
            {
                Next = false;
                ms = "Bạn Chưa nhập tên.";
            }
            if (KiemtraNhapLieu(txtName.Text.Trim()))
            {
                Next = false;
                ms = "Vui lòng tên nhập không chứa kí tự đặc biệt!";
            }
            if (Next == true)
            {
                string sql = "insert into Tbl_NhomToa(TenNhomToa)values(N'"+txtName.Text+"') ";
                
                _NhomNCC_Class.set(sql);
                DuLieu.LuuLuocSu(Main_Form._tk, this.Name, "", txtName.Text.ToUpper(), "Thêm");
                this.Text = this.Text + ".";
                if (msgbox.dialog_yes_no("Đã lưu \n \n Bạn muốn tiếp tục không ?") == DialogResult.Yes)
                {
                    txtName.Focus();
                    txtName.BackColor = Color.NavajoWhite;
                    txtName.Focus();
                }
                else
                {
                    this.Close();
                }



            }
            else
            {
                msgbox.dialog_OK_warning(ms);
            }

            #endregion
        }
        #endregion

        #region button event
        void simpleButton_ok_Click(object sender, EventArgs e)
        {
            save();
        }
        #endregion

        #region load
        private void CauHinh_Form_Load(object sender, EventArgs e)
        {
            if (dr != null)
            {
                txtName.Text = dr["TenNhomToa"].ToString();
                
            }
        }
        #endregion  

        #region phim tat 
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Enter))
            {
               
            }
            if (keyData == (Keys.Control|Keys.S))
            {
                save();
            }
           return base.ProcessCmdKey(ref msg, keyData);
        }

        #endregion

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            string sql = "select TenNhomToa as Ten from Tbl_NhomToa where TenNhomToa like N'" + txtName.Text.Trim().ToUpper() + "%'";
            ABC.KetNoi.Class_KetNoi db = new KetNoi.Class_KetNoi();
            DataTable dt = db.T_opendataset(sql).Tables[0];
            if (dt.Rows.Count > 0)
                labelControl1.Text = "NHÓM TOA LÁI: có " + dt.Rows.Count.ToString() + " thông tin gần giống.Click!";
            else
                labelControl1.Text = "NHÓM TOA LÁI";
        }

        private void labelControl1_Click(object sender, EventArgs e)
        {
            if (labelControl1.Text.Contains("gần giống"))
            {
                string sql = "select TenNhomToa as Ten from Tbl_NhomToa where TenNhomToa like N'" + txtName.Text.Trim().ToUpper() + "%'";
                Form frm = new frmPreview(sql);
                frm.ShowDialog();
            }
        }
    }
}
