using BBWY.Client.APIServices;
using BBWY.Client.Helpers;
using BBWY.Client.Models.PackTask;
using BBWY.Client.ViewModels;
using BBWY.Common.Models;
using HandyControl.Controls;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
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>
    /// PackTaskTotal.xaml 的交互逻辑
    /// </summary>
    public partial class PackTaskTotal : Page
    {
        public PackTaskTotal()
        {
            InitializeComponent();
            this.Loaded += Load;

        }

        public ShopService shopService;
        private void Load(object sender, RoutedEventArgs e)
        {


            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;
            HttpClientHelper helper = new HttpClientHelper(QKApiHost);

            string url = $"{QKApiHost}/api/PackTask/GetAllDepartment";//获取所有数据
            var data = helper.Get(url);

            var res = JsonConvert.DeserializeObject<ApiResponse<UserDepartment[]>>(data);
            if (res == null)
            {
                System.Windows.MessageBox.Show("网络异常");
                return;
            }
            if (!res.Success)
            {
                System.Windows.MessageBox.Show(res.Msg);
                return;
            }

            //创建一个ListBoxIem
            if (res.Data != null && res.Data.Length > 0)
            {
                foreach (var department in res.Data)
                {
                    if (!departments.Contains(department.DePartmentName))
                    {
                        departments.Add(department.DePartmentName);
                    }



                }
            }



        }
        public string QKApiHost { get; set; }
        public void SelectionChangeCommand(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;
                tbDepartment.Text = content.Content.ToString();
                tipBoxDepartment.Visibility = Visibility.Collapsed;
            }
            catch (Exception)
            {


            }

        }
        List<string> departments = new List<string>();
        private void tb_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                var textBoxt = (System.Windows.Controls.TextBox)sender;
                //创建一个ListBox

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

                }

                if (departments.Count <= 0)
                    return;

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

                }

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


            }

        }
    }
}