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.NhanVien;
using DevExpress.XtraEditors;
using DevExpress.XtraLayout;
using ABC.File;
using ABC.DanhMuc;
using ABC.KetNoi;

namespace ABC.NhanVien
{
    
    public partial class frmNhanVienCan : DevExpress.XtraEditors.XtraForm {
        NhanVien_Class _NhanVien_Class = new NhanVien_Class();
        public DataRow dr; 
        #region hàm dựng
        public frmNhanVienCan()
        {           
            InitializeComponent();           
            this.Text = Get_CauHinh.Dt_DAT.Rows[0]["Name_Soft"].ToString();      
            simpleButton_ok.Click += new EventHandler(simpleButton_ok_Click);
        }
        public frmNhanVienCan(string id)
        {
            InitializeComponent();
            this.Text = Get_CauHinh.Dt_DAT.Rows[0]["Name_Soft"].ToString();
            _id = id;
            simpleButton_ok.Click += new EventHandler(simpleButton_ok_Click);
        }
        string _id = "";
        #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 (txtMaNV.Text == "")
            {
                Next = false;
                ms = "Bạn Chưa nhập tên.";
            }
            if (KiemtraNhapLieu(txtMaNV.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 = "select * from LichSu_NguoiCan where MaCan like N'" + txtMaNV.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)
                {
                    Next = false;
                    ms = "Trùng tên.";
                }

            }

            if (Next == true)
            {
                string sql = "sp_LichSu_NguoiCan_Insert ";
                sql += "N'" + txtMaNV.Text.Trim().ToUpper() + "',";
                sql += "N'" + txtHoTen.Text.Trim().ToUpper() + "'";
                DuLieu.LuuLuocSu(Main_Form._tk, this.Name, " ", txtMaNV.Text.ToUpper()+" - "+txtHoTen.Text.ToUpper(),"Thêm");
                _NhanVien_Class.set(sql);
                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)
                {
                    txtMaNV.Focus();
                    txtMaNV.BackColor = Color.NavajoWhite;
                    txtMaNV.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)
        {
            Class_KetNoi cls = new Class_KetNoi();
            int thamso = 1;
            string[] arr_name = new string[thamso];
            object[] arr_value = new object[thamso];
            arr_name[0] = "@IDCan";
            arr_value[0] = _id;
            DataTable dt = cls.T_opendataset_store("sp_LichSu_NguoiCan_Load_id", arr_name, arr_value, thamso).Tables[0];
            if(dt.Rows.Count>0)
            {
                txtMaNV.Text = dt.Rows[0]["MaCan"].ToString();
                txtHoTen.Text = dt.Rows[0]["HoTen"].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 * from LichSu_NguoiCan where MaCan like N'" + txtMaNV.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 && txtMaNV.Text != "")
                labelControl1.Text = "Khai báo Nhân Viên: có " + dt.Rows.Count.ToString() + " thông tin gần giống.Click!";
            else
                labelControl1.Text = "Khai báo Nhân Viên";
        }

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