<AdminDataPanel Loading="@Loading">
    <AdminFormSection Title="코드 목록" Description="그룹별 공통코드와 상태를 관리합니다." CssClass="mb-4">
        <MudTable Items="@Codes" Dense="true" Hover="true">
            <HeaderContent>
                <MudTh>그룹</MudTh>
                <MudTh>값</MudTh>
                <MudTh>이름</MudTh>
                <MudTh>순서</MudTh>
                <MudTh>상태</MudTh>
                <MudTh>작업</MudTh>
            </HeaderContent>
            <RowTemplate>
                <MudTd>@context.CodeGroup</MudTd>
                <MudTd>@context.CodeValue</MudTd>
                <MudTd>@context.CodeName</MudTd>
                <MudTd>@context.SortOrder</MudTd>
                <MudTd>@(context.IsActive ? "활성" : "비활성")</MudTd>
                <MudTd>
                    <MudButton Size="Size.Small" Variant="Variant.Text" OnClick="@(async () => await InvokeEditAsync(context))">수정</MudButton>
                    <MudButton Size="Size.Small" Variant="Variant.Text" Color="Color.Error" OnClick="@(async () => await InvokeDeleteAsync(context))">삭제</MudButton>
                </MudTd>
            </RowTemplate>
        </MudTable>
    </AdminFormSection>

    <MudDivider Class="my-4" />

    <AdminFormSection Title="코드 편집" Description="공백 없는 값과 일관된 이름만 허용합니다.">
        <MudForm>
            <MudTextField @bind-Value="EditModel.CodeGroup" Label="그룹" Variant="Variant.Outlined" FullWidth="true" Required="true" Disabled="@(!IsCreateMode)" MaxLength="80" Class="mb-3" />
            <MudTextField @bind-Value="EditModel.CodeValue" Label="값" Variant="Variant.Outlined" FullWidth="true" Required="true" Disabled="@(!IsCreateMode)" MaxLength="120" Class="mb-3" />
            <MudTextField @bind-Value="EditModel.CodeName" Label="이름" Variant="Variant.Outlined" FullWidth="true" Required="true" MaxLength="200" Class="mb-3" />
            <MudNumericField T="int" @bind-Value="EditModel.SortOrder" Label="순서" Variant="Variant.Outlined" FullWidth="true" Class="mb-3" />
            <MudSwitch T="bool" @bind-Value="EditModel.IsActive" Color="Color.Primary">활성</MudSwitch>
            <div class="d-flex gap-2 mt-4">
                <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="OnSaveRequested">저장</MudButton>
                <MudButton Variant="Variant.Outlined" OnClick="OnResetRequested">초기화</MudButton>
            </div>
        </MudForm>
    </AdminFormSection>
</AdminDataPanel>

@code {
    [Parameter]
    public bool Loading { get; set; }

    [Parameter, EditorRequired]
    public IReadOnlyList<CommonCode> Codes { get; set; } = [];

    [Parameter, EditorRequired]
    public CommonCode EditModel { get; set; } = new();

    [Parameter]
    public bool IsCreateMode { get; set; }

    [Parameter, EditorRequired]
    public EventCallback<CommonCode> EditRequested { get; set; }

    [Parameter, EditorRequired]
    public EventCallback<CommonCode> DeleteRequested { get; set; }

    [Parameter, EditorRequired]
    public EventCallback SaveRequested { get; set; }

    [Parameter, EditorRequired]
    public EventCallback ResetRequested { get; set; }

    private Task InvokeEditAsync(CommonCode code) => EditRequested.InvokeAsync(code);
    private Task InvokeDeleteAsync(CommonCode code) => DeleteRequested.InvokeAsync(code);
    private Task OnSaveRequested() => SaveRequested.InvokeAsync();
    private Task OnResetRequested() => ResetRequested.InvokeAsync();
}
