Boards.razor 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. @* @page "/setting/boardsetting" *@
  2. @page "/setting/boardsetting"
  3. @using EasyTemplate.Service
  4. @using EasyTemplate.Tool.Entity.App
  5. @inject NozzleService nozzleService
  6. @inject NavigationManager navigationManager
  7. @inject IJSRuntime JSRuntime
  8. <h3>主板管理</h3>
  9. <div class="row mb-3">
  10. <div class="col-md-6">
  11. <button class="btn btn-primary" @onclick="ShowCreateModal">添加主板</button>
  12. <a class="btn btn-secondary ms-2" href="/setting/enginesetting">返回油机列表</a>
  13. </div>
  14. <div class="col-md-6">
  15. <div class="input-group">
  16. <span class="input-group-text">筛选油机ID</span>
  17. <select class="form-select" @bind="filterEngineId">
  18. <option value="">全部</option>
  19. @if (engines != null)
  20. {
  21. @foreach (var engine in engines)
  22. {
  23. <option value="@engine.EngineId">@engine.EngineId</option>
  24. }
  25. }
  26. </select>
  27. <button class="btn btn-outline-secondary" @onclick="ApplyFilter">筛选</button>
  28. <button class="btn btn-outline-danger" @onclick="ClearFilter">清除</button>
  29. </div>
  30. </div>
  31. </div>
  32. <table class="table table-striped">
  33. <thead>
  34. <tr>
  35. <th>ID</th>
  36. <th>所属油机ID</th>
  37. <th>IP地址</th>
  38. <th>油枪数量</th>
  39. <th>操作</th>
  40. </tr>
  41. </thead>
  42. <tbody>
  43. @if (filteredBoards != null)
  44. {
  45. @foreach (var board in filteredBoards)
  46. {
  47. <tr>
  48. <td>@board.BoardId</td>
  49. <td>@board.EngineId</td>
  50. <td>@board.ip</td>
  51. <td>@(board.nozzles?.Count ?? 0)</td>
  52. <td>
  53. <button class="btn btn-sm btn-info me-1" @onclick="() => ShowEditModal(board)">编辑</button>
  54. <button class="btn btn-sm btn-danger me-1" @onclick="() => DeleteBoard(board.BoardId)">删除</button>
  55. <a class="btn btn-sm btn-secondary" href="/setting/nozzlesetting?boardId=@board.BoardId">管理油枪</a>
  56. </td>
  57. </tr>
  58. }
  59. }
  60. </tbody>
  61. </table>
  62. <!-- 创建/编辑模态框 -->
  63. <div class="modal @(showModal ? "show" : "")" tabindex="-1" style="display: @(showModal ? "block" : "none"); background-color: rgba(0,0,0,0.5);">
  64. <div class="modal-dialog">
  65. <div class="modal-content">
  66. <div class="modal-header">
  67. <h5 class="modal-title">@modalTitle</h5>
  68. <button type="button" class="btn-close" @onclick="CloseModal"></button>
  69. </div>
  70. <div class="modal-body">
  71. <div class="mb-3">
  72. <label class="form-label">所属油机ID</label>
  73. <select class="form-select" @bind="currentBoard.EngineId">
  74. <option value="">请选择油机</option>
  75. @if (engines != null)
  76. {
  77. @foreach (var engine in engines)
  78. {
  79. <option value="@engine.EngineId">@engine.EngineId</option>
  80. }
  81. }
  82. </select>
  83. </div>
  84. <div class="mb-3">
  85. <label class="form-label">主板id</label>
  86. <input class="form-control" @bind="currentBoard.BoardId" />
  87. </div>
  88. <div class="mb-3">
  89. <label class="form-label">IP地址</label>
  90. <input class="form-control" @bind="currentBoard.ip" />
  91. </div>
  92. </div>
  93. <div class="modal-footer">
  94. <button type="button" class="btn btn-secondary" @onclick="CloseModal">取消</button>
  95. <button type="button" class="btn btn-primary" @onclick="SaveBoard">保存</button>
  96. </div>
  97. </div>
  98. </div>
  99. </div>
  100. @code {
  101. private List<TBoard> allBoards = new();
  102. private List<TBoard> filteredBoards = new();
  103. private List<TEngine> engines = new();
  104. private TBoard currentBoard = new();
  105. private string modalTitle = "";
  106. private bool isEditMode = false;
  107. private bool showModal = false;
  108. private int? filterEngineId;
  109. protected override async Task OnInitializedAsync()
  110. {
  111. await LoadData();
  112. // 检查URL参数
  113. var uri = navigationManager.ToAbsoluteUri(navigationManager.Uri);
  114. if (Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query).TryGetValue("engineId", out var engineId))
  115. {
  116. if (int.TryParse(engineId, out int id))
  117. {
  118. filterEngineId = id;
  119. ApplyFilter();
  120. }
  121. }
  122. }
  123. private async Task LoadData()
  124. {
  125. engines = await nozzleService.GetEnginesAsync();
  126. allBoards = await nozzleService.GetBoardsAsync();
  127. // 加载油枪信息
  128. foreach (var board in allBoards)
  129. {
  130. board.nozzles = await nozzleService.GetNozzlesByBoardAsync(board.BoardId);
  131. }
  132. ApplyFilter();
  133. }
  134. private void ApplyFilter()
  135. {
  136. if (filterEngineId.HasValue)
  137. {
  138. filteredBoards = allBoards.Where(b => b.EngineId == filterEngineId.Value).ToList();
  139. }
  140. else
  141. {
  142. filteredBoards = new List<TBoard>(allBoards);
  143. }
  144. StateHasChanged();
  145. }
  146. private void ClearFilter()
  147. {
  148. filterEngineId = null;
  149. ApplyFilter();
  150. }
  151. private void ShowCreateModal()
  152. {
  153. currentBoard = new TBoard();
  154. modalTitle = "添加主板";
  155. isEditMode = false;
  156. showModal = true;
  157. StateHasChanged();
  158. }
  159. private void ShowEditModal(TBoard board)
  160. {
  161. currentBoard = new TBoard
  162. {
  163. BoardId = board.BoardId,
  164. EngineId = board.EngineId,
  165. ip = board.ip
  166. };
  167. modalTitle = "编辑主板";
  168. isEditMode = true;
  169. showModal = true;
  170. StateHasChanged();
  171. }
  172. private void CloseModal()
  173. {
  174. showModal = false;
  175. StateHasChanged();
  176. }
  177. private async Task SaveBoard()
  178. {
  179. if (currentBoard.EngineId <= 0)
  180. {
  181. await JSRuntime.InvokeVoidAsync("alert", "请选择有效的油机ID");
  182. return;
  183. }
  184. try
  185. {
  186. if (isEditMode)
  187. {
  188. await nozzleService.UpdateBoardAsync(currentBoard);
  189. }
  190. else
  191. {
  192. await nozzleService.CreateBoardAsync(currentBoard);
  193. }
  194. CloseModal();
  195. await LoadData();
  196. }
  197. catch (Exception ex)
  198. {
  199. await JSRuntime.InvokeVoidAsync("alert", $"操作失败: {ex.Message}");
  200. }
  201. }
  202. private async Task DeleteBoard(int id)
  203. {
  204. var confirmed = await JSRuntime.InvokeAsync<bool>("confirm", "确定要删除这个主板吗?");
  205. if (confirmed)
  206. {
  207. try
  208. {
  209. await nozzleService.DeleteBoardAsync(id);
  210. await LoadData();
  211. }
  212. catch (Exception ex)
  213. {
  214. await JSRuntime.InvokeVoidAsync("alert", $"删除失败: {ex.Message}");
  215. }
  216. }
  217. }
  218. }