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 Bolum{
        public string bolum_id{get;set;}="";
        public string bolum_adi{get;set;}="";
    }
    public  class BolumBase: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 Bolum";
        static SqlDataAdapter BolumlerDataAdapter= new SqlDataAdapter (KomutKodu, Baglanti);
        SqlCommandBuilder BolumlerKomut = new SqlCommandBuilder(BolumlerDataAdapter);
        DataSet BolumlerDataSet = new DataSet();


       
        public List<Bolum> bolumler=new List<Bolum>();
        public string inputid1{get;set;}

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

         public string inputadi3{get;set;}

      public async Task InsertData() 
       {           
       SqlCommand komut=Baglanti.CreateCommand();
       komut.CommandText="insert into Bolum values('"+inputadi3+"')";
       Baglanti.Open();
       komut.ExecuteNonQuery();
       Baglanti.Close();
       //BolumlerDataSet.Tables[0].Rows.Add(null,inputadi3); 
       //BolumlerDataAdapter.Update(BolumlerDataSet, "bolumler");
       BolumlerDataSet.Tables["bolumler"].Clear();
       inputadi3="";
       await OnInitializedAsync();
      }


    public async Task UpdateData() 
    { 
      /* DataRowCollection itemColumns = BolumlerDataSet.Tables[0].Rows;
       for (int i = 0; i < BolumlerDataSet.Tables[0].Rows.Count; i++)  
       {  
         if(BolumlerDataSet.Tables[0].Rows[i]["bolum_id"].ToString()==inputid2)  
                 BolumlerDataSet.Tables[0].Rows[i]["bolum_adi"]=inputadi2;  
       }   
             
       BolumlerDataAdapter.Update(BolumlerDataSet,"bolumler");
       BolumlerKomut.Dispose();
       BolumlerDataSet.Tables["bolumler"].Clear();*/
    
       SqlCommand komut=Baglanti.CreateCommand();
       komut.CommandText="update Bolum set bolum_adi='"+inputadi2+"' where bolum_id="+inputid2+"";
       Baglanti.Open();
       komut.ExecuteNonQuery();
       Baglanti.Close();

       BolumlerKomut.Dispose();
       BolumlerDataSet.Tables["bolumler"].Clear();
       inputid2="";
       inputadi2="";
       
       await OnInitializedAsync();
    }


     public async Task DeleteData() 

    { /* foreach (DataRow row in BolumlerDataSet.Tables[0].Rows) {
           if (row["bolum_id"].ToString() == inputid1) 
                                 row.Delete();
        }*/
       SqlCommand komut=Baglanti.CreateCommand();
       komut.CommandText="delete from Bolum where bolum_id="+inputid1+"";
       Baglanti.Open();
       komut.ExecuteNonQuery();
       Baglanti.Close();


       //BolumlerDataAdapter.Update(BolumlerDataSet, "bolumler");
       BolumlerKomut.Dispose();
       BolumlerDataSet.Tables["bolumler"].Clear();
       
       
       inputid1="";
      

       await OnInitializedAsync();
    }






        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")
        {
        bolumler.Clear();
        bolumler=BolumGetir();
        }
        else
        {
        JSRuntime.InvokeVoidAsync("logingit");
        }    
        }
        
        public List<Bolum> BolumGetir(){
        try
            {
                BolumlerDataAdapter.Fill(BolumlerDataSet, "bolumler");
               
                for (int i = 0; i < BolumlerDataSet.Tables[0].Rows.Count; i++)  
                {  
                  Bolum pm = new Bolum();  
                  pm.bolum_id =  BolumlerDataSet.Tables[0].Rows[i]["bolum_id"].ToString();  
                  pm.bolum_adi = BolumlerDataSet.Tables[0].Rows[i]["bolum_adi"].ToString();  
                 bolumler.Add(pm);  
                 }   
              }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

        return bolumler;
       }
    }
}