using System;
using System.Data.SqlClient;
using Microsoft.JSInterop;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using System.Data;
using Todo_App.Data;
using System.Text.Json;
using Todo_App.Data;
using static Todo_App.Data.Hotmai_islemleri;

namespace Todo_App.Pages
{

    class Todo_Gorev_Getir
    {
        public int task_id { get; set; }
        public int task_team_id { get; set; }
        public string task_start { get; set; }
        public string task_finish { get; set; }
        public string task_content { get; set; }
        public int task_sender_id { get; set; }
    }
 
    class uyeler
    {
        public int uye_id { get; set; }
        public string uye_adi { get; set; }

        public string uye_soyadi { get; set; }
    }

    public partial class EkipLideri
    {
        [Inject]
        IJSRuntime JSRuntime { get; set; }
        SqlConnection baglanti = new SqlConnection("workstation id=BlazorOSM.mssql.somee.com;packet size=4096;user id=mssql_58_SQLLogin_1;pwd=ztdvdv9bog;data source=BlazorOSM.mssql.somee.com;persist security info=False;initial catalog=BlazorOSM");
        SqlConnection baglanti1 = new SqlConnection("workstation id=BlazorOSM.mssql.somee.com;packet size=4096;user id=mssql_58_SQLLogin_1;pwd=ztdvdv9bog;data source=BlazorOSM.mssql.somee.com;persist security info=False;initial catalog=BlazorOSM");

        List<uyeler> uyeler_list = new List<uyeler>();
        List<Todo_Gorev_Getir> todo_list_gorev = new List<Todo_Gorev_Getir>();

        protected override void OnInitialized()
        {

            try
            {
                SqlCommand cmd = new SqlCommand("Todo_Ekip_Uyeleri_Getir", baglanti);
                cmd.CommandType = CommandType.StoredProcedure;
                System.Console.WriteLine(Login_info.id);
                cmd.Parameters.AddWithValue("@ekip_lideri_ID", Int32.Parse(Login_info.id.ToString()));
                baglanti.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    uyeler_list.Add(new uyeler { uye_id = Int32.Parse(dr["User_ID"].ToString()), uye_adi = dr["User_Name"].ToString(), uye_soyadi = dr["User_Surname"].ToString() });
                    System.Console.WriteLine("name " + dr["User_Name"].ToString() + "surname" + dr["User_Surname"].ToString());
                }
                baglanti.Close();


            }
            catch (Exception e1)
            {
                System.Console.WriteLine(e1.Message + " Ekip Lideri ");
            }


            try
            {
                SqlCommand cmd = new SqlCommand("Todo_Gorev_Getir_Ekip_Lideri", baglanti1);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@lider_id",Login_info.id);
                baglanti1.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    todo_list_gorev.Add(new Todo_Gorev_Getir
                    {
                        task_id = Int32.Parse(dr["Task_ID"].ToString()),
                        task_team_id = Int32.Parse(dr["Task_Team_ID"].ToString()),
                        task_start = dr["Task_start_date"].ToString(),
                        task_finish = dr["Task_finish_date"].ToString(),
                        task_content = dr["Task_Content"].ToString(),
                        task_sender_id = Int32.Parse(dr["Task_sender_ID"].ToString())
                    
                    });
                }
                baglanti1.Close();
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message + "a" + e.InnerException);
            }
        }



        public ElementReference edit { get; set; }

        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
            {
                //  bool result= await JSRuntime.InvokeAsync<bool>("FroalaEditor",edit);

                var result = await JSRuntime.InvokeAsync<object>("eval", @"(function() { new FroalaEditor('#edit');  })()");

            }
        }

        public string editMessage { get; set; }

        public async void getEdit()
        {
            editMessage = await JSRuntime.InvokeAsync<string>("eval", @"(function() { return document.getElementById('edit').value;  })()", new object[] { edit });
            await InvokeAsync(StateHasChanged);

            try
            {
                SqlCommand cmd = new SqlCommand("Todo_Gorev_Kaydet", baglanti);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@gorev_veren_id", Int32.Parse(Login_info.id.ToString()));
                cmd.Parameters.AddWithValue("@gorev_alan_id", Int32.Parse(uye_id_select.ToString()));
                cmd.Parameters.AddWithValue("@gorev_basla", baslangic_tarih.ToString());
                cmd.Parameters.AddWithValue("@gorev_bitis", bitis_tarih.ToString());
                cmd.Parameters.AddWithValue("@gorev_icerik", editMessage.ToString());
                cmd.Parameters.AddWithValue("@gorev_tekrar", 1);
                baglanti.Open();
                cmd.ExecuteNonQuery();
                baglanti.Close();
                Hotmai_islemleri.Microsoft(Login_info.name + " " + Login_info.surname, Login_info.email, "farcry58", "Todo_deneme@hotmail.com", "Yeni Gorev Bildirimi", editMessage.ToString(), null);
            }
            catch (Exception e1)
            {
                System.Console.WriteLine(e1.Message + " Uye Change catch bloğu");
            }
        }
        public string uye_id_select { get; set; }
        public void uye_sec(ChangeEventArgs e)
        {
            JSRuntime.InvokeVoidAsync("buton_aktif");
            uye_id_select = e.Value.ToString();
            System.Console.WriteLine(uye_id_select);
        }
        public string baslangic_tarih { get; set; }
        public string bitis_tarih { get; set; }
        public void tarih_getir(int a, ChangeEventArgs eval)
        {
            switch (a)
            {
                case 1:
                    baslangic_tarih = eval.Value.ToString();
                    break;
                case 2:
                    bitis_tarih = eval.Value.ToString();
                    break;
            }

        }


    }
}