using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using SSWMS.Common;

namespace SSWMS.Client
{
    public partial class PLAN_OUT_AUTO : Window
    {
        private int iPlanID;
        private List<CheckBox> lcbStation;
        private List<RadioButton> lrbStation;
        //private CheckBox cbLevel;

        public PLAN_OUT_AUTO(int iID)
        {
            InitializeComponent();
            try
            {
                iPlanID = iID;
                lrbStation = new List<RadioButton>();
                lcbStation = new List<CheckBox>();
                List<string> lIsChecked = new List<string>();
                PLAN_MAIN pm = WCFChannel._I_PlanService.GetPlanMain(iPlanID);
                if (pm != null && !string.IsNullOrWhiteSpace(pm.PLAN_STATION))
                {
                    lIsChecked = pm.PLAN_STATION.Split(',').ToList();
                }
                this.tbLevel.Text = pm.PLAN_PROPERTY_04;
                DataTable dt;
                if (pm.PLAN_TYPE == SystemCode.PLAN_TYPE.PlanOut)
                {
                    dt = SystemCode.WH_CELL_CODE.GetCellCodeOut();
                }
                else
                {
                    dt = SystemCode.WH_CELL_CODE.GetCellCodeIn();
                }
                RadioButton rb;
                foreach (DataRow dr in dt.Rows)
                {
                    rb = new RadioButton();
                    rb.Content = dr["NAME"].ToString();
                    rb.DataContext = dr["VALUE"].ToString();
                    rb.IsChecked = lIsChecked.Contains(rb.DataContext);
                    rb.Margin = new Thickness(10);
                    rb.HorizontalContentAlignment = HorizontalAlignment.Left;
                    rb.Style = Application.Current.Resources["CommonCheckBox"] as Style;
                    rb.GroupName = "PlanOutStation";
                    this.wpStation.Children.Add(rb);
                    lrbStation.Add(rb);
                }
                //CheckBox cb;
                //foreach (DataRow dr in dt.Rows)
                //{
                //    cb = new CheckBox();
                //    cb.Content = dr["NAME"].ToString();
                //    cb.DataContext = dr["VALUE"].ToString();
                //    cb.IsChecked = lIsChecked.Contains(cb.DataContext);
                //    cb.Margin = new Thickness(10);
                //    cb.HorizontalContentAlignment = HorizontalAlignment.Left;
                //    cb.Style = Application.Current.Resources["CommonCheckBox"] as Style;
                //    this.wpStation.Children.Add(cb);
                //    lcbStation.Add(cb);
                //}
                //cbLevel = new CheckBox();
                //cbLevel.Content = "优先出库";
                //cbLevel.Width = 100;
                //cbLevel.IsChecked = pm != null && pm.PLAN_PROPERTY_01 == SystemCode.FLAG.Enable;
                //cbLevel.Margin = new Thickness(10);
                //cbLevel.HorizontalContentAlignment = HorizontalAlignment.Center;
                //cbLevel.Style = Application.Current.Resources["CommonCheckBox"] as Style;
                //spStationLevel.Children.Add(cbLevel);
            }
            catch (Exception ex)
            {
                MessageDialog.ShowException(ex);
            }
        }

        private void bSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (MessageDialog.ShowDialog("确认执行自动出库"))
                {
                    string sPlanStation = string.Empty;
                    foreach (RadioButton cb in lrbStation)
                    {
                        if (cb.IsChecked == true)
                        {
                            sPlanStation = cb.DataContext.ToString();
                            break;
                        }
                    }
                    //foreach (CheckBox cb in lcbStation)
                    //{
                    //    if (cb.IsChecked == true)
                    //    {
                    //        sPlanStation += $"{cb.DataContext},";
                    //    }
                    //}
                    if (string.IsNullOrWhiteSpace(sPlanStation))
                    {
                        MessageDialog.ShowException("请选择出库站台");
                        return;
                    }
                    string sResult = string.Empty;
                    if (WCFChannel._I_PlanService.PlanAutoOut(iPlanID, sPlanStation.Trim(','),
                         this.tbLevel.Text, out sResult))
                    {
                        this.DialogResult = true;
                        this.Close();
                    }
                    else
                    {
                        MessageDialog.ShowException(sResult);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageDialog.ShowException(ex);
            }
        }
    }
}