using BO.BO.Bill;
using BO.Utils.BillRpc;
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 ButcherOrder
{
  public partial class ViewDetail : Form
  {
    List<SecondOrder_Detail> details;
    SecondOrder secondOrder;
    public ViewDetail(SecondOrder entity)
    {
      InitializeComponent();
      secondOrder = entity;
      orderLabel.Text = entity.Order.ToString();
      planNumberLabel.Text = entity.PlanNumber.ToString();
      hotFadeNumberLabel.Text = entity.HotFadeNumber.ToString();
      detailGridView1.AutoGenerateColumns = false;
      details = SecondOrderRpc.GetSecondOrderDetails(entity.ID).OrderBy(x => x.ID).ToList();
      detailGridView1.DataSource = null;
      detailGridView1.DataSource = details;
      detailGridView1.Refresh();
    }

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

    static bool changed = false;
    private void detailGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
      if (e.RowIndex == -1)
        return;
      if (e.ColumnIndex != detailGridView1.ColumnCount - 1)
        return;
      var entity = detailGridView1.CurrentRow.DataBoundItem as SecondOrder_Detail;
      SecondOrderRpc.DeleteDetail(entity);
      secondOrder.HotFadeNumber -= entity.Number;
      hotFadeNumberLabel.Text = secondOrder.HotFadeNumber.ToString();
      details.Remove(entity);
      if (!changed)
        changed = true;
      detailGridView1.DataSource = null;
      if (details.Any())
        detailGridView1.DataSource = details;
      detailGridView1.Refresh();
    }

    public int HotFadeNumber { get { return secondOrder.HotFadeNumber; } }
  }
}
