using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components;
using System.Diagnostics;
using System.Globalization;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using Microsoft.JSInterop;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Net.Http;
using System.Data;
using System.Data.SqlClient;
using static pro_todo.Pages.Users;

namespace pro_todo.Pages
{
  
    public class LoginBase:ComponentBase
    {

    [Inject]
    IJSRuntime JSRuntime { get; set; }
    public string kullanici_adi{get;set;}
    public string kullanici_soyadi{get;set;}
    public string kayit_email{get;set;}
    public string kayit_sifre{get;set;}
    public string giris_email{get;set;}
    public string giris_sifre{get;set;}

    public string kullanici_rol{get;set;}
    public string KayitDonut{get;set;}
    public string Donut{get;set;}

    protected override async Task OnInitializedAsync() 
        {
            MyUser.kullanici_email="NULL";
            MyUser.kullanici_sifre="NULL";
            MyUser.kullanici_rol="NULL";
            MyUser.kullanici_bolum="NULL";
            MyUser.kullanici_id="NULL";
        }

   

    public void kayitol(){
            string veritabani = @"workstation id=protodo.mssql.somee.com;packet size=4096;user id=Ruveyda044_SQLLogin_1;pwd=xyzm27u3xo;data source=protodo.mssql.somee.com;persist security info=False;initial catalog=protodo";
            SqlConnection baglanti = new SqlConnection(veritabani);
            SqlCommand cmd = baglanti.CreateCommand();
            cmd.CommandText ="exec kayitol '"+kullanici_adi+"','"+kullanici_soyadi+"','"+kayit_email+"','"+kayit_sifre+"',@sonuc OUTPUT";
                SqlParameter sonucop = cmd.Parameters.Add("@sonuc", SqlDbType.Int);
                sonucop.Direction = ParameterDirection.Output;
                baglanti.Open();
                cmd.ExecuteNonQuery();
                baglanti.Close();
                if(sonucop.Value.ToString() == "1")
                {
                  JSRuntime.InvokeVoidAsync("kullanicizatenvar");
                  KayitDonut="Bu Kullanici Zaten Var";
                }
                else if(sonucop.Value.ToString() == "2")
                {
                   JSRuntime.InvokeVoidAsync("kayitbasarili");
                  KayitDonut="Kayıt Başarılı";
                }
    }

     public void girisyap(){
            string veritabani = @"workstation id=protodo.mssql.somee.com;packet size=4096;user id=Ruveyda044_SQLLogin_1;pwd=xyzm27u3xo;data source=protodo.mssql.somee.com;persist security info=False;initial catalog=protodo";
            SqlConnection baglanti = new SqlConnection(veritabani);
            SqlCommand cmd = baglanti.CreateCommand();
            cmd.CommandText ="exec giris '"+giris_email+"','"+giris_sifre+"',@sonuc OUTPUT,@rol Output,@bolum Output,@id Output"; 

                SqlParameter sonucop = cmd.Parameters.Add("@sonuc", SqlDbType.Int);
                sonucop.Direction = ParameterDirection.Output;
                SqlParameter rolop = cmd.Parameters.Add("@rol", SqlDbType.Int);
                rolop.Direction = ParameterDirection.Output;
                SqlParameter bolumop = cmd.Parameters.Add("@bolum", SqlDbType.Int);
                bolumop.Direction = ParameterDirection.Output;
                SqlParameter idop = cmd.Parameters.Add("@id", SqlDbType.Int);
                idop.Direction = ParameterDirection.Output;
                baglanti.Open();
                cmd.ExecuteNonQuery();
                
				  if(sonucop.Value.ToString()=="3")
				  {
                    JSRuntime.InvokeVoidAsync("kayityok");
                    Donut="Kayıt Yok";
                  }
          else if(sonucop.Value.ToString()=="2")
				  {
                    JSRuntime.InvokeVoidAsync("onaybekleyen");
                    Donut="Bu kullanıcı onay bekliyor";
                  }
				  else if(sonucop.Value.ToString()=="1")
				  {
                    JSRuntime.InvokeVoidAsync("girisbasarili");
                    Donut="Giriş Başarılı";
					          MyUser.kullanici_email=giris_email;
                    MyUser.kullanici_sifre=giris_sifre;
                    MyUser.kullanici_rol=rolop.Value.ToString();
                    MyUser.kullanici_bolum=bolumop.Value.ToString();
                    MyUser.kullanici_id=idop.Value.ToString();
                    JSRuntime.InvokeVoidAsync("yonlendir",MyUser.kullanici_rol);
				  }
                baglanti.Close();
                //Console.WriteLine(MyUser.kullanici_email+" "+MyUser.kullanici_sifre+" "+MyUser.kullanici_rol+" "+MyUser.kullanici_bolum+" "+MyUser.kullanici_id);
        }

    }

    
 
 }
 

