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 pro_todo.Pages.Users;

namespace pro_todo.Pages{  
    public class kullanicitablo{
        public string kullaniciAdi{get;set;}
        public string kullaniciSoyadi{get;set;}
        public string kullaniciEmail {get;set;}
        public string kullaniciRolId {get;set;}
        public string kullaniciEkipId {get;set;}
        public string kullaniciOnay { get; set; }
    }

    public class Bolumler{
        public string bolum_id{get;set;}
        public string bolum_adi{get;set;}
    }

    public class Roller{
        public string rol_id{get;set;}
        public string rol_adi{get;set;}
    }

       public  class AdminPage5Base:ComponentBase{
        [Inject]
        IJSRuntime JSRuntime { get; set; }
        static string BaglantiKoduu="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";
        static  SqlConnection Baglantii = new SqlConnection(BaglantiKoduu);
        static string KomutKoduu = "select *from kullanicilar";
        static SqlDataAdapter KullanicilarDataAdapterr= new SqlDataAdapter (KomutKoduu, Baglantii);
        SqlCommandBuilder KullanicilarKomutt = new SqlCommandBuilder(KullanicilarDataAdapterr);
        DataSet KullanicilarDataSett = new DataSet();

       public  List<kullanicitablo> kullanicii= new List<kullanicitablo>();
       public  List<Bolumler> bolumtablo= new List<Bolumler>();
       public  List<Roller> roltablo= new List<Roller>();

         protected override async Task OnInitializedAsync() 
      {
        if(MyUser.kullanici_email != "NULL" && MyUser.kullanici_sifre != "NULL" && MyUser.kullanici_rol == "1" && MyUser.kullanici_bolum != "NULL" && MyUser.kullanici_id != "NULL")
        {
        kullanicii=KullaniciGetirr();
        bolumtablo=BolumGetir();
        roltablo=RolGetir();
        }
        else
        {
        JSRuntime.InvokeVoidAsync("logingit");
        }
      }


      public List<kullanicitablo> KullaniciGetirr(){
        try
            {
                KullanicilarDataAdapterr.Fill(KullanicilarDataSett, "kullanicii");
               
                for (int i = 0; i < KullanicilarDataSett.Tables[0].Rows.Count; i++)  
                {  
                  kullanicitablo pm = new kullanicitablo();  
                 
                  pm.kullaniciAdi = KullanicilarDataSett.Tables[0].Rows[i]["kullanici_adi"].ToString();  
                  pm.kullaniciSoyadi = KullanicilarDataSett.Tables[0].Rows[i]["kullanici_soyadi"].ToString();  
                  pm.kullaniciEmail = KullanicilarDataSett.Tables[0].Rows[i]["kullanici_email"].ToString();  
                  pm.kullaniciRolId = KullanicilarDataSett.Tables[0].Rows[i]["kullanici_rol_id"].ToString();  
                  pm.kullaniciEkipId = KullanicilarDataSett.Tables[0].Rows[i]["kullanici_bolum_id"].ToString();  
                  pm.kullaniciOnay = KullanicilarDataSett.Tables[0].Rows[i]["kullanici_onay"].ToString(); 
                  kullanicii.Add(pm);
                 }   
              }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

        return kullanicii;
       }


        public List<Bolumler> BolumGetir(){
            string KomutKoduu = "select *from bolum";
            SqlDataAdapter RollerDataAdapter= new SqlDataAdapter (KomutKoduu, Baglantii);
            SqlCommandBuilder RollerKomut = new SqlCommandBuilder(RollerDataAdapter);
            DataSet RollerDataSet = new DataSet();
            try
                {
                    RollerDataAdapter.Fill(RollerDataSet, "bolumtablo");
                
                    for (int i = 0; i < RollerDataSet.Tables[0].Rows.Count; i++)  
                    {  
                    Bolumler pm = new Bolumler();  
                    pm.bolum_id = RollerDataSet.Tables[0].Rows[i]["bolum_id"].ToString();  
                    pm.bolum_adi = RollerDataSet.Tables[0].Rows[i]["bolum_adi"].ToString();  
                    bolumtablo.Add(pm);
                    }   
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

            return bolumtablo;
       }

       public List<Roller> RolGetir(){
            string KomutKoduu = "select *from rol";
            SqlDataAdapter RollerDataAdapter= new SqlDataAdapter (KomutKoduu, Baglantii);
            SqlCommandBuilder RollerKomut = new SqlCommandBuilder(RollerDataAdapter);
            DataSet RollerDataSet = new DataSet();
            try
                {
                    RollerDataAdapter.Fill(RollerDataSet, "roltablo");
                
                    for (int i = 0; i < RollerDataSet.Tables[0].Rows.Count; i++)  
                    {  
                    Roller pm = new Roller();  
                    pm.rol_id = RollerDataSet.Tables[0].Rows[i]["rol_id"].ToString();  
                    pm.rol_adi = RollerDataSet.Tables[0].Rows[i]["rol_adi"].ToString();  
                    roltablo.Add(pm);
                    }   
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            return roltablo;
       }


       public void RolDegis(ChangeEventArgs e,string email)
       {
        string secilenrolid=e.Value.ToString();
        SqlCommand komut=Baglantii.CreateCommand();
        komut.CommandText="update kullanicilar set kullanici_rol_id="+secilenrolid+" where kullanici_email='"+email+"'";
        Baglantii.Open();
        komut.ExecuteNonQuery();
        Baglantii.Close();
       }


        public void BolumDegis(ChangeEventArgs e,string email)
        {
            string secilenbolumid=e.Value.ToString();
            SqlCommand komut=Baglantii.CreateCommand();
            komut.CommandText="update kullanicilar set kullanici_bolum_id="+secilenbolumid+" where kullanici_email='"+email+"'";
            Baglantii.Open();
            komut.ExecuteNonQuery();
            Baglantii.Close();
        }






    }
    
}