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 HotelProject
{
    public partial class FrmLogin : Form
    {

        SqlConnection con;
        public Boolean Result = false;

        public Boolean RoomManagement;
        public Boolean Roomtypes;
        public Boolean service;
        public Boolean user;
        public Boolean Paziresh;
        public Boolean Reporting;
        public Boolean BackUp;

        DataTable dt;





        public FrmLogin()
        {
            InitializeComponent();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {

            try
            {
                con = new SqlConnection("Server = . ; DataBase = DbHotel ; Integrated Security = True");
                SqlDataAdapter da = new SqlDataAdapter("SelectAllAdmins", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;

                dt = new DataTable();
                da.Fill(dt);

                CmbUserName.DataSource = dt;
                CmbUserName.DisplayMember = "UserName";
                CmbUserName.ValueMember = "PassWord";
            }
            catch
            {
                con = new SqlConnection("Server = . ; DataBase = master ; Integrated Security = True");
                con.Open();

                string str = "use master;" +
                "EXECUTE sp_attach_db @dbname = N'DbHotel' , " +
                " @filename1 = N'" + System.Environment.CurrentDirectory + "\\DbHotel.mdf'," +
                "@filename2 = N'" + System.Environment.CurrentDirectory + "\\DbHotel_log.ldf'";

                SqlCommand cmd = new SqlCommand(str, con);
                cmd.CommandTimeout = 100;
                cmd.ExecuteNonQuery();
                con.Close();

                Application.Restart();



            }


        }

        private void PicOk_Click(object sender, EventArgs e)
        {

            NumCorrection.DoCorrection(TxtPass);
            
            if (TxtPass.Text == CmbUserName.SelectedValue.ToString())
            {
                Result = true;
                Roomtypes = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["TarifAnvaOtaq"].ToString());
                RoomManagement = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["ModiriatOtaq"].ToString());
                service = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["ModiriatKhadamat"].ToString());
                user = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["ModiriatKarbaran"].ToString());
                Paziresh = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["Paziresh"].ToString());
                Reporting = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["PoshtibanGiri"].ToString());
                BackUp = Convert.ToBoolean(((DataRowView)CmbUserName.SelectedItem)["GozareshGiri"].ToString());
                
                
                
                
                
                
                this.Close();
            
            
            }
            else
            {
                errorProvider1.SetError(TxtPass, "رمز عبور وارد شده صحیح نیست");
            }


        }
    }
}
