using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPA.SingleDevice.ViewModel
{
    public class SetBindMaterialViewModel : NotifyBase, IDialogDataContext
    {
        public SetBindMaterialViewModel()
        {
            for (int i = 1; i <= 2; i++)
            { DeviceNums.Add(i); }
            for (int i = 4; i <= 5; i++)
            { DeviceNums.Add(i); }
            for (int i = 1; i <= 14; i++)
            { ChNum.Add(i); }

            

            SaveCommand = new BPARelayCommand(() => {
                RawMaterResultInfo.MaterialName = SelectedItem.Name;
                RawMaterResultInfo.MaterialCode = SelectedItem.MaterialCode;
                RequestClose?.Invoke(RawMaterResultInfo); 
            });
            CancelCommand = new BPARelayCommand(() => { RequestClose?.Invoke(null); });
        }

        public string Title => "绑定物料";

        public event Action<object> RequestClose;

        public async void OnDialogOpened(object parameters)
        {
            await GetAllMaterials();
            if (parameters is MaterialBindTB rm)
            {
                RawMaterResultInfo = rm;
                //SelectedItem.Name = rm.MaterialName;
                //SelectedItem.MaterialCode = rm.MaterialCode;
                if (rm.MaterialCode!=null)
                {
                    var isSelect = MaterialInfos.FirstOrDefault(m => m.MaterialCode == rm.MaterialCode);
                    if (isSelect != null)
                    {
                        SelectedItem = isSelect;
                    }
                }
                
            }
                
        }

        public ObservableCollection<int> DeviceNums { get; set; } = new ObservableCollection<int>();
        public ObservableCollection<int> ChNum { get; set; } = new ObservableCollection<int>();
        public ObservableCollection<MaterialTB> MaterialInfos { get; set; } = new();
        public MaterialBindTB RawMaterResultInfo
        { get { return _mRawMaterResultInfo; } set { _mRawMaterResultInfo = value; OnPropertyChanged(); } }
        private MaterialBindTB _mRawMaterResultInfo = new MaterialBindTB();

        private MaterialTB _SelectedItem=new();

        public MaterialTB SelectedItem
        {
            get { return _SelectedItem; }
            set { _SelectedItem = value; OnPropertyChanged(); }
        }


        public BPARelayCommand SaveCommand { get; set; }
        public BPARelayCommand CancelCommand { get; set; }

        private async Task GetAllMaterials()
        {
            var result = await SqlHelper.GetInstance.GetListAsync<MaterialTB>();
            if (result.IsSuccess)
            {
                MaterialInfos.Clear();
                var list = result.Content.OrderBy(m => m.MaterialCode);
                foreach (var item in list)
                {
                    MaterialInfos.Add(item);
                }
            }
        }
    }
}
