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

using System.Xml;
using System.IO;
using ABC.DanhMuc.NhanVien;
using System.Data;
using ABC.File;
using DevExpress.XtraSpreadsheet;
using ABC.KetNoi;
using static DevExpress.XtraPrinting.Native.ExportOptionsPropertiesNames;

namespace ABC.NhanVien
{
    
    public partial class ucNhanVienCan : DevExpress.XtraEditors.XtraUserControl {
        public ucNhanVienCan()
        {           
            InitializeComponent();
            this.Text = "Danh mục nhân viên cân";
            event_click();
            data();
            gv.CellValueChanged += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(gv_CellValueChanged);
        }
        NhanVien_Class _NhanVien_Class = new NhanVien_Class();
        #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("ucNhanVienCan", "Sua",Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
                Class_KetNoi cls = new Class_KetNoi();
                DataTable dt = cls.T_opendataset("sp_LichSu_NguoiCan_Load_id '"+ dr["IDCan"].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");
                string sql1 = "Delete from LichSu_NguoiCan where IDCan = '" + dr["IDCan"].ToString() + "'";
                cls.T_exc(sql1);
                //
                string sql = "sp_LichSu_NguoiCan_Insert ";
                sql += "N'" + dr["MaCan"].ToString().ToUpper().ToUpper() + "',";
                sql += "N'" + dr["HoTen"].ToString().ToUpper() + "'";
                _NhanVien_Class.set(sql);
                data();
                #endregion
            }
            else
            {
                gv.CancelUpdateCurrentRow();
            }
        }
        #endregion

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

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

        }
        public void excel()
        {
        }
        public void xoa()
        {
            Class_KetNoi cls = new Class_KetNoi();
            if (msgbox.dialog_yes_no("Xóa dòng chọn?") == DialogResult.Yes)
            {
                for (int i = 0; i < gv.RowCount; i++)
                {
                    string _value = gv.GetRowCellValue(i, "Select").ToString().Trim();
                    string _IDCan = gv.GetRowCellValue(i, "IDCan").ToString().Trim();
                    if(_value.ToUpper()=="TRUE")
                    {
                        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á");
                        string sql = "Delete from LichSu_NguoiCan where IDCan = '" + _IDCan + "'";
                        cls.T_exc(sql);
                    }
                }
                data();
            }
        
        }
        public void sua()
        {
        }       
        public void chon()
        {
            for (int i = 0; i < gv.RowCount; i++)
            {
                gv.SetRowCellValue(i, "Select", "True");
            }
            gv.RefreshData();
        }
        public void bochon()
        {
            for (int i = 0; i < gv.RowCount; i++)
            {
                gv.SetRowCellValue(i, "Select", "False");
            }
            gv.RefreshData();
        }
      
        public void tailai()
        {
            data();
        }

        #endregion

        #region menu click
        public void event_click()
        {
            btn_new.Click += new EventHandler(btn_new_Click);
            btn_del.Click += new EventHandler(btn_del_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_del_Click(object sender, EventArgs e)
        {
            xoa();
        }

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


        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
    
        public void data()
        {
            Class_KetNoi cls = new Class_KetNoi();
            gc.DataSource = cls.T_opendataset_store("sp_LichSu_NguoiCan_Load").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 + "\\NhanVienCan.xls");
            }
        }

       
    }
}
