using System;
using System.Windows;
using System.Windows.Input;
using System.Data;
using SSWMS.Common;
using System.Collections.Generic;

namespace SSWMS.Client
{
    public partial class PLAN_EDIT : AvalonDock.DocumentContent
    {
        private readonly int PLAN_ID;

        public PLAN_EDIT(int iPlanID)
        {
            InitializeComponent();
            this.PLAN_ID = iPlanID;
            DocumentContent_Loaded(null, null);
        }

        private void DocumentContent_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbPlanType.ItemsSource = SystemCodeData.DicDataView["PLAN_TYPE"];
            //this.cbPlanType.SelectionChanged += (a, b) =>
            //{
            //    if (this.cbPlanType.SelectedIndex == -1)
            //    {
            //        this.cbPlanStation.ItemsSource = null;
            //    }
            //    else
            //    {
            //        string sPlanType = this.cbPlanType.SelectedValue as string;
            //        if (sPlanType == SystemCode.PLAN_TYPE.PlanIn)
            //        {
            //            this.cbPlanStation.ItemsSource = SystemCodeData.DicDataView["CELL_CODE_IN"];
            //        }
            //        else if (sPlanType == SystemCode.PLAN_TYPE.PlanOut)
            //        {
            //            this.cbPlanStation.ItemsSource = SystemCodeData.DicDataView["CELL_CODE_OUT"];
            //        }
            //    }
            //    this.cbPlanStation.SelectedIndex = -1;
            //};

            this.ucGoodsMain.dgData.U_WindowName = "COMMON";
            this.ucGoodsMain.dgData.U_XmlTableName = "GOODS_MAIN";
            this.ucGoodsMain.dgData.U_TableName = "V_GOODS_MAIN_GOODS";
            this.ucGoodsMain.dgData.U_OrderField = "GOODS_CODE";
            this.ucGoodsMain.dgData.U_AllowOperatData = false;
            this.ucGoodsMain.dgData.gridApp.MouseDoubleClick += new MouseButtonEventHandler((a, b) => {
                DataRowView drv = this.ucGoodsMain.dgData.gridApp.SelectedItem as DataRowView;
                if (drv != null)
                {
                    //this.dgPlanList.U_DataTable.Rows.Clear();
                    DataRow dr = this.dgPlanList.U_DataTable.NewRow();
                    dr["GOODS_ID"] = drv["GOODS_ID"].ToString();
                    dr["GOODS_CLASS"] = drv["GOODS_CLASS"].ToString();
                    dr["GOODS_CODE"] = drv["GOODS_CODE"].ToString();
                    dr["GOODS_NAME"] = drv["GOODS_NAME"].ToString();
                    dr["GOODS_PROPERTY_01"] = drv["GOODS_PROPERTY_01"].ToString();
                    dr["GOODS_PROPERTY_02"] = drv["GOODS_PROPERTY_02"].ToString();
                    dr["GOODS_PROPERTY_03"] = drv["GOODS_PROPERTY_03"].ToString();
                    dr["GOODS_PROPERTY_04"] = drv["GOODS_PROPERTY_04"].ToString();
                    dr["STORAGE_PROPERTY_01"] = string.Empty;
                    dr["STORAGE_PROPERTY_02"] = string.Empty;
                    dr["STORAGE_PROPERTY_03"] = string.Empty;
                    dr["STORAGE_PROPERTY_04"] = string.Empty;
                    dr["STORAGE_PROPERTY_05"] = string.Empty;
                    dr["STORAGE_PROPERTY_06"] = string.Empty;
                    dr["STORAGE_PROPERTY_07"] = string.Empty;
                    dr["STORAGE_PROPERTY_08"] = string.Empty;
                    dr["STORAGE_PROPERTY_09"] = string.Empty;
                    dr["STORAGE_PROPERTY_10"] = string.Empty;
                    dr["PLAN_LIST_QUANTITY"] = 0;
                    dr["PLAN_LIST_FINISHED_QUANTITY"] = 0;
                    this.dgPlanList.U_DataTable.Rows.Add(dr);
                }
            });
            this.ucGoodsMain.U_InitControl();
            this.dgPlanList.U_WindowName = "COMMON";
            this.dgPlanList.U_XmlTableName = "V_PLAN_LIST_EDIT";
            this.dgPlanList.U_TableName = "V_PLAN_LIST";
            this.dgPlanList.U_OrderField = "PLAN_LIST_ID";
            this.dgPlanList.U_Where = "1<>1";
            this.dgPlanList.U_AllowOperatData = true;
            this.dgPlanList.U_AllowAdd = false;
            this.dgPlanList.U_AllowSave = false;
            this.dgPlanList.U_InitControl();
            this.LoadPlan();
        }

