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;

namespace Todo_App.Pages
{
    public partial class MainPage
    {
        public string admin_isim { get; set; }

        public string admin_soyisim { get; set; }

        public int ekip_sayisi { get; set; }
        public int kullanici_sayisi { get; set; }
        public class User_Class
        {
            public Int32 ID { get; set; }
            public String name { get; set; }
            public string surname { get; set; }
            public string email { get; set; }
            public string password { get; set; }
            public string Registration_Date { get; set; }
        }
        [Inject]
        IJSRuntime jSRuntime { get; set; }
        [Inject]
        NavigationManager navigationManager { get; set; }

        public void kullanici_onayla_sil(int par, int secim)
        {
            try
            {
                SqlCommand cmd = new SqlCommand("Todo_hesap_onayla_sil", baglanti);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@user_id", par);
                cmd.Parameters.AddWithValue("@secenek", secim);
                baglanti.Open();
                cmd.ExecuteNonQuery();

                jSRuntime.InvokeVoidAsync("element_sil_mainpage", par, secim);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
            finally
            {
                baglanti.Close();

            }
        }

        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");
        List<User_Class> list = new List<User_Class>();



       
        protected override async void OnInitialized()
        {















            try
            {
                SqlCommand cmd = new SqlCommand("Todo_Admin_bilgi_getir", baglanti);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter cikti1 = cmd.Parameters.Add("@isim", SqlDbType.NVarChar, 50);
                SqlParameter cikti2 = cmd.Parameters.Add("@soyisim", SqlDbType.NVarChar, 50);
                SqlParameter cikti3 = cmd.Parameters.Add("@ekip_sayisi", SqlDbType.Int);
                SqlParameter cikti4 = cmd.Parameters.Add("@kullanici_sayisi", SqlDbType.Int);
                SqlParameter cikti5 = cmd.Parameters.Add("@admin_id", SqlDbType.Int);
                cikti1.Direction = ParameterDirection.Output;
                cikti2.Direction = ParameterDirection.Output;
                cikti3.Direction = ParameterDirection.Output;
                cikti4.Direction = ParameterDirection.Output;
                cikti5.Direction = ParameterDirection.Output;
                baglanti.Open();
                cmd.ExecuteNonQuery();
                admin_isim = cikti1.Value.ToString();
                Login_info.name = admin_isim;
                admin_soyisim = cikti2.Value.ToString();
                Login_info.surname = admin_soyisim;
                ekip_sayisi = Int32.Parse(cikti3.Value.ToString());
                kullanici_sayisi = Int32.Parse(cikti4.Value.ToString());
                Login_info.id =Int32.Parse(cikti5.Value.ToString());
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
            finally
            {
                baglanti.Close();
            }

            try
            {

                SqlCommand cmd = new SqlCommand("Todo_Onaylanmamis_Kullaniciler", baglanti);
                cmd.CommandType = CommandType.StoredProcedure;
                baglanti.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    list.Add(new User_Class { ID = Int32.Parse(dr["User_ID"].ToString()), name = dr["User_Name"].ToString(), surname = dr["User_Surname"].ToString(), email = dr["User_Email"].ToString(), password = dr["User_Password"].ToString(), Registration_Date = dr["User_Registration_Date"].ToString() });
                    System.Console.WriteLine(list[0].email);
                }
            }
            catch (Exception err)
            {
                System.Console.WriteLine(err.Message);
            }
            finally
            {
                baglanti.Close();
                /*
                string serializedString = JsonSerializer.Serialize(list);
                jSRuntime.InvokeVoidAsync("Todo_Onaylanmamis_Kullaniciler_Json",list);*/
            }
        }
    }
}
