using ButcherFactory.BO;
using ButcherFactory.BO.LocalBL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ButcherFactory.SegmentInStore_
{
  public partial class InStoreSummaryView : Form
  {
    BindingList<SegmentInStore> list;
    public InStoreSummaryView()
    {
      InitializeComponent();
      inStoreGrid.RowPrePaint += uDataGridView1_RowPrePaint;
      inStoreGrid.CellPainting += uDataGridView1_CellPainting;
    }

    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);
      list = SegmentInStoreBL.GetInStoreSummary();
      list.Add(new SegmentInStore { Number = list.Sum(x => x.Number ?? 0), Weight = list.Sum(x => x.Weight ?? 0) });
      inStoreGrid.DataSource = list;
      inStoreGrid.Refresh();
    }

    private void uDataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
    {
      if (e.RowIndex == inStoreGrid.Rows.Count - 1)
      {
        var row = inStoreGrid.Rows[e.RowIndex];
        row.DefaultCellStyle.SelectionForeColor = Color.Black;
        row.DefaultCellStyle.BackColor = Color.Khaki;
        row.DefaultCellStyle.SelectionBackColor = Color.Khaki;
      }
    }

    private void uDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
      var last = inStoreGrid.Rows.Count - 1;
      if (e.RowIndex == last)
      {
        if (e.ColumnIndex == 2)
        {
          e.PaintBackground(e.ClipBounds, false);
          e.Handled = true;
        }
        else if (e.ColumnIndex == 3)
        {
          using (Brush foreColor = new SolidBrush(e.CellStyle.ForeColor))
          {
            e.PaintBackground(e.ClipBounds, false);
            StringFormat drawFormat = new StringFormat();
            drawFormat.LineAlignment = StringAlignment.Center;
            drawFormat.Alignment = System.Drawing.StringAlignment.Center;

            e.Graphics.DrawString("合计", e.CellStyle.Font, foreColor, e.CellBounds, drawFormat);
            e.Handled = true;
          }
        }
      }
    }

    private void closeBtn_Click(object sender, EventArgs e)
    {
      this.Close();
    }
  }
}
