Skip to content

تقرير #186

@YasserAhmed152

Description

@YasserAhmed152

using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Printing;
using System.Windows.Xps;
using System.Threading.Tasks;
using Employee_Management_System.DAL;

namespace Employee_Management_System.Services
{
public static class EvaluationPrintService
{
// ألوان متطورة مع تدرجات أكثر احترافية
private static readonly Color PrimaryColor = Color.FromRgb(30, 50, 90);
private static readonly Color SecondaryColor = Color.FromRgb(70, 130, 180);
private static readonly Color AccentColor = Color.FromRgb(0, 150, 136);
private static readonly Color LightBackground = Color.FromRgb(248, 249, 250);
private static readonly Color BorderColor = Color.FromRgb(230, 230, 230);

    // خطوط أكثر احترافية
    private static readonly FontFamily TitleFont = new FontFamily("Arial");
    private static readonly FontFamily TextFont = new FontFamily("Segoe UI");

    public static async Task PrintEvaluationAsync(int evaluationId)
    {
        try
        {
            var evaluationData = await DatabaseHelper.GetEvaluationFormData(evaluationId);

            if (evaluationData == null || evaluationData.Rows.Count == 0)
            {
                ShowMessage("لا توجد بيانات متاحة للطباعة", "تحذير", MessageBoxImage.Warning);
                return;
            }

            var document = CreatePrintableDocument(evaluationData);
            ShowPrintPreviewAndPrint(document);
        }
        catch (Exception ex)
        {
            ShowError("حدث خطأ أثناء محاولة الطباعة", ex);
        }
    }

    private static void ShowPrintPreviewAndPrint(FixedDocument document)
    {
        var previewWindow = new PrintPreviewWindow(document);
        if (previewWindow.ShowDialog() == true)
        {
            ExecutePrinting(document);
        }
    }

    private static void ExecutePrinting(FixedDocument document)
    {
        var printDialog = new PrintDialog();
        if (printDialog.ShowDialog() == true)
        {
            printDialog.PrintTicket.PageOrientation = PageOrientation.Portrait;
            printDialog.PrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4);

            var writer = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue);
            writer.Write(document);

