| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- @* @page "/setting/boardsetting" *@
- @page "/setting/boardsetting"
- @using EasyTemplate.Service
- @using EasyTemplate.Tool.Entity.App
- @inject NozzleService nozzleService
- @inject NavigationManager navigationManager
- @inject IJSRuntime JSRuntime
- <h3>主板管理</h3>
- <div class="row mb-3">
- <div class="col-md-6">
- <button class="btn btn-primary" @onclick="ShowCreateModal">添加主板</button>
- <a class="btn btn-secondary ms-2" href="/setting/enginesetting">返回油机列表</a>
- </div>
- <div class="col-md-6">
- <div class="input-group">
- <span class="input-group-text">筛选油机ID</span>
- <select class="form-select" @bind="filterEngineId">
- <option value="">全部</option>
- @if (engines != null)
- {
- @foreach (var engine in engines)
- {
- <option value="@engine.EngineId">@engine.EngineId</option>
- }
- }
- </select>
- <button class="btn btn-outline-secondary" @onclick="ApplyFilter">筛选</button>
- <button class="btn btn-outline-danger" @onclick="ClearFilter">清除</button>
- </div>
- </div>
- </div>
- <table class="table table-striped">
- <thead>
- <tr>
- <th>ID</th>
- <th>所属油机ID</th>
- <th>IP地址</th>
- <th>油枪数量</th>
- <th>操作</th>
- </tr>
- </thead>
- <tbody>
- @if (filteredBoards != null)
- {
- @foreach (var board in filteredBoards)
- {
- <tr>
- <td>@board.BoardId</td>
- <td>@board.EngineId</td>
- <td>@board.ip</td>
- <td>@(board.nozzles?.Count ?? 0)</td>
- <td>
- <button class="btn btn-sm btn-info me-1" @onclick="() => ShowEditModal(board)">编辑</button>
- <button class="btn btn-sm btn-danger me-1" @onclick="() => DeleteBoard(board.BoardId)">删除</button>
- <a class="btn btn-sm btn-secondary" href="/setting/nozzlesetting?boardId=@board.BoardId">管理油枪</a>
- </td>
- </tr>
- }
- }
- </tbody>
- </table>
- <!-- 创建/编辑模态框 -->
- <div class="modal @(showModal ? "show" : "")" tabindex="-1" style="display: @(showModal ? "block" : "none"); background-color: rgba(0,0,0,0.5);">
- <div class="modal-dialog">
- <div class="modal-content">
- <div class="modal-header">
- <h5 class="modal-title">@modalTitle</h5>
- <button type="button" class="btn-close" @onclick="CloseModal"></button>
- </div>
- <div class="modal-body">
- <div class="mb-3">
- <label class="form-label">所属油机ID</label>
- <select class="form-select" @bind="currentBoard.EngineId">
- <option value="">请选择油机</option>
- @if (engines != null)
- {
- @foreach (var engine in engines)
- {
- <option value="@engine.EngineId">@engine.EngineId</option>
- }
- }
- </select>
- </div>
- <div class="mb-3">
- <label class="form-label">主板id</label>
- <input class="form-control" @bind="currentBoard.BoardId" />
- </div>
- <div class="mb-3">
- <label class="form-label">IP地址</label>
- <input class="form-control" @bind="currentBoard.ip" />
- </div>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-secondary" @onclick="CloseModal">取消</button>
- <button type="button" class="btn btn-primary" @onclick="SaveBoard">保存</button>
- </div>
- </div>
- </div>
- </div>
- @code {
- private List<TBoard> allBoards = new();
- private List<TBoard> filteredBoards = new();
- private List<TEngine> engines = new();
- private TBoard currentBoard = new();
- private string modalTitle = "";
- private bool isEditMode = false;
- private bool showModal = false;
- private int? filterEngineId;
- protected override async Task OnInitializedAsync()
- {
- await LoadData();
- // 检查URL参数
- var uri = navigationManager.ToAbsoluteUri(navigationManager.Uri);
- if (Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query).TryGetValue("engineId", out var engineId))
- {
- if (int.TryParse(engineId, out int id))
- {
- filterEngineId = id;
- ApplyFilter();
- }
- }
- }
- private async Task LoadData()
- {
- engines = await nozzleService.GetEnginesAsync();
- allBoards = await nozzleService.GetBoardsAsync();
- // 加载油枪信息
- foreach (var board in allBoards)
- {
- board.nozzles = await nozzleService.GetNozzlesByBoardAsync(board.BoardId);
- }
- ApplyFilter();
- }
- private void ApplyFilter()
- {
- if (filterEngineId.HasValue)
- {
- filteredBoards = allBoards.Where(b => b.EngineId == filterEngineId.Value).ToList();
- }
- else
- {
- filteredBoards = new List<TBoard>(allBoards);
- }
- StateHasChanged();
- }
- private void ClearFilter()
- {
- filterEngineId = null;
- ApplyFilter();
- }
- private void ShowCreateModal()
- {
- currentBoard = new TBoard();
- modalTitle = "添加主板";
- isEditMode = false;
- showModal = true;
- StateHasChanged();
- }
- private void ShowEditModal(TBoard board)
- {
- currentBoard = new TBoard
- {
- BoardId = board.BoardId,
- EngineId = board.EngineId,
- ip = board.ip
- };
- modalTitle = "编辑主板";
- isEditMode = true;
- showModal = true;
- StateHasChanged();
- }
- private void CloseModal()
- {
- showModal = false;
- StateHasChanged();
- }
- private async Task SaveBoard()
- {
- if (currentBoard.EngineId <= 0)
- {
- await JSRuntime.InvokeVoidAsync("alert", "请选择有效的油机ID");
- return;
- }
- try
- {
- if (isEditMode)
- {
- await nozzleService.UpdateBoardAsync(currentBoard);
- }
- else
- {
- await nozzleService.CreateBoardAsync(currentBoard);
- }
- CloseModal();
- await LoadData();
- }
- catch (Exception ex)
- {
- await JSRuntime.InvokeVoidAsync("alert", $"操作失败: {ex.Message}");
- }
- }
- private async Task DeleteBoard(int id)
- {
- var confirmed = await JSRuntime.InvokeAsync<bool>("confirm", "确定要删除这个主板吗?");
- if (confirmed)
- {
- try
- {
- await nozzleService.DeleteBoardAsync(id);
- await LoadData();
- }
- catch (Exception ex)
- {
- await JSRuntime.InvokeVoidAsync("alert", $"删除失败: {ex.Message}");
- }
- }
- }
- }
|