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 System.Windows.Input;
using WinFormControl;

namespace ButcherFactory.Dialogs
{
  public partial class ClientGoodsSetDialog : Form
  {
    Dictionary<string, Dictionary<string, IEnumerable<ClientGoodsSet_Detail>>> goodsSetDic;
    public ClientGoodsSetDialog()
    {
      InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);
      goodsSetDic = FormClientGoodsSetBL.GetGoodsSetDic();

      foreach (var item in goodsSetDic)
      {
        var btn = new ColorButton() { Width = 120, Height = 62, Text = item.Key, Font = new Font("宋体", 15), Margin = new Padding(10, 5, 10, 5), EnableGroup = true };
        btn.Click += GroupBtnClick01;
        flowLayoutPanel1.Controls.Add(btn);
      }
       
    }

    string SelectName = "";

    Color goodsColor = Color.FromArgb(250, 120, 24);
    void GroupBtnClick01(object sender, EventArgs e)
    {
      flowLayoutPanel2.Controls.Clear();
      flowLayoutPanel3.Controls.Clear();
      var groupBtn = sender as ColorButton;
      var arr = goodsSetDic[groupBtn.Text];
      SelectName = groupBtn.Text;
      foreach (var item in arr)
      {
        var btn = new ColorButton() { Width = 120, Height = 62, Text = item.Key, Font = new Font("宋体", 15), Margin = new Padding(10, 5, 10, 5), EnableGroup = true }; 
        btn.Click += GroupBtnClick;
        flowLayoutPanel3.Controls.Add(btn);
      }
    }

    void GroupBtnClick(object sender, EventArgs e)
    {
      flowLayoutPanel2.Controls.Clear();
      var groupBtn = sender as ColorButton;
      var arr01 = goodsSetDic[SelectName];
      var arr = arr01[groupBtn.Text];
      foreach (var item in arr)
      {
        var btn = new ColorButton() { Width = 140, Height = 90, Text = item.Goods_Name, Tag = item, Font = new Font("宋体", 12, FontStyle.Bold), BackColor = goodsColor, Margin = new Padding(10, 10, 10, 35), PlaySound = true, StateHold = true };
        if (item.Selected)
          btn.Selected = true;
        btn.Click += GoodsBtnClick;
        flowLayoutPanel2.Controls.Add(btn);
      }
    }

    void GoodsBtnClick(object sender, EventArgs e)
    {
      var btn = sender as ColorButton;
      var detail = btn.Tag as ClientGoodsSet_Detail;
      if (detail.Selected)
        FormClientGoodsSetBL.DeleteWorkGoodsSet(detail.ID);
      else
        FormClientGoodsSetBL.InsertWorkerGoodsSet(detail.ID);
      detail.Selected = !detail.Selected;
    }

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

    private void searchBtn_Click(object sender, EventArgs e)
    {
     var  goodsSet = FormClientGoodsSetBL.GetGoodsSetDic2(textBox1.Text.ToLower());
      flowLayoutPanel2.Controls.Clear();
      foreach (var item in goodsSet)
      {
        var btn = new ColorButton() { Width = 140, Height = 90, Text = item.Goods_Name, Tag = item, Font = new Font("宋体", 12, FontStyle.Bold), BackColor = goodsColor, Margin = new Padding(10, 10, 10, 35), PlaySound = true, StateHold = true };
        if (item.Selected)
          btn.Selected = true;
        btn.Click += GoodsBtnClick;
        flowLayoutPanel2.Controls.Add(btn);
      }

    }

    private void PwdTextBoxClick(object sender, MouseButtonEventArgs e)
    {

    }

    private void textBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      var keyBoard = new VirtualKeyPad();
      if (keyBoard.ShowDialog() == true)
        textBox1.Text = keyBoard.Result;
    }
  }
}
