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.SegmentSaleOut_
{
  public partial class WeightRecordDialog : Form
  {

    BindingList<SegmentSaleOut_Detail> list;
    SaleOutStore_Detail mDetail;
    long? mBatchID = null;
    public bool Changed = false;
    public WeightRecordDialog(SaleOutStore_Detail detail, bool readOnly, long? batchID)
    {
      InitializeComponent(); 
      mDetail = detail;
      mBatchID = batchID;
      if (readOnly)
      {
        addBtn.Enabled = false;
        deleteBtn.Enabled = false;
      }
      uDataGridView1.BorderStyle = BorderStyle.FixedSingle;
      BindGrid();

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

    void uDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
      var last = uDataGridView1.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;
          }
        }
      }
    }

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

    void BindGrid()
    {
      list = SegmentSaleOutBL.GetWeightRecord(mDetail.ID);
      var total = new SegmentSaleOut_Detail();
      total.Number = list.Sum(x => x.Number);
      total.SecondNumber = list.Sum(x => x.SecondNumber);
      list.Add(total);
      uDataGridView1.DataSource = list;
      ReBuildTotalRow();
      uDataGridView1.Refresh();
    }

    private void ReBuildTotalRow()
    {
      var row = uDataGridView1.Rows[list.Count - 1];
      row.Cells["R_Time"].Value = null;
    }

    private void addBtn_Click(object sender, EventArgs e)
    {
      if (new AddWeightRecord(mDetail, mBatchID).ShowDialog() == DialogResult.OK)
      {
        BindGrid();
        Changed = true;
      }
    }

    private void deleteBtn_Click(object sender, EventArgs e)
    {
      if (uDataGridView1.CurrentRow == null)
        return;
      var id = (long)uDataGridView1.CurrentRow.Cells[0].Value;
      if (id == 0)
        return;
      var tag = list.First(x => x.ID == id);
      SegmentSaleOutBL.DeleteAndUpdate(tag);
      BindGrid();
      Changed = true;
    }

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