﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using BazaZamowien.Classes;

namespace BazaZamowien
{
    public partial class Logowanie : Form
    {
        private string server = "";

        public Logowanie()
        {
            InitializeComponent();

            label4.Text = "Wersja " + Application.ProductVersion;

            //bazyComboBox.Items.Add(new BoundItem(BazaZamowien.Properties.Settings.Default.LOKALNE,"LOKALNE"));
            bazyComboBox.Items.Add(new BoundItem(BazaZamowien.Properties.Settings.Default.ZDALNE,"ZDALNE"));
            bazyComboBox.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {            
            if (textBox1.Text == "") { return; }
            //if (textBox2.Text == "") { return; }

            label1.Text = "";

            server = ((BoundItem)bazyComboBox.SelectedItem).StringValue;

            this.Cursor = Cursors.WaitCursor;

            User.getUser().UprZamowienia = loginTestZamowienia();
            User.getUser().UprPremie = loginTestPremie();

            this.Cursor = Cursors.Default;          

            if (User.getUser().UprZamowienia)
            {
                this.Cursor = Cursors.WaitCursor;
                if (Login())           
                {
                    this.Cursor = Cursors.WaitCursor;

                    if (Login())
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                       // komunikatLabel.Text = "Konto nieaktywne lub brak uprawnień.";
                    }

                    this.Cursor = Cursors.Default;  
                }
                this.Cursor = Cursors.Default;                
            }
        }

        private void Logowanie_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                button1.PerformClick();
            }          
        }

       
        private bool loginTestZamowienia() {

            SqlConnectionStringBuilder sqlBldr = new SqlConnectionStringBuilder();
            sqlBldr.DataSource = this.server;
            sqlBldr.InitialCatalog = Properties.Settings.Default.zamowieniaDATABASE;
            sqlBldr.UserID = textBox1.Text;
            sqlBldr.Password = textBox2.Text;
            sqlBldr.ConnectTimeout = 10000;
            sqlBldr.ApplicationName = "BazaZamowien_Premii";
            
            SqlConnection conn = new SqlConnection(sqlBldr.ConnectionString);

            try
            {
                conn.Open();
                label1.Text = "OK";
                conn.Close();
                ConnString.getConnString().ZamowieniaConnStr = sqlBldr.ConnectionString;
                return true;
            }
            catch (SqlException e1) {
                
                switch (e1.Number) { 
                    case 18456:
                        label1.Text = "Nieprawidłowy login lub hasło";
                        break;
                    default:
                        label1.Text = "Server niedostępny " + sqlBldr.ConnectionString;
                        MessageBox.Show(e1.Message);
                        break;
                }  
            }
            return false; 
        }


        private bool loginTestPremie()
        {
            SqlConnectionStringBuilder sqlBldr = new SqlConnectionStringBuilder();
            sqlBldr.DataSource = this.server;
            sqlBldr.InitialCatalog = Properties.Settings.Default.premieDATABASE;
            sqlBldr.UserID = textBox1.Text;
            sqlBldr.Password = textBox2.Text;
            sqlBldr.ConnectTimeout = 10000;

            SqlConnection conn = new SqlConnection(sqlBldr.ConnectionString);

            try
            {
                conn.Open();
                label1.Text = "OK";
                conn.Close();
                ConnString.getConnString().PremieConnStr = sqlBldr.ConnectionString;               
                return true;
            }
            catch (SqlException e1)
            {/*
                switch (e1.Number)
                {
                    case 18456:
                        label1.Text = "Nieprawidłowy login lub hasło";
                        break;
                    default:
                        label1.Text = "Server niedostępny " + sqlBldr.ConnectionString;
                        MessageBox.Show(e1.Message);
                        break;
                }*/
            }
            return false;
        }

        
        private bool Login()
        {
            //textBox1.Text = "kinga";

            if (User.getUser().UprPremie)
            {
                User.getUser().IDE_CT = PremieUtils.pobierzeIDE_CT(textBox1.Text);
                User.getUser().IDEDzialuCT = PremieUtils.pobierzeIDEDzialuCT(User.getUser().IDE_CT);
                User.getUser().IDEOddzialuCT = PremieUtils.pobierzeIDEOdzialuCT(User.getUser().IDE_CT);
                User.getUser().PremieGrupa = PremieUtils.pobierzeIDEGrupy(textBox1.Text);
            }

            /*
            if (User.getUser().UprPremie)
            {
                User.getUser().IDE_CT = PremieUtils.pobierzeIDE_CT("winkler");
                User.getUser().PremieGrupa = PremieUtils.pobierzeIDEGrupy("winkler");
            }
             */

            //zczytyje prawa uzytkownika do bazy zamowien
            
            SqlConnection conn = new SqlConnection(ConnString.getConnString().ZamowieniaConnStr);
            SqlCommand command = new SqlCommand();            
            command.CommandText = "select * from dbo.Users where symbol = @login";
            command.Parameters.AddWithValue("@login", textBox1.Text);
            //command.Parameters.AddWithValue("@login", "klimczak");            
            command.Connection = conn;

            conn.Open();

            SqlDataReader reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    User.getUser().IDE = Convert.ToInt32(reader.GetValue(0));
                    User.getUser().Symbol = reader.GetValue(1).ToString();
                    User.getUser().Imie = reader.GetValue(2).ToString();
                    User.getUser().Nazwisko = reader.GetValue(3).ToString();
                    User.getUser().Email = reader.GetValue(4).ToString();
                    User.getUser().Grupa = reader.IsDBNull(5) ? 0 : Convert.ToInt32(reader.GetValue(5));
                   // User.getUser().Administracja = Convert.ToInt32(reader.GetValue(5)) == 1 ? true : false;
                    User.getUser().IDEDzialu = Convert.ToInt32(reader.GetValue(6));

                    User.getUser().OueryString = reader.IsDBNull(9) ? "" : reader.GetValue(9).ToString();
                }

                conn.Close();

                User.getUser().Numer_Roz = ZamowieniaUtils.Numer_Roz(User.getUser().IDEDzialu);
                User.getUser().St_dyrektorFinansowy = ZamowieniaUtils.czyJestDyrFinansowym(User.getUser().IDE);
                User.getUser().St_dyrektorGeneralny = ZamowieniaUtils.czyJestDyrGeneralnym(User.getUser().IDE);
                User.getUser().St_glownyKsiegowy = ZamowieniaUtils.czyJestGlownymKsiegowym(User.getUser().IDE);
                User.getUser().St_kierownik = ZamowieniaUtils.czyJestKierownikiem(User.getUser().IDE);

                if (User.getUser().St_dyrektorFinansowy | User.getUser().St_dyrektorGeneralny | User.getUser().St_glownyKsiegowy)
                {
                    User.getUser().Filtr = "1=1";
                }
                else
                {
                    User.getUser().Filtr = "ZamowienieDzial=" + User.getUser().IDEDzialu;
                }

                return true;
            }
            else
            {
                label1.Text = "Brak uprawnień.";
                return false;
            }
        }

        private void Logowanie_Load(object sender, EventArgs e)
        {

        }
    }
}