Files
Atomx/Atomx.Admin/Atomx.Admin.Client/Pages/Settings/CountryEdit.razor
2025-12-25 18:43:30 +08:00

191 lines
6.4 KiB
Plaintext

@page "/country/create"
@page "/country/edit/{id:long}"
@page "/{locale}/country/create"
@page "/{locale}/country/edit/{id:long}"
@inject ILogger<CountryEdit> Logger
@attribute [Authorize]
<PageContainer Title="@(Id > 0 ? "编辑国家信息" : "新增国家信息")">
<Breadcrumb>
<Breadcrumb>
<BreadcrumbItem Href="/">管理后台</BreadcrumbItem>
<BreadcrumbItem Href="/settings">系统配置</BreadcrumbItem>
<BreadcrumbItem Href="/country/list">国家管理</BreadcrumbItem>
</Breadcrumb>
</Breadcrumb>
<ChildContent>
<Spin Spinning="pageLoading">
<Card Title="国家信息">
<Form @ref="editform" Model="@model" LabelColSpan="5" WrapperColSpan="14" OnFinish="OnFormFinishAsync">
@if (Id > 0 && languageList.Count() > 0)
{
<Tabs ActiveKey="@context.LanguageId" OnTabClick="OnLanguageTabChange">
<TabPane Key="0">
<TabTemplate>
<span>标准</span>
</TabTemplate>
</TabPane>
@foreach (var item in languageList)
{
<TabPane Key="@item.Key.ToString()">
<TabTemplate>
<span>@item.Value</span>
</TabTemplate>
</TabPane>
}
</Tabs>
}
<FormItem Label="名称" Required>
<Input @bind-Value="@context.Name" Placeholder="国家" />
</FormItem>
<FormItem Label="首字母">
<Input @bind-Value="@context.Initial" Placeholder="首字母" />
</FormItem>
<FormItem Label="两个字母ISO代码">
<Input @bind-Value="@context.TwoLetterISOCode" Placeholder="两个字母ISO代码" />
</FormItem>
<FormItem Label="三字母ISO代码">
<Input @bind-Value="@context.ThreeLetterISOCode" Placeholder="三字母ISO代码" />
</FormItem>
<FormItem Label="数字ISO代码">
<Input @bind-Value="@context.NumericISOCode" Placeholder="数字ISO代码" />
</FormItem>
<FormItem Label="允许发货">
<Checkbox Checked="@context.AllowShipping">允许发货</Checkbox>
</FormItem>
<FormItem Label="显示排序">
<Input @bind-Value="@context.DisplayOrder" Placeholder="显示排序" />
</FormItem>
<FormItem Label="状态">
<Checkbox Checked="@context.Enabled">启用</Checkbox>
</FormItem>
<FormItem WrapperCol="new ColLayoutParam { Span = 24, Offset = 5 }">
<Button Type="@ButtonType.Primary" HtmlType="submit" Loading="saving">
提交保存
</Button>
</FormItem>
</Form>
</Card>
</Spin>
</ChildContent>
</PageContainer>
@code {
[Parameter]
public string Locale { get; set; } = string.Empty;
[Parameter]
public long Id { get; set; }
[SupplyParameterFromForm]
CountryModel model { get; set; } = new();
Form<CountryModel> editform = null!;
CountryLocalizedModel country = new();
List<KeyValue> languageList = new();
bool pageLoading = false;
bool saving = false;
protected override void OnInitialized()
{
base.OnInitialized();
}
protected override void OnParametersSet()
{
_ = LoadLanguage();
if (Id > 0)
{
LoadData();
}
base.OnParametersSet();
}
async Task LoadLanguage()
{
var url = $"/api/language/enabled";
var apiResult = await HttpService.Get<ApiResult<List<KeyValue>>>(url);
if (apiResult.Success)
{
languageList = apiResult.Data ?? new List<KeyValue>();
StateHasChanged();
}
}
async void LoadData()
{
pageLoading = true;
var url = $"/api/country/detail?id={Id}";
var apiResult = await HttpService.Get<ApiResult<CountryLocalizedModel>>(url);
if (apiResult.Success)
{
if (apiResult.Data == null)
{
Navigation.NavigateTo($"/country/create");
}
else
{
country = apiResult.Data;
model = apiResult.Data.Adapt<CountryModel>();
}
}
else
{
Navigation.NavigateTo($"/country/create");
}
pageLoading = false;
StateHasChanged();
}
async void OnFormFinishAsync()
{
if (editform.Validate())
{
saving = true;
var url = $"api/country/save";
var result = new ApiResult<string>();
result = await HttpService.Post<ApiResult<string>>(url, model);
if (result.Success)
{
saving = false;
await ModalService.InfoAsync(new ConfirmOptions() { Title = "提示", Content = "数据提交成功!" });
Navigation.NavigateTo($"/country/list");
}
else
{
saving = false;
await ModalService.ErrorAsync(new ConfirmOptions() { Title = "服务异常", Content = result.Message });
}
}
}
void OnLanguageTabChange(string key)
{
if (key != "0")
{
model.LanguageId = key;
var data = country.Locales.Where(p => p.LanguageId == key.ToInt()).ToList();
if (data.Any())
{
var name = nameof(model.Name);
model.Name = data.SingleOrDefault(p => p.Key == name)?.Value ?? "";
}
else
{
model.Name = string.Empty;
}
}
else
{
model = country.Adapt<CountryModel>();
model.LanguageId = key;
}
}
}