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;
using System.Windows.Controls;
using WCS.Model.ApiModel.InterfaceRecord;

namespace 智慧物流软件系统.ViewModel
{
    public class InterfaceRecordViewModel : BindableBase
    {
        #region Property
        private List<SystemApiLogModel> dataGridItemSource;
        public List<SystemApiLogModel> DataGridItemSource
        {
            get { return dataGridItemSource; }
            set
            {
                SetProperty(ref dataGridItemSource, value);
            }
        }

        /// <summary>
        /// 接口地址
        /// </summary>
        private string requestUrl;
        public string RequestUrl
        {
            get { return requestUrl; }
            set
            {
                SetProperty(ref requestUrl, value);
            }
        }

        /// <summary>
        /// 请求参数
        /// </summary>
        private string requestBody;
        public string RequestBody
        {
            get { return requestBody; }
            set
            {
                SetProperty(ref requestBody, value);
            }
        }

        /// <summary>
        /// 请求时间的类型
        /// </summary>
        private TimeType timeType;
        public TimeType TimeType
        {
            get { return timeType; }
            set
            {
                SetProperty(ref timeType, value);
            }
        }

        public IEnumerable<string> TimeTypes => GetEnumValues();
        private IEnumerable<string> GetEnumValues()
        {
            return Enum.GetNames(typeof(TimeType));
        }

        private DateTime startTime;
        public DateTime StartTime
        {
            get { return startTime; }
            set
            {
                SetProperty(ref startTime, value);
            }
        }

        private DateTime endTime;
        public DateTime EndTime
        {
            get { return endTime; }
            set
            {
                SetProperty(ref endTime, value);
            }
        }

        /// <summary>
        /// 调用类型 调用/被调用
        /// </summary>
        private string requestType;
        public string RequestType
        {
            get { return requestType; }
            set
            {
                SetProperty(ref requestType, value);
            }
        }
        #endregion

        #region Command
        public ICommand BtnResetCommand { get => new DelegateCommand(BtnReset); }
        public void BtnReset()
        {
            RequestUrl = string.Empty;
            RequestBody = string.Empty;
            RequestType = string.Empty;
            TimeType = TimeType.请求时间;
            StartTime = DateTime.Now.Date;
            EndTime = DateTime.Now.Date.AddDays(1);
        }

        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 GetInterfaceRecordsRequest()
                {
                    RequestUrl = RequestUrl,
                    RequestBody = RequestBody,
                    RequestType = RequestType,
                    TimeType = TimeType,
                    StartTime = StartTime,
                    EndTime = EndTime,

                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                    PageNumber = CurrentPage,
                    PageSize = PageSize,

                };
                var Result = ApiHelp.GetDataFromHttp<PageQueryResponse<SystemApiLogModel>>(LocalFile.Config.ApiIpHost + "interfaceRecord/getInterfaceRecord", 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 BtnExportCommand { get => new DelegateCommand(BtnExport); }
        public async void BtnExport()
        {

            try
            {
                #region 选择文件保存路径
                Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
                sfd.Filter = ".xlsx文件(*.xlsx)|*.xlsx";
                sfd.FileName = "接口记录" + DateTime.Now.ToString("yyyyMMddhhmmss");
                sfd.OverwritePrompt = true;
                if (sfd.ShowDialog() != true)
                {
                    return;
                }
                string path = sfd.FileName;
                #endregion

                var body = new GetInterfaceRecordsRequest()
                {
                    RequestUrl = RequestUrl,
                    RequestBody = RequestBody,
                    RequestType = RequestType,
                    TimeType = TimeType,
                    StartTime = StartTime,
                    EndTime = EndTime,

                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                    PageNumber = CurrentPage,
                    PageSize = 65535,

                };
                await ApiHelp.PostDownloadFileAsync(path, System.Net.Http.HttpMethod.Post, LocalFile.Config.ApiIpHost + "interfaceRecord/exportInterfaceRecord", body);
                Growl.Success("导出成功！");
            }
            catch (Exception ex)
            {
                Growl.Error("导出失败：" + ex.Message);
            }
        }
        #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
    }
}
