using MeterVision.db;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MeterVision.Patch
{
    /// <summary>
    /// AddCompDlg.xaml 的交互逻辑
    /// </summary>
    public partial class AddCompDlg : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public TComp NewComp { get; set; }


        //下拉框绑定的数据
        public ObservableCollection<VPatch> NPatchList { get; set; }

        public ObservableCollection<VPatch> OPatchList { get; set; }

        public List<VPatch> mPatchList;


        public AddCompDlg()
        {
            InitializeComponent();

            NPatchList = new ObservableCollection<VPatch>();
            cmbNPatch.ItemsSource = NPatchList;
            OPatchList = new ObservableCollection<VPatch>();
            cmbOPatch.ItemsSource = OPatchList;

            lvNPatch.ItemsSource = NPatchList;

            this.DataContext = this;
            LoadNPatchList();
        }

        private async void LoadNPatchList()
        {
            try
            {
                mPatchList = await Task.Run(() => DBPatch.GetAllVPatchs());
                NPatchList.Clear();
                foreach(VPatch patch in mPatchList)
                {
                    NPatchList.Add(patch);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show($"加载数据失败：{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }


        private void CmbNPatch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var patchItem = cmbNPatch.SelectedItem as VPatch;
            OPatchList.Clear();
            if (patchItem != null)
            {
                foreach(VPatch patch in mPatchList)
                {
                    if( patch.StandId == patchItem.StandId &&
                        double.Parse(patch.CreateTime) < double.Parse(patchItem.CreateTime))
                    {
                        OPatchList.Add(patch);
                    }
                }//foreach
            }
        }

        private void CmbOPatch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void BtnOK_Click(object sender, RoutedEventArgs e)
        {
            if(cmbNPatch.SelectedItem == null || cmbOPatch.SelectedItem == null)
            {
                MessageBox.Show("请先选择任务！");
                return;
            }

            NewComp = new TComp
            {
                CompId = Guid.NewGuid().ToString(),
                CreateTime = ThisApp.GetNowTime_yyyyMMddHHmmss(),
                NPatchId = ((VPatch)cmbNPatch.SelectedItem).PatchId,
                OPatchId = ((VPatch)cmbOPatch.SelectedItem).PatchId,
                StandId = ((VPatch)cmbNPatch.SelectedItem).StandId
            };
            bool result = DBComp.InsertTComp(NewComp);
            if (result)
            {
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                MessageBox.Show("保存失败，请重试！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
