using HandyControl.Controls;
using Ping9719.WpfEx;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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.Shapes;
using WCS.Model;
using WCS.Model.ApiModel.StoreInfo;
using 智能仓储WCS管理系统.Api;
using 智能仓储WCS管理系统.ViewModel;
using 智能仓储WCS管理系统.Views.Controls;

namespace 智能仓储WCS管理系统
{
    public partial class ModuleInfoView : UserControlBase
    {
        public ModuleInfoViewModel viewModel { get; set; } = new ModuleInfoViewModel();
        public ModuleInfoView()
        {
            InitializeComponent();
            this.DataContext = viewModel;
        }

        private void LoadedVisible(object sender, EventArgs e)
        {
            viewModel.BtnReset();
            viewModel.BtnSearchReset();
        }

        private void allChecked_Checked(object sender, RoutedEventArgs e)
        {
            if (viewModel != null && viewModel.DataGridItemSource != null && viewModel.DataGridItemSource.Count() > 0)
            {
                foreach (var item in viewModel.DataGridItemSource)
                {
                    item.IsSelected = true;
                }
            }
        }

        private void allChecked_Unchecked(object sender, RoutedEventArgs e)
        {
            if (viewModel != null && viewModel.DataGridItemSource != null && viewModel.DataGridItemSource.Count() > 0)
            {
                foreach (var item in viewModel.DataGridItemSource)
                {
                    item.IsSelected = false;
                }
            }
        }

        private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                // 更新选中项的状态
                if (dataGrid.SelectedCells.Count > 0 && viewModel.SelectedataGridItem != null)
                {
                    viewModel.SelectedataGridItem.IsSelected = !viewModel.SelectedataGridItem.IsSelected;
                    //dataGrid.UnselectAllCells(); // 取消选中所有单元格
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("SelectionChanged event handler failed: " + ex.Message);
            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            ;
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            ;
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            ;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            //先获取选中的模组
            var module = viewModel.SelectedataGridItem;
            #region 调用接口 发送指令进行查询
            try
            {
                var body = new QueryModuleVoltageRequest()
                {
                    MouduleIds = new List<int>() { module.Id },
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                };
                var Result = ApiHelp.GetDataFromHttp<ResponseCommon>(LocalFile.Config.ApiIpHost + "storeInfo/queryModuleVoltage", body, "POST");
                if (Result != null && Result.Code == 200)
                {
                    Growl.Success("操作成功：可以进行查询！");
                }
                else if (Result != null)
                {
                    Growl.Success(Result.Message);
                }
            }
            catch (Exception ex)
            {
                Growl.Error("操作失败：" + ex.Message);
            }
            finally
            {

            }
            #endregion
        }

        private void CalibrationOffset_Click(object sender, RoutedEventArgs e)
        {
            //获取选中得模组
            var module = viewModel.SelectedataGridItem;
            #region 弹出输入账号密码的窗口 输入偏移量的功能
            var window = new CalibrationWindow(module.BoardId,module.Id,module.ModuleCode);
            window.Owner = Application.Current.MainWindow;
            window.Topmost = true;
            window.ShowDialog();
            #endregion
        }
    }
}
