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.SqlClient;
using System.Data;
using Todo.Pages;
using static Todo.Pages.LoginCheck;

namespace Todo.Pages
{
    public partial class Login
    {
    [Inject]
     IJSRuntime JSRuntime { get; set; }
    [Inject]
    public NavigationManager NavigationManager { get; set; }
    public string loginemail{get;set;}
    public string loginpassword{get;set;}
    public string Result { get; set; }

     protected override void OnInitialized()
        {
            Loginclass.m1="NULL";
            Loginclass.m2="NULL";
        }

        public void login_giris(){
            try{
            string db = @"workstation id=blazortodos.mssql.somee.com;packet size=4096;user id=latral4_SQLLogin_1;pwd=516swqticy;data source=blazortodos.mssql.somee.com;persist security info=False;initial catalog=blazortodos";
            SqlConnection baglanti = new SqlConnection(db);
            SqlCommand cmd = baglanti.CreateCommand();
            cmd.CommandText ="exec user_login '"+loginemail+"','"+loginpassword+"',@sayi OUTPUT,@onay OUTPUT,@roleid OUTPUT,@bolumsid OUTPUT,@kisiid OUTPUT"; 
            Console.WriteLine(cmd.CommandText);
            SqlParameter outputParam = cmd.Parameters.Add("@sayi", SqlDbType.Int);
            outputParam.Direction = ParameterDirection.Output;
            SqlParameter outputParam2 = cmd.Parameters.Add("@onay", SqlDbType.Int);
            outputParam2.Direction = ParameterDirection.Output;
            SqlParameter Roleid = cmd.Parameters.Add("@roleid", SqlDbType.Int);
            Roleid.Direction = ParameterDirection.Output;
            SqlParameter Bolumeid = cmd.Parameters.Add("@bolumsid", SqlDbType.Int);
            Bolumeid.Direction = ParameterDirection.Output;
            SqlParameter Kisiid = cmd.Parameters.Add("@kisiid", SqlDbType.Int);
            Kisiid.Direction = ParameterDirection.Output;
            baglanti.Open();
            cmd.ExecuteNonQuery();
            baglanti.Close();
				if(outputParam.Value.ToString()=="1")
				{
                Loginclass.m1="NULL";
                Loginclass.m2="NULL";
                 JSRuntime.InvokeVoidAsync("JSCallback2");
            }
            else if(outputParam.Value.ToString()=="2")
				{
                 JSRuntime.InvokeVoidAsync("JSCallback3");
				}
				else if(outputParam.Value.ToString()=="3" && Roleid.Value.ToString()=="0")
				{
                Loginclass.m1=loginemail;
                Loginclass.m2=loginpassword;
                Loginclass.rol_id_tut=Roleid.Value.ToString();
                Loginclass.bolum_id_tut=Bolumeid.Value.ToString();
                Loginclass.kisi_id_tut=Kisiid.Value.ToString();
                JSRuntime.InvokeVoidAsync("JSCallback");
                JSRuntime.InvokeVoidAsync("yenile",Loginclass.rol_id_tut);
                
				NavigationManager.NavigateTo("/Admin");
				}
            else if(outputParam.Value.ToString()=="3" && Roleid.Value.ToString()=="1")
            {
                Loginclass.m1=loginemail;
                Loginclass.m2=loginpassword;
                Loginclass.rol_id_tut=Roleid.Value.ToString();
                Loginclass.bolum_id_tut=Bolumeid.Value.ToString();
                Loginclass.kisi_id_tut=Kisiid.Value.ToString();
                JSRuntime.InvokeVoidAsync("JSCallback");
                JSRuntime.InvokeVoidAsync("yenile",Loginclass.rol_id_tut);
				NavigationManager.NavigateTo("/gorevekleata");
            }
            else if(outputParam.Value.ToString()=="3" && Roleid.Value.ToString()=="2")
            {
            Loginclass.m1=loginemail;
            Loginclass.m2=loginpassword;
            Loginclass.rol_id_tut=Roleid.Value.ToString();
            Loginclass.bolum_id_tut=Bolumeid.Value.ToString();
            Loginclass.kisi_id_tut=Kisiid.Value.ToString();
            JSRuntime.InvokeVoidAsync("JSCallback");
            JSRuntime.InvokeVoidAsync("yenile",Loginclass.rol_id_tut);
			NavigationManager.NavigateTo("/todo");
            }
        } catch(Exception e){
            Console.WriteLine(e.Message);
        }
    }
}}