        private void LoadPlan()
        {
            if (this.PLAN_ID > 0)
            {
                PLAN_MAIN pm = WCFChannel._I_PlanService.GetPlanMain(this.PLAN_ID);
                IList<PLAN_LIST> lPlanList = WCFChannel._I_PlanService.GetPlanList(this.PLAN_ID);
                if (pm == null || lPlanList == null || lPlanList.Count == 0)
                {
                    MessageDialog.ShowException($"未找到计划ID {this.PLAN_ID}");
                    return;
                }
                else
                {
                    foreach (PLAN_LIST pl in lPlanList)
                    {
                        DataRow dr = this.dgPlanList.U_DataTable.NewRow();
                        GOODS_MAIN gm = WCFChannel._I_StorageService.GetGoodsMain(pl.GOODS_ID);
                        if (gm == null)
                        {
                            MessageDialog.ShowException($"未找到物料ID {pl.GOODS_ID}");
                            return;
                        }
                        dr["GOODS_ID"] = gm.GOODS_ID;
                        dr["GOODS_CLASS"] = gm.GOODS_CLASS;
                        dr["GOODS_CODE"] = gm.GOODS_CODE;
                        dr["GOODS_NAME"] = gm.GOODS_NAME;
                        dr["GOODS_PROPERTY_01"] = gm.GOODS_PROPERTY_01;
                        dr["GOODS_PROPERTY_02"] = gm.GOODS_PROPERTY_02;
                        dr["GOODS_PROPERTY_03"] = gm.GOODS_PROPERTY_03;
                        dr["GOODS_PROPERTY_04"] = gm.GOODS_PROPERTY_04;
                        dr["STORAGE_PROPERTY_01"] = pl.STORAGE_PROPERTY_01;
                        dr["STORAGE_PROPERTY_02"] = pl.STORAGE_PROPERTY_02;
                        dr["STORAGE_PROPERTY_03"] = pl.STORAGE_PROPERTY_03;
                        dr["STORAGE_PROPERTY_04"] = pl.STORAGE_PROPERTY_04;
                        dr["STORAGE_PROPERTY_05"] = pl.STORAGE_PROPERTY_05;
                        dr["STORAGE_PROPERTY_06"] = pl.STORAGE_PROPERTY_06;
                        dr["STORAGE_PROPERTY_07"] = pl.STORAGE_PROPERTY_07;
                        dr["STORAGE_PROPERTY_08"] = pl.STORAGE_PROPERTY_08;
                        dr["STORAGE_PROPERTY_09"] = pl.STORAGE_PROPERTY_09;
                        dr["STORAGE_PROPERTY_10"] = pl.STORAGE_PROPERTY_10;
                        dr["PLAN_LIST_FINISHED_QUANTITY"] = pl.PLAN_LIST_FINISHED_QUANTITY;
                        dr["PLAN_LIST_QUANTITY"] = pl.PLAN_LIST_QUANTITY;
                        this.dgPlanList.U_DataTable.Rows.Add(dr);
                    }
                    this.tbPlanCode.Text = pm.PLAN_CODE;
                    //this.cbPlanType.SelectedValue = pm.PLAN_TYPE;
                    for (int i = 0; i < this.cbPlanType.Items.Count; ++i)
                    {
                        if ((this.cbPlanType.Items[i] as DataRowView)["VALUE"].ToString() == pm.PLAN_TYPE)
                        {
                            this.cbPlanType.SelectedIndex = i;
                            break;
                        }
                    }
                    //this.cbPlanStation.SelectedValue = pm.PLAN_STATION;
                    this.tbPlanStation.Text = pm.PLAN_STATION;
                    this.tbRemark.Text = pm.PLAN_REMARK;
                }
            }
            else
            {
                this.tbPlanCode.Text = string.Empty;
                //this.cbPlanStation.SelectedIndex = -1;
                this.tbPlanStation.Text = string.Empty;
                this.cbPlanType.SelectedIndex = -1;
                this.tbRemark.Text = string.Empty;
            }
        }