            ShowMessage("تم إرسال التقرير للطباعة بنجاح", "تمت العملية", MessageBoxImage.Information);
        }
    }

    private static FixedDocument CreatePrintableDocument(DataTable data)
    {
        var doc = new FixedDocument();

        // الصفحة الأولى (الغلاف)
        var coverPage = CreateCoverPage(data.Rows[0]);
        doc.Pages.Add(new PageContent { Child = coverPage });

        // الصفحة الثانية (التفاصيل)
        var detailsPage = CreateDetailsPage(data.Rows[0]);
        doc.Pages.Add(new PageContent { Child = detailsPage });

        return doc;
    }

    private static FixedPage CreateCoverPage(DataRow data)
    {
        var page = new FixedPage
        {
            Width = 794,
            Height = 1123,
            Background = new LinearGradientBrush(
                Color.FromRgb(245, 248, 250),
                Color.FromRgb(230, 240, 250),
                90),
            FlowDirection = FlowDirection.LeftToRight,
            HorizontalAlignment = HorizontalAlignment.Right
        };

        var mainContainer = new StackPanel
        {
            Margin = new Thickness(50),
            FlowDirection = FlowDirection.RightToLeft,
            HorizontalAlignment = HorizontalAlignment.Right
        };

        // شعار الجامعة
        var logoContainer = new StackPanel
        {
            Orientation = Orientation.Horizontal,
            HorizontalAlignment = HorizontalAlignment.Right,
            Margin = new Thickness(0, 20, 0, 40),
            FlowDirection = FlowDirection.LeftToRight
        };

        var logo = new Image
        {
            Source = new BitmapImage(new Uri("pack://application:,,,/Employee-Management-System;component/Resources/logo.png")),
            Width = 80,
            Height = 80,
            Margin = new Thickness(10),
            HorizontalAlignment = HorizontalAlignment.Right
        };

        var universityInfo = new StackPanel
        {
            VerticalAlignment = VerticalAlignment.Center,
            Margin = new Thickness(10),
            HorizontalAlignment = HorizontalAlignment.Right,
            FlowDirection = FlowDirection.LeftToRight
        };

        var universityName = new TextBlock
        {
            Text = "جامعة التقنية والإدارة",
            FontFamily = TitleFont,
            FontSize = 18,
            FontWeight = FontWeights.Bold,
            Foreground = new SolidColorBrush(PrimaryColor),
            TextAlignment = TextAlignment.Right,
            HorizontalAlignment = HorizontalAlignment.Right
        };

        var facultyName = new TextBlock
        {
            Text = "كلية علوم الحاسب ونظم المعلومات",
            FontFamily = TextFont,
            FontSize = 14,
            Foreground = new SolidColorBrush(Colors.Gray),
            TextAlignment = TextAlignment.Right,
            HorizontalAlignment = HorizontalAlignment.Right,
            Margin = new Thickness(0, 5, 0, 0)
        };

        universityInfo.Children.Add(universityName);
        universityInfo.Children.Add(facultyName);
        logoContainer.Children.Add(universityInfo);
        logoContainer.Children.Add(logo);

        // عنوان التقرير
        var reportTitle = new TextBlock
        {
            Text = "تقرير التقييم الأكاديمي",
            FontFamily = TitleFont,
            FontSize = 28,
            FontWeight = FontWeights.Bold,
            Foreground = new SolidColorBrush(PrimaryColor),
            TextAlignment = TextAlignment.Right,
            HorizontalAlignment = HorizontalAlignment.Right,
            Margin = new Thickness(0, 20, 0, 40)
        };

        // بطاقة الطالب
        var studentCard = CreateStudentCard(data, true);

        // التقييم العام
        var ratingPanel = CreateOverallRatingPanel(data);

        mainContainer.Children.Add(logoContainer);
        mainContainer.Children.Add(reportTitle);
        mainContainer.Children.Add(studentCard);
        mainContainer.Children.Add(ratingPanel);

        page.Children.Add(mainContainer);
        return page;
    }

    private static FixedPage CreateDetailsPage(DataRow data)
    {
        var page = new FixedPage
        {
            Width = 794,
            Height = 1123,
            Background = Brushes.White,
            FlowDirection = FlowDirection.RightToLeft,
            HorizontalAlignment = HorizontalAlignment.Right
        };

        var mainContainer = new StackPanel
        {
            Margin = new Thickness(40),
            FlowDirection = FlowDirection.RightToLeft,
            HorizontalAlignment = HorizontalAlignment.Right
        };

        // رأس الصفحة
        var header = new StackPanel
        {
            Orientation = Orientation.Horizontal,
            HorizontalAlignment = HorizontalAlignment.Right,
            Margin = new Thickness(0, 0, 0, 30),
            FlowDirection = FlowDirection.RightToLeft
        };

        var logo = new Image
        {
            Source = new BitmapImage(new Uri("pack://application:,,,/Employee-Management-System;component/Resources/logo.png")),
            Width = 50,
            Height = 50,
            Margin = new Thickness(10, 0, 0, 0),
            HorizontalAlignment = HorizontalAlignment.Right
        };

        var title = new TextBlock
        {
            Text = "تفاصيل التقييم",
            FontFamily = TitleFont,
            FontSize = 22,
            FontWeight = FontWeights.Bold,
            Foreground = new SolidColorBrush(PrimaryColor),
            VerticalAlignment = VerticalAlignment.Center,
            HorizontalAlignment = HorizontalAlignment.Right,
            TextAlignment = TextAlignment.Right
        };

        header.Children.Add(title);
        header.Children.Add(logo);

        // تفاصيل التقييم
        var evaluationDetails = CreateEvaluationDetails(data);

        // تذييل الصفحة
        var footer = CreateFooter(data);

        mainContainer.Children.Add(header);
        mainContainer.Children.Add(evaluationDetails);
        mainContainer.Children.Add(footer);

        page.Children.Add(mainContainer);
        return page;
    }

    private static UIElement CreateStudentCard(DataRow data, bool isCoverPage = false)
    {
        var card = new Border
        {
            Background = Brushes.White,
            BorderBrush = new SolidColorBrush(BorderColor),
            BorderThickness = new Thickness(1),
            CornerRadius = new CornerRadius(8),
            Padding = new Thickness(20),
            Margin = new Thickness(0, 0, 0, isCoverPage ? 40 : 20),
            HorizontalAlignment = HorizontalAlignment.Right,
            FlowDirection = FlowDirection.RightToLeft,
            Effect = new System.Windows.Media.Effects.DropShadowEffect
            {
                BlurRadius = 8,
                ShadowDepth = 3,
                Opacity = 0.1,
                Color = Colors.Black
            }
        };

        var grid = new Grid
        {
            FlowDirection = FlowDirection.RightToLeft,
            HorizontalAlignment = HorizontalAlignment.Right
        };

        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

        // العمود الأول
        var col1 = new StackPanel
        {
            Margin = new Thickness(0, 0, 15, 0),
            FlowDirection = FlowDirection.RightToLeft,
            HorizontalAlignment = HorizontalAlignment.Right
        };
        AddStudentInfoItem(col1, "الاسم الكامل:", data["FullName"]);
        AddStudentInfoItem(col1, "القسم:", data["DepartmentName"]);
        AddStudentInfoItem(col1, "البريد الإلكتروني:", data["Email"]);

        // العمود الثاني
        var col2 = new StackPanel
        {
            FlowDirection = FlowDirection.RightToLeft,
            HorizontalAlignment = HorizontalAlignment.Right
        };
        AddStudentInfoItem(col2, "الرقم الجامعي:", data["UniversityNumber"]);
        AddStudentInfoItem(col2, "السنة الدراسية:", data["CurrentAcademicYear"]);
        AddStudentInfoItem(col2, "تاريخ التسجيل:", ((DateTime)data["CreatedDate"]).ToString("yyyy/MM/dd"));

        grid.Children.Add(col1);
        grid.Children.Add(col2);
        Grid.SetColumn(col1, 0);
        Grid.SetColumn(col2, 1);

        card.Child = grid;
        return card;
    }

    private static void AddStudentInfoItem(Panel parent, string label, object value)
    {
        var stack = new StackPanel
        {
            Orientation = Orientation.Horizontal,
            HorizontalAlignment = HorizontalAlignment.Right,
            Margin = new Thickness(0, 0, 0, 8),
            FlowDirection = FlowDirection.RightToLeft
        };

        var labelBlock = new TextBlock
        {
            Text = label,
            FontFamily = TextFont,
            FontWeight = FontWeights.SemiBold,
            Foreground = new SolidColorBrush(Colors.Gray),
            Margin = new Thickness(5, 0, 0, 0),
            MinWidth = 100,
            TextAlignment = TextAlignment.Right
        };

        var valueBlock = new TextBlock
        {
            Text = value?.ToString(),
            FontFamily = TextFont,
            TextAlignment = TextAlignment.Right,
            TextWrapping = TextWrapping.Wrap,
            HorizontalAlignment = HorizontalAlignment.Right
        };

        stack.Children.Add(valueBlock);
        stack.Children.Add(labelBlock);
        parent.Children.Add(stack);
    }

    private static UIElement CreateOverallRatingPanel(DataRow data)
    {
        var panel = new StackPanel
        {
            Orientation = Orientation.Horizontal,
            HorizontalAlignment = HorizontalAlignment.Right,
            Margin = new Thickness(0, 20, 0, 0),
            FlowDirection = FlowDirection.RightToLeft
        };

        AddRatingCircle(panel, "إداري", data["Administrative_FinalEvaluation"], SecondaryColor);
        AddRatingCircle(panel, "تعليمي", data["Educational_FinalEvaluation"], PrimaryColor);
        AddRatingCircle(panel, "نهائي", data["FinalEvaluation"], AccentColor, true);

        return panel;
    }

    private static void AddRatingCircle(StackPanel parent, string title, object value, Color color, bool isLarge = false)
    {
        var size = isLarge ? 130 : 100;
        var fontSize = isLarge ? 26 : 20;
        var titleFontSize = isLarge ? 16 : 14;

        var stack = new StackPanel
        {
            Width = size,
            Margin = new Thickness(20),
            HorizontalAlignment = HorizontalAlignment.Right,
            FlowDirection = FlowDirection.RightToLeft
        };

        var ellipse = new Border
        {
            Width = size,
            Height = size,
            CornerRadius = new CornerRadius(size / 2),
            Background = new LinearGradientBrush(
                Color.FromArgb(200, color.R, color.G, color.B),
                color,
                45),
            BorderBrush = new SolidColorBrush(Colors.White),
            BorderThickness = new Thickness(4),
            HorizontalAlignment = HorizontalAlignment.Right,
            Effect = new System.Windows.Media.Effects.DropShadowEffect
            {
                BlurRadius = 10,
                ShadowDepth = 3,
                Opacity = 0.3,
                Color = Colors.Black
            },
            Child = new TextBlock
            {
                Text = value.ToString(),
                FontFamily = TitleFont,
                FontSize = fontSize,
                FontWeight = FontWeights.Bold,
                Foreground = Brushes.White,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                TextAlignment = TextAlignment.Center
            }
        };

        var titleBlock = new TextBlock
        {
            Text = title,
            FontFamily = TextFont,
            FontSize = titleFontSize,
            FontWeight = FontWeights.SemiBold,
            HorizontalAlignment = HorizontalAlignment.Right,
            Margin = new Thickness(0, 12, 0, 0),
            Foreground = new SolidColorBrush(Colors.DimGray),
            TextAlignment = TextAlignment.Right
        };

        stack.Children.Add(ellipse);
        stack.Children.Add(titleBlock);
        parent.Children.Add(stack);
    }

    private static UIElement CreateEvaluationDetails(DataRow data)
    {
        var container = new StackPanel
        {
            FlowDirection = FlowDirection.RightToLeft,
            HorizontalAlignment = HorizontalAlignment.Right
        };

        // قسم المهارات الإدارية
        var adminSection = CreateEvaluationSection(
            "المهارات الإدارية",
            new[]
            {
                ("المساهمة في الخطط", data["Administrative_PlansContribution"]),
                ("القدرة على التكيف", data["Administrative_Adaptability"]),
                ("التحليل والإبداع", data["Administrative_AnalysisCreativity"]),
                ("التحسين والتطوير", data["Administrative_Improvement"])
            },
            SecondaryColor);

        // قسم المهارات التعليمية
        var eduSection = CreateEvaluationSection(
            "المهارات التعليمية",
            new[]
            {
                ("المسؤولية", data["Educational_Responsibility"]),
                ("التطوير الذاتي", data["Educational_Improvement"]),
                ("الفهم والاستيعاب", data["Educational_Understanding"]),
                ("المساهمة", data["Educational_Contribution"])
            },
            PrimaryColor);

        container.Children.Add(adminSection);
        container.Children.Add(eduSection);

        return container;
    }

    private static UIElement CreateEvaluationSection(string title, (string, object)[] items, Color accentColor)
    {
        var section = new StackPanel
        {
            Margin = new Thickness(0, 0, 0, 30),
            FlowDirection = FlowDirection.RightToLeft,
            HorizontalAlignment = HorizontalAlignment.Right
        };

        // عنوان القسم
        var titleBorder = new Border
        {
            Background = new SolidColorBrush(accentColor),
            CornerRadius = new CornerRadius(4),
            Padding = new Thickness(10, 6, 10, 6),
            HorizontalAlignment = HorizontalAlignment.Right,
            Margin = new Thickness(0, 0, 0, 15),
            FlowDirection = FlowDirection.RightToLeft
        };

        var titleBlock = new TextBlock
        {
            Text = title,
            FontFamily = TitleFont,
            FontSize = 18,
            FontWeight = FontWeights.Bold,
            Foreground = Brushes.White,
            HorizontalAlignment = HorizontalAlignment.Right,
            TextAlignment = TextAlignment.Right
        };

        titleBorder.Child = titleBlock;
        section.Children.Add(titleBorder);

        // عناصر التقييم
        var itemsGrid = new Grid
        {
            FlowDirection = FlowDirection.LeftToRight,
            HorizontalAlignment = HorizontalAlignment.Right
        };

        itemsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        itemsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) });

        for (int i = 0; i < items.Length; i++)
        {
            itemsGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });

            // تسمية المهارة
            var skillLabel = new TextBlock
            {
                Text = items[i].Item1,
                FontFamily = TextFont,
                FontSize = 14,
                Margin = new Thickness(15, 8, 0, 8),
                HorizontalAlignment = HorizontalAlignment.Right,
                TextAlignment = TextAlignment.Right
            };
            Grid.SetRow(skillLabel, i);
            Grid.SetColumn(skillLabel, 1);
            itemsGrid.Children.Add(skillLabel);

            // شريط التقييم
            var ratingBar = CreateRatingBar(items[i].Item2, accentColor);
            Grid.SetRow(ratingBar, i);
            Grid.SetColumn(ratingBar, 0);
            itemsGrid.Children.Add(ratingBar);
        }

        section.Children.Add(itemsGrid);
        return section;
    }

    private static UIElement CreateRatingBar(object value, Color color)
    {
        int rating = Convert.ToInt32(value);

        var container = new StackPanel
        {
            Orientation = Orientation.Horizontal,
            HorizontalAlignment = HorizontalAlignment.Right,
            VerticalAlignment = VerticalAlignment.Center,
            FlowDirection = FlowDirection.LeftToRight
        };

        // النجوم المملوءة
        for (int i = 1; i <= rating; i++)
        {
            container.Children.Add(CreateStarIcon(true, color));
        }

        // النجوم الفارغة
        for (int i = rating + 1; i <= 5; i++)
        {
            container.Children.Add(CreateStarIcon(false, color));
        }

        // النسبة المئوية
        var percentage = new TextBlock
        {
            Text = $"{rating * 20}%",
            FontFamily = TextFont,
            FontSize = 12,
            Foreground = new SolidColorBrush(Colors.Gray),
            Margin = new Thickness(8, 0, 0, 0),
            VerticalAlignment = VerticalAlignment.Center,
            HorizontalAlignment = HorizontalAlignment.Right
        };

        var mainStack = new StackPanel
        {
            Orientation = Orientation.Horizontal,
            HorizontalAlignment = HorizontalAlignment.Right,
            FlowDirection = FlowDirection.LeftToRight
        };

        mainStack.Children.Add(container);
        mainStack.Children.Add(percentage);

        return mainStack;
    }

    private static UIElement CreateStarIcon(bool isFilled, Color color)
    {
        var path = new System.Windows.Shapes.Path
        {
            Width = 16,
            Height = 16,
            Margin = new Thickness(2),
            Data = Geometry.Parse("M 8,1 L 10,6 L 15,6 L 11,9 L 13,14 L 8,11 L 3,14 L 5,9 L 1,6 L 6,6 Z"),
            Fill = isFilled ? new SolidColorBrush(color) : new SolidColorBrush(Colors.LightGray),
            Stretch = Stretch.Fill,
            HorizontalAlignment = HorizontalAlignment.Right
        };

        return path;
    }

    private static UIElement CreateFooter(DataRow data)
    {
        var footer = new StackPanel
        {
            Margin = new Thickness(0, 30, 0, 0),
            FlowDirection = FlowDirection.LeftToRight,
            HorizontalAlignment = HorizontalAlignment.Right
        };

        // ملاحظات
        if (!string.IsNullOrWhiteSpace(data["GeneralNotes"].ToString()))
        {
            var notesHeader = new TextBlock
            {
                Text = "ملاحظات إضافية:",
                FontFamily = TitleFont,
                FontSize = 14,
                FontWeight = FontWeights.SemiBold,
                Foreground = new SolidColorBrush(Colors.DimGray),
                Margin = new Thickness(0, 0, 0, 5),
                HorizontalAlignment = HorizontalAlignment.Right,
                TextAlignment = TextAlignment.Right
            };

            var notesBox = new Border
            {
                Background = new SolidColorBrush(Color.FromArgb(20, 0, 0, 0)),
                BorderThickness = new Thickness(0),
                CornerRadius = new CornerRadius(4),
                Padding = new Thickness(12),
                Margin = new Thickness(0, 0, 0, 20),
                HorizontalAlignment = HorizontalAlignment.Right,
                FlowDirection = FlowDirection.RightToLeft
            };

            var notesText = new TextBlock
            {
                Text = data["GeneralNotes"].ToString(),
                FontFamily = TextFont,
                FontSize = 13,
                TextWrapping = TextWrapping.Wrap,
                TextAlignment = TextAlignment.Right,
                LineHeight = 22,
                HorizontalAlignment = HorizontalAlignment.Right
            };

            notesBox.Child = notesText;
            footer.Children.Add(notesHeader);
            footer.Children.Add(notesBox);
        }

        // معلومات المقيّم
        var evaluatorGrid = new Grid
        {
            HorizontalAlignment = HorizontalAlignment.Right,
            Margin = new Thickness(0, 10, 0, 0),
            FlowDirection = FlowDirection.LeftToRight
        };

        evaluatorGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
        evaluatorGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

        var signatureLine = new Border
        {
            BorderThickness = new Thickness(0, 0, 0, 1),
            BorderBrush = new SolidColorBrush(Colors.Gray),
            Width = 150,
            Margin = new Thickness(20, 0, 0, 0),
            HorizontalAlignment = HorizontalAlignment.Right
        };

        var evaluatorInfo = new StackPanel
        {
            HorizontalAlignment = HorizontalAlignment.Right,
            FlowDirection = FlowDirection.RightToLeft
        };

        var evaluatorName = new TextBlock
        {
            Text = $"المقيّم: {data["EvaluatorName"]}",
            FontFamily = TextFont,
            FontSize = 13,
            FontWeight = FontWeights.SemiBold,
            HorizontalAlignment = HorizontalAlignment.Right,
            TextAlignment = TextAlignment.Right
        };

        var evaluatorPosition = new TextBlock
        {
            Text = $"الوظيفة: {data["EvaluatorPosition"]}",
            FontFamily = TextFont,
            FontSize = 12,
            Foreground = new SolidColorBrush(Colors.Gray),
            HorizontalAlignment = HorizontalAlignment.Right,
            Margin = new Thickness(0, 2, 0, 0),
            TextAlignment = TextAlignment.Right
        };

        var evaluationDate = new TextBlock
        {
            Text = $"تاريخ التقييم: {((DateTime)data["CreatedDate"]):yyyy/MM/dd}",
            FontFamily = TextFont,
            FontSize = 12,
            Foreground = new SolidColorBrush(Colors.Gray),
            HorizontalAlignment = HorizontalAlignment.Right,
            Margin = new Thickness(0, 10, 0, 0),
            TextAlignment = TextAlignment.Right
        };

        evaluatorInfo.Children.Add(evaluatorName);
        evaluatorInfo.Children.Add(evaluatorPosition);
        evaluatorInfo.Children.Add(evaluationDate);

        Grid.SetColumn(signatureLine, 0);
        Grid.SetColumn(evaluatorInfo, 1);

        evaluatorGrid.Children.Add(signatureLine);
        evaluatorGrid.Children.Add(evaluatorInfo);

        footer.Children.Add(evaluatorGrid);

        return footer;
    }

    private static void ShowMessage(string message, string title, MessageBoxImage icon)
    {
        MessageBox.Show(message, title, MessageBoxButton.OK, icon);
    }

    private static void ShowError(string message, Exception ex)
    {
        MessageBox.Show($"{message}\n\nالخطأ: {ex.Message}", "خطأ", MessageBoxButton.OK, MessageBoxImage.Error);
    }
}

} عدل التصميم

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions