using ATC_MaterialBind.Entity;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SlnMesnac.Config;
using SlnMesnac.Repository;
using SlnMesnac.Repository.service;
using SlnMesnac.WPF.Attribute;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SlnMesnac.WPF.ViewModel.IndexPage
{
    [RegisterAsSingletonAttribute]
    public partial class ChangeTypeViewModel : ObservableObject
    {
        private ILogger<ChangeTypeViewModel> _logger;
        private Isys_dict_dataService _dataService;
        private AppsettingsConfig _appSettings = new AppsettingsConfig();
        public ChangeTypeViewModel()
        {
            UpdateCommand = new RelayCommand<object>(t => Update(t));
            UpdateCommand1 = new RelayCommand<object>(t => Update1(t));
            _dataService = App.ServiceProvider.GetService<Isys_dict_dataService>();
            ProductType = _appSettings.ReadAppSettings("ChangeTypeConfig:ProductType");
            try
            {
                string KT = _appSettings.ReadAppSettings("ChangeTypeConfig:KTProductType");
                if (!string.IsNullOrEmpty(KT))
                {
                    KTProductType = KT;
                }
            }
            catch (Exception ex)
            {

            }


            LoadDataAsync();
        }

        private void Update(object parameter)
        {
            string materialType = parameter as string;
            ProductType = materialType;

            _appSettings.WriteAppSettings("ChangeTypeConfig", "ProductType", ProductType);
        }
        private void Update1(object parameter)
        {
            string materialType = parameter as string;
            KTProductType = materialType;
            _appSettings.WriteAppSettings("ChangeTypeConfig", "KTProductType", KTProductType);
        }
        private async void LoadDataAsync()
        {
           List<sys_dict_data> sys_Dict_Datas = await _dataService.GetProductType();
            if (sys_Dict_Datas != null && sys_Dict_Datas.Count > 0)
            {
                ChangeTypePanel.Clear();
                ChangeTypePanel2.Clear();
                foreach (var item in sys_Dict_Datas)
                {
                    ChangeTypePanel.Add(item);
                    ChangeTypePanel2.Add(item);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public RelayCommand<object> UpdateCommand { get; set; }
        public RelayCommand<object> UpdateCommand1 { get; set; }
        private string _ProductType;
        public string ProductType
        {
            get => _ProductType;
            set
            {
                if (_ProductType != value)
                {
                    SetProperty(ref _ProductType, value);
                }
            }
        }
        private string _KTProductType;
        public string KTProductType
        {
            get => _KTProductType;
            set
            {
                if (_KTProductType != value)
                {
                    SetProperty(ref _KTProductType, value);
                }
            }
        }

        private ObservableCollection<sys_dict_data> _ChangeTypePanel = new ObservableCollection<sys_dict_data>();
        public ObservableCollection<sys_dict_data> ChangeTypePanel
        {
            get => _ChangeTypePanel;
            set => SetProperty(ref _ChangeTypePanel, value);
        }

        private ObservableCollection<sys_dict_data> _ChangeTypePanel2 = new ObservableCollection<sys_dict_data>();
        public ObservableCollection<sys_dict_data> ChangeTypePanel2
        {
            get => _ChangeTypePanel2;
            set => SetProperty(ref _ChangeTypePanel2, value);
        }
    }
}
