@page "/Chat" @using Common.Dtos.Conversation @using Common.Dtos.Group @using Common.Enums @using HushianWebApp.Service @inject ChatService chatService @inject GroupService groupService @inject UserService userService @inject IJSRuntime JS
گفتگو های اخیر
@if (isSelectedInbox1) { @foreach (var item in Inbox1Items) {
@item.UserFullName @item.LastMsgdate @item.LastMsgtime
@item.LastText
@item.Responses.Count()
} } @if (isSelectedInbox2) { @foreach (var item in Inbox2Items) {
@item.UserFullName @item.LastMsgdate @item.LastMsgtime
@item.LastText
@if (item.Responses.Count(c => !c.IsRead && c.Type == ConversationType.UE) > 0) { @item.Responses.Count(c => !c.IsRead && c.Type == ConversationType.UE) }
} } @if (isSelectedInbox3) { @foreach (var item in Inbox3Items) {
@item.UserFullName @item.LastMsgdate @item.LastMsgtime
@item.LastText
} }
@if (ChatCurrent != null) {

@SelectedChatUserName

@if (ChatCurrent.status == Common.Enums.ConversationStatus.InProgress) { } else if (ChatCurrent.status == Common.Enums.ConversationStatus.Finished && (CurrentUser.Role == "Company" || ChatCurrent.ExperID == CurrentUser.ExperID)) { } }
@if (ChatCurrent != null && ChatCurrent.Responses != null) {
@{ bool target = false; } @foreach (var msg in ChatCurrent?.Responses) { @if (!target && ((!msg.IsRead && msg.Type == Common.Enums.ConversationType.UE) || ChatCurrent.Responses.Last() == msg)) { target = true;
@if (!msg.IsRead && msg.Type == Common.Enums.ConversationType.UE) {

ـــــــــــــــــــــــــ

}
}
@msg.text
@if (msg.Type == Common.Enums.ConversationType.EU) { if (msg.IsRead) { } else { } }
} @{ target = false; }
} else {

هوشیان

}
@if (ChatCurrent != null && ChatCurrent.status != Common.Enums.ConversationStatus.Finished && ChatCurrent.Responses != null) {
}
@code { public Common.Dtos.CurrentUserInfo CurrentUser { get; set; } List _Group = new List(); //------------------------------------- bool isSelectedInbox1 = false; public List Inbox1Items { get; set; } = new(); bool isSelectedInbox2 = true; public List Inbox2Items { get; set; } = new(); bool isSelectedInbox3 = false; public List Inbox3Items { get; set; } = new(); ///////////// public ChatItemDto? ChatCurrent { get; set; } = null; public string MsgInput { get; set; } bool chatloading = false; string SelectedChatUserName = "مهدی ربیع نژاد"; private bool _shouldObserveVisibility = false; } @functions { protected override async Task OnInitializedAsync() { CurrentUser = await userService.GetCurrentUserInfo(); _Group = await groupService.GetGroups(); Inbox1Items = await chatService.ChatAwaitingOurResponse(); Inbox2Items = await chatService.MyChatsIsInProgress(); Inbox3Items = await chatService.MyChatsIsFinished(); await base.OnInitializedAsync(); } protected override async Task OnAfterRenderAsync(bool firstRender) { if (_shouldObserveVisibility) { _shouldObserveVisibility = false; await JS.InvokeVoidAsync("observeVisibility", DotNetObjectReference.Create(this)); await JS.InvokeVoidAsync("scrollToTarget"); } } async Task OnclickInbox(int ID) { switch (ID) { case 1: isSelectedInbox1 = true; isSelectedInbox2 = false; isSelectedInbox3 = false; break; case 2: isSelectedInbox2 = true; isSelectedInbox1 = false; isSelectedInbox3 = false; break; case 3: isSelectedInbox3 = true; isSelectedInbox2 = false; isSelectedInbox1 = false; break; } ChatCurrent = null; } async Task OnClickSendMsg() { if (!string.IsNullOrEmpty(MsgInput) && ChatCurrent != null) { Common.Enums.ConversationType type = CurrentUser.Role == "Company" ? Common.Enums.ConversationType.CU : Common.Enums.ConversationType.EU; await chatService.ADDChatResponse(ChatCurrent.ID, MsgInput, type); ChatCurrent?.Responses.Add(new() { text = MsgInput, Type = type }); ChatCurrent.LastText = MsgInput; await Task.Yield(); await JS.InvokeVoidAsync("scrollToBottom", "B1"); MsgInput = string.Empty; } } async Task onClickSelectedChat(int InboxID, ChatItemDto chatItem) { chatloading = true; SelectedChatUserName = "در حال گفتگو با " + chatItem.UserFullName; ChatCurrent = chatItem; _shouldObserveVisibility = true; // فعال کن تا در OnAfterRenderAsync صدا زده بشه StateHasChanged(); // مجبور کن Blazor رندر کنه chatloading = false; } [JSInvokable] public async Task MarkAsRead(int id) { var msg = ChatCurrent.Responses.FirstOrDefault(m => m.ID == id); if (msg != null && !msg.IsRead && msg.Type == Common.Enums.ConversationType.UE) { msg.IsRead = true; await chatService.MarkAsReadChatItemAsync(id); } StateHasChanged(); await Task.CompletedTask; } }