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

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

namespace ABC.NhomNCC
{
    
    public partial class NhomToaLai : DevExpress.XtraEditors.XtraUserControl {
        public NhomToaLai()
        {           
            InitializeComponent();
            this.Text = "Danh mục nhóm toa lái";
            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);
                save(dr);
            }
        }
        public void save(DataRow dr)
        {
            if (Pow.kiemtra("NhomToaLai", "Sua", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region

                string sql = "update Tbl_NhomToa set TenNhomToa=N'"+ dr["TenNhomToa"].ToString()+ "' where IDNhomToa='"+ dr["IDNhomToa"].ToString()+"'";

                _NhomNCC_Class.set(sql);
                Class_KetNoi cls = new Class_KetNoi();
                DataTable dt = cls.T_opendataset("select * from Tbl_NhomToa where IDNhomToa='" + dr["IDNhomToa"].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
        NhomToaLai_Form _NhomNCC_Form;
        public void them()
        {
            if (Pow.kiemtra("NhomToaLai", "Them", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                _NhomNCC_Form = new NhomToaLai_Form();
                _NhomNCC_Form.TextChanged += new EventHandler(_NhanVien_Form_TextChanged);
                _NhomNCC_Form.FormClosed += new FormClosedEventHandler(_NhomNCC_Form_FormClosed);
                _NhomNCC_Form.ShowDialog();
            }
        }

        void _NhomNCC_Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            tailai();
        } 
        void _NhanVien_Form_TextChanged(object sender, EventArgs e)
        {
            _NhomNCC_Class.get_store("sp_NhomToa_load");
            tailai();
            data();
            gv.RefreshData();

        }
        string _IDNhom = "0";
        public void excel()
        {
        }
        public void xoa()
        {
            if (Pow.kiemtra("NhomToaLai", "Xoa", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                if (msgbox.dialog_yes_no("Xóa dòng chọn?") == DialogResult.Yes)
                {
                    string sql = "Delete from Tbl_NhomToa where IDNhomToa = '" + _IDNhom + "'";
                    _NhomNCC_Class.set(sql);
                    //
                    string ndCu = "";
                    for (int j = 0; j < gv.Columns.Count; j++)
                    {
                        ndCu += gv.Columns[j].FieldName + "(" + gv.GetRowCellValue(gv.FocusedRowHandle, gv.Columns[j].FieldName).ToString().Trim() + ") ";
                    }
                    DuLieu.LuuLuocSu(Main_Form._tk, this.Name, ndCu, "", "Xoá");
                    tailai();
                    data();

                }
            }
        }
        public void sua()
        {
        }       
        public void chon()
        {
           
            gv.RefreshData();
        }
      
        public void chep()
        {
            DataRow dr = gv.GetDataRow(gv.FocusedRowHandle);
            if (Pow.kiemtra("NhomToaLai", "Them", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                _NhomNCC_Form = new NhomToaLai_Form();
                _NhomNCC_Form.TextChanged += new EventHandler(_NhanVien_Form_TextChanged);
                _NhomNCC_Form.dr = dr;
                _NhomNCC_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(bt//n_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
        NhomNCC_Class _NhomNCC_Class = new NhomNCC_Class();
     
        public void data()
        {
            clsConnect cn = new clsConnect();
            gc.DataSource = cn.LoadTable("Select *,cast(0 as bit) as [Select] from Tbl_NhomToa order by TenNhomToa ASC");
            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 + "\\NhomToaLai.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_NhomToa where TenNhomToa=@TenNhomToa";
                        int thamso1 = 1;
                        string[] name1 = new string[thamso1];
                        object[] value1 = new object[thamso1];
                        name1[0] = "@TenNhomToa";
                        value1[0] = _name;
                        DataTable dt1 = cls.LoadTable(sql1, name1, value1, thamso1).Tables[0];
                        if (dt1.Rows.Count == 0)
                        {
                            //
                            string sql = "insert into Tbl_NhomToa(TenNhomToa)" +
                            "values(@TenNhomToa)";
                            cls.UpdateTable(sql, name1, value1, thamso1);
                        }
                    }
                }
                MessageBox.Show("Đã đưa vào được " + count.ToString() + " dòng!");
                tailai();
                data();
                #endregion
            }
        }

        private void gv_CustomRowCellEditForEditing(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            try
            {
                _IDNhom = gv.GetFocusedRowCellValue("IDNhomToa").ToString().Trim();
            }
            catch (Exception)
            {
            }
        }
    }
}
