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 Users3
       {
        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 GorevAtama2
    {
        public int atama_id{get;set;}
        public int user_id{get;set;}
        public int gorev_id{get;set;}
        public int bolum_id{get;set;}
        public int gorev_durum{get;set;}
    }

    public class Gorevs3{
     public int gorev_id{get;set;}
     public int proje_id{get;set;}
     public string gorev {get;set;}
     public int bolum_id{get;set;}
     public int gorev_durum{get;set;}
    }

    public class Bolums3{
     public int bolum_id{get;set;}
     public string bolum_adi {get;set;}
    }

    public class Projes4
    {
        public int proje_id {get;set;}
        public string proje_adi {get;set;}
        public string proje_baslangic {get;set;}
        public string proje_bitis {get;set;}
        public int bolum_id{get;set;}
        public bool proje_durum{get;set;}
    }

    public class SirkettodoBase:ComponentBase
    {
    [Inject]
    public NavigationManager NavigationManager { get; set; }
    public List<Projes4> proje3=new List<Projes4>();
    public List<Gorevs3> gorev3=new List<Gorevs3>();
    public List<Bolums3> bolum3=new List<Bolums3>();
    public  List<GorevAtama2> atama2= new List<GorevAtama2>();
    public List<Users3> user3=new List<Users3>();


    protected override async Task OnInitializedAsync()
    {
        if(Loginclass.m1=="NULL" && Loginclass.m2=="NULL")
        {
            NavigationManager.NavigateTo("/");
        }
        else
        {
        proje3.Clear();
        gorev3.Clear();
        bolum3.Clear();
        user3.Clear();
        atama2.Clear();
        proje3=Projegetir2();
        gorev3=Gorevgetir2();
        bolum3=BolumGetir2();
        user3=Usergoster2();
        atama2=Gorevatamagetir2();
        }
    }

    public void GorevGuncelle(int GorevID, 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(var atama in atama2){
          if(atama.atama_id == GorevID){
        if(atama.gorev_durum==1)
        {
          atama.gorev_durum=0;
          komut.CommandText = "update gorev_ata set gorev_durum=0 where atama_id="+GorevID+"";
        }
        else
        {
          atama.gorev_durum=1;
          komut.CommandText = "update gorev_ata set gorev_durum=1 where atama_id="+GorevID+"";
        }
        Console.WriteLine(komut.CommandText);
        baglanti.Open();
        komut.ExecuteNonQuery();
        baglanti.Close();
                break;
          }
      }
    }


    public List<GorevAtama2> Gorevatamagetir2(){
        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 gorev_ata";
       SqlDataAdapter daps= new SqlDataAdapter (sql,baglanti);
        DataSet dsps = new DataSet();
       try
        {
        daps.Fill(dsps, "atama2");
        for (int i = 0; i < dsps.Tables[0].Rows.Count; i++)  
        { 
        GorevAtama2 pm = new GorevAtama2();
        pm.atama_id = Int32.Parse(dsps.Tables[0].Rows[i]["atama_id"].ToString());  
        pm.user_id = Int32.Parse(dsps.Tables[0].Rows[i]["user_id"].ToString());
        pm.gorev_id =  Int32.Parse(dsps.Tables[0].Rows[i]["gorev_id"].ToString());
        pm.bolum_id = Int32.Parse(dsps.Tables[0].Rows[i]["bolum_id"].ToString());
        pm.gorev_durum = Int32.Parse(dsps.Tables[0].Rows[i]["gorev_durum"].ToString());
        atama2.Add(pm);
        }
        }
        catch (Exception ex)
        {
        Console.WriteLine(ex.Message);
        }
        return atama2;
     }


     public List<Users3> Usergoster2(){
        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, "user3");
        for (int i = 0; i < dsps.Tables[0].Rows.Count; i++)  
        { 
        Users3 pm = new Users3();
        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.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());
        user3.Add(pm);  
        }
        }
        catch (Exception ex)
        {
        Console.WriteLine(ex.Message);
        }
        return user3;
    }


    public List<Gorevs3> Gorevgetir2(){
        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 Gorev";
       SqlDataAdapter daps= new SqlDataAdapter (sql,baglanti);
        DataSet dsps = new DataSet();
       try
        {
        daps.Fill(dsps, "gorev3");
        for (int i = 0; i < dsps.Tables[0].Rows.Count; i++)  
        { 
        Gorevs3 pm = new Gorevs3();
        pm.gorev_id = Int32.Parse(dsps.Tables[0].Rows[i]["gorev_id"].ToString());  
        pm.proje_id = Int32.Parse(dsps.Tables[0].Rows[i]["proje_id"].ToString());
        pm.gorev = dsps.Tables[0].Rows[i]["gorev"].ToString();
        pm.bolum_id = Int32.Parse(dsps.Tables[0].Rows[i]["bolum_id"].ToString());
        pm.gorev_durum = Int32.Parse(dsps.Tables[0].Rows[i]["gorev_durum"].ToString());
        gorev3.Add(pm);
        }
        }
        catch (Exception ex)
        {
        Console.WriteLine(ex.Message);
        }
        return gorev3;
     }


 public List<Bolums3> BolumGetir2(){
        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, "bolum3");
        for (int i = 0; i < dsps.Tables[0].Rows.Count; i++)  
        { 
        Bolums3 pm = new Bolums3();
        pm.bolum_id = Int32.Parse(dsps.Tables[0].Rows[i]["bolum_id"].ToString());  
        pm.bolum_adi = dsps.Tables[0].Rows[i]["bolum_adi"].ToString();
        bolum3.Add(pm);  
        }
        }
        catch (Exception ex)
        {
        Console.WriteLine(ex.Message);
        }
        return bolum3;
     }


      public List<Projes4> Projegetir2(){
     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 Proje";
       SqlDataAdapter daps= new SqlDataAdapter (sql,baglanti);
        DataSet dsps = new DataSet();
            try
            {
            daps.Fill(dsps, "proje3");
            for (int i = 0; i < dsps.Tables[0].Rows.Count; i++)  
            { 
            Projes4 pm = new Projes4();
                 pm.proje_id =  Int32.Parse(dsps.Tables[0].Rows[i]["proje_id"].ToString());  
                 pm.proje_adi = dsps.Tables[0].Rows[i]["proje_adi"].ToString();
                 pm.proje_baslangic = dsps.Tables[0].Rows[i]["proje_baslangic"].ToString();
                 pm.proje_bitis = dsps.Tables[0].Rows[i]["proje_bitis"].ToString();
                 pm.bolum_id = Int32.Parse(dsps.Tables[0].Rows[i]["bolum_id"].ToString());
            proje3.Add(pm);
            }
            }
            catch (Exception ex)
            {
             Console.WriteLine(ex.Message);
            }
        return proje3;
    }
    }
}

