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 partial class BaskanPage1
    {
        
    


    }
    public class proje_gorev{
        public string proje_id {get;set;}
        public string proje_gorev_id {get;set;}
        public string proje_gorevi {get;set;}
        public string proje_bolum {get;set;}
    }
    public class BaskanPageBaseGorev: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 proje_gorevleri";
        static SqlDataAdapter ProjelerDataAdapter= new SqlDataAdapter (KomutKodu, Baglanti);
        SqlCommandBuilder ProjelerKomut = new SqlCommandBuilder(ProjelerDataAdapter);
        DataSet ProjelerDataSet = new DataSet();

        public List<proje_gorev> gorevler=new List<proje_gorev>();

        public string inputgorevadi{get;set;}
        public string inputgorevid{get;set;}
        public List<proje> projeler=new List<proje>();

        public string MyOption { get; set; }
        public void Option(ChangeEventArgs e)
        {
            MyOption = e.Value as string  ;
        } 


        public async void GorevEkle(){
            SqlCommand komut=Baglanti.CreateCommand();
            komut.CommandText="insert into proje_gorevleri values("+MyOption+",'"+inputgorevadi+"',"+MyUser.kullanici_bolum+")";
            Baglanti.Open();
            komut.ExecuteNonQuery();
            Baglanti.Close();
            await OnInitializedAsync();
        }

         public async Task GorevUpdate()  
        { 
            Boolean kontrol=false;
            SqlCommand komut=Baglanti.CreateCommand();
            foreach(var gorev in gorevler){
                if(gorev.proje_gorev_id==inputgorevid && gorev.proje_bolum==MyUser.kullanici_bolum)
                {
                    kontrol=true;
                }
            }
            if(kontrol)
            {
            komut.CommandText="update proje_gorevleri set proje_gorevi='"+inputgorevadi+"' where proje_gorev_id="+inputgorevid+"";
            Baglanti.Open();
            komut.ExecuteNonQuery();
            Baglanti.Close();
            inputgorevadi="";
            await OnInitializedAsync();
            }
            else
            {
                Console.WriteLine("Böyle bi gorev yok");
            }
        }
            
        public List<proje> ProjeGetir(){
            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";
            SqlConnection Baglanti = new SqlConnection(BaglantiKodu);
            string KomutKodu = "select *from proje";
            SqlDataAdapter ProjelerDataAdapter= new SqlDataAdapter (KomutKodu, Baglanti);
            SqlCommandBuilder ProjelerKomut = new SqlCommandBuilder(ProjelerDataAdapter);
            DataSet ProjelerDataSet = new DataSet();
            try
            {
                ProjelerDataAdapter.Fill(ProjelerDataSet, "proje");
               
                for (int i = 0; i < ProjelerDataSet.Tables[0].Rows.Count; i++)  
                {  
                  proje pm = new proje();  
                  pm.proje_id =  ProjelerDataSet.Tables[0].Rows[i]["proje_id"].ToString();  
                  pm.proje_adi = ProjelerDataSet.Tables[0].Rows[i]["proje_adi"].ToString();  
                  pm.proje_bitis_tarihi = ProjelerDataSet.Tables[0].Rows[i]["proje_bitiş_tarihi"].ToString();  
                  pm.proje_bolum_id = ProjelerDataSet.Tables[0].Rows[i]["bolum_id"].ToString();  
                  pm.proje_baskani = ProjelerDataSet.Tables[0].Rows[i]["proje_baskani"].ToString();
                 projeler.Add(pm);  
                 }   
              }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        return projeler;
       }


       public List<proje_gorev> GorevGetir(){
            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";
            SqlConnection Baglanti = new SqlConnection(BaglantiKodu);
            string KomutKodu = "select *from proje_gorevleri";
            SqlDataAdapter GorevlerDataAdapter= new SqlDataAdapter (KomutKodu, Baglanti);
            SqlCommandBuilder GorevlerKomut = new SqlCommandBuilder(GorevlerDataAdapter);
            DataSet GorevlerDataSet = new DataSet();
            try
            {
                GorevlerDataAdapter.Fill(GorevlerDataSet, "proje_gorevleri");
                for (int i = 0; i < GorevlerDataSet.Tables[0].Rows.Count; i++)  
                {  
                  proje_gorev pm = new proje_gorev();  
                  pm.proje_gorev_id =  GorevlerDataSet.Tables[0].Rows[i]["proje_gorev_id"].ToString();  
                  pm.proje_id = GorevlerDataSet.Tables[0].Rows[i]["proje_id"].ToString();  
                  pm.proje_gorevi = GorevlerDataSet.Tables[0].Rows[i]["proje_gorevi"].ToString();  
                  pm.proje_bolum = GorevlerDataSet.Tables[0].Rows[i]["proje_bolum"].ToString();  
                 gorevler.Add(pm);  
                 }   
              }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        return gorevler;
       }

        protected override async Task OnInitializedAsync() 
        {
            if(MyUser.kullanici_email != "NULL" && MyUser.kullanici_sifre != "NULL" && MyUser.kullanici_rol == "2" && MyUser.kullanici_bolum != "NULL" && MyUser.kullanici_id != "NULL")
            {
            projeler.Clear();
            gorevler.Clear();
            projeler=ProjeGetir();
            gorevler=GorevGetir();
            }
            else
            {
            JSRuntime.InvokeVoidAsync("logingit");
            }
        }
    
    
    
     }
}
