using DM_Weight.msg;
using DM_Weight.Report;
using DM_Weight.util;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using DM_Weight.Models;
using Prism.Services.Dialogs;
using System.Threading.Tasks;
namespace DM_Weight.ViewModels
{
    public class AccountWindowForDrugViewModel : BindableBase, INavigationAware, IRegionMemberLifetime
    {
        public static AccountWindowForDrugViewModel vm;
        private DateTime? _startDate = DateTime.Now;
        public DateTime? StartDate
        {
            get => _startDate;
            set
            {
                SetProperty(ref _startDate, value);
            }
        }
        private DateTime? _endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
        public DateTime? EndDate
        {
            get => _endDate;
            set
            {
                SetProperty(ref _endDate, value);
            }
        }
        IEventAggregator _eventAggregator;
        IDialogService _dialogService;
        public AccountWindowForDrugViewModel(IEventAggregator eventAggregator, IDialogService dialogService)
        {
            _eventAggregator = eventAggregator;
            _dialogService = dialogService;
            vm = this;
        }
        /// 
        /// 导出账册
        /// 
        public DelegateCommand DownloadAccountBook
        {
            get => new DelegateCommand(() =>
            {
                if (DrugInfo == null || string.IsNullOrEmpty(DrugInfo.DrugId))
                {
                    AlertMsg alertMsg = new AlertMsg
                    {
                        Message = $"请选择药品!",
                        Type = MsgType.ERROR,
                    };
                    _eventAggregator.GetEvent().Publish(alertMsg);
                    return;
                }
                GridReportUtil.PrintReportAccountBook(StartDate, EndDate, DrugInfo.DrugId);
            });
        }
        public bool KeepAlive => false;
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //药品信息
            GetAllDrugInfos();
        }
        /// 
        /// 药品
        /// 
        private List? _drugInfos;
        public List? DrugInfos
        {
            get => _drugInfos;
            set => SetProperty(ref _drugInfos, value);
        }
        private DrugInfo? _drugInfo;
        public DrugInfo? DrugInfo
        {
            get => _drugInfo;
            set
            {
                SetProperty(ref _drugInfo, value);
            }
        }
        private void GetAllDrugInfos()
        {
            var list = SqlSugarHelper.Db.Queryable().Includes(di => di.DrugManuNos).OrderBy(di => di.DrugId).ToList();
            DrugInfos = list;
            DrugInfo = list[0];
        }
        public void UpdateComboBoxItems(string text)
        {
            string str = @"SELECT d.drug_id,d.py_code,d.drug_barcode,d.drug_name,d.drug_brand_name,d.drug_spec,d.dosage,d.pack_unit,
                        d.manufactory,d.max_stock,CONCAT(drug_name,'  ',drug_spec)as DrugName FROM `drug_info` d";
            if (string.IsNullOrEmpty(text))
            {
                DrugInfos = SqlSugarHelper.Db.SqlQueryable(str).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
                return;
            }
            if (DrugInfos != null)
            {
                DrugInfos.Clear();
            }
            DrugInfos = SqlSugarHelper.Db.SqlQueryable(str).Where(di => di.DrugName.Contains(text) || di.PyCode.Contains(text)).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
        }
        public DelegateCommand SelectTimeAction
        {
            get => new DelegateCommand(async (s) =>
            {
                // 此处延时1毫秒,等待页面渲染
                await Task.Delay(TimeSpan.FromMilliseconds(1));
                DialogParameters dialogParameters = new DialogParameters();
                dialogParameters.Add("DateTime", StartDate);
                dialogParameters.Add("Type", s);
                DialogServiceExtensions.ShowDialogHost(_dialogService, "DatetimeDialog", dialogParameters, DoDialogResult, "RootDialog");
            });
        }
        private void DoDialogResult(IDialogResult dialogResult)
        {
            // 委托   被动执行     被子窗口执行
            // dialogResult  第一方面可以拿到任意参数   第二方面   可判断关闭状态
            if (dialogResult.Result == ButtonResult.OK)
            {
                if (dialogResult.Parameters.GetValue("Type").Equals("1"))
                {
                    StartDate = dialogResult.Parameters.GetValue("DateTime");
                }
                else
                {
                    EndDate = dialogResult.Parameters.GetValue("DateTime");
                }
            }
            //MessageBox.Show("返回值:" + dialogResult.Result.ToString());
        }
    }
}