using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;

using ABC.KetNoi;
using ABC.NhanVien;
using ABC.NhomKhachHang;
using ABC.NhomNCC;
using ABC.NhomHang;
using ABC.DanhMuc.NhomNCC;
using ABC.DanhMuc.NhomKhachHang;
using ABC.DanhMuc.NhomHang;
using ABC.DanhMuc.NCC;
using ABC.DanhMuc.Hang;
using ABC.DanhMuc.KhachHang;



using ABC.DanhMuc.HeThong;
using ABC.GiaoDich;
using ABC.BaoCao;
using ABC.DanhMuc.NhanVien;
using DevExpress.XtraSplashScreen;
using ABC.BanLe;
using ABC.CongNo;
using ABC.Toa;
using ABC.KetChuyen;
using ABC.File;
using System.Reflection;
using ABC.Report;
using DevExpress.XtraNavBar;

namespace ABC
{
    public partial class Main_Form : DevExpress.XtraBars.FluentDesignSystem.FluentDesignForm
    {


        public Main_Form()
        {           
            InitializeComponent();


         

}

        public void MoMenuCon(LabelControl p)
        {
            string name = p.Name.Trim();
            string text = p.Text.Trim();
            if (CheckTabPage(name))
            {
                XtraTabPage tab = new XtraTabPage();
                tab.Text = text;
                tab.Name = name;
                xtraTabControl1.TabPages.Add(tab);
                xtraTabControl1.SelectedTabPageIndex = xtraTabControl1.TabPages.Count - 1;
            }

        }
        string phienban = "S";
        #region Đăng Nhập
        Login_Form _Login_Form= new Login_Form();
        public void DangNhap_Fun()
        {
            _Login_Form.ShowDialog();
        }
        bool dangnhapthanhcong = false;
        void _Login_Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_Login_Form.DialogResult == DialogResult.Cancel)
            {
                this.Close();
            }
            else
            {
                dangnhapthanhcong = true;
               
            }
        }

       
        #endregion

        #region Khai Báo Form
        public Form active_form;
        public void khoitao()
        {
            Class_KetNoi db = new Class_KetNoi();
            Get_CauHinh.RE = db.T_open_store("sp_Re").Tables[0];
           
        }

        void _BanLe_Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            GiaoDien._Le_Forms = new BanLe.Le_Forms();
        }
        #endregion

        #region load danh muc nhóm
        
        void load_nhom_danhmuc()
        {
            Class_KetNoi db = new Class_KetNoi();
            NhomNCC_Class _NhomNCC_Class = new NhomNCC_Class();
            _NhomNCC_Class.get_store("sp_NhomNCC_Load");

            NhomKhachHang_Class _NhomKhachHang_Class = new NhomKhachHang_Class();
            _NhomKhachHang_Class.get_store("sp_NhomKhachHang_Load");
           
            NhomHang_Class _NhomHang_Class = new NhomHang_Class();
            _NhomHang_Class.get_store("sp_NhomHang_Load");
            _dtNhomTOA = db.T_opendataset("sp_NhomToa_load").Tables[0];
            
        }
      
        void load_danhmuc()
        {
            NCC_Class _NCC_Class = new NCC_Class();
            _NCC_Class.get_store("sp_NhaCC_Load");

            KhachHang_Class _KhachHang_Class = new KhachHang_Class();
            _KhachHang_Class.get_store("sp_KhachHang_Load");
          

            Hang_Class _Hang_Class = new Hang_Class();
            _Hang_Class.get_store("sp_Hang_Load");

            NhanVien_Class _NhanVien_Class = new NhanVien_Class();
            _NhanVien_Class.get_store("sp_NhanVien_Load");
        }
        private DataTable ThongTinCauHinh()
        {
            Class_KetNoi db = new Class_KetNoi();
            int thamso = 1;
            string[] arr_name = new string[thamso];
            object[] arr_value = new object[thamso];
            arr_name[0] = "@ID";
            arr_value[0] = Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper();
            return db.T_opendataset_store("sp_CauHinh_TheoID", arr_name, arr_value, thamso).Tables[0];
        }
        void load_nhapSi()
        {
            //NhapSi_Class _NhapSi_Class = new NhapSi_Class();
            //NhapSi_Class._Ngay = Get_CauHinh.Ngay_DN;
            //int thamso = 1;
            //string[] arr_name = new string[thamso];
            //object[] arr_value = new object[thamso];
            //arr_name[0] = "@Ngay";
            //arr_value[0] = Get_CauHinh.Ngay_DN;
            //_NhapSi_Class.get_store("sp_NhapSiDaLuu_TheoNgay",arr_name,arr_value,thamso);
            Class_KetNoi clsKT = new Class_KetNoi();

            NhapLe_Class _NhapLe_Class = new NhapLe_Class();
            _NhapLe_Class.get_NCC_store("sp_NhapSe_NCC_Load");
            _NhapLe_Class.get_ca_store("sp_NhapLe_Ca_Load");
            //
            DuLieu.CongNo_1Ngay_NVCan = clsKT.T_opendataset("XemNo_1Ngay_nvCan " + Get_CauHinh.Ngay_DN + ",NULL,NULL,NULL");
            if (DuLieu.CongNo_1Ngay_NVCan != null)
                DuLieu.CongNo_1Ngay_NVCan.Tables.Remove("Table");

            DuLieu.CongNo_1Ngay = _NhapLe_Class.get_dataset("XemNo_1Ngay " + Get_CauHinh.Ngay_DN + ",NULL,NULL,NULL");
           if(DuLieu.CongNo_1Ngay!=null)
              DuLieu.CongNo_1Ngay.Tables.Remove("Table");
          

          
        }

        #endregion

        #region Event menu
      
          T_Update ud;
        void ud_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (ud.DialogResult == DialogResult.OK)
            {
                Application.Restart();
            }
        }
        

        void Menu_Khoa_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            khoa();
        }

        void Menu_look_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Main_Load(sender, e);
        }
       

        #region command


        #region cac nut it thay doi

        #region khóa
        public void khoa()
        {
            DangNhap_Fun();
            if (dangnhapthanhcong == true)
            {
                SplashScreenManager.ShowForm(typeof(WaitForm1));

                load_nhom_danhmuc();
                load_danhmuc();
                load_nhapSi();
                khoitao();
                SplashScreenManager.CloseForm();
            }
        }
        #endregion

        #region nut exit
        void btn_exit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (msgbox.dialog_yes_no("Đóng chương trình ?") == DialogResult.Yes)
            {
                this.Close();
            }
        }
        #endregion


        #region nut undo
        void btn_redo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // throw new NotImplementedException();
        }
        #endregion

        #region nut redo
        void btn_undo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // throw new NotImplementedException();
        }
        #endregion
        #endregion

  
        #endregion

      

        XtraTabPage Active_Tab;

        void show(Form formshow)
        {

            if (Pow.kiemtra(formshow.Name, "Xem", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
            {
                #region
              
                active_form = formshow;
                active_form.FormClosed += new FormClosedEventHandler(active_form_FormClosed);
                #region
               
                formshow.TopLevel = false;
                formshow.FormBorderStyle = FormBorderStyle.None;
                formshow.Dock = System.Windows.Forms.DockStyle.Fill;
                formshow.Visible = true;
                //add tab page  
                Active_Tab = new DevExpress.XtraTab.XtraTabPage();
                Active_Tab.Text = active_form.Text;
                Active_Tab.Name = active_form.Name;
                Active_Tab.Controls.Add(active_form);
                xtraTabControl1.TabPages.Add(Active_Tab);
                xtraTabControl1.SelectedTabPage = Active_Tab;
                #endregion
                //bool stop = false;
                //for (int i = 0; i < xtraTabControl1.TabPages.Count; i++)
                //{
                //    if (xtraTabControl1.TabPages[i].Name == active_form.Name)
                //    {
                //        xtraTabControl1.SelectedTabPage = xtraTabControl1.TabPages[i];
                //        stop = true;
                //        break;
                //    }
                //}
                //if (stop == false)
                //{
                //    #region

                //    formshow.TopLevel = false;
                //    formshow.FormBorderStyle = FormBorderStyle.None;
                //    formshow.Dock = System.Windows.Forms.DockStyle.Fill;
                //    formshow.Visible = true;
                //    //add tab page  
                //    Active_Tab = new DevExpress.XtraTab.XtraTabPage();
                //    Active_Tab.Text = active_form.Text;
                //    Active_Tab.Name = active_form.Name;
                //    Active_Tab.Controls.Add(active_form);
                //    xtraTabControl1.TabPages.Add(Active_Tab);
                //    xtraTabControl1.SelectedTabPage = Active_Tab;
                //    #endregion
                //}
                //else
                //{
                //}
                #endregion

            }
            else
            {
                //closeform();
            }
        }

        void active_form_FormClosed(object sender, FormClosedEventArgs e)
        {
           
        }
        private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            XtraTabControl tab = (XtraTabControl)sender;
            tab.SelectedTabPage.Controls.Clear();
            string _name1 = tab.SelectedTabPage.Name;
            switch (_name1)
            {
                case "menuKhoa":
                    {
                        khoa();
                        break;
                    }
                case "Menu_KhachHang":
                    {
                        KhachHang_Forms control = new KhachHang_Forms();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }

                case "SlackCauHinh":
                    {
                        FrmSlackWebhookDevExpress frm = new FrmSlackWebhookDevExpress();
                        frm.ShowDialog();  // hoặc Show()
                        break;
                    }

                // chi tiết khách hàng_nhiên Jason đã fix tối ưu thời gian lọc dữ liệu

                case "Menu_BaoCao_SoChiTiet_KhachHang2":
                    {
                        frmBaoCao_SoBanHang_ChiTiet_KH frm = new frmBaoCao_SoBanHang_ChiTiet_KH();    // Hiển thị form một cách độc lập
                        frm.WindowState = FormWindowState.Maximized;
                        frm.Show();     
                        break;
                    }

                case "KhachHang_Forms":
                    {
                        KhachHang_Forms control = new KhachHang_Forms();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
               
                case "NCC_Forms":
                    {
                        NCC_Forms control = new NCC_Forms();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "tabHome":
                    {
                        ucHome control = new ucHome();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_NhomKhachHang":
                    {
                        NhomKhachHang_Forms control = new NhomKhachHang_Forms();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_NCC":
                    {
                        NCC_Forms control = new NCC_Forms();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_NhomHang":
                    {
                        NhomHang_Forms control = new NhomHang_Forms();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "menu_NhomToaLai":
                    {
                        NhomToaLai control = new NhomToaLai();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_Hang":
                    {
                        Hang_Forms control = new Hang_Forms();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_NhomNCC":
                    {
                        NhomNCC_Forms control = new NhomNCC_Forms();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "CongNo_SoThuTien_NVCan":
                    {
                        CongNo_SoThuTien_NVCan control = new CongNo_SoThuTien_NVCan();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_NhanVien":
                    {
                        NhanVien_Forms control = new NhanVien_Forms();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "menuCauHinhFile":
                    {
                        ucCauHinhFile control = new ucCauHinhFile();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_nhapsi":
                    {
                        int thamso = 1;
                        string[] arr_name = new string[thamso];
                        object[] arr_value = new object[thamso];
                        NhapSi_Class _NhapSi_Class = new NhapSi_Class();
                        arr_name[0] = "@Ngay";
                        arr_value[0] = Get_CauHinh.Ngay_DN;
                        _NhapSi_Class.get_store("sp_NhapSiDaLuu_TheoNgay", arr_name, arr_value, thamso);
                        NhapSi_Forms control = new NhapSi_Forms();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "menu_capnhatgia":
                    {
                        CapNhatGiaChiTietBien control = new CapNhatGiaChiTietBien();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "menu_capnhatgia_chitiet":
                    {
                        CapNhatGiaChiTiet control = new CapNhatGiaChiTiet();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                //case "Menu_NhapLe":
                //    {
                //        if (Pow.kiemtra("Le_Forms", "Xem", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
                //        {
                //            Le_Forms control = new Le_Forms();
                //            control.Dock = System.Windows.Forms.DockStyle.Fill;
                //            tab.SelectedTabPage.Controls.Add(control);
                //        }
                //        break;
                //    }     
                // đoạn code dưới là thay thế cho  case "Menu_NhapLe" ở trên để mở form toàn màn hình 

                case "Menu_NhapLe":
                    {
                        if (Pow.kiemtra("Le_Forms", "Xem", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true)
                        {
                            frmLeFormsContainer frm = new frmLeFormsContainer();
                            frm.WindowState = FormWindowState.Maximized;
                            frm.ShowDialog();
                        }
                        break;
                    }


                case "Menu_BaoCao_ToaCa_Si":
                    {
                        BaoCao_ToaSi control = new BaoCao_ToaSi();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_BaoCao_ToaCa_Le":
                    {
                        BaoCao_ToaLe control = new BaoCao_ToaLe();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_BaoCao_SoBaoCao_Si_Bien":
                    {
                        BaoCao_SoBaoCao_Si_Bien control = new BaoCao_SoBaoCao_Si_Bien();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_BaoCao_SoBaoCao_Si_Tra":
                    {
                        BaoCao_SoBaoCao_Si_Tra control = new BaoCao_SoBaoCao_Si_Tra();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_BaoCao_SoBaoCao_Le":
                    {
                        BaoCao_SoBaoCao_Le control = new BaoCao_SoBaoCao_Le();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_BaoCao_SoBaoCao_Si_Le":
                    {
                        BaoCao_SoBaoCao_Si_Le control = new BaoCao_SoBaoCao_Si_Le();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_BaoCao_SoBanHang_KhachHang":
                    {
                        BaoCao_SoBanHang_KH control = new BaoCao_SoBanHang_KH();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_BaoCao_SoBanHang_ThuongLai":
                    {
                        BaoCao_SoBanHang_TL control = new BaoCao_SoBanHang_TL();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                //case "Menu_BaoCao_SoChiTiet_KhachHang":        // phần cũ lọc dữ liệu rất chậm không nên dùng
                //    {
                //        BaoCao_SoBanHang_ChiTiet_KH control = new BaoCao_SoBanHang_ChiTiet_KH();
                //        control.Dock = System.Windows.Forms.DockStyle.Fill;
                //        tab.SelectedTabPage.Controls.Add(control);
                //        break;
                //    }
                case "Menu_BaoCao_DoanhSo":
                    {
                        BaoCao_BaoCaoDoanhSo control = new BaoCao_BaoCaoDoanhSo();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_CongNo_SoThuSot":
                    {
                        CongNo_SoThuSot control = new CongNo_SoThuSot();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_CongNo_SoThuKet":
                    {
                        CongNo_SoThuKet control = new CongNo_SoThuKet();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_CongNo_SoThuTien":
                    {
                        CongNo_SoThuTien control = new CongNo_SoThuTien();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_CongNo_SoThuLu":
                    {
                        CongNo_SoThuLu control = new CongNo_SoThuLu();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_CongNo_SoThuCat":
                    {
                        CongNo_SoThuCat control = new CongNo_SoThuCat();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_CongNo_SoThuBom":
                    {
                        CongNo_SoThuBom control = new CongNo_SoThuBom();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                //case "menu_toa":
                //    {
                //        LamToa control = new LamToa();
                //        control.Dock = System.Windows.Forms.DockStyle.Fill;
                //        tab.SelectedTabPage.Controls.Add(control);
                //        break;
                //    }
                case "menu_toa":
                    {
                        frmLamToaContainer frm = new frmLamToaContainer();
                        frm.WindowState = FormWindowState.Maximized;
                        frm.ShowDialog();
                        break;
                    }
                case "Menu_Toa_QuanLy":
                    {
                        Toa_QuanLy control = new Toa_QuanLy();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_Toa_quanlydulieu":
                    {
                        Toa_QuanLyDuLieu control = new Toa_QuanLyDuLieu();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_Toa_ThongKeChiTiet":
                    {
                        Toa_ThongKeChiTiet control = new Toa_ThongKeChiTiet();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_Toa_ThongKeTongHop":
                    {
                        Toa_ThongTongHop control = new Toa_ThongTongHop();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_PhanQuyen":
                    {

                        if (Get_CauHinh.Dt_Login.Rows[0]["_Text8"].ToString() == "admin")
                        {
                            NhanVien_PhanQuyen_Forms control = new NhanVien_PhanQuyen_Forms();
                            control.Dock = System.Windows.Forms.DockStyle.Fill;
                            tab.SelectedTabPage.Controls.Add(control);
                        }
                        else
                        {
                            msgbox.dialog_OK_info("Xin lỗi ! chức năng này chỉ dùng cho Admin.");
                        }
                        break;
                    }
                case "Menu_thongtinphieu":
                    {
                        T_Thongtinphieu control = new T_Thongtinphieu();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "menu_doimatkhau":
                    {
                        Doipass_Form control = new Doipass_Form();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "Menu_chiase":
                    {
                        if (phienban == "S")
                        {
                            T_Share control = new T_Share();
                            control.Dock = System.Windows.Forms.DockStyle.Fill;
                            tab.SelectedTabPage.Controls.Add(control);
                        }
                        break;
                    }
                case "menu_capnhat":
                    {
                        T_Update control = new T_Update();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "menu_ChuyenTienToa":
                    {
                        Toa_ChuyenTienToa control = new Toa_ChuyenTienToa();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "menu_ToaConThieu":
                    {
                        Toa_NhungToaChuaTra control = new Toa_NhungToaChuaTra();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "menu_ToaDaTra":
                    {
                        Toa_NhungDaTra control = new Toa_NhungDaTra();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "menu_BaoCaoTrongNgay":
                    {
                        Toa_BaoCaoToaTrongNgay control = new Toa_BaoCaoToaTrongNgay();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "menuKetChuyen":
                    {
                        menuKetChuyen control = new menuKetChuyen();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "menuThongKeHangHoa":
                    {
                        menuThongKeHangHoa control = new menuThongKeHangHoa();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "menuThongKeNhomToaLai":
                    {
                        menuThongKeNhomToaLai control = new menuThongKeNhomToaLai();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "menuThongKeKhachHang":
                    {
                        menuThongKeKhachHang control = new menuThongKeKhachHang();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "ucTelegram":
                    {
                        XtraUserControl  control = new HeThong.ucTelegram();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "ucSetNhanVien":
                    {
                        XtraUserControl control = new ABC.NhanVien.ucSetNhanVien();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "ucNhanVienCan":
                    {
                        XtraUserControl control = new ABC.NhanVien.ucNhanVienCan();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                case "ucNhanVienLuocSu":
                    {
                        XtraUserControl control = new ABC.NhanVien.ucNhanVienLuocSu();
                        control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tab.SelectedTabPage.Controls.Add(control);
                        break;
                    }
                default:
                    { break; }
            }
        }
        #endregion
        public static DataTable _dtCauHinh;
        public static DataTable _dtNhomKH;
        public static DataTable _dtKH;
        public static DataTable _dtNhaCC;
        public static DataTable _dtNhomNhaCC;
        public static DataTable _dtHang;
        public static DataTable _dtNhomHang;
        public static DataTable _dtNhomTOA;
        public static DataTable _dtNhanVien;
        public static string _tk;
        #region Hàm Load
        private void Main_Load(object sender, EventArgs e)
        {
            navBarControl.Enabled = false;
            string template_config = Application.StartupPath + "/update/template.txt";
            if (System.IO.File.Exists(template_config))
            {
                System.IO.StreamReader read = new System.IO.StreamReader(template_config);
                UserLookAndFeel.Default.SetSkinStyle(read.ReadLine());
                read.Dispose();
                read.Close();
                
            }
                _Login_Form.FormClosed += new FormClosedEventHandler(_Login_Form_FormClosed);
            DangNhap_Fun();
            if (dangnhapthanhcong == true)
            {
                SplashScreenManager.ShowForm(typeof(SplashScreen1));
                
               load_nhom_danhmuc();
                load_danhmuc();
                load_nhapSi();
                khoitao();
                phienban= Assembly.GetEntryAssembly().GetName().Version.ToString();
                SplashScreenManager.CloseForm();
                _dtCauHinh= ThongTinCauHinh();
                navBarControl.Enabled = true;
                
            }
        }
        #endregion

        private bool CheckTabPage(string tabPageName)
        {
            bool ischeck = true;
            for (int i = 0; i < xtraTabControl1.TabPages.Count; i++)
            {
                if (xtraTabControl1.TabPages[i].Name == tabPageName)
                {
                    xtraTabControl1.SelectedTabPageIndex = i;
                    ischeck = false;
                    break;
                }
            }
            return ischeck;
        }
        private void xtraTabControl1_CloseButtonClick(object sender, EventArgs e)
        {
            XtraTabControl tab = (XtraTabControl)sender;
            int i = xtraTabControl1.SelectedTabPageIndex;
            tab.TabPages.RemoveAt(i);
            tab.SelectedTabPageIndex = tab.TabPages.Count - 1;
        }

        private void navBarControl_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            try
            {
                string name = e.Link.Item.Tag.ToString();
                string text = e.Link.Item.Caption;
                if (Pow.kiemtra(name, "Xem", Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper()) == true && Get_CauHinh.Dt_Login.Rows[0]["ID"].ToString().ToUpper() != "ADMIN")
                {
                    if (CheckTabPage(e.Link.Item.Name))
                    {
                        XtraTabPage tab = new XtraTabPage();
                        tab.Text = text;
                        tab.Name = e.Link.Item.Name;
                        xtraTabControl1.TabPages.Add(tab);
                        xtraTabControl1.SelectedTabPageIndex = xtraTabControl1.TabPages.Count - 1;
                    }
                }

                else
                    MessageBox.Show("Bạn chưa được phân quyền để truy cập tính năng này", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


            catch (Exception ex)
            {

            }
           
        }

     

        private void pictureEdit1_EditValueChanged(object sender, EventArgs e)
        {

        }

       

        private void skinDropDownButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           
           
        }

        private void Main_Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            string template = UserLookAndFeel.Default.ActiveSkinName;
            string template_config = Application.StartupPath + "/update/template.txt";
            if (System.IO.File.Exists(template_config))
            {
                System.IO.File.Delete(template_config);
            }
            System.IO.StreamWriter write = new System.IO.StreamWriter(template_config);
            write.Write(template);
            write.Close();

        }

        private void barStaticItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void Db_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            string newDatabaseName = "fish"; // Hoặc tên database cố định khác mà bạn muốn

            // Bước tùy chọn: Hiển thị hộp thoại xác nhận trước khi thực hiện
            DialogResult confirmation = MessageBox.Show(
                $"Bạn có chắc chắn dùng chức năng này không ? ",
                "Xác nhận thay đổi ",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning);

            if (confirmation == DialogResult.Yes)
            {
                // Nếu người dùng chọn "Yes", gọi phương thức từ ConfigManager để cập nhật
                if (ABC.KetNoi.ConfigManager.UpdateDatabaseNameInFile(newDatabaseName)) // Đảm bảo namespace đúng
                {
                    MessageBox.Show(
                        $" THÀNH CÔNG" +
                        ".",
                        "Thành công",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show(
                        "Không thể cập nhật tên cơ sở dữ liệu. Vui lòng kiểm tra lại file Data.dat hoặc quyền ghi file.",
                        "Lỗi cập nhật",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
            }
            // else: Người dùng chọn "No", không làm gì cả.
        }
    }
    }







