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 Projes2{
        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 int proje_durum{get;set;}
    }

     public class GorevekleBase:ComponentBase
    {
    [Inject]
    public NavigationManager NavigationManager { get; set; }
    public string Proje{get;set;}
    public DateTime Bitistarih{get;set;}
    public string tarihstr{get;set;}
    public List<Projes2> projes=new List<Projes2>();
    public string Projesid{get;set;}
    public string YeniProje{get;set;}

     protected override async Task OnInitializedAsync()
        {
        if(Loginclass.m1=="NULL" && Loginclass.m2=="NULL")
        {
            NavigationManager.NavigateTo("/");
        }
        else
        {
        projes.Clear();
        projes=Projegoster();
    }
    }

    public void projeekle()
    {
    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 cmd = baglanti.CreateCommand();
    cmd.CommandText =  "insert into proje(proje_adi,proje_bitis,bolum_id) values('"+Proje+"','"+tarihstr+"',"+Loginclass.bolum_id_tut+")";
    Console.WriteLine(cmd.CommandText);
    baglanti.Open();
    cmd.ExecuteNonQuery();
    baglanti.Close();
    OnInitializedAsync();
    }

    public void projeguncelle()
    {
    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 cmd = baglanti.CreateCommand();
    cmd.CommandText =  "update proje set proje_adi='"+YeniProje+"' where proje_id="+Projesid+"";
    Console.WriteLine(cmd.CommandText);
    baglanti.Open();
    cmd.ExecuteNonQuery();
    baglanti.Close();
    OnInitializedAsync();
    }
    



    public async Task tarihal(ChangeEventArgs e)
    {
        tarihstr=e.Value.ToString();
        Console.WriteLine(tarihstr);
    }

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

    }
    
}