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

using System.Xml;
using System.IO;
using ABC.DanhMuc.NhomKhachHang;
using System.Data;
using ABC.File;
using DevExpress.XtraSpreadsheet;
using ABC.KetNoi;

namespace ABC.NhomKhachHang
{
    
    public partial class NhomKhachHang_Forms : DevExpress.XtraEditors.XtraUserControl {
        public NhomKhachHang_Forms()
        {           
            InitializeComponent();
            this.Text = "Danh mục nhóm khách hàng";
            event_click();
            data();
            gv.CellValueChanged += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv_CellValueChanged);
        }
        #region gv_value_changed
        void gv_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName != "Select")
            {
                DataRow dr = gv.GetDataRow(gv.FocusedRowHandle);
                if (e.Column.FieldName != "Name")
                {
                    save(dr);
                }
                else
                {
                    DataRow[] dr_name = DuLieu.List_Nhom_Hang.Tables[0].Select("Name = '" + e.Value.ToString().ToUpper() + "' AND ID <> '" + dr["ID"].ToString().ToUpper() + "'");
                    if (dr_name.Length > 0)
                    {
                        msgbox.dialog_OK_Questios("Trùng tên!");
                        gv.CancelUpdateCurrentRow();
                    }
                    else
                    {
                        save(dr);
                    }
                }
            }
        }
        public void save(DataRow dr)
        {
            if (Pow.kiemtra("NhomKhachHang_Forms", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region

                string sql = "Tbl_NhomKhachHang_insert ";
                sql += "N'" + dr["ID"].ToString().ToUpper() + "',";
                sql += "N'" + dr["Name"].ToString().ToUpper() + "',";
                sql += "N'" + dr["Note"].ToString() + "',";

                sql += "N'" + Get_CauHinh.Ngay_DN + "',";
                sql += "N'" + Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper() + "',";
                // sql += "N'1',";
                sql += dr["IS_User"].ToString() + ",";
                sql += "N'" + dr["_Text1"].ToString() + "',";
                sql += "N'',";
                sql += "N'',";
                sql += "N'',";
                sql += "N'',";
                sql += "N'',";
                sql += "N'',";
                sql += "N'" + dr["_Text8"].ToString() + "',";
                sql += "N'" + dr["_Text9"].ToString() + "',";

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

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

                _NhomKhachHang_Class.set(sql);
                Class_KetNoi cls = new Class_KetNoi();
                DataTable dt = cls.T_opendataset("select * from Tbl_NhomKhachHang where ID='" + dr["ID"].ToString() + "'").Tables[0];
                string ndCu = "", ndMoi = "";
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        ndCu += dt.Columns[i].ColumnName + "(" + dt.Rows[0][dt.Columns[i].ColumnName].ToString().Trim() + ") ";
                        ndMoi += dt.Columns[i].ColumnName + "(" + dr[dt.Columns[i].ColumnName].ToString().Trim() + ") ";
                    }
                }
                DuLieu.LuuLuocSu(Main_Form._tk, this.Name, ndCu, ndMoi, "Sửa");
                data();



                #endregion
            }
            else
            {
                gv.CancelUpdateCurrentRow();
            }
        }
        #endregion

        #region lệnh dùng chung
        NhomKhachHang_Form _NhomKhachHang_Form;
        public void them()
        {
            if (Pow.kiemtra("NhomKhachHang_Forms", "Them", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                _NhomKhachHang_Form = new NhomKhachHang_Form();
                _NhomKhachHang_Form.TextChanged += new EventHandler(_NhanVien_Form_TextChanged);
                _NhomKhachHang_Form.FormClosed += new FormClosedEventHandler(_NhomKhachHang_Form_FormClosed);
                _NhomKhachHang_Form.ShowDialog();
            }
        }

        void _NhomKhachHang_Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            tailai();
        } 
        void _NhanVien_Form_TextChanged(object sender, EventArgs e)
        {
            tailai();
            data();
            gv.RefreshData();

        }
        public void excel()
        {
        }
        public void xoa()
        {
            if (Pow.kiemtra("NhomKhachHang_Forms", "Xoa", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                if (msgbox.dialog_yes_no("Xóa dòng chọn?") == DialogResult.Yes)
                {
                    DataRow[] dr = DuLieu.List_Nhom_KhachHang.Tables[0].Select("Select = " + true + "");
                    for (int i = 0; i < gv.RowCount; i++)
                    {
                        if (gv.GetRowCellValue(i, "Select") != null)
                        {
                            string _key = gv.GetRowCellValue(i, "Select").ToString().ToLower();
                            if (_key == "true")
                            {
                                string _id = gv.GetRowCellValue(i, "ID").ToString().ToLower();
                                string sql = "Delete from Tbl_NhomKhachHang where ID = '" + _id + "'";
                                _NhomKhachHang_Class.set(sql);
                                //
                                string ndCu = "";
                                for (int j = 0; j < gv.Columns.Count; j++)
                                {
                                    ndCu += gv.Columns[j].FieldName + "(" + gv.GetRowCellValue(i, gv.Columns[j].FieldName).ToString().Trim() + ") ";
                                }
                                DuLieu.LuuLuocSu(Main_Form._tk, this.Name, ndCu, "", "Xoá");
                            }
                            tailai();
                            data();
                        }
                    }

                }
            }
        }
        public void sua()
        {
        }       
        public void chon()
        {
            for (int i = 0; i < DuLieu.List_Nhom_KhachHang.Tables[0].Rows.Count; i++)
            {
                DuLieu.List_Nhom_KhachHang.Tables[0].Rows[i]["Select"] = true;
            }
            DuLieu.List_Nhom_KhachHang.Tables[0].AcceptChanges();
            gv.RefreshData();
        }
        public void bochon()
        {
            for (int i = 0; i < DuLieu.List_Nhom_KhachHang.Tables[0].Rows.Count; i++)
            {
                DuLieu.List_Nhom_KhachHang.Tables[0].Rows[i]["Select"] = false;
            }
            DuLieu.List_Nhom_KhachHang.Tables[0].AcceptChanges();
            gv.RefreshData();
        }
        public void chep()
        {
            DataRow dr = gv.GetDataRow(gv.FocusedRowHandle);
            if (Pow.kiemtra("NhanVien_Forms", "Them", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                _NhomKhachHang_Form = new NhomKhachHang_Form();
                _NhomKhachHang_Form.TextChanged += new EventHandler(_NhanVien_Form_TextChanged);
                _NhomKhachHang_Form.dr = dr;
                _NhomKhachHang_Form.ShowDialog();
            }
        }
        public void tailai()
        {
           
            data();
        }
        #endregion

        #region menu click
        public void event_click()
        {
            //btn_new.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btn_new_ItemClick);
            //btn_excel.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btn_excel_ItemClick);
            //btn_del.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btn_del_ItemClick);
            //btn_all.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btn_all_ItemClick);
            //btn_none.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btn_none_ItemClick);
            //btn_copy.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btn_copy_ItemClick);
            btn_new.Click += new EventHandler(btn_new_Click);
            btn_del.Click += new EventHandler(btn_del_Click);
            btn_copy.Click += new EventHandler(btn_copy_Click);
            btn_all.Click += new EventHandler(btn_all_Click);
            btn_none.Click += new EventHandler(btn_none_Click);
          //  btn_close.Click += new EventHandler(btn_close_Click);
            btn_load.Click += new EventHandler(btn_load_Click);
        }

        void btn_load_Click(object sender, EventArgs e)
        {
            tailai();
        }

        void btn_close_Click(object sender, EventArgs e)
        {
           // this.Close();
        }

        void btn_none_Click(object sender, EventArgs e)
        {
            bochon();
        }

        void btn_all_Click(object sender, EventArgs e)
        {
            chon();
        }

        void btn_copy_Click(object sender, EventArgs e)
        {
            chep();
        }

        void btn_del_Click(object sender, EventArgs e)
        {
            xoa();
        }

        void btn_new_Click(object sender, EventArgs e)
        {
            them();
        }

        void btn_copy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            chep();
        }

        void btn_none_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bochon();
        }

        void btn_all_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            chon();
        }

        void btn_del_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xoa();
        }

        void btn_excel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            excel();
        }


        #endregion

        #region data
        NhomKhachHang_Class _NhomKhachHang_Class = new NhomKhachHang_Class();
        public void data()
        {
            //_NhomKhachHang_Class.get_store("sp_NhomKhachHang_Load");
            //gc.DataSource = DuLieu.List_Nhom_KhachHang.Tables[0];
            Class_KetNoi cls = new Class_KetNoi();
            //  _NhomKhachHang_Class.get_store("sp_NhomKhachHang_Load");
            gc.DataSource = cls.T_opendataset("Select *,cast (0 as bit)as 'Select' from Tbl_NhomKhachHang  order by Name ASC").Tables[0];
            gv.ExpandAllGroups();
        }
        #endregion

        private void NhanVien_Forms_Load(object sender, EventArgs e)
        {
           
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if(dialog.ShowDialog()==DialogResult.OK)
            {
                string path = dialog.SelectedPath;
                gv.ExportToXls(path + "\\NhomKH.xls");
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            clsConnect cls = new clsConnect();
            SpreadsheetControl sheet = new SpreadsheetControl();
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Excel Files(.xls)|*.xls|Excel Files(.xlsx)| *.xlsx";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                sheet.LoadDocument(dialog.FileName);
                #region code insert
                int count = sheet.ActiveWorksheet.Rows.LastUsedIndex;
                for (int i = 1; i <= count; i++)
                {
                    string _name = sheet.ActiveWorksheet.Rows[i]["A"].Value.ToString();
                    if (_name != "")
                    {
                        string sql1 = "select * from Tbl_NhomKhachHang where Name=@Name";
                        int thamso1 = 1;
                        string[] name1 = new string[thamso1];
                        object[] value1 = new object[thamso1];
                        name1[0] = "@Name";
                        value1[0] = _name;
                        DataTable dt1 = cls.LoadTable(sql1, name1, value1, thamso1).Tables[0];
                        if (dt1.Rows.Count == 0)
                        {
                            string sql = "insert into Tbl_NhomKhachHang(ID,Name,IS_User,Date_Num_Final_UpDate,User_ID_Final_UpDate)" +
                                "values(@ID,@Name,@IS_User,@Date_Num_Final_UpDate,@User_ID_Final_UpDate)";
                            int thamso = 5;
                            string[] name = new string[thamso];
                            object[] value = new object[thamso];
                            name[0] = "@ID";
                            name[1] = "@Name";
                            name[2] = "@IS_User";
                            name[3] = "@Date_Num_Final_UpDate";
                            name[4] = "@User_ID_Final_UpDate";
                            value[0] = Guid.NewGuid().ToString().ToUpper();
                            value[1] = _name;
                            value[2] = 1;
                            value[3] = Get_CauHinh.Ngay_DN;
                            value[4] = Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper();
                            cls.UpdateTable(sql, name, value, thamso);
                        }
                    }
                }
                MessageBox.Show("Đã đưa vào được "+count.ToString()+" dòng!");
                tailai();
                data();
                #endregion
            }

        }
    }
}
