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 Rol{
        public string rol_id{get;set;}="";
        public string rol_adi{get;set;}="";
    }
    public  class RolBase:ComponentBase{
        [Inject]
        IJSRuntime JSRuntime { get; set; }
        static string BaglantiKodu="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 Baglanti = new SqlConnection(BaglantiKodu);
        static string KomutKodu = "select *from rol";
        static SqlDataAdapter RollerDataAdapter= new SqlDataAdapter (KomutKodu, Baglanti); 
        SqlCommandBuilder RollerKomut = new SqlCommandBuilder(RollerDataAdapter);
        DataSet RollerDataSet = new DataSet(); 



        public List<Rol> roller=new List<Rol>();

         public string inputid1{get;set;}
        public string inputid2{get;set;}
        public string inputadi2{get;set;}
        public string inputadi3{get;set;}

        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")
        {
        roller.Clear();
        roller=RolGetir();
        }
        else
        {
        JSRuntime.InvokeVoidAsync("logingit");
        }
        }


        public async Task InsertData() 
       {           
       SqlCommand komut=Baglanti.CreateCommand();
       komut.CommandText="insert into rol values('"+inputadi3+"')";
       Baglanti.Open();
       komut.ExecuteNonQuery();
       Baglanti.Close();
       //RollerKomut.Dispose();
       //RollerDataSet.Tables["roller"].Clear();
       inputadi3="";
       RollerKomut.Dispose();
       RollerDataSet.Tables["rol"].Clear();
       await OnInitializedAsync();

      }



      public async Task UpdateData()  
    { 
    
       SqlCommand komut=Baglanti.CreateCommand();
       komut.CommandText="update rol set rol_adi='"+inputadi2+"' where rol_id="+inputid2+"";
       Baglanti.Open();
       komut.ExecuteNonQuery();
       Baglanti.Close();

       //RollerKomut.Dispose();
       //RollerDataSet.Tables["roller"].Clear();
       inputid2="";
       inputadi2="";
       RollerKomut.Dispose();
       RollerDataSet.Tables["rol"].Clear();
       await OnInitializedAsync();
    }



    public async Task DeleteData() 

    { 
       SqlCommand komut=Baglanti.CreateCommand();
       komut.CommandText="delete from rol where rol_id="+inputid1+"";
       Baglanti.Open();
       komut.ExecuteNonQuery();
       Baglanti.Close();


       //RollerDataAdapter.Update(RollerDataSet, "roller");
       //RollerKomut.Dispose();
      //RollerDataSet.Tables["roller"].Clear();
       
       
       inputid1="";
      
       RollerKomut.Dispose();
       RollerDataSet.Tables["rol"].Clear();
       await OnInitializedAsync();
    }













        
        
        public List<Rol> RolGetir(){
        try
            {
                RollerDataAdapter.Fill(RollerDataSet, "rol");
               
                for (int i = 0; i < RollerDataSet.Tables[0].Rows.Count; i++)  
                {  
                  Rol pm = new Rol();  
                  pm.rol_id =  RollerDataSet.Tables[0].Rows[i]["rol_id"].ToString();  
                  pm.rol_adi = RollerDataSet.Tables[0].Rows[i]["rol_adi"].ToString();  
                 roller.Add(pm);  
                 }   
              }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

        return roller;
       }
    }
}