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 garment_factory
{
    public partial class fAvtoriz : Form
    {
        public fAvtoriz()
        {
            InitializeComponent();
        }
        public string con = "Data Source=213.155.192.79,3002;Initial Catalog=Fabrika2024;Persist Security Info=True;User ID=u22kazakov;Password=exh7";
        string GetCapcha()
        {
            string TxtLetter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string TxtDigit = "0123456789";
            string t1 = TxtLetter + TxtDigit;
            string capcha = "";
            bool FlagDigit = false;
            Random rnd = new Random();
            for (int i = 1; i <= 3; i++)
            {
                char sim = t1[rnd.Next(t1.Length)];
                if (char.IsDigit(sim)) FlagDigit = true;
                capcha += sim;
            }
            if (FlagDigit == false)
                capcha += TxtDigit[rnd.Next(TxtDigit.Length)];
            else
                capcha += TxtLetter[rnd.Next(TxtLetter.Length)];
            return capcha;
        }

        private void CbxShowPass_CheckedChanged(object sender, EventArgs e)
        {
            TbxPass.UseSystemPasswordChar = !TbxPass.UseSystemPasswordChar;
        }

        private void fAvtoriz_Load(object sender, EventArgs e)
        {
            // TODO: данная строка кода позволяет загрузить данные в таблицу "fabrika2024DataSet.Users". При необходимости она может быть перемещена или удалена.
            this.usersTableAdapter.Fill(this.fabrika2024DataSet.Users);
            LblCapcha.Text = GetCapcha();
            
            CmbRole.SelectedIndex = 0;
        }

        private void LblCapcha_Paint(object sender, PaintEventArgs e)
        {
            Color[] colors = { Color.Green, Color.Black, Color.Yellow, Color.White };
            Random rnd = new Random();
            for (int i = 1; i <= rnd.Next(5, 11); i++)
            {
                int x1 = rnd.Next(LblCapcha.Width);
                int y1 = rnd.Next(LblCapcha.Height);
                int x2 = rnd.Next(LblCapcha.Width);
                int y2 = rnd.Next(LblCapcha.Height);
                Color col = colors[rnd.Next(colors.Length)];
                e.Graphics.DrawLine(new Pen(col), x1, y1, x2, y2);
            }
        }

        private void BtnOpen_Click(object sender, EventArgs e)
        {
            if (tbxCapcha.Text.ToUpper() != LblCapcha.Text)
            {
                MessageBox.Show("Капча введена неверно!\nПопробуйте еще раз.");
                LblCapcha.Text = GetCapcha();
                tbxCapcha.Clear();
                return;
            }
            string role = CmbRole.SelectedItem.ToString();
            if (role == "Заказчик")
                role = 1.ToString();
            if (role == "Менеджер")
                role = 2.ToString();
            if (role == "Кладовщик")
                role = 3.ToString();
            string tbxFilter = String.Format("login = '{0}' and Passw = '{1}' and id_role = {2}", TbxLogin.Text, TbxPass.Text, role);
            string tbxFilterForUser = string.Format("login = '{0}'", TbxLogin.Text);
            BsUsers.Filter = tbxFilter;

            if (BsUsers.Count == 0)
            {
                MessageBox.Show(String.Format("Нет пользователя '{0}' с указанным логином и паролем!", role));
                return;
            }

            TbxLogin.Clear();
            TbxPass.Clear();
            this.Visible = false;

            if (role == "1")
            {
                fZakazchik frm = new fZakazchik();
                frm.LblIdUser.Text = ((DataRowView)BsUsers.Current)["id_user"].ToString();
                frm.BsUsers.Filter = tbxFilterForUser;
                frm.ShowDialog();
            }

            if (role == "2")
            {
                fManager frm = new fManager();
                frm.usersBindingSource.Filter = tbxFilterForUser;
                frm.ShowDialog();
            }

            if (role == "3")
            {
                fKladovschik frm = new fKladovschik();
                frm.usersBindingSource.Filter = tbxFilterForUser;
                frm.ShowDialog();
            }

            this.Visible = true;
            this.usersTableAdapter.Fill(this.fabrika2024DataSet.Users);
            TbxLogin.Focus();
            tbxCapcha.Clear();
            LblCapcha.Text = GetCapcha();
        }

        private void LblReg_Click(object sender, EventArgs e)
        {
            fRegistracia frm1 = new fRegistracia();
            //frm1.ShowDialog();
            if (frm1.ShowDialog() == DialogResult.OK)
                this.usersTableAdapter.Fill(this.fabrika2024DataSet.Users);
        }
    }
}
