using ButcherFactory.BO;
using ButcherFactory.BO.LocalBL;
using ButcherFactory.BO.Utils;
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.SegmentStockUp_
{
  public partial class DeliverLineGroupSelectDialog : Form
  {
    Color color = Color.FromArgb(105, 105, 105);
    Color selectColor = Color.FromArgb(250, 120, 24);
    SegmentStockUpConfig config;
    public DeliverLineGroupSelectDialog()
    {
      InitializeComponent();
      this.FormClosing += DeliverLineGroupSelectDialog_FormClosing;
    }

    void DeliverLineGroupSelectDialog_FormClosing(object sender, FormClosingEventArgs e)
    {
      XmlUtil.SerializerObjToFile(config);
    }

    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);
      config = XmlUtil.DeserializeFromFile<SegmentStockUpConfig>();
      BindList();
    }

    void BindList()
    {
      flowLayoutPanel1.Controls.Clear();
      var list = SegmentStockUpBL.GetDeliverLineGroupList();
      foreach (var item in list)
      {
        var btn = new ColorButton() { Width = 150, Height = 60, Text = item.Name, Tag = item, Font = new Font("宋体", 14), Margin = new Padding(30) };
        btn.BackColor = config.LineGroup.Any(x => x.ID == item.ID) ? selectColor : color;
        btn.Click += ItemClick;
        flowLayoutPanel1.Controls.Add(btn);
      }
    }

    private void ItemClick(object sender, EventArgs e)
    {
      var btn = sender as ColorButton;
      var item = (DeliverLineGroup)btn.Tag;
      var first = config.LineGroup.FirstOrDefault(x => x.ID == item.ID);
      if (first == null)
      {
        config.LineGroup.Add(item);
        btn.BackColor = selectColor;
      }
      else
      {
        config.LineGroup.Remove(first);
        btn.BackColor = color;
      }
    }

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