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;

namespace СУБД_Альфапол
{
    public partial class FormAutorization : Form
    {
        public FormAutorization()
        {
            InitializeComponent();
        }


        public static string txtCon = "Data Source=213.155.192.79,3002;Initial Catalog=dbAlphapolAG;Persist Security Info=True;User ID=u22grozdev;Password=ft9k;Encrypt=False;TrustServerCertificate=True";
        string Captcha;

        void GetCaptcha()
        {
            Captcha = Guid.NewGuid().ToString();
            Captcha = Captcha.Substring(0, 4).ToUpper();
            LblC1.Text = Captcha[0].ToString();
            LblC2.Text = Captcha[1].ToString();
            LblC3.Text = Captcha[2].ToString();
            LblC4.Text = Captcha[3].ToString();
            TbxCaptcha.Clear();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dbAlphapolAGDataSet.Manager' table. You can move, or remove it, as needed.
            this.managerTableAdapter.Fill(this.dbAlphapolAGDataSet.Manager);
        }

        private void cbxShowPass_CheckedChanged(object sender, EventArgs e)
        {
            tbxPass.UseSystemPasswordChar = !cbxShowPass.Checked;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {

            BsUsers.Filter = $"Login = '{tbxLogin.Text}' and Password = '{tbxPass.Text}'";
            if (PanelInputCaptcha.Visible)
            {
                if (TbxCaptcha.Text != Captcha)
                {
                    GetCaptcha();

                    btnLogin.Enabled = false;
                    timerForLogin.Start();

                    MessageBox.Show("Символы капчи введены неверно!", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            if (BsUsers.Count == 0)
            {
                if (PanelInputCaptcha.Visible == false)
                {
                    GetCaptcha();
                    PanelInputCaptcha.Visible = true;
                }
                else
                {
                    btnLogin.Enabled = false;
                    timerForLogin.Start();
                }
                MessageBox.Show("Нет пользователя с указанным логином и паролем", "Внимание", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                return;
            }
            this.Hide();
            FormManager frmManager = new FormManager();
            frmManager.bsUsers.Filter = BsUsers.Filter;
            frmManager.ShowDialog();
            this.Show();
            tbxLogin.Clear();
            tbxPass.Clear();
            tbxLogin.Focus();
            PanelInputCaptcha.Visible = false;
            this.managerTableAdapter.Fill(this.dbAlphapolAGDataSet.Manager);

        }

        private void timerForLogin_Tick(object sender, EventArgs e)
        {
            timerForLogin.Stop();
            btnLogin.Enabled = true;
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void tbxPass_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
