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 AdminUsersPage
    {
        public int user_ekip_bind { get; set; }
        [Inject]
        public IJSRuntime jSRuntime{get;set;}
        public class Get_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; }
            public bool User_Account_Confirmation_Status { get; set; }
            public int user_team_id { get; set; }
            public int User_rol { get; set; }
        }
        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<Get_User_Class> user_info_list = new List<Get_User_Class>();
        protected override void OnInitialized()
        {
            try
            {
                SqlCommand cmd = new SqlCommand("Todo_Get_Users", baglanti1);
                cmd.CommandType = CommandType.StoredProcedure;
                baglanti1.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    user_info_list.Add(new Get_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(), user_team_id = Int32.Parse(dr["User_Team_ID"].ToString()), User_rol = Int32.Parse(dr["User_Rol"].ToString()), User_Account_Confirmation_Status = Boolean.Parse(dr["User_Account_Confirmation_Status"].ToString()), Registration_Date = dr["User_Registration_Date"].ToString() });
                }
                
                baglanti1.Close();
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
        }
        public void secim_rol(int a, ChangeEventArgs e)
        {
            System.Console.WriteLine(a + " " + e.Value);
            try
            {
                SqlCommand cmd = new SqlCommand("Todo_Rol_Guncelle", baglanti1);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@gelen_id", a);
                cmd.Parameters.AddWithValue("@gelen_rol",Int32.Parse(e.Value.ToString()));
                baglanti1.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception e1)
            {
                System.Console.WriteLine(e1.Message);
            }
            finally
            {
                baglanti1.Close();
            }
        }
        public void aktiflik_degistir(int id,ChangeEventArgs e,int rol){

            switch(e.Value.ToString()){
                case "true":
                SqlCommand cmd = new SqlCommand("Todo_aktiflik_guncelle", baglanti1);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@id", id);
                cmd.Parameters.AddWithValue("@aktif_durum",1);
                baglanti1.Open();
                cmd.ExecuteNonQuery();
                baglanti1.Close();
                // jSRuntime.InvokeVoidAsync("aktiflik_yazi_rengi",id,1,rol);
                break;
                case "false":
                
                SqlCommand cmd1 = new SqlCommand("Todo_aktiflik_guncelle", baglanti1);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@id", id);
                cmd1.Parameters.AddWithValue("@aktif_durum",0);
                baglanti1.Open();
                cmd1.ExecuteNonQuery();
                baglanti1.Close();
              //  jSRuntime.InvokeVoidAsync("aktiflik_yazi_rengi",id,0,rol);
                break;
                
            }
        }
        
        public string input_style{get;set;}="width: 70%; text-indent: 50x;";
        public void takim_guncelle(int id,ChangeEventArgs e){
            input_style="width: 70%; text-indent: 50x; pointer-events: none;";
            System.Console.WriteLine(e.Value.ToString());
             SqlCommand cmd = new SqlCommand("Todo_Update_User_Team", baglanti1);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@user_id", id);
                cmd.Parameters.AddWithValue("@team_id",Int32.Parse(e.Value.ToString()));
                SqlParameter outparam=cmd.Parameters.Add("@sonuc",SqlDbType.Int);
                outparam.Direction=ParameterDirection.Output;
                baglanti1.Open();
                cmd.ExecuteNonQuery();
                System.Console.WriteLine(outparam.Value.ToString());
                if(outparam.Value.ToString()=="-1"){
                    jSRuntime.InvokeVoidAsync("takim_degistir_hata",e.Value.ToString());
                 input_style="width: 70%; text-indent: 50x;";
                }else{
                    jSRuntime.InvokeVoidAsync("takim_degistir_ok",id,e.Value.ToString()); 
                     input_style="width: 70%; text-indent: 50x;";
                }
                baglanti1.Close();
        }
    }
}