@using TaxBaik.Application.DTOs
@using TaxBaik.Application.Services
@using TaxBaik.WasmClient.Components.Admin.Shared

<MudForm @ref="form">
    <AdminFormSection Title="연락처" Description="고객 식별과 기본 회신 정보입니다." CssClass="mb-4">
        <MudTextField @bind-Value="model.Name" Label="이름"
            Variant="Variant.Outlined" Class="mb-4" Required="true" ReadOnly="@IsEditMode" />

        <MudTextField @bind-Value="model.Phone" Label="전화번호 (예: 010-1234-5678)"
            Variant="Variant.Outlined" Class="mb-4" Required="true" ReadOnly="@IsEditMode" />

        <MudTextField @bind-Value="model.Email" Label="이메일"
            Variant="Variant.Outlined" Class="mb-4" InputType="InputType.Email" ReadOnly="@IsEditMode" />
    </AdminFormSection>

    <AdminFormSection Title="문의 내용" Description="@(IsEditMode ? "상태와 메모만 변경 가능합니다." : "운영 분류와 처리 메모를 함께 관리합니다.")" CssClass="mb-4">
        <CommonCodeSelect @bind-Value="model.ServiceType" Group="INQUIRY_SERVICE_TYPE" Label="문의 유형 *" Class="mb-4" Required="true" Disabled="@IsEditMode" Placeholder="문의 유형을 선택하세요" />

        <MudTextField @bind-Value="model.Message" Label="문의 내용"
            Variant="Variant.Outlined" Lines="5" Class="mb-4" Required="true" ReadOnly="@IsEditMode" />

        <CommonCodeSelect @bind-Value="model.Status" Group="INQUIRY_STATUS" Label="상태" Class="mb-4" Clearable="true" Placeholder="상태를 선택하세요" />

        <MudTextField @bind-Value="model.AdminMemo" Label="관리 메모"
            Variant="Variant.Outlined" Lines="3" Class="mb-4" />
    </AdminFormSection>

    <AdminFormActions SubmitText="@ButtonText"
                      LoadingText="저장 중..."
                      CancelText="취소"
                      SubmitIcon="@Icons.Material.Filled.Save"
                      OnSubmit="@HandleSubmit"
                      OnCancel="@OnCancel"
                      IsSubmitting="false" />
</MudForm>

@code {
    [Parameter, EditorRequired]
    public string ButtonText { get; set; } = "저장";

    [Parameter]
    public EventCallback<InquiryFormModel> OnSubmit { get; set; }

    [Parameter]
    public EventCallback OnCancel { get; set; }

    [Parameter]
    public InquiryFormModel? InitialData { get; set; }

    [Parameter]
    public bool IsEditMode { get; set; } = false;

    private MudForm? form;
    private InquiryFormModel model = new();
    protected override async Task OnInitializedAsync()
    {
        if (InitialData != null)
        {
            model = new InquiryFormModel
            {
                Name = InitialData.Name,
                Phone = InitialData.Phone,
                Email = InitialData.Email,
                ServiceType = InitialData.ServiceType,
                Message = InitialData.Message,
                Status = InitialData.Status,
                AdminMemo = InitialData.AdminMemo
            };
        }

    }

    private async Task HandleSubmit()
    {
        if (form == null)
            return;

        await form.Validate();
        if (!form.IsValid)
            return;

        await OnSubmit.InvokeAsync(model);
    }

    public class InquiryFormModel
    {
        public string Name { get; set; } = "";
        public string Phone { get; set; } = "";
        public string? Email { get; set; }
        public string ServiceType { get; set; } = "기타";
        public string Message { get; set; } = "";
        public string Status { get; set; } = "new";
        public string? AdminMemo { get; set; }
    }
}
