using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_management_system
{
    public partial class frmMainMenu : Form
    {
        DataTable dtable = new DataTable();
        SqlConnection con = null;
        SqlCommand cmd = null;
        ConnectionString cs = new ConnectionString();

        clsFunc cf = new clsFunc();
        string st1;
        string st2;
        public frmMainMenu()
        {
            InitializeComponent();
        }

        private void registrationToolStripMenuItem_Click(object sender, EventArgs e)  
        {
            frmUserRegistration frm = new frmUserRegistration();
            frm.lblUser.Text = lblUser.Text;
            frm.Show();
        }

        private void loginDetailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmLoginDetails frm = new frmLoginDetails();
            frm.Show();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 frm = new AboutBox1();
            frm.Show();
        }

        private void categoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmCategory frm = new frmCategory();
            frm.lblUser.Text = lblUser.Text;
            frm.Show();
        }

        private void employeeToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }

        private void timer1_Tick(object sender, EventArgs e)   
        {
            ToolStripStatusLabel4.Text = System.DateTime.Now.ToString();
        }

        private void frmMainMenu_Load(object sender, EventArgs e)
        {

        }

        private void stagesToolStripMenuItem_Click(object sender, EventArgs e) 
        {
            frmStage frm = new frmStage();
            frm.lblUser.Text = lblUser.Text;
            frm.Show();
        }

        private void notepadToolStripMenuItem_Click(object sender, EventArgs e)   
        {
            System.Diagnostics.Process.Start("notepad.exe");
        }

        private void calculatorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void mSWordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Winword.exe");
        }

        private void taskManagerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("TaskMgr.exe");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        

        private void wordpadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Wordpad.exe");
        }

      
        private void projectManagementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmProjectManagement frm = new frmProjectManagement();
            frm.lblUser.Text = lblUser.Text;
            frm.Show();
        }

        private void menuStrip2_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void logsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Logs frm = new Logs();
            frm.Reset();
            frm.lblUser.Text = lblUser.Text;
            frm.ShowDialog();
        }

        private void registrationToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmUserRegistration frm = new frmUserRegistration();
            frm.lblUser.Text = lblUser.Text;
            frm.Show();
        }

        private void employeeToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            EmployeeDetails frm = new EmployeeDetails();
            frm.lblUser.Text = lblUser.Text;
            frm.Show();
        }

        private void projectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmProjectManagement frm = new frmProjectManagement();
            frm.lblUser.Text = lblUser.Text;
            frm.Show();
        }

        private void logoutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            st1 = lblUser.Text;
            st2 = "Successfully logged out";
            cf.LogFunc(st1, System.DateTime.Now, st2);
            this.Hide();
            frmLogin frm = new frmLogin();
            frm.Show();
            frm.txtUserName.Text = "";
            frm.txtPassword.Text = "";
            frm.ProgressBar1.Visible = false;
            frm.txtUserName.Focus();
        }

        private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            st1 = lblUser.Text;
            st2 = "Successfully logged out";
            cf.LogFunc(st1, System.DateTime.Now, st2);
            this.Hide();
            frmLogin frm = new frmLogin();
            frm.Show();
            frm.txtUserName.Text = "";
            frm.txtPassword.Text = "";
            frm.ProgressBar1.Visible = false;
            frm.txtUserName.Focus();
        }

        private void toolStripTextBox1_Click(object sender, EventArgs e)
        {

        }

        private void profileEntryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EmployeeDetails frm = new EmployeeDetails();
            frm.lblUser.Text = lblUser.Text;
            frm.Show();
        }

        private void databaseBackupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                timer2.Enabled = true;
                if ((!System.IO.Directory.Exists("C:\\DBBackup")))
                {
                    System.IO.Directory.CreateDirectory("C:\\DBBackup");
                }
                string destdir = "C:\\DBBackup\\pms.mdf " + DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss") + ".bak";
                con = new SqlConnection(cs.ConDB);
                con.Open();
                string cb = "backup database [" + System.Windows.Forms.Application.StartupPath + "\\pms.mdf] to disk='" + destdir + "'with init,stats=10";
                cmd = new SqlCommand(cb);
                cmd.Connection = con;
                cmd.ExecuteReader();
                con.Close();
                st1 = lblUser.Text;
                st2 = "Successfully backup the database";
                cf.LogFunc(st1, System.DateTime.Now, st2);
                MessageBox.Show("Successfully performed", "Database Backup", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            Cursor = Cursors.Default;
            timer2.Enabled = false;
        }

        private void databaseRecoveryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                var _with1 = openFileDialog1;
                _with1.Filter = ("DB Backup File|*.bak;");
                _with1.FilterIndex = 4;
                //Clear the file name
                openFileDialog1.FileName = "";

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    Cursor = Cursors.WaitCursor;
                    timer2.Enabled = true;
                    SqlConnection.ClearAllPools();
                    con = new SqlConnection(cs.ConDB);
                    con.Open();
                    string cb = "USE Master ALTER DATABASE [" + System.Windows.Forms.Application.StartupPath + "\\pms.mdf] SET Single_User WITH Rollback Immediate Restore database [" + System.Windows.Forms.Application.StartupPath + "\\pms.mdf] FROM disk='" + openFileDialog1.FileName + "' WITH REPLACE ALTER DATABASE [" + System.Windows.Forms.Application.StartupPath + "\\pms.mdf] SET Multi_User ";
                    cmd = new SqlCommand(cb);
                    cmd.Connection = con;
                    cmd.ExecuteReader();
                    con.Close();
                    st1 = lblUser.Text;
                    st2 = "Successfully restore the database";
                    cf.LogFunc(st1, System.DateTime.Now, st2);
                    MessageBox.Show("Successfully performed", "Database Restore", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Contact_Me form2 = new  Contact_Me();
            form2.Show();
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            frmClientProfile frm = new frmClientProfile();
            frm.lblUser.Text = lblUser.Text;
            frm.Show();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            frmClientProfile frm = new frmClientProfile();
            frm.lblUser.Text = lblUser.Text;
            frm.Show();
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {

            Contact_Me form2 = new Contact_Me();
            form2.Show();
        }
    }
}
