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

namespace Self_Project
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
            DataBase_SQL.Server = ".";
            DataBase_SQL.DataBaseName = "Self_Service";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            User U = new User();
            U.ShowUsers(txt_UserName);
            if (txt_UserName.Items.Count == 0)
            {
                MessageBox.Show("تا کنون هیچ کاربری ایجاد نشده است", "کاربر سیستم", MessageBoxButtons.OK);
                Application.Exit();
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            //---------------------------
            if (txt_Pass.TEXT == "")
            {
                MessageBox.Show("لطفا رمز عبور را وارد نمایید", "رمز عبور", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txt_Pass.TEXT = "";
                txt_Pass.Focus();
                return;
            }
            //---------------------------
            User U = new User();
            try
            {
                U.UserName = txt_UserName.SelectedValue.ToString();
                U.Password = txt_Pass.TEXT;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "اطلاعات نادرست", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            if (!U.IsCorrectUser())
            {
                MessageBox.Show("رمز عبور اشتباه است", "رمز عبور", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txt_Pass.TEXT = "";
                txt_Pass.Focus();
                return;
            }
            else
            {
                this.Hide();
                MainPage Form = new MainPage();
                Functions.UserName = U.UserName;
                this.Hide();
                Form.Show();
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Login_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}