using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Platform.Common.LogSystem;

namespace Platform.Controls.ComponentContainer
{
    /// <summary>
    /// DataItems.xaml 的交互逻辑
    /// </summary>
    public partial class DataBoxItem
    {
        public DataBoxItem(List<Tuple<string, string>> list)
        {
            InitializeComponent();
            init(list);
        }

        private void init(List<Tuple<string, string>> list)
        {
            try
            {
                if (list == null || list.Count == 0)
                    return;
                //若数据为奇数，需要补位一个空项，解决奇数尾项不显示的问题
                if (list.Count % 2 == 1)
                    list.Add(new Tuple<string, string>("", ""));
                var table = new Table { FontFamily = new FontFamily("SimSun"), CellSpacing = 0, BorderBrush = Brushes.Black, BorderThickness = new Thickness(1, 1, 1, 0) };
                //设置列
                table.Columns.Add(new TableColumn { Background = Brushes.LightSkyBlue, Width = new GridLength(120) });
                table.Columns.Add(new TableColumn { Width = new GridLength(250) });
                table.Columns.Add(new TableColumn { Background = Brushes.LightSkyBlue, Width = new GridLength(120) });
                table.Columns.Add(new TableColumn { Width = new GridLength(250) });
                //设置行
                var rowGroup = new TableRowGroup();
                var row = new TableRow();
                list.ForEach(x =>
                {
                    row.Cells.Add(new TableCell(new Paragraph(new Run(x.Item1))) { Padding = new Thickness(2), BorderBrush = Brushes.Black, TextAlignment = TextAlignment.Right, BorderThickness = new Thickness(0, 0, 0, 1) });
                    row.Cells.Add(new TableCell(new Paragraph(new Run(x.Item2))) { Padding = new Thickness(2), BorderBrush = Brushes.Black, BorderThickness = new Thickness(0, 0, 0, 1) });
                    if (row.Cells.Count > 3)
                    {
                        rowGroup.Rows.Add(row);
                        row = new TableRow();
                    }
                });
                //组装
                table.RowGroups.Add(rowGroup);
                viewer.Document = new FlowDocument(table);
            }
            catch (Exception ex)
            {
                PlatformLogger.SystemErrorInfo("DataBoxItem:展示发生异常！", ex);
            }
        }
    }
}
