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 智能仓储WCS管理系统.Views.Controls;
using 智能仓储WCS管理系统.Api;
using WCS.Model;
using WCS.Model.ApiModel.Home;
using WCS.Model.ApiModel.StoreInfo;
using WCS.BLL.DbModels;
using WCS.Model.ApiModel.MatBaseInfo;
using WCS.Model.ApiModel.User;
using WCS.Model.ApiModel;
using Newtonsoft.Json.Bson;
using System.Windows;
using System.Security.Cryptography;

namespace 智能仓储WCS管理系统.ViewModel
{
    public class StoreInfoViewModel : BindableBase
    {
        public StoreInfoViewModel()
        {

        }

        #region Property
        private List<StoreInfoModel> dataGridItemSource;
        public List<StoreInfoModel> DataGridItemSource
        {
            get { return dataGridItemSource; }
            set
            {
                SetProperty(ref dataGridItemSource, value);
            }
        }

        private StoreInfoModel selectedataGridItem;
        public StoreInfoModel SelectedataGridItem
        {
            get { return selectedataGridItem; }
            set
            {
                SetProperty(ref selectedataGridItem, value);
            }
        }


        private string shelfCode;
        public string ShelfCode
        {
            get { return shelfCode; }
            set
            {
                SetProperty(ref shelfCode, value);
            }
        }

        private string moduleCode;
        public string ModuleCode
        {
            get => moduleCode;
            set { SetProperty(ref moduleCode, value); }
        }

        private string storeCode;
        public string StoreCode
        {
            get => storeCode;
            set { SetProperty(ref storeCode, value); }
        }

        private string currentMatSN;
        public string CurrentMatSN
        {
            get => currentMatSN;
            set { SetProperty(ref currentMatSN, value); }
        }

        #endregion

        #region Command
        public ICommand BtnResetCommand { get => new DelegateCommand(BtnReset); }
        public void BtnReset()
        {
            ShelfCode = string.Empty;
            ModuleCode = string.Empty;
            StoreCode = string.Empty;
            CurrentMatSN = string.Empty;
        }

        public ICommand BtnSearchCommand { get => new DelegateCommand(BtnSearchReset); }
        public void BtnSearchReset()
        {
            BtnSearch(true);
        }

        public void BtnSearch(bool IsPageReset = true)
        {
            if (CurrentPage == 0 || IsPageReset)
            {
                CurrentPage = 1;
                return;
            }
            #region 调用接口获取数据
            var dia = Dialog.Show(new TextDialog());
            try
            {
                var body = new GetStoresRequest()
                {
                    ShelfCode = ShelfCode,
                    ModuleCode = ModuleCode,
                    StoreCode = StoreCode,
                    CurrentMatSN = CurrentMatSN,
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                    PageNumber = CurrentPage,
                    PageSize = PageSize,
                };
                var Result = ApiHelp.GetDataFromHttp<PageQueryResponse<StoreInfoModel>>(LocalFile.Config.ApiIpHost + "storeInfo/getStores", body, "POST");
                if (Result != null && Result.Data != null && Result.Data.Lists != null)
                {
                    DataGridItemSource = Result.Data.Lists;
                    MaxPage = Result.Data.MaxPage;
                    TotalCount = Result.Data.TotalCount;
                }
            }
            catch (Exception ex)
            {
                Growl.Error("加载数据失败：" + ex.Message);
            }
            finally
            {
                dia.Close();
            }
            #endregion
        }

