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 Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        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 Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'fabricDeryugoDataSet.Users' table. You can move, or remove it, as needed.
            this.usersTableAdapter.Fill(this.fabricDeryugoDataSet.Users);
            CmbRole.SelectedIndex = 0;
            LblCapcha.Text = GetCapcha();
        }

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            string role = CmbRole.SelectedItem.ToString();
            string TxtFilter = String.Format("login = '{0}' and pass = '{1}' and role = '{2}'", TbxLogin.Text, TbxPass.Text, role);
            string TxtFilterForUser = string.Format("login = '{0}'", TbxLogin.Text);
            usersBindingSource.Filter = TxtFilter;  

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

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

            if (role == "заказчик")
            {
                FormZakazchik frm = new FormZakazchik();
                frm.usersBindingSource.Filter = TxtFilterForUser;
                frm.ShowDialog();
            }

            if (role == "менеджер")
            {
                FormManager frm = new FormManager();
                frm.usersBindingSource.Filter = TxtFilterForUser;
                frm.ShowDialog();
            }

            if (role == "кладовщик")
            {
                FormKladov frm = new FormKladov();
                frm.usersBindingSource.Filter = TxtFilterForUser;
                frm.ShowDialog();
            }

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

        private void CmbRole_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

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

        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 LblReg_Click(object sender, EventArgs e)
        {
            FormRegistr frm = new FormRegistr();
            if (frm.ShowDialog() == DialogResult.OK)
                this.usersTableAdapter.Fill(this.fabricDeryugoDataSet.Users);
        }

        private void usersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.usersBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.fabricDeryugoDataSet);

        }
    }
    }
    

