using HandyControl.Controls;
using MiniExcelLibs;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;
using SqlSugar;
using HandyControl.Data;
using System.Windows;
using Newtonsoft.Json.Linq;
using 智慧物流软件系统.Views.Controls;
using WCS.Model.ApiModel.User;
using 智慧物流软件系统.Api;
using WCS.Model;
using WCS.Model.ApiModel;

namespace 智慧物流软件系统.ViewModel
{
    public class UserViewModel : BindableBase
    {
        private List<UserModel> dataList = new List<UserModel>();
        /// <summary>
        /// 列表数据
        /// </summary>
        public List<UserModel> DataList { get => dataList; set { SetProperty(ref dataList, value); } }

        #region 筛选
        private string info1;
        public string Info1 { get => info1; set { SetProperty(ref info1, value); } }
        #endregion

        public ICommand UpdateListCommand { get => new DelegateCommand(UpdateList); }
        /// <summary>
        /// 更新信息
        /// </summary>
        public void UpdateList()
        {
            var dia = Dialog.Show(new TextDialog());
            try
            {
                var body = new GetUsersRequest()
                {
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                    Info = Info1,
                };
                var Result = ApiHelp.GetDataFromHttp<ResponseBase<List<UserModel>>>(LocalFile.Config.ApiIpHost + "user/getUsers", body, "POST");
                if (Result != null && Result.Data != null)
                {
                    DataList = Result.Data;
                }
            }
            catch (Exception ex)
            {
                Growl.Error("加载数据失败：" + ex.Message);
            }
            finally
            {
                dia.Close();
            }
        }

        public ICommand SeeCommand { get => new DelegateCommand<UserModel>(See); }
        public void See(UserModel obj)
        {
            UserEditView.Show(obj, CrudEnum.Read);
        }

        public ICommand AddCommand { get => new DelegateCommand(Add); }
        public void Add()
        {
            var isUp = UserEditView.Show(new UserModel(), CrudEnum.Create);
            if (isUp)
            {
                UpdateList();
                Growl.Success("创建成功");
            }
        }

        public ICommand UpdateCommand { get => new DelegateCommand<UserModel>(Update); }
        public void Update(UserModel obj)
        {
            var isUp = UserEditView.Show(obj, CrudEnum.Update);
            if (isUp)
            {
                UpdateList();
                Growl.Success("更新成功");
            }
        }


        public ICommand DelCommand { get => new DelegateCommand<UserModel>(Del); }
        public void Del(UserModel obj)
        {
            Growl.Ask($"是否删除用户[{obj.LoginName}]!", isConfirmed =>
            {
                if (isConfirmed)
                {
                    try
                    {
                        var body = new AddUserRequest<UserModel>()
                        {
                            UserName = LocalStatic.CurrentUser,
                            DeviceType = LocalFile.Config.DeviceType,
                            User = obj,
                            AddOrUpdate = AddOrUpdate.Delete
                        };
                        var Result = ApiHelp.GetDataFromHttp<ResponseBase<UserModel>>(LocalFile.Config.ApiIpHost + "user/addUser", body, "POST");
                        if (Result != null && Result.Code == 200)
                        {
                            UpdateList();
                            Growl.Success("删除成功");
                        }
                        else
                        {
                            Growl.Error($"删除失败：{Result.Message.ToString()}");
                        }

                    }
                    catch (Exception ex)
                    {
                        Growl.Error($"删除失败：{ex.ToString()}");
                        return true;
                    }
                  
                }
                return true;
            });
        }
    }
}
