@page "/Chat" @page "/" @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 @inject ToastService toastService گفتمان
@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)) { }
} else if (isSelectedInbox1) {
نکته مهم

از انتخاب گفتگو مطمئن شوید، بعد از انتخاب شما مسئول بررسی می‌باشد

}
@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.UE) { 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; private ConfirmDialog dialog = default!; } @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; } async Task OpenChat() { if (CurrentUser.Role == "Company" || CurrentUser.Role == "Exper" && ChatCurrent.ExperID==CurrentUser.ExperID) { if (ChatCurrent.status != Common.Enums.ConversationStatus.Finished) return; if (await chatService.OpenChat(ChatCurrent.ID)) { ChatCurrent.status = Common.Enums.ConversationStatus.InProgress; StateHasChanged(); } } else toastService.Notify(new ToastMessage(ToastType.Danger, "دسترسی به این گفتگو ندارید")); } async Task CloseChat() { if (ChatCurrent.status == Common.Enums.ConversationStatus.Finished) return; var options = new ConfirmDialogOptions { YesButtonText = "بله", YesButtonColor = ButtonColor.Success, NoButtonText = "انصراف", NoButtonColor = ButtonColor.Danger }; var confirmation = await dialog.ShowAsync( title: "پایان دادن به گفتگو", message1: "اطمینان دارید ؟", confirmDialogOptions: options); if (confirmation) { if (await chatService.ChatIsFinish(ChatCurrent.ID)) { ChatCurrent.status = Common.Enums.ConversationStatus.Finished; StateHasChanged(); } } } }