1@using Castle.Core.Internal
2@using Microsoft.AspNetCore.Components
3@using Escali.LicenseControl.Client.ClientModels
4@using Escali.LicenseControl.Client.ViewModels
6@using Escali.LicenseControl.Client.Utils
8@inject CustomerViewModel _customerViewService
9@inject PriorityViewModel _priorityViewService
10@inject MainSegmentViewModel _mainSegmentViewService
11@inject SegmentViewModel _segmentViewService
12@inject RegionViewModel _regionViewService
13@implements IDisposable
14@inject StateService _stateService
15@inject UserViewModel _userViewService
17@if (_stateService.EditCustomer.Value != null)
19 <EditForm Model="_stateService.EditCustomer.Value" OnValidSubmit="Submit" OnInvalidSubmit="EventCallback.Empty">
20 <StandardModal Header="Kundeinfo" SubmitButtonName="Oppdater kunde" ShowModal="true" OnModalClosed="ResetCustomer">
22 <div class="create-modal-container">
23 <ObjectGraphDataAnnotationsValidator />
25 <FloatingInput Name="Kundenavn*">
26 <InputText @bind-Value="_stateService.EditCustomer.Value.CustomerName" type="text" class="form-control input-text" id="floatingInput" placeholder=" " autocomplete="off" />
29 <FloatingInput Name="Org.Nr.*">
30 <InputText @bind-Value="_stateService.EditCustomer.Value.OrganizationNumber" type="text" class="form-control input-text" id="floatingInput" placeholder=" " autocomplete="off" />
33 <FloatingInput Name="Reskontronummer">
34 <InputText @bind-Value="_stateService.EditCustomer.Value.LedgerNumber" type="text" class="form-control input-text" id="floatingInput" placeholder=" " autocomplete="off" />
37 @if (_regionViewService.Regions != null)
39 <FloatingInput Name="Land*">
40 <InputText @bind-Value="CountryInput" type="text" class="form-control input-text " id="floatingInput" placeholder=" " list="countryList" autocomplete="off" />
42 <datalist id="countryList">
43 @foreach (var r in _regionViewService.Regions.DistinctBy(r => r.Country).ToList())
45 <option value="@r.Country">@r.Country</option>
50 @if (_regionViewService.Regions != null && CountryInput != string.Empty)
52 <FloatingInput Name="Region">
53 <InputText @bind-Value="AreaInput" type="text" class="form-control input-text" id="floatingInput" placeholder=" " list="areaList" autocomplete="off" />
55 <datalist id="areaList">
56 @foreach (var r in _regionViewService.Regions.Where(r => r.Country == _stateService.EditCustomer.Value.Region.Country).DistinctBy(r => r.Area).ToList())
58 <option value="@r.Area">@r.Area</option>
63 <div class="input-container">
64 <InputSelect class="form-select form-select-modal" @bind-Value="_stateService.EditCustomer.Value.PrioritySeq">
65 <option selected disabled>Velg prioritet</option>
66 @if (_priorityViewService.Priorities != null)
68 @foreach (var p in _priorityViewService.Priorities)
70 <option value="@p.PrioritySeq">@p.PriorityName.ToString()?.CapitalizeFirstLetter()</option>
76 <div class="input-container">
77 <InputSelect class="form-select form-select-modal" @bind-Value="_stateService.EditCustomer.Value.UserSeq">
78 <option selected disabled>Velg kundeansvarlig</option>
79 @if (_userViewService.Users != null)
81 @foreach (var u in _userViewService.Users)
83 <option value="@u.UserSeq">@u.UserEmail</option>
89 <div class="input-container">
90 <InputSelect class="form-select form-select-modal" @bind-Value="SelectedMainSegmentSeq">
91 <option selected disabled>Velg hovedbransje</option>
92 @if (_mainSegmentViewService.MainSegments != null)
94 @foreach (var ms in _mainSegmentViewService.MainSegments)
96 <option value="@ms.MainSegmentSeq">@ms.MainSegmentName</option>
102 @if (SelectedMainSegmentSeq != null && !_mainSegmentViewService.MainSegments.IsNullOrEmpty())
104 <div class="input-container">
105 <InputSelect class="form-select form-select-modal" @bind-Value="@_stateService.EditCustomer.Value.SegmentSeq">
106 <option disabled selected>Velg bransje</option>
107 @if (SelectedMainSegmentSeq != null && !_mainSegmentViewService.MainSegments.SingleOrDefault(s => s.MainSegmentSeq == SelectedMainSegmentSeq).Segments.IsNullOrEmpty() ) {
108 @foreach (var s in _mainSegmentViewService.MainSegments.SingleOrDefault(s => s.MainSegmentSeq == SelectedMainSegmentSeq).Segments)
110 <option value="@s.SegmentSeq">@s.SegmentName</option>
125 private int? SelectedMainSegmentSeq { get; set; }
127 private string CountryInput { get; set; }
128 private string AreaInput { get; set; }
130 private async void Submit()
132 _stateService.EditCustomer.Value!.Region = new RegionClientModel
134 Country = CountryInput,
135 Area = AreaInput == string.Empty ? null : AreaInput
138 var updatedCustomer = await _customerViewService.UpdateCustomer(_stateService.EditCustomer.Value!);
140 if (updatedCustomer != null)
146 private void ResetCustomer()
148 _stateService.EditCustomer.ValueChanged -= SetInputFields;
149 _stateService.EditCustomer.Value = null;
150 SelectedMainSegmentSeq = null;
152 _stateService.EditCustomer.ValueChanged += SetInputFields;
155 private async void SetInputFields()
157 // var region = _regionViewService.Regions?.SingleOrDefault(r => r.RegionSeq == _stateService.EditCustomer.Value!.RegionSeq);
158 var region = _stateService.EditCustomer.Value!.Region;
159 CountryInput = region!.Country;
160 AreaInput = region.Area;
161 if (_stateService.EditCustomer.Value.Segment != null && _mainSegmentViewService.MainSegments != null)
163 SelectedMainSegmentSeq = _mainSegmentViewService.MainSegments.Single(ms => ms.MainSegmentSeq == _stateService.EditCustomer.Value.Segment.MainSegmentSeq).MainSegmentSeq;
166 await InvokeAsync(_regionViewService.GetRegions);
169 protected override void OnInitialized()
171 _stateService.EditCustomer.ValueChanged += StateHasChanged;
172 _regionViewService.DataChanged += StateHasChanged;
173 _mainSegmentViewService.DataChanged += StateHasChanged;
174 _segmentViewService.DataChanged += StateHasChanged;
176 _stateService.EditCustomer.ValueChanged += SetInputFields;
178 if (_stateService.EditCustomer.Value != null) SetInputFields();
181 void IDisposable.Dispose()
183 _stateService.EditCustomer.ValueChanged -= StateHasChanged;
184 _regionViewService.DataChanged -= StateHasChanged;
185 _mainSegmentViewService.DataChanged -= StateHasChanged;
186 _segmentViewService.DataChanged -= StateHasChanged;
188 _stateService.EditCustomer.ValueChanged -= SetInputFields;