using ButcherFactory.BO;
using ButcherFactory.BO.LocalBL;
using ButcherFactory.Controls;
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;
using WinFormControl;

namespace ButcherFactory.SegmentProductionAuto_
{
  public partial class GoodsNumberInputDialog : Form
  {
    ClientGoodsSet_Detail goodsInfo;
    decimal number = 0;
    public GoodsNumberInputDialog(ClientGoodsSet_Detail mGoods)
    {
      goodsInfo = mGoods;
      InitializeComponent();
      goodsNameLbl.Text = goodsInfo.Goods_Name;
    }

    private void okBtn_Click(object sender, EventArgs e)
    {
      ParseNumber();
      if (number == 0)
        throw new Exception("请输入数量");
      var log = new SegmentGoodsProductNumLog();
      log.Goods_ID = goodsInfo.Goods_ID;
      log.Goods_Code = goodsInfo.Goods_Code;
      log.Goods_Name = goodsInfo.Goods_Name;
      log.Number = number;
      SegmentProductionBL.InsertProductNumLog(log);
      DialogResult = DialogResult.OK;
    }

    private void cancelBtn_Click(object sender, EventArgs e)
    {
      DialogResult = DialogResult.Cancel;
    }

    private void NumBtnClick(object sender, EventArgs e)
    {
      var btn = sender as ColorButton;
      switch (btn.Text)
      {
        case "退格":
          if (this.numberBox.Text != null && this.numberBox.Text.Length > 0)
            this.numberBox.Text = this.numberBox.Text.Substring(0, this.numberBox.Text.Length - 1);
          break;
        case ".":
          if (this.numberBox.Text.Contains("."))
            return;
          if (string.IsNullOrEmpty(this.numberBox.Text))
            numberBox.Text = "0.";
          else
            numberBox.Text += btn.Text;
          break;
        case "0":
          if (this.numberBox.Text != "0")
            numberBox.Text += btn.Text;
          break;
        default:
          if (this.numberBox.Text == "0")
            this.numberBox.Text = btn.Text;
          else
            numberBox.Text += btn.Text;
          break;
      }
    }

    void ParseNumber()
    {
      if (!decimal.TryParse(numberBox.Text, out number))
        throw new Exception("输入有误！");
    }
  }
}
