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.SegmentProductionAuto_
{
  public partial class ViewTaskForm : Form
  {
    BindingList<ProductTaskFull> list;
    DateTime mDate;
    long mBatchId;
    IEnumerable<Goods> mGoods;
    public ViewTaskForm(DateTime date, long batchId, IEnumerable<Goods> goods)
    {
      InitializeComponent();
      mDate = date;
      mBatchId = batchId;
      mGoods = goods;

      taskGrid.RowPrePaint += uDataGridView1_RowPrePaint;
      taskGrid.CellPainting += uDataGridView1_CellPainting;
    }

    private void uDataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
    {
      if (e.RowIndex == taskGrid.Rows.Count - 1)
      {
        var row = taskGrid.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 = taskGrid.Rows.Count - 1;
      if (e.RowIndex == last)
      {
        //if (e.ColumnIndex == 2)
        //{
        //  e.PaintBackground(e.ClipBounds, false);
        //  e.Handled = true;
        //}
       // else 
        if (e.ColumnIndex == 0)
        {
          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;
          }
        }
      }
    }

    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);

      list = new BindingList<ProductTaskFull>(SegmentProductionBL.GetProductTaskFull(mDate, mBatchId, mGoods));
      list.Add(new ProductTaskFull { OrderNumber = list.Sum(x => x.OrderNumber ?? 0), FinishNumber = list.Sum(x => x.FinishNumber ), OrderWeight = list.Sum(x => x.OrderWeight ?? 0), FinishWeight = list.Sum(x => x.FinishWeight) });
      taskGrid.DataSource = list;
      taskGrid.Refresh();
    }

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

  }
}
