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 static Todo.Pages.LoginCheck;
namespace Todo.Pages
{
       public class Users
       {
        public int user_id { get; set; }
        public string isim { get; set; }
        public string soyisim { get; set; }
        public string email { get; set; }
        public string sifre { get; set; }
        public string kayit_tarihi { get; set; }
        public int rol_id { get; set; }
        public int bolum_id { get; set; }
        public bool kisi_aktiflik { get; set; }
       }
       public class Rols
       {
         public int rol_id {get;set;}
         public string rol {get;set;}
        public static string rol_id_tut {get;set;}
       }
       public class Bolums
       {
        public int bolum_id {get;set;}
        public string bolum_adi {get;set;}
       }
       
       public class LoginBase:ComponentBase
       {
        [Inject]
        IJSRuntime JSRuntime { get; set; }
        [Inject]
        public NavigationManager NavigationManager { get; set; }
        public  List<Users> user= new List<Users>();
        public  List<Rols> rol= new List<Rols>();
        public  List<Bolums> bolum= new List<Bolums>();
        

      public void IsDoneSet(int userID, object checkedValue)
      {
      bool newIsDone=(bool)checkedValue?true:false;  
      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);
      Console.WriteLine(newIsDone);
      SqlCommand komut=baglanti.CreateCommand();
      
      foreach(Users todoobj in user){
          if(todoobj.user_id == userID){
        if(todoobj.kisi_aktiflik==true)
        {
          todoobj.kisi_aktiflik=newIsDone;
          komut.CommandText = "update users set kisi_aktiflik=0 where user_id="+userID+"";
        }
        else
        {
          todoobj.kisi_aktiflik=newIsDone;
          komut.CommandText = "update users set kisi_aktiflik=1 where user_id="+userID+"";
        }
        Console.WriteLine(komut.CommandText);
        baglanti.Open();
        komut.ExecuteNonQuery();
        baglanti.Close();
                break;
          }
      }
    }



   protected override async Task OnInitializedAsync()
    {
      if(Loginclass.m1=="NULL" && Loginclass.m2=="NULL")
        {
            NavigationManager.NavigateTo("/");
        }
        else
        {
        if(Loginclass.rol_id_tut=="0"){
        rol=Rolgetir();
        bolum=Bolumgetir();
        user=KisiGetir();
    }
        }
    }


    public List<Rols> Rolgetir(){
       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);
       string sql = "select *from Rol";
       SqlDataAdapter daps= new SqlDataAdapter (sql,baglanti);
        DataSet dsps = new DataSet();
         try
            {
                daps.Fill(dsps, "rol");
                for (int i = 0; i < dsps.Tables[0].Rows.Count; i++)  
                {  
                 Rols pm = new Rols();
                 pm.rol_id =  Int32.Parse(dsps.Tables[0].Rows[i]["rol_id"].ToString());  
                 pm.rol = dsps.Tables[0].Rows[i]["rol"].ToString();
                 rol.Add(pm);  
                 }
              }
            catch (Exception ex)
            {
             Console.WriteLine(ex.Message);
            }
        return rol;
    }

    public void Roloption(ChangeEventArgs e,int userID,string tridstr){
       string rol_id2=e.Value.ToString();
       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 komut=baglanti.CreateCommand();
        komut.CommandText="update users set rol_id="+rol_id2+" where user_id="+userID+"";
        baglanti.Open();
        komut.ExecuteNonQuery();
        baglanti.Close();
        Console.WriteLine(tridstr);

        /*JSRuntime.InvokeVoidAsync("JSAdmin",tridstr,rol_id2);*/
    }


     public void Bolumoption(ChangeEventArgs e,int userID){
       string bolum_id2=e.Value.ToString();
       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 komut=baglanti.CreateCommand();
        komut.CommandText="update users set bolum_id="+bolum_id2+" where user_id="+userID+"";
        baglanti.Open();
        komut.ExecuteNonQuery();
        baglanti.Close();
     }


      public List<Bolums> Bolumgetir(){
       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);
       string sql = "select *from Bolum";
       SqlDataAdapter daps= new SqlDataAdapter (sql,baglanti);
        DataSet dsps = new DataSet(); 
       try
            {
                daps.Fill(dsps, "bolum");
                for (int i = 0; i < dsps.Tables[0].Rows.Count; i++)  
                {  
                 Bolums pm = new Bolums();
                 pm.bolum_id =  Int32.Parse(dsps.Tables[0].Rows[i]["bolum_id"].ToString());  
                 pm.bolum_adi = dsps.Tables[0].Rows[i]["bolum_adi"].ToString();
                 bolum.Add(pm);  
                 }
              }
            catch (Exception ex)
            {
             Console.WriteLine(ex.Message);
            }
        return bolum;
    }


    public List<Users> KisiGetir(){
       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);
       string sql = "select *from users";
       SqlDataAdapter daps= new SqlDataAdapter (sql,baglanti);
        DataSet dsps = new DataSet();
            try
            {
                daps.Fill(dsps, "user");
                for (int i = 0; i < dsps.Tables[0].Rows.Count; i++)  
                {  
                 Users pm = new Users();
                 pm.user_id =  Int32.Parse(dsps.Tables[0].Rows[i]["user_id"].ToString());  
                 pm.isim = dsps.Tables[0].Rows[i]["isim"].ToString();
                 pm.soyisim = dsps.Tables[0].Rows[i]["soyisim"].ToString();
                 pm.email = dsps.Tables[0].Rows[i]["email"].ToString();
                 pm.sifre = dsps.Tables[0].Rows[i]["sifre"].ToString();
                 pm.kayit_tarihi = dsps.Tables[0].Rows[i]["kayit_tarihi"].ToString();
                 pm.rol_id = Int32.Parse(dsps.Tables[0].Rows[i]["rol_id"].ToString());
                 pm.bolum_id = Int32.Parse(dsps.Tables[0].Rows[i]["bolum_id"].ToString());
                 string aktiflik= dsps.Tables[0].Rows[i]["kisi_aktiflik"].ToString();
                 if(aktiflik=="0")
                 pm.kisi_aktiflik=false;
                 else
                 pm.kisi_aktiflik=true;
                 user.Add(pm);  
                 }
            }
            catch (Exception ex)
            {
             Console.WriteLine(ex.Message);
            }
        return user;
    }



    }
}

