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 FormAuthorizathion : Form
    {
        public FormAuthorizathion()
        {
            InitializeComponent();
        }

        private void cbxShowPass_CheckedChanged(object sender, EventArgs e)
        {
            tbxPass.UseSystemPasswordChar = !cbxShowPass.Checked;
        }
        public static string txtcon = @"Data Source=213.155.192.79,3002;Initial Catalog=AlfafloorGDV;Persist Security Info=True;User ID=u23golovnya;Password=br4v;Encrypt=False";
        string captcha = "";//правильная капча
        int countmiss = 0;

        void Getcaptcha()
        {
            tbxCaptcha.Text = "";
            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();

        }

        private void FormAuthorizathion_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'alfafloorGDVDataSet1.Manager' table. You can move, or remove it, as needed.
            this.managerTableAdapter.Fill(this.alfafloorGDVDataSet1.Manager);
            //this.managerTableAdapter.Update(this.alfafloorGDVDataSet1.Manager);
            //panelContainerCaptcha.Hide();
            Getcaptcha();
            tbxLogin.Text = "m2";
            tbxPass.Text = "m2";

        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            if (panelContainerCaptcha.Visible)//если капча видна
            {
                if (tbxCaptcha.Text != captcha)
                {
                    MessageBox.Show("Капчи не совпадают", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    Getcaptcha() ;
                    btnEnter.Enabled = false;
                    timer4Login.Start();
                    
                    return;
                }
            }

            bsManager0.Filter = $"Login='{tbxLogin.Text}' and Password = '{tbxPass.Text}'";
            if (bsManager0.Count > 0 )
            {
                
                FormManager frm = new FormManager();
                this.Hide();
                frm.bsManager.Filter = $"Login = '{tbxLogin.Text}'";
                frm.ShowDialog();
                this.Show();
                Getcaptcha();
                tbxLogin.Clear();
                tbxPass.Clear();
                tbxLogin.Focus();
                panelContainerCaptcha.Visible = false;
            }
            else
            {
                MessageBox.Show("Менеджера с таким логином нет", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                countmiss++ ;
                if (panelContainerCaptcha.Visible ==false )
                    panelContainerCaptcha.Visible = true ;

                return;
            }
        }

        private void timer4Login_Tick(object sender, EventArgs e)
        {
            btnEnter.Enabled = true ;
            timer4Login.Stop();
        }

        private void panelCaptcha_Paint(object sender, PaintEventArgs e)
        {
            
            Random rnd = new Random();
            for (int i = 0; i < 7; i++)
            {
                e.Graphics.DrawLine(new Pen(Color.Black, 5),79,200,-100,-200);
                e.Graphics.DrawLine(new Pen(Color.Black, 2), rnd.Next(panelCaptcha.Width), rnd.Next(panelCaptcha.Height), rnd.Next(panelCaptcha.Width), rnd.Next(panelCaptcha.Height));//отрисовать линию с координатами панели с капчей
            }
        }

    }
}
