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;

namespace ButcherFactory.Dialogs
{
  public partial class SelectDeliverGoodsLineDialog : Form
  {
    public Tuple<string, long> Result;

    IEnumerable<ExtensionObj> list;
    public SelectDeliverGoodsLineDialog()
    {
      InitializeComponent();
      list = DialogBL.GetDeliverGoodsLineList();
      tabControl1.Selected += tabControl_Selected;
      foreach (var c in list.GroupBy(x => x.StringExt2))
      {
        tabControl1.TabPages.Add(c.Key);
      }
      if (tabControl1.TabPages.Count > 0)
        AddButtons(tabControl1.TabPages[0]);
    }

    void tabControl_Selected(object sender, TabControlEventArgs e)
    {
      if (tabControl1.SelectedTab.Controls.Count == 0)
        AddButtons(tabControl1.SelectedTab);
    }

    Color color = Color.FromArgb(250, 120, 24);
    private void AddButtons(TabPage tabPage)
    {
      if (tabPage.Controls.Count != 0)
        return;
      tabPage.BackColor = Color.White;
      var flw = new FlowLayoutPanel() { Dock = DockStyle.Fill, AutoScroll = true };
      var arr = list.Where(x => x.StringExt2 == tabPage.Text);
      foreach (var item in arr)
      {
        var btn = new ColorButton() { Width = 100, Height = 60, Text = item.StringExt1.ToString(), Tag = item.LongExt1, BackColor = color, Font = new Font("宋体", 10), Margin = new Padding(12) };
        btn.Click += btnClick;
        flw.Controls.Add(btn);
      }
      tabPage.Controls.Add(flw);
    }

    private void btnClick(object sender, EventArgs e)
    {
      var btn = sender as ColorButton;
      Result = new Tuple<string, long>(btn.Text, Convert.ToInt64(btn.Tag));
      DialogResult = DialogResult.OK;
      this.Close();
    }
  }
}
