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 System.Net.Mail;
using static pro_todo.Pages.Users;

namespace pro_todo.Pages{

    public class BaskanPageBaseAtama: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 kullanicilar";
        static SqlDataAdapter KullanicilarDataAdapter= new SqlDataAdapter (KomutKodu, Baglanti);
        SqlCommandBuilder KullanicilarKomut = new SqlCommandBuilder(KullanicilarDataAdapter);
        DataSet KullanicilarDataSet = 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  List<kullanicilar> kullanici= new List<kullanicilar>();

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


        public void GorevAta(){
            SqlCommand komut=Baglanti.CreateCommand();
            komut.CommandText="insert into proje_gorevlendirme(proje_gorev_id,proje_gorev_sorumlusu,proje_bolum) values("+inputgorevid+","+MyOption+","+MyUser.kullanici_bolum+")";
            Baglanti.Open();
            komut.ExecuteNonQuery();
            Baglanti.Close();
            string email="";
            string gorev="";
            foreach(var user in kullanici)
            {
                if(user.kullaniciId==MyOption)
                {
                    email=user.kullaniciEmail;
                }
            }

            foreach(var gorevs in gorevler)
            {
                if(gorevs.proje_gorev_id==inputgorevid)
                {
                    gorev=gorevs.proje_gorevi;
                }
            }

             MailGonder(email,gorev);
        }

         public async Task GorevUpdate()  
        { 
            SqlCommand komut=Baglanti.CreateCommand();
            komut.CommandText="update proje_gorevleri set proje_gorevi='"+inputgorevadi+"' where proje_gorev_id="+inputgorevid+"";
            Baglanti.Open();
            komut.ExecuteNonQuery();
            Baglanti.Close();
            inputgorevadi="";
            await OnInitializedAsync();
        }


        public List<kullanicilar> KullaniciGetir(){
        try
            {
                KullanicilarDataAdapter.Fill(KullanicilarDataSet, "kullanici");
               
                for (int i = 0; i < KullanicilarDataSet.Tables[0].Rows.Count; i++)  
                {  
                  kullanicilar pm = new kullanicilar();  
                  pm.kullaniciId =  KullanicilarDataSet.Tables[0].Rows[i]["kullanici_id"].ToString();  
                  pm.kullaniciAdi = KullanicilarDataSet.Tables[0].Rows[i]["kullanici_adi"].ToString();  
                  pm.kullaniciSoyadi = KullanicilarDataSet.Tables[0].Rows[i]["kullanici_soyadi"].ToString();  
                  pm.kullaniciEmail = KullanicilarDataSet.Tables[0].Rows[i]["kullanici_email"].ToString();  
                  pm.kullaniciSifre = KullanicilarDataSet.Tables[0].Rows[i]["kullanici_sifre"].ToString();  
                  pm.kullaniciRolId = KullanicilarDataSet.Tables[0].Rows[i]["kullanici_rol_id"].ToString();  
                  pm.kullaniciBolumId = KullanicilarDataSet.Tables[0].Rows[i]["kullanici_bolum_id"].ToString();  
                  pm.kullaniciKayitTarih = KullanicilarDataSet.Tables[0].Rows[i]["kullanici_kayit_tarihi"].ToString();  
                  string onaystr = KullanicilarDataSet.Tables[0].Rows[i]["kullanici_onay"].ToString();  
                  if (onaystr=="0")
                  pm.kullaniciOnay=false;
                  else
                  pm.kullaniciOnay=true;
                 kullanici.Add(pm);  
                 }   
              }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

        return kullanici;
       }
            
        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();
            kullanici.Clear();
            projeler=ProjeGetir();
            gorevler=GorevGetir();
            kullanici=KullaniciGetir();
            }
            else
            {
            JSRuntime.InvokeVoidAsync("logingit");
            }
        }

        public string ResultMessage { get; set; } = ""; 
        private void MailGonder(string email,string gorev) 
        {
            try
            {   
                using (MailMessage mail = new MailMessage()) { 
                mail.From = new MailAddress("bilgilendirme44@hotmail.com"); 
                mail.To.Add(email); 
                mail.Subject ="Gorev Bilgilendirme"; 
                mail.Body =gorev; 
                mail.IsBodyHtml = true; 
                using (SmtpClient smtp = new SmtpClient("smtp.outlook.com", 587)) 
                {
                    smtp.Credentials = new System.Net.NetworkCredential("bilgilendirme44@hotmail.com", "ruvi1234");
                    smtp.EnableSsl =true;
                    smtp.Send(mail);
                    ResultMessage = "Mail Gitti";
                }
            }
        }

        catch(Exception e)
        {
            ResultMessage=e.Message;
        }
        }
    
    
    
     }
}
