using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using BPA.Model;
using System.Diagnostics;
using BPA.SingleDevice.View;
using NetTaste;
using BPA.SingleDevice.Helper;
using BPA.Model.Table;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;


namespace BPA.SingleDevice.ViewModel
{
    public class RawMaterialManagementViewModel : NotifyBase
    {
        public RawMaterialManagementViewModel()
        {
            SqlHelper.GetInstance.GetListAsync<RawMaterTB>().Result.OnSuccess(s =>
            {
                s.ForEach(item =>
                {
                    RawMaterInfos.Add(new RawMaterInfo(item.Id, item.Name, item.DeviceNum, item.WarehouseNum, item.LastModified));
                });
            });

            AddCommand = new BPARelayCommand(async () =>
            {
                var content = new AddRawMaterialDialogView();
                var para = await Dialog.Show(content, new Parameters(), "添加原料", (d) => { }, (d, o) =>
                {
                    if (o is RawMaterResult rm)
                    {
                        string id = Guid.NewGuid().ToString();
                        string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        SqlHelper.GetInstance.AddAsync(new RawMaterTB()
                        {
                            Id = id,
                            Name = rm.Name,
                            DeviceNum = rm.DeviceNum,
                            WarehouseNum = rm.ChNum,
                            LastModified = time
                        }).Result.OnSuccess(() =>
                        {
                            RawMaterInfos.Add(new RawMaterInfo(id, rm.Name, rm.DeviceNum, rm.ChNum, time));
                        }).OnFail(s => { Message.Error("RawMaterialManagementView", $"添加失败：{s}"); });
                    }
                });
            });

            EditCommand = new BPARelayCommand<object>(async (o) =>
            {
                if (o != null && !string.IsNullOrEmpty(o.ToString()))
                {
                    var index = RawMaterInfos.ToList().FindIndex(p => p.Id == o.ToString());
                    if (index >= 0)
                    {
                        var para = await Dialog.Show(new AddRawMaterialDialogView(), new RawMaterResult()
                        {
                            Name = RawMaterInfos[index].Name,
                            DeviceNum = RawMaterInfos[index].DeviceNum,
                            ChNum = RawMaterInfos[index].WarehouseNum,
                            Id = RawMaterInfos[index].Id,
                        }, "原料编辑", (d) => { }, (d, o) =>
                        {
                            if (o is RawMaterResult rm)
                            {
                                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                SqlHelper.GetInstance.UpdateAsync(new RawMaterTB()
                                {
                                    Id = RawMaterInfos[index].Id,
                                    Name = rm.Name,
                                    DeviceNum = rm.DeviceNum,
                                    WarehouseNum = rm.ChNum,
                                    LastModified = time
                                }).Result.OnSuccess(() =>
                                {
                                    RawMaterInfos[index].Name = rm.Name;
                                    RawMaterInfos[index].DeviceNum = rm.DeviceNum;
                                    RawMaterInfos[index].WarehouseNum = rm.ChNum;
                                    RawMaterInfos[index].LastModified = time;
                                }).OnFail(s => { Message.Error("RawMaterialManagementView", $"修改失败：{s}"); });
                            }
                        });
                    }
                }
            });

            RemoveCommand = new BPARelayCommand<object>(async (o) =>
            {
                if (o != null && !string.IsNullOrEmpty(o.ToString()))
                {
                    var index = RawMaterInfos.ToList().FindIndex(p => p.Id == o.ToString());
                    if (index >= 0)
                    {
                        var result = await MessageBoxR.Confirm("是否删除改数据?");
                        if (result == System.Windows.MessageBoxResult.Yes)
                        {
                            SqlHelper.GetInstance.DeleteAsync<RawMaterTB>(o.ToString()).OnSuccess(() =>
                            {
                                RawMaterInfos.RemoveAt(index);
                            }).OnFail(s => { Message.Error("RawMaterialManagementView", $"删除失败：{s}"); });
                        }
                    }
                }
            });

        }

        public ObservableCollection<RawMaterInfo> RawMaterInfos { get; set; } = new ObservableCollection<RawMaterInfo>();

        public BPARelayCommand AddCommand { get; set; }
        public BPARelayCommand SaveCommand { get; set; }
        public BPARelayCommand<object> CopyCommand { get; set; }
        public BPARelayCommand<object> EditCommand { get; set; }
        public BPARelayCommand<object> RemoveCommand { get; set; }
    }
}
