using BO;
using BO.BO.BaseInfo;
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 WeighAndGrading
{
  public partial class BodyDiscontSetting : Form
  {
    List<BodyDiscontItem> list;
    public BodyDiscontSetting()
    {
      InitializeComponent();
      list = GradeAndWeightRpc.GetBodyDiscontItem().OrderBy(x => x.ID).ToList();
      uDataGridView1.DataSource = list;
      uDataGridView1.Refresh();
    }

    bool changed = false;
    private void saveBtn_Click(object sender, EventArgs e)
    {
      var list = new List<CTuple<long, decimal?>>();
      foreach (DataGridViewRow row in uDataGridView1.Rows)
      {
        var entity = row.DataBoundItem as BodyDiscontItem;
        list.Add(new CTuple<long, decimal?>(entity.ID, entity.Discont));
      }
      GradeAndWeightRpc.SaveBodyDiscontItem(list);
      if (!changed)
        changed = true;
      MessageBox.Show("保存成功！");
    }

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

    private void uDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
      if (e.RowIndex == -1)
        return;
      if (e.ColumnIndex == uDataGridView1.ColumnCount - 1)
      {
        var keyBoard = new NumberPad();
        if (keyBoard.ShowDialog() == true)
        {
          if (string.IsNullOrEmpty(keyBoard.Result))
            return;
          decimal v;
          if (decimal.TryParse(keyBoard.Result, out v))
            uDataGridView1.CurrentRow.Cells["S_Discont"].Value = v;
          else
            throw new Exception("扣重输入不正确");
        }
      }
    }
  }
}
