using HandyControl.Controls;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using SqlSugar;
using WCS.Model.ApiModel;
using HandyControl.Tools.Extension;
using 智慧物流软件系统.Views.Controls;
using WCS.Model.ApiModel.User;
using 智慧物流软件系统.Api;
using WCS.Model;

namespace 智慧物流软件系统.ViewModel
{
    public class RoleViewModel : BindableBase
    {
        private List<RoleModel> dataList = new List<RoleModel>();
        /// <summary>
        /// 列表数据
        /// </summary>
        public List<RoleModel> 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<RoleModel>>>(LocalFile.Config.ApiIpHost + "user/getRoles", 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<RoleModel>(See); }
        public void See(RoleModel obj)
        {
            RoleEditView.Show(obj, CrudEnum.Read);
        }

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

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


        public ICommand DelCommand { get => new DelegateCommand<RoleModel>(Del); }
        public void Del(RoleModel obj)
        {
            Growl.Ask($"是否删除角色[{obj.Name}]!", isConfirmed =>
            {
                if (isConfirmed)
                {
                    //try
                    //{
                    //    //var isContains = AuthDb1.db.Queryable<UserModel>().Select(o => o.RoleIds).ToList().SelectMany(o => o).Contains(obj.Id);
                    //    //if (isContains)
                    //    //{
                    //    //    Growl.Info($"此角色被用户使用中，无法删除");
                    //    //    return true;
                    //    //}

                    //    //AuthDb1.db.Deleteable(obj).ExecuteCommand();
                    //}
                    //catch (Exception ex)
                    //{
                    //    Growl.Error($"删除失败：{ex.ToString()}");
                    //    return true;
                    //}

                    try
                    {
                        var body = new AddRoleRequest<RoleModel>()
                        {
                            UserName = LocalStatic.CurrentUser,
                            DeviceType = LocalFile.Config.DeviceType,
                            Role = obj,
                            AddOrUpdate = AddOrUpdate.Delete,
                        };
                        var Result = ApiHelp.GetDataFromHttp<ResponseCommon<object>>(LocalFile.Config.ApiIpHost + "user/addRole", body, "POST");
                        if (Result.Code == 200)
                        {
                            Growl.Success("删除成功");
                            UpdateList();
                            return true;
                        }
                        else
                        {
                            Growl.Error(Result?.Message);
                            return true;
                        }
                    }
                    catch (Exception ex)
                    {
                        Growl.Error($"删除失败：{ex.ToString()}");
                        return true;
                    }
                }
                else
                    return true;
            });
        }
    }
}
