corp-app/CORP-MAUI/Pages/EmployeeProfilePage.xaml.cs

230 lines
8.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Diagnostics;
using CORP_MAUI.Models;
namespace CORP_MAUI.Pages;
public partial class EmployeeProfilePage
{
private User? _user;
private readonly int _userId;
public EmployeeProfilePage(int userId)
{
try
{
Trace.WriteLine($"=== EMPLOYEEPROFILEPAGE CONSTRUCTOR START (UserID: {userId}) ===");
InitializeComponent();
_userId = userId;
// Загружаем данные
_ = LoadUserProfileAsync();
Trace.WriteLine("=== EMPLOYEEPROFILEPAGE CONSTRUCTOR END ===");
}
catch (Exception ex)
{
Trace.WriteLine("=== EMPLOYEEPROFILEPAGE CONSTRUCTOR ERROR ===");
Trace.WriteLine($"[EmployeeProfilePage] ❌ Тип ошибки: {ex.GetType().FullName}");
Trace.WriteLine($"[EmployeeProfilePage] ❌ Сообщение: {ex.Message}");
Trace.WriteLine($"[EmployeeProfilePage] ❌ StackTrace: {ex.StackTrace}");
Task.Run(async () =>
{
await Task.Delay(100);
var app = Application.Current;
if (app != null && app.Windows.Count > 0)
{
Page? mainPage = app.Windows[0].Page;
if (mainPage != null)
{
await mainPage.DisplayAlert("Ошибка EmployeeProfilePage",
$"Ошибка инициализации:\n{ex.Message}", "OK");
}
}
});
throw;
}
}
/// <summary>
/// Конструктор для передачи готового объекта пользователя
/// </summary>
public EmployeeProfilePage(User user)
{
try
{
ArgumentNullException.ThrowIfNull(user);
Trace.WriteLine($"=== EMPLOYEEPROFILEPAGE CONSTRUCTOR START (User object: {user.FullName}) ===");
InitializeComponent();
_user = user;
_userId = user.Id;
// Загружаем данные (будет использован переданный объект, если он полный)
_ = LoadUserProfileAsync();
Trace.WriteLine("=== EMPLOYEEPROFILEPAGE CONSTRUCTOR END ===");
}
catch (Exception ex)
{
Trace.WriteLine("=== EMPLOYEEPROFILEPAGE CONSTRUCTOR ERROR ===");
Trace.WriteLine($"[EmployeeProfilePage] ❌ Тип ошибки: {ex.GetType().FullName}");
Trace.WriteLine($"[EmployeeProfilePage] ❌ Сообщение: {ex.Message}");
Trace.WriteLine($"[EmployeeProfilePage] ❌ StackTrace: {ex.StackTrace}");
Task.Run(async () =>
{
await Task.Delay(100);
var app = Application.Current;
if (app != null && app.Windows.Count > 0)
{
Page? mainPage = app.Windows[0].Page;
if (mainPage != null)
{
await mainPage.DisplayAlert("Ошибка EmployeeProfilePage",
$"Ошибка инициализации:\n{ex.Message}", "OK");
}
}
});
throw;
}
}
private async Task LoadUserProfileAsync()
{
try
{
Trace.WriteLine($"[EmployeeProfilePage] >>> LoadUserProfileAsync START (UserID: {_userId})");
LoadingIndicator.IsVisible = true;
LoadingIndicator.IsRunning = true;
// Если пользователь уже передан, используем его, иначе загружаем
if (_user == null)
{
Trace.WriteLine($"[EmployeeProfilePage] >>> Вызов GetUserByIdAsync({_userId})");
_user = await ServiceProvider.ApiService.GetUserByIdAsync(_userId);
if (_user == null)
{
Trace.WriteLine($"[EmployeeProfilePage] ❌ Пользователь с ID {_userId} не найден");
await DisplayAlert("Ошибка", "Не удалось загрузить профиль сотрудника", "OK");
await Navigation.PopAsync();
return;
}
}
Trace.WriteLine($"[EmployeeProfilePage] ✅ Профиль загружен: {_user.FullName}");
// Предзагружаем аватар пользователя
Trace.WriteLine($"[EmployeeProfilePage] >>> Загрузка аватара, URL: {_user.DisplayAvatarUrl}");
await ServiceProvider.ApiService.LoadUserAvatarAsync(_user);
Trace.WriteLine($"[EmployeeProfilePage] IsAvatarLoaded: {_user.IsAvatarLoaded}, CachedPath: {_user.CachedAvatarFilePath}");
// Заполняем основную информацию
FullNameLabel.Text = _user.FullName;
PositionLabel.Text = _user.PositionName;
AvatarImage.Source = _user.DisplayAvatarSource;
Trace.WriteLine($"[EmployeeProfilePage] ✅ Аватар установлен");
OnlineIndicator.IsVisible = _user.IsOnline;
// Email
if (!string.IsNullOrEmpty(_user.Email))
{
EmailLabel.Text = _user.Email;
EmailSection.IsVisible = true;
}
// Телефон
if (!string.IsNullOrEmpty(_user.PhoneNumber))
{
PhoneLabel.Text = _user.PhoneNumber;
PhoneSection.IsVisible = true;
}
// Отделы
if (_user.Departments != null && _user.Departments.Any())
{
DepartmentsSection.IsVisible = true;
foreach (var department in _user.Departments)
{
var label = new Label
{
Text = $"• {department.Name}",
FontSize = 14,
TextColor = Color.FromArgb("#404040")
};
DepartmentsContainer.Children.Add(label);
}
Trace.WriteLine($"[EmployeeProfilePage] >>> Отделов: {_user.Departments.Count}");
}
// Навыки
if (_user.Skills != null && _user.Skills.Any())
{
SkillsSection.IsVisible = true;
foreach (var skill in _user.Skills)
{
var skillBadge = new Border
{
Padding = new Thickness(12, 6),
Margin = new Thickness(5),
StrokeShape = new Microsoft.Maui.Controls.Shapes.RoundRectangle { CornerRadius = 15 },
BackgroundColor = Color.FromArgb("#E1E1E1"),
Content = new Label
{
Text = skill.Name,
FontSize = 13,
TextColor = Color.FromArgb("#404040")
}
};
SkillsContainer.Children.Add(skillBadge);
}
Trace.WriteLine($"[EmployeeProfilePage] >>> Навыков: {_user.Skills.Count}");
}
Trace.WriteLine($"[EmployeeProfilePage] ✅ Профиль отображен успешно");
}
catch (Exception ex)
{
Trace.WriteLine($"[EmployeeProfilePage] ❌ Ошибка загрузки: {ex.GetType().Name}");
Trace.WriteLine($"[EmployeeProfilePage] Message: {ex.Message}");
Trace.WriteLine($"[EmployeeProfilePage] StackTrace: {ex.StackTrace}");
await DisplayAlert("Ошибка", $"Не удалось загрузить профиль сотрудника:\n{ex.Message}", "OK");
}
finally
{
LoadingIndicator.IsVisible = false;
LoadingIndicator.IsRunning = false;
}
}
private async void OnChatButtonClicked(object sender, EventArgs e)
{
if (_user == null)
return;
Trace.WriteLine($"[EmployeeProfilePage] >>> Открытие чата с: {_user.FullName}");
try
{
await Shell.Current.Navigation.PushAsync(new ChatPage(_user));
}
catch (Exception ex)
{
Trace.WriteLine($"[EmployeeProfilePage] ❌ Ошибка перехода к чату: {ex.Message}");
await DisplayAlert("Ошибка", "Не удалось открыть чат", "OK");
}
}
private async void OnBackTapped(object sender, TappedEventArgs e)
{
await Shell.Current.Navigation.PopAsync();
}
}