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.MANAGE
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MANAGE_STOCK_IN : AvalonDock.DocumentContent
    {
        string strGoodsClassCode = string.Empty;         
        
        SiaSun.LMS.Model.MANAGE_TYPE mMANAGE_TYPE = null;

        public MANAGE_STOCK_IN()
        {
            InitializeComponent();
        }

        public MANAGE_STOCK_IN(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;
        }

        private void DocumentContent_Loaded(object sender, RoutedEventArgs e)
        {
            this.grpbManage.Header = string.Format(this.grpbManage.Tag.ToString(), this.Title);

            try
            {
                if (mMANAGE_TYPE == null)
                {
                    MainApp._MessageDialog.ShowException("未找到任务类型");
                    return;
                }
                else
                {
                    ManagePositin_Init();

                    ucStockIn_Init();

                    this.ucStockIn.U_StockTypeChanged += new UC.ucStockIn.U_StockTypeValueChangedHandler(ucStockIn_U_StockTypeChanged);
                }
            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
        }

        void ucStockIn_U_StockTypeChanged()
        {
            if( this.ucStockIn.U_GoodsID != 0)
                this.StorageListBind();
        }

        private void ManagePositin_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);
            }
        }


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

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


        private void StorageListBind()
        {
            this.ucStockStorageGroup.U_WindowName = this.GetType().Name;
            this.ucStockStorageGroup.U_TableName = "V_STORAGE_LIST";
            this.ucStockStorageGroup.U_XmlTableName = "V_STORAGE_LIST";
            this.ucStockStorageGroup.U_TotalColumnName = "STORAGE_LIST_QUANTITY";
            this.ucStockStorageGroup.U_OrderField = "STORAGE_LIST_ID";
            this.ucStockStorageGroup.U_Where = string.Format(" goods_id = {0} ", this.ucStockIn.U_GoodsID);
            this.ucStockStorageGroup.U_AllowOperatData = false;
            this.ucStockStorageGroup.U_AllowChecked = false;
            this.ucStockStorageGroup.U_AllowShowPage = true;

            //拆分列属性
            this.ucStockStorageGroup.U_SplitPropertyType = "GOODS_TYPE";
            this.ucStockStorageGroup.U_SplitGroupColumn = "GOODS_TYPE_ID";
            this.ucStockStorageGroup.U_SplitGroupHeader = "GOODS_TYPE.GOODS_TYPE_NAME";
            this.ucStockStorageGroup.U_SplitPropertyColumn = "GOODS_PROPERTY";

            this.ucStockStorageGroup.U_InitControl();
        }

        /// <summary>
        /// 按钮事件
        /// </summary>
        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.ShowResult(false, strResult);
                return;
            }

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

            if (!boolResult)
            {
                return;
            }


            if (MainApp._MessageDialog.ShowDialog("ConfirmCreateTask", null) == 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;

                    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.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.WaitingSend.ToString();

                    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,                                                                                
                                                                                 true, 
                                                                                 this.ucManagePosition.U_CheckStockExistStorage,
                                                                                 this.ucManagePosition.U_AutoCompleteTask,
                                                                                 this.ucManagePosition.U_AutoDownloadControlTask
                                                                                 },
                                                                  out strResult);

                    if (!boolResult)
                    {
                        MainApp._MessageDialog.ShowResult(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.ShowResult(true, sBatchResult + "\n" + "任务下达成功");


            if (boolBatchResult)
                this.Refresh();

        }



    }
}