using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Text.Json;

namespace Front.Program.Views.Project;

public partial class Register : ComponentBase
{
    [Inject] IJSRuntime JS { get; set; }
    [Inject] private NavigationManager NavigationManager { get; set; } = default!;
    
    private ElementReference dateInputRef;
    
    private string name = "";
    private DateTime? birth;
    private string email = "";
    private string phone = "";
    private string address = "";
    private string detailAddress = "";
    
    
    private string phonePart1 = "";
    private string phonePart2 = "";
    private string phonePart3 = "";

    private void SubmitPhone()
    {
        if (phonePart1.Length == 3 && phonePart2.Length == 4 && phonePart3.Length == 4)
        {
            var fullPhone = $"{phonePart1}-{phonePart2}-{phonePart3}";
            Console.WriteLine($"입력된 전화번호: {fullPhone}");
        }
        else
        {
            Console.WriteLine("전화번호를 올바르게 입력해주세요.");
        }
    }

    private void ConfirmData()
    {
        if (name == "" || phone == "" || address == "" || detailAddress == "")
        {
            
        }
        Console.WriteLine($"{name} - {birth.ToString()} - {email} - {phone} - {address}");
    }

    private async Task OpenDatePicker()
    {
        if (birth==null) birth = DateTime.Now;
        await JS.InvokeVoidAsync("openDatePicker", dateInputRef);
    }
    private void OnBirthChanged(ChangeEventArgs e)
    {
        Console.WriteLine($"선택된 생일: {birth}");
        
    }

    
    private DotNetObjectReference<Register> objRef;

    protected override void OnInitialized()
    {
        objRef = DotNetObjectReference.Create(this);
    }

    protected async Task OnClickAddress()
    {
        await JS.InvokeVoidAsync("openKakaoPostcodePopup", objRef);
    }

    [JSInvokable]
    public void SetAddress(string roadAddress, string jibunAddress, string zonecode)
    {
        address = roadAddress;
        Console.WriteLine($"SetAddress 호출됨: {roadAddress}, {jibunAddress}, {zonecode}");
        StateHasChanged();
    }
}