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 SelectCustomerDialog : Form
  {
    public Tuple<string, long> Result;
    public SelectCustomerDialog()
    {
      InitializeComponent();
      InitKeyBoard();
    }

    Color numberColor = Color.FromArgb(250, 120, 24);
    Color simpleColor = Color.FromArgb(77, 135, 245);

    void InitKeyBoard()
    {
      var chars = new char[][] { 
        new char[]{'1','2','3','4','5','6','7','8','9','0'},
        new char[] { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P' }, 
        new char[] { 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L' }, 
        new char[] { 'Z', 'X', 'C', 'V', 'B', 'N', 'M' } };
      var panel = new FlowLayoutPanel[] { flowLayoutPanel5, flowLayoutPanel2, flowLayoutPanel3, flowLayoutPanel4 };
      var idx = 0;
      foreach (var v in chars)
      {
        foreach (var c in v)
        {
          var btn = new ColorButton() { Width = 80, Height = 50, Text = c.ToString(), Font = new Font("宋体", 15), Margin = new Padding(10, 0, 10, 0) };
          btn.BackColor = idx == 0 ? numberColor : simpleColor;
          btn.Click += CharClick;
          panel[idx].Controls.Add(btn);
        }
        idx++;
      }
    }

    void CharClick(object sender, EventArgs e)
    {
      textBox1.Text += ((ColorButton)sender).Text;
    }

    private void uButton1_Click(object sender, EventArgs e)
    {
      if (textBox1.Text.Length > 0)
        textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
    }

    Color customerColor = Color.FromArgb(144, 98, 222);
    void InitCustomerBtn()
    {
      flowLayoutPanel1.Controls.Clear();
      if (string.IsNullOrEmpty(textBox1.Text))
        return;
      var customers = DialogBL.GetCustomerList(textBox1.Text.ToLower());
      foreach (var item in customers)
      {
        var btn = new ColorButton() { Width = 100, Height = 60, Text = item.StringExt1.ToString(), Tag = item.LongExt1, BackColor = customerColor, Font = new Font("宋体", 10), Margin = new Padding(12) };
        btn.Click += CustomerClick;
        flowLayoutPanel1.Controls.Add(btn);
      }
    }

    void CustomerClick(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();
    }

    private void searchBtn_Click(object sender, EventArgs e)
    {
      InitCustomerBtn();
    }
  }
}
