using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Data;

namespace SiaSun.LMS.WPFClient.TASK
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ManageStockUp : AvalonDock.DocumentContent
    {
        string strGoodsClassCode = string.Empty;         
        
        SiaSun.LMS.Model.MANAGE_TYPE mMANAGE_TYPE = null;

        #region 构造函数

        public ManageStockUp()
        {
            InitializeComponent();
        }

        public ManageStockUp(string MANAGE_TYPE_CODE, string GOODS_CLASS_CODE)
        {
            InitializeComponent();

            this.strGoodsClassCode = GOODS_CLASS_CODE;

            this.mMANAGE_TYPE = (Model.MANAGE_TYPE)MainApp._I_BaseService.GetModel("MANAGE_TYPE_SELECT_BY_MANAGE_TYPE_CODE",
                                                                                     MANAGE_TYPE_CODE).RequestObject;
        }

        #endregion

        #region 系统方法

        private void DocumentContent_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (mMANAGE_TYPE == null)
                {
                    MainApp._MessageDialog.ShowException("未找到任务类型");
                    return;
                }
                else
                {
                    ManagePosition_Init();

                    ucStockIn_Init();
                }
            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
        }

        #endregion

        #region 自定义方法

        private void ManagePosition_Init()
        {
            try
            {
                this.ucManagePosition.U_InitControl(this.mMANAGE_TYPE.MANAGE_TYPE_ID);
            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
        }

        private void ucStockIn_Init()
        {
            try
            {

                Model.GOODS_CLASS mGOODS_CLASS = (Model.GOODS_CLASS)MainApp._I_BaseService.GetModel("GOODS_CLASS_SELECT_BY_GOODS_CLASS_CODE", strGoodsClassCode).RequestObject;

                this.ucStockIn.U_GoodsClassID = mGOODS_CLASS.GOODS_CLASS_ID;

                this.ucStockIn.U_GoodsTypeID = mGOODS_CLASS.GOODS_TYPE_ID;

                this.ucStockIn.U_InitControl();
            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
        }

        #endregion

        #region 按钮事件

        private void WrapPanel_Click(object sender, RoutedEventArgs e)
        {
            Button btn = e.OriginalSource as Button;
            if (btn != null)
            {
                switch (btn.Name)
                {
                    case "btnCreateTask":
                        this.CreateStockTask();
                        break;
                    case "btnRefresh":
                        this.Refresh();
                        break;
                }
            }
        }

        private void CreateStockTask()
        {
            bool boolResult = true;

            bool boolBatchResult = true;

            string strResult = string.Empty;

            string sBatchResult = string.Empty;

            boolResult = this.ucStockIn.U_CheckValidate(out strResult);

            if (!boolResult)
            {
                MainApp._MessageDialog.Show(false, strResult);
                return;
            }

            //判断输送位置是否合法
            boolResult = this.ucManagePosition.U_CHECK_WAREHOUSE();

            if (!boolResult)
            {
                return;
            }


            if (MainApp._MessageDialog.ShowDialog("您确认创建任务?") == Sid.Windows.Controls.TaskDialogResult.Cancel)
                return;

            try
            {
                int intStartPositionID = this.ucManagePosition.U_START_POSITION_ID;

                List<Model.MANAGE_LIST> listMANAGE_LIST = new List<Model.MANAGE_LIST>();

                Model.MANAGE_LIST mMANAGE_LIST = new Model.MANAGE_LIST();

                mMANAGE_LIST.GOODS_ID = this.ucStockIn.U_StockType;

                mMANAGE_LIST.MANAGE_LIST_QUANTITY = this.ucStockIn.U_StockQuantity;

                listMANAGE_LIST.Add(mMANAGE_LIST);

                foreach (string sStockBarcode in this.ucStockIn.U_lsStockBarcodes)
                {

                    SiaSun.LMS.Model.MANAGE_MAIN mMANAGE_MAIN = new Model.MANAGE_MAIN();

                    mMANAGE_MAIN.PLAN_ID = 0;

                    mMANAGE_MAIN.MANAGE_TYPE_CODE = mMANAGE_TYPE.MANAGE_TYPE_CODE.TrimEnd();

                    mMANAGE_MAIN.STOCK_BARCODE = sStockBarcode.ToUpper();

                    mMANAGE_MAIN.CELL_MODEL = this.ucManagePosition.U_CELL_MODEL;

                    mMANAGE_MAIN.START_CELL_ID = this.ucManagePosition.U_START_POSITION_ID;

                    mMANAGE_MAIN.END_CELL_ID = this.ucManagePosition.U_END_POSITION_ID;
                    //mMANAGE_MAIN.END_CELL_ID = this.ucStockIn.rbSingleContinue.IsChecked.Value ? 0 : this.ucManagePosition.U_END_POSITION_ID;

                    mMANAGE_MAIN.MANAGE_OPERATOR = MainApp._USER.USER_NAME;

                    mMANAGE_MAIN.MANAGE_BEGIN_TIME = SiaSun.LMS.Common.StringUtil.GetDateTime();

                    mMANAGE_MAIN.MANAGE_STATUS = SiaSun.LMS.Enum.MANAGE_STATUS.Waiting.ToString();

                    mMANAGE_MAIN.FULL_FLAG = this.ucManagePosition.U_OCCUPY_PERCENT;

                    mMANAGE_MAIN.MANAGE_LEVEL = string.Empty;

                    mMANAGE_MAIN.MANAGE_REMARK = string.Empty;

                    boolResult = MainApp._I_BaseService.Invoke(mMANAGE_TYPE.MANAGE_TYPE_CLASS.TrimEnd(),
                                                                 "ManageCreate",
                                                                  new object[] { mMANAGE_MAIN,
                                                                                 listMANAGE_LIST,    
                                                                                 this.cbControl.IsChecked,
                                                                                 true},                                                                                 
                                                                  out strResult);
                    if (!boolResult)
                    {
                        MainApp._MessageDialog.Show(boolResult, strResult);

                        boolBatchResult = false;

                        break;
                    }
                    else
                    {
                        sBatchResult += mMANAGE_MAIN.STOCK_BARCODE + "\n";

                    }
                }
            }
            catch (Exception ex)
            {
                boolResult = false;

                strResult = ex.Message;
            }

            if (sBatchResult != string.Empty)
                MainApp._MessageDialog.Show(true, sBatchResult + "\n" + "任务下达成功");


            if (boolBatchResult)
                this.Refresh();

        }

        private void Refresh()
        {
            try
            {
                this.ucManagePosition.U_Update();

                this.ucStockIn.U_Refresh();
            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
        }

        #endregion

    }
}