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 Todo.Pages.LoginCheck;
namespace Todo.Pages
{
    public class Users2
       {
        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 Projes
    {
        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 GorevAtama
    {
        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 Gorevs{

     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 Bolums1{
     public int bolum_id{get;set;}
     public string bolum_adi {get;set;}
    }




    public class GorevEkleAtaBase:ComponentBase
    {
    [Inject]
    IJSRuntime JSRuntime { get; set; }
    [Inject]
    public NavigationManager NavigationManager { get; set; }

    public List<Projes> proje=new List<Projes>();
    public List<Gorevs> gorev=new List<Gorevs>();
    public List<Bolums1> bolum1=new List<Bolums1>();
    public  List<GorevAtama> atama= new List<GorevAtama>();
    public List<Users2> user2=new List<Users2>();
    public string Gorev{get;set;}
    public string Proje_id{get;set;}
    public string GorevID{get;set;}
    public string KisiID{get;set;}
    public string ResultMessage { get; set; } = ""; 


    public ElementReference RefMulSelect;
     public IList<string> ChoosedMulItem { get; set; } = new List<string>();
     
    

    public void gorevekle(){
        try{
    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 gorev(proje_id,gorev,bolum_id) values("+Proje_id+",'"+Gorev+"',"+Loginclass.bolum_id_tut+")";
     Console.WriteLine(cmd.CommandText);
    baglanti.Open();
    cmd.ExecuteNonQuery();
    baglanti.Close();
    /*OnInitializedAsync();*/
    }
    catch (Exception ex)
        {
        Console.WriteLine(ex.Message);
        }
    }

    public void gorevata(){
        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();
        string email="";
        string gorevtext="";
        string projeadi="";
        try
        {
        foreach(var mlt in ChoosedMulItem){
        
        cmd.CommandText = "insert into gorev_ata(user_id,gorev_id,bolum_id) values("+mlt+","+GorevID+","+Loginclass.bolum_id_tut+")";
        baglanti.Open();
        cmd.ExecuteNonQuery();
        baglanti.Close();
        foreach(var user in user2)
        {
            if(user.user_id.ToString()==mlt)
            {
                email=user.email;
            }
        }

        foreach(var gorev1 in gorev)
        {
            if(gorev1.gorev_id.ToString()==GorevID)
            {
                gorevtext=gorev1.gorev;
            }
        }

        foreach(var gorev1 in gorev)
        {
        foreach(var prj in proje)
        {
            if(gorev1.proje_id==prj.proje_id && gorev1.gorev_id.ToString()==GorevID)
            {
                projeadi=prj.proje_adi;
            }
        }
        }
        MailYolla(email,gorevtext,projeadi);
        }
        ChoosedMulItem.Clear();
        
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    protected override async Task OnInitializedAsync()
    {
        if(Loginclass.m1=="NULL" && Loginclass.m2=="NULL")
        {
            NavigationManager.NavigateTo("/");
        }
        else
        {
        if(Loginclass.rol_id_tut=="1"){
        proje.Clear();
        gorev.Clear();
        bolum1.Clear();
        user2.Clear();
        /*atama.Clear();*/
        proje=Projegetir();
        gorev=Gorevgetir();
        bolum1=BolumGetir1();
        user2=Usergoster();
        /*atama=Gorevatamagetir();*/
        }
    }
    }
    
     public List<Users2> Usergoster(){
        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, "user2");
        for (int i = 0; i < dsps.Tables[0].Rows.Count; i++)  
        { 
        Users2 pm = new Users2();
        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());
        user2.Add(pm);  
        }
        }
        catch (Exception ex)
        {
        Console.WriteLine(ex.Message);
        }
        return user2;
    }


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

     

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


     
        public void MailYolla(string email,string gorev,string projeadi) 
        {  
            using (MailMessage mail = new MailMessage()) { 
            mail.From = new MailAddress("todo_bilgilendirme@hotmail.com"); 
            mail.To.Add(email); 
            mail.Subject = "Görev Bilgilendirme"; 
            mail.Body = projeadi+" projesinin "+gorev+" görevine atandın"; 
            mail.IsBodyHtml = true; 
            using (SmtpClient smtp = new SmtpClient("smtp.outlook.com", 587)) 
            {
                smtp.Credentials = new System.Net.NetworkCredential("todo_bilgilendirme@hotmail.com", "02021982Ali");
                smtp.EnableSsl =true;
                smtp.Send(mail);
                ResultMessage = "Mail Gitti";
            }
            }
        
        }


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



    public void ProjeID(ChangeEventArgs e){
       Proje_id=e.Value.ToString();
    }

    public async void ChangedMulSelect()
    {
        ChoosedMulItem = await JSRuntime.InvokeAsync<IList<string>>("mulselect", new object[] {RefMulSelect});
        foreach(var kisi2 in ChoosedMulItem)
        {
            Console.WriteLine(kisi2);
        }
        await InvokeAsync(StateHasChanged);
    }

    public void Gorevekleacil(){
        JSRuntime.InvokeVoidAsync("JSgorevekle");
    }

     public void Gorevatamaacil(){
        JSRuntime.InvokeVoidAsync("JSgorevatama");
    }

    }
}


