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

namespace ABC.NhomHang
{
    
    public partial class NhomHang_Form : DevExpress.XtraEditors.XtraForm {
        NhomHang_Class _NhomHang_Class = new NhomHang_Class();
        public DataRow dr; 
        #region hàm dựng
        public NhomHang_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)
            {
                DataRow[] dr = DuLieu.List_Nhom_Hang.Tables[0].Select("Name = '" + txtName.Text.ToUpper() + "'");
                if (dr.Length > 0)
                {
                    Next = false;
                    ms = "Trùng tên.";
                }

            }

            if (Next == true)
            {
                string sql = "Tbl_NhomHang_insert ";
                sql += "N'" + Guid.NewGuid().ToString().ToUpper() + "',";
                sql += "N'" + txtName.Text.ToUpper() + "',";
                sql += "N'" + Note.Text + "',";
                
                sql += "N'" + Get_CauHinh.Ngay_DN + "',";
                sql += "N'" + Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper() + "',";
                sql += "N'1',";

                sql += "N'" + _Text1.Text + "',";
                sql += "N'',";
                sql += "N'',";
                sql += "N'',";
                sql += "N'',";
                sql += "N'',";
                sql += "N'',";
                sql += "N'',";
                sql += "N'',";

                sql += "NULL,";
                sql += "NULL,";
                sql += "NULL,";

                sql += "NULL,";
                sql += "NULL,";
                sql += "NULL";

                _NhomHang_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["Name"].ToString();
                Note.Text = dr["Note"].ToString();
                _Text1.Text = dr["_Text1"].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 [Name] as Ten from Tbl_NhomHang where [Name] 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 && txtName.Text != "")
                labelControl1.Text = "Khai báo nhóm cá: có " + dt.Rows.Count.ToString() + " thông tin gần giống.Click!";
            else
                labelControl1.Text = "Khai báo nhóm cá";
        }

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