using BBWY.Client.APIServices;
using BBWY.Client.Helpers;
using BBWY.Client.Models.PackTask;
using BBWY.Client.ViewModels.PackTask;
using BBWY.Common.Models;
using BBWY.Controls;
using GalaSoft.MvvmLight.Messaging;
using HandyControl.Controls;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BBWY.Client.Views.PackTask
{
    /// <summary>
    /// Consumable.xaml 的交互逻辑
    /// </summary>
    public partial class Consumable : Page
    {

      public  ConsumableViewModel model;
    //  public  ConsumableService consumableService;
        public Consumable()
        {
            //this.consumableService = consumableService;
            InitializeComponent();
          //  model = new ConsumableViewModel(consumableService);
            //model.ReflashDatas = GetList;
            //this.DataContext = model;


            var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var builder = new ConfigurationBuilder().SetBasePath(applicationPath).AddJsonFile("BBWYAppSettings.json", false, true);
            var Configuration = builder.Build();

            QKApiHost = Configuration.GetSection("QKApiHost").Value;


            Messenger.Default.Register<bool>(this, message =>
            {
               
                GetList();
            });


           
        }

        string QKApiHost = "";
       


        List<string> consumables = new List<string>();

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var list = (ListBox)sender;
                if (list.Items.Count <= 0)
                {
                    return;
                }
                var value = (ListBoxItem)list.SelectedValue;
                var content = (Label)value.Content;
                consumableName.Text = content.Content.ToString();
                tipBox.Visibility = Visibility.Collapsed;
            }
            catch (Exception)
            {


            }
        }

        private void consumableName_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                var textBoxt = (BTextBox)sender;
                //创建一个ListBox

                if (tipBox != null && tipBox.Items.Count > 0)
                {
                    tipBox.Items.Clear();

                }

                if (consumables.Count <= 0)
                {
                    GetList();
                }

                if (string.IsNullOrEmpty(textBoxt.Text))
                {
                    tipBox.Visibility = Visibility.Collapsed;
                    return;
                }
                foreach (var department in consumables)
                {
                    if (department.Contains(textBoxt.Text))
                    {
                        ListBoxItem item = new ListBoxItem();
                        Label lb = new Label();
                        lb.Content = department;
                        item.Content = lb;
                        tipBox.Items.Add(item);
                    }

                }

                tipBox.Visibility = Visibility.Visible;
            }
            catch (Exception)
            {


            }
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        private void GetList()
        {
            HttpClientHelper helper = new HttpClientHelper(QKApiHost);

            string url = $"{QKApiHost}/api/Consumable/SearchAll";//获取所有数据
            var data = helper.Get(url);
            var res = JsonConvert.DeserializeObject<ApiResponse<ConsumableModel[]>>(data);
            //创建一个ListBoxIem
            if (res.Success)
            {

                if (res.Data != null && res.Data.Count() > 0)
                {
                    consumables = new List<string>();
                    foreach (var department in res.Data)
                    {
                        if (!consumables.Contains(department.Name))
                        {
                            consumables.Add(department.Name);
                        }
                    }
                }
            }
        }
    }

   
}