using 智慧物流软件系统.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 HandyControl.Controls;
using 智慧物流软件系统.Views.Controls;
using WCS.Model.ApiModel.User;
using 智慧物流软件系统.Api;
using WCS.Model;
using Newtonsoft.Json;
using SqlSugar;
using WCS.Model.ApiModel;

namespace 智慧物流软件系统
{
    /// <summary>
    /// 用户登录
    /// </summary>
    public partial class UserLoginView : System.Windows.Window
    {
        UserInfoViewModel viewModel = UserInfoView.viewModel;
        public UserLoginView()
        {
            InitializeComponent();
            this.DataContext = viewModel;
        }

        private void load(object sender, RoutedEventArgs e)
        {
            //登录记忆
            if (LocalFile.Config.Sys.IsSaveLogin && LocalFile.Config.Sys.SaveLogin.Any())
            {
                ComboBoxId.ItemsSource = LocalFile.Config.Sys.SaveLogin;
                ComboBoxId.SelectedIndex = 0;

                PasswordBoxPass.Password = LocalFile.Config.Sys.LastSaveLoginPassword;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var loginName = ComboBoxId.Text.Trim();
            var pass = PasswordBoxPass.Password.ToString();

            if (string.IsNullOrEmpty(loginName))
            {
                HandyControl.Controls.MessageBox.Warning("请输入账号！", "提示");
                return;
            }
            if (!LocalFile.Config.IsMx)
                if (string.IsNullOrEmpty(pass))
                {
                    HandyControl.Controls.MessageBox.Warning("请输入密码！", "提示");
                    return;
                }

            try
            {
                try
                {
                    var body = new UserLoginRequest()
                    {
                        UserName = loginName,
                        DeviceType = LocalFile.Config.DeviceType,
                        GroupNames = LocalFile.Config.GroupName,
                        PassWord = pass,
                        IsNoLogin = false,
                    };
                    //if (LocalFile.Config.IsMx)
                    //    body.IsNoLogin = true;
                    var Result = ApiHelp.GetDataFromHttp<ResponseBase<UserModel>>(LocalFile.Config.ApiIpHost + "user/userLogin",
                                                                                        body, "POST");
                    if (Result != null && Result.Code != 200)
                    {
                        HandyControl.Controls.MessageBox.Warning(Result.Message, "提示");
                        return;
                    }
                    else
                    {
                        viewModel.User = Result?.Data;
                    }
                }
                catch (Exception ex)
                {
                    HandyControl.Controls.MessageBox.Warning($"登录发生异常：{ex.Message}", "提示");
                    return;
                }

                //登录记忆
                if (LocalFile.Config.Sys.IsSaveLogin)
                {
                    LocalFile.Config.Sys.SaveLogin.RemoveAll(t => t == loginName);
                    LocalFile.Config.Sys.SaveLogin.Insert(0, loginName);
                    LocalFile.Config.Sys.LastSaveLoginPassword = pass;
                    LocalFile.Config.Sys.SaveLogin = LocalFile.Config.Sys.SaveLogin.Take(LocalFile.Config.Sys.SaveLoginCount).ToList();
                    LocalFile.SaveConfig();
                }
                viewModel.Roles = viewModel.User?.GetRoles;
                viewModel.IsLogin = true;
                LocalStatic.CurrentUser = loginName;
                BatchBindMatDetailView.viewModel.StationCode = loginName;
                BatchBindMatDetailView.viewModel.BtnSearchReset();

                this.DialogResult = true;
                this.Close();
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error(ex.Message, "错误");
                return;
            }
        }

        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Button_Click(null, null);
            }
            else if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        /// <summary>
        /// 不登录，直接使用最高权限
        /// </summary>
        public static void NotLogin()
        {
            //try
            //{
            //    var body = new GetUsersRequest()
            //    {
            //        UserName = "admin",
            //        DeviceType = "WCS前端",
            //        Info = "123",
            //    };
            //    var Result = ApiHelp.Post<ResponseCommon<List<UserModel>>>([LocalFile.Config.ApiIpHost, "user/getUsers"], body).Result;
            //    if (Result != null && Result.Data.Any())
            //    {
            //        UserInfoView.viewModel.User = Result.Data.First();
            //    }

            //    var bodyRole = new GetUsersRequest()
            //    {
            //        UserName = "admin",
            //        DeviceType = "WCS前端",
            //        Info = "123",

            //    };
            //    var ResultRole = ApiHelp.Post<ResponseCommon<List<RoleModel>>>([LocalFile.Config.ApiIpHost, "user/getRoles"], body).Result;
            //    if (ResultRole != null && ResultRole.Data.Any())
            //    {
            //        UserInfoView.viewModel.Roles = ResultRole.Data;
            //    }

            //    UserInfoView.viewModel.IsLogin = true;
            //}
            //catch (Exception ex)
            //{
            //    Growl.Error("加载数据失败：" + ex.Message);
            //}
            //finally
            //{
            //    //dia.Close();
            //}
        }

        //接口获取User对象

    }
}