        public ICommand DisableCommand { get => new DelegateCommand<StoreInfoModel>(Disable); }
        public void Disable(StoreInfoModel store)
        {
            var result = HandyControl.Controls.MessageBox.Show("库位屏蔽仅用于临时屏蔽硬件损坏识别的未扫描上架！\r\n" +
                "操作时会删除对应库位的数据，需要保证对应库位物料已取出。\r\n" +
                "请确认是否进行操作？"
                , "提示", MessageBoxButton.YesNo);
            if (result == MessageBoxResult.Yes)
            {
                #region 调用接口 临时禁用库位 删除库存数据
                try
                {
                    var body = new DisableOrEnableStoreRequest()
                    {
                        StoreId = store.Id,
                        SroreCode = store.StoreCode,
                        DisableOrEnable = DisableOrEnableEnum.Disable,
                        UserName = LocalStatic.CurrentUser,
                        DeviceType = LocalFile.Config.DeviceType,
                    };
                    var Result = ApiHelp.GetDataFromHttp<ResponseCommon>(LocalFile.Config.ApiIpHost + "storeInfo/disableOrEnableStore", body, "POST");
                    if (Result != null && Result.Code == 200)
                    {
                        Growl.Success("禁用成功");
                        BtnSearch();
                    }
                    else if (Result != null)
                    {
                        Growl.Warning(Result.Message);
                    }
                    else
                    {
                        Growl.Warning("操作失败：请重试！");
                    }
                }
                catch (Exception ex)
                {
                    Growl.Error("操作失败：" + ex.Message);
                }
                #endregion
            }
            else
            {
                return;
            }
        }

        public ICommand EnableCommand { get => new DelegateCommand<StoreInfoModel>(Enable); }
        public void Enable(StoreInfoModel store)
        {
            if (store.CurrentMatSn != "禁用")
            {
                Growl.Warning("库位未被禁用！");
                return;
            }
            #region 调用接口 临时禁用库位 删除库存数据
            try
            {
                var body = new DisableOrEnableStoreRequest()
                {
                    StoreId = store.Id,
                    SroreCode = store.StoreCode,
                    DisableOrEnable = DisableOrEnableEnum.Enable,
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                };
                var Result = ApiHelp.GetDataFromHttp<ResponseCommon>(LocalFile.Config.ApiIpHost + "storeInfo/disableOrEnableStore", body, "POST");
                if (Result != null && Result.Code == 200)
                {
                    Growl.Success("操作成功！");
                    BtnSearch();
                }
                else if (Result != null)
                {
                    Growl.Warning(Result.Message);
                }
                else
                {
                    Growl.Warning("操作失败：请重试！");
                }
            }
            catch (Exception ex)
            {
                Growl.Error("操作失败：" + ex.Message);
            }
            #endregion
        }
        #endregion

        #region PageOperation 分页操作
        private int currentPage;
        public int CurrentPage
        {
            get { return currentPage; }
            set
            {
                SetProperty(ref currentPage, value);
                BtnSearch(false);
            }
        }

        private int maxPage;
        public int MaxPage
        {
            get { return maxPage; }
            set { SetProperty(ref maxPage, value); }
        }

        //总数量
        private int totalCount;
        public int TotalCount
        {
            get { return totalCount; }
            set { SetProperty(ref totalCount, value); }
        }

        private int pageSize = 10;
        public int PageSize
        {
            get => pageSize;
            set
            {
                SetProperty(ref pageSize, value);
                BtnSearch(true);
            }
        }


        public ICommand BtnFirstPageCommand { get => new DelegateCommand(BtnFirstPage); }
        public void BtnFirstPage()
        {
            CurrentPage = 1;
        }

        public ICommand BtnPrePageCommand { get => new DelegateCommand(BtnPrePage); }
        public void BtnPrePage()
        {
            if (CurrentPage > 1)
            {
                CurrentPage--;
            }
        }

        public ICommand BtnNextPageCommand { get => new DelegateCommand(BtnNextPage); }
        public void BtnNextPage()
        {
            if (CurrentPage < MaxPage)
            {
                CurrentPage++;
            }
        }

        public ICommand BtnLastPageCommand { get => new DelegateCommand(BtnLastPage); }
        public void BtnLastPage()
        {
            if (CurrentPage != MaxPage)
            {
                CurrentPage = MaxPage;
            }
        }
        #endregion
    }
}
