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


namespace AzmoonGir
{
    public partial class FrmLogin : Form
    {


        SqlConnection con;

        public FrmLogin()
        {
            InitializeComponent();
        }

        private void ImgEnter_Click(object sender, EventArgs e)
        {

            if (TxtPasswoed.Text == CmbUserName.SelectedValue.ToString())
            {

               

                FrmMain Fm = new FrmMain();

                Fm.BtnAdd.Enabled = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["AddGroup"].ToString());
                Fm.BtnEdit.Enabled = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["EditGroup"].ToString());
                Fm.btnaddquestion.Enabled = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["AddQuestion"].ToString());
                Fm.btneditquestion.Enabled = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["EditQuestion"].ToString());
                Fm.Btnmanagement.Enabled = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["Management"].ToString());

                Fm.ribbonBar3.Enabled = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["Azmoon"].ToString());
                Fm.ribbonBar4.Enabled = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["Azmoon"].ToString());

                Fm.ribbonBar6.Enabled = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["Report"].ToString());
                Fm.ribbonBar11.Enabled = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["Report"].ToString());
                Fm.ribbonBar12.Enabled = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["Report"].ToString());


                //Fm.RibbonBackup.Enabled = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["BackupRestore"].ToString());



                
                Fm.Show();
                this.Hide();
                
               


            }
            else
            {
                errorProvider1.SetError(TxtPasswoed, "رمز عبور وارد شده صحیح نیست");

            }




        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            string strserver = "";

            try
            {
                // create reader & open file
                StreamReader tr = new StreamReader((Application.StartupPath + "\\Server.Azm"));

                // read a line of text
                strserver = tr.ReadLine().ToString();

                // close the stream
                tr.Close();
            }
            catch
            {
                // create a writer and open the file
                TextWriter tw = new StreamWriter((Application.StartupPath + "\\Server.Azm"));

                // write a line of text to the file
                tw.WriteLine("SQLEXPRESS");

                // close the stream
                tw.Close();
            }




           try
            {
                con = new SqlConnection("Data Source=.\\" + strserver + ";AttachDbFilename=|DataDirectory|\\DbAzmoon.mdf;Integrated Security=True;Connect Timeout=60;User Instance=True");
                SqlDataAdapter da = new SqlDataAdapter("SelectAllAdmins", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);

                CmbUserName.DataSource = dt;
                CmbUserName.DisplayMember = "UserName";
                CmbUserName.ValueMember = "PassWord";

                TxtPasswoed.Focus();

          }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                Frm_Message Fm = new Frm_Message();
                Fm.lbltext.Text = "در اتصال اتوماتیک به بانک مشکل وجود دارد.با پشتیبانی تماس بگیرید";
               Fm.ImgClose.Visible = true;
               Fm.ImgYes.Visible = false;
                Fm.ImgNo.Visible = false;

               Fm.ShowDialog();
                Application.Exit();

            }













        }

        private void TxtPasswoed_TextChanged(object sender, EventArgs e)
        {
            errorProvider1.SetError(TxtPasswoed, "");
        }

        private void TxtPasswoed_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void TxtPasswoed_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt16(e.KeyChar) == 13)
            {
                ImgEnter_Click(null, null);
            }


        }

        private void FrmLogin_Activated(object sender, EventArgs e)
        {
            TxtPasswoed.Focus();

        
        }
    }
}
