using BO.BO.Bill;
using BO.Utils.BillRpc;
using BWP.WinFormControl;
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 QualityAndOrder
{
  public partial class HurryRecordView : Form
  {
    bool changed = false;
    List<HurryRecord> mRecords;
    int mTotalNumber = 0;
    public HurryRecordView(long fromOrderID,int totalNumbet)
    {
      InitializeComponent();
      recordGridView.AutoGenerateColumns = false;
      mRecords = OrderDetailRpc.GetHurryRecordList(fromOrderID).OrderBy(x => x.ID).ToList();
      mTotalNumber = totalNumbet;
      recordGridView.DataSource = null;
      recordGridView.DataSource = mRecords;
      recordGridView.Refresh();
    }

    private void closeBtn_Click(object sender, EventArgs e)
    {
      if (changed)
        DialogResult = DialogResult.OK;
      this.Close();
    }

    private void recordGridView_CellClick(object sender, DataGridViewCellEventArgs e)
    {
      if (e.RowIndex == -1)
        return;
      if (e.ColumnIndex < recordGridView.ColumnCount - 2)
        return;
      var entity = recordGridView.CurrentRow.DataBoundItem as HurryRecord;
      if (e.ColumnIndex == recordGridView.ColumnCount - 2)
      {
        if (OrderDetailRpc.DeleteHurryRecord(entity.ID))
          UMessageBox.Show("排宰顺序出现多条记录，请手动修改", "注意");
        _result += entity.HurryNumber;
        mRecords.Remove(entity);
        if (!changed)
          changed = true;
        recordGridView.DataSource = null;
        if (mRecords.Any())
          recordGridView.DataSource = mRecords;
        recordGridView.Refresh();
      }
      else
      {
        entity.TotalNumber = mTotalNumber;
        HurryRecordPrint.Print(entity);
        UMessageBox.Show("打印成功");
      }
    }

    private int _result = 0;
    public int Result
    {
      get { return _result; }
    }
  }
}