        private void bSave_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.tbPlanCode.Text))
            {
                MessageDialog.ShowException("请输入拣货单号");
                return;
            }
            if (this.cbPlanType.SelectedIndex == -1)
            {
                MessageDialog.ShowException("请选择计划类型");
                return;
            }
            string sPlanType = this.cbPlanType.SelectedValue.ToString();
            //string sPlanStation = string.Empty;
            //if (this.cbPlanStation.SelectedIndex == -1)
            //{
            //    if (sPlanType == SystemCode.PLAN_TYPE.PlanOut)
            //    {
            //        MessageDialog.ShowException("请选择计划站台");
            //        return;
            //    }
            //}
            //else
            //{
            //    sPlanStation = this.cbPlanStation.SelectedValue.ToString();
            //}
            string sPlanStation = this.tbPlanStation.Text.Trim();
            if (this.dgPlanList.gridApp.Items.Count == 0)
            {
                MessageDialog.ShowException("请输入计划物料");
                return;
            }

            List<PLAN_LIST> lPlanList = new List<PLAN_LIST>();
            foreach (DataRowView drv in this.dgPlanList.gridApp.Items)
            {
                if (drv.IsEdit)
                {
                    MessageDialog.ShowException($"请输入计划物料信息 物料编码 {drv["GOODS_CODE"]}");
                    return;
                }
                decimal dPlanListQuantity = Convert.ToDecimal(drv["PLAN_LIST_QUANTITY"]);
                if (dPlanListQuantity <= 0)
                {
                    MessageDialog.ShowException($"请输入计划数量 物料编码 {drv["GOODS_CODE"]}");
                    return;
                }
                decimal dPlanListFinishedQuantity = Convert.ToDecimal(drv["PLAN_LIST_FINISHED_QUANTITY"]);
                if (dPlanListFinishedQuantity < 0)
                {
                    MessageDialog.ShowException($"请输入完成数量 物料编码 {drv["GOODS_CODE"]}");
                    return;
                }
                PLAN_LIST pl = new PLAN_LIST
                {
                    GOODS_ID = Convert.ToInt32(drv["GOODS_ID"]),
                    PLAN_LIST_FINISHED_QUANTITY = Convert.ToInt32(dPlanListFinishedQuantity),
                    PLAN_LIST_QUANTITY = Convert.ToInt32(dPlanListQuantity),
                    STORAGE_PROPERTY_01 = drv["STORAGE_PROPERTY_01"].ToString(),
                    STORAGE_PROPERTY_02 = drv["STORAGE_PROPERTY_02"].ToString(),
                    STORAGE_PROPERTY_03 = drv["STORAGE_PROPERTY_03"].ToString(),
                    STORAGE_PROPERTY_04 = drv["STORAGE_PROPERTY_04"].ToString(),
                    STORAGE_PROPERTY_05 = drv["STORAGE_PROPERTY_05"].ToString(),
                    STORAGE_PROPERTY_06 = drv["STORAGE_PROPERTY_06"].ToString(),
                    STORAGE_PROPERTY_07 = drv["STORAGE_PROPERTY_07"].ToString(),
                    STORAGE_PROPERTY_08 = drv["STORAGE_PROPERTY_08"].ToString(),
                    STORAGE_PROPERTY_09 = drv["STORAGE_PROPERTY_09"].ToString(),
                    STORAGE_PROPERTY_10 = drv["STORAGE_PROPERTY_10"].ToString()
                };
                lPlanList.Add(pl);
            }

            if (MessageDialog.ShowDialog("确认执行保存计划操作"))
            {
                try
                {
                    int iPlanID = 0;
                    string sResult = string.Empty;
                    bool bResult = WCFChannel._I_PlanService.PlanAddOrUpdate(new PLAN_MAIN()
                        {
                            PLAN_ID = this.PLAN_ID,
                            PLAN_TYPE = sPlanType,
                            PLAN_STATION = sPlanStation,
                            PLAN_CODE = this.tbPlanCode.Text,
                            PLAN_REMARK = this.tbRemark.Text,
                            PLAN_CREATER = MainApp._USER.USER_NAME,
                            PLAN_CREATE_TIME = StringUtils.GetCurrentTime(),
                            PLAN_STATUS = SystemCode.PLAN_STATUS.Waiting
                        }, lPlanList, out iPlanID, out sResult);
                    if (bResult)
                    {
                        //this.PLAN_ID = iPlanID;
                        //this.Title = string.Format("计划-[{0}]", this.PLAN_ID);
                        this.bRefresh_Click(null, null);
                    }
                    MessageDialog.ShowResult(bResult, sResult);
                }
                catch (Exception ex)
                {
                    MessageDialog.ShowException(ex);
                }
            }
        }

        private void bRefresh_Click(object sender, RoutedEventArgs e)
        {
            this.ucGoodsMain.U_InitControl();
            this.dgPlanList.U_DataTable.Rows.Clear();
            this.LoadPlan();
        }
    }
